Guest User

Illumi -lO_Ol--

a guest
Jul 9th, 2020
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.86 KB | None | 0 0
  1. PVector[] p;         // array of points
  2. float[] dists;       // array of distances between consecutive points
  3. float[] frac;        // array of fraction of total distance (normalized to TWO_PI)
  4. float totalDist;     // total distance covered by dists
  5.  
  6. int n = 3;           // n-pointed star
  7. int N = n*20;        // number of circles
  8. int r = 5;           // circle radius
  9. float R = .8;        // star radius
  10.  
  11. float time = 0;
  12. float step = PI/420; // increase time by step every iteration
  13.  
  14. void setup() {
  15.   size(500, 500);
  16.  
  17.   p = new PVector[n];
  18.   int k = 0;
  19.   for (int i=0; i<n; i++) {
  20.     float x = R*cos(TWO_PI*k/n);
  21.     float y = R*sin(TWO_PI*k/n);
  22.     p[i] = new PVector(x, y);
  23.     k = (k-1)%n;
  24.   }
  25.  
  26.   dists = new float[n];
  27.   frac = new float[n];
  28.   totalDist = 0;
  29.   PVector pprev;
  30.   PVector pnext;
  31.   for (int i=0; i<n; i++) {
  32.     pprev = p[i];
  33.     if (i == n-1) {
  34.       pnext = p[0];
  35.     } else {
  36.       pnext = p[i+1];
  37.     }
  38.     float d = dist(pprev.x, pprev.y, pnext.x, pnext.y);
  39.     totalDist += d;
  40.     dists[i] = d;
  41.   }
  42.   for (int i=0; i<n; i++) {
  43.     frac[i] = dists[i]/totalDist * TWO_PI;
  44.   }
  45.  
  46.   fill(255);
  47.   noStroke();
  48.   background(0);
  49. }
  50.  
  51. void draw() {
  52.   fill(0, 10);
  53.   rect(0, 0, width, height);
  54.   translate(width/2, height/2);
  55.   rotate(time);
  56.   fill(0, 255, 255);
  57.   for (int k=0; k<N; k++) {
  58.     float t = (time + TWO_PI/N*k) % TWO_PI;
  59.     float ftotal = 0;
  60.     for (int i=0; i<n; i++) {
  61.       float f = frac[i];
  62.       ftotal += f;
  63.       if (t <= ftotal) {
  64.         float fdir = (f-(ftotal-t))/f;
  65.         PVector start = PVector.mult(p[i], width/2);
  66.         PVector end = PVector.mult(p[(i+1)%n], width/2);
  67.         PVector direction = PVector.sub(end, start);
  68.         PVector pos = PVector.add(start, PVector.mult(direction, fdir));
  69.         circle(pos.x, pos.y, r);
  70.         break;
  71.       }
  72.     }
  73.   }
  74.   time += step;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment