Advertisement
xeromino

rotvert

Jan 10th, 2015
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.96 KB | None | 0 0
  1. int frames=360, cols, rows, dir;
  2. animatedPolygon[] aps;
  3. float t, new_t, offSet, partialSegment, fullSegments;
  4. //color[] palette = { #ECD27C, #BCD38D, #689D7E };
  5. color[] palette = { #FCBF05, #E00072, #007D98 };
  6. color bg = #120930;
  7.  
  8. void setup() {
  9.   size(750, 750,P2D);
  10.   int i = 0;
  11.   int j = 0;
  12.   int cellSize = 150;
  13.   cols = int(width/cellSize)-2;
  14.   rows = int(height/cellSize)-2;  
  15.   aps = new animatedPolygon[rows*cols];
  16.   for (int y=0; y<rows; y++) {
  17.     if (y%2==0) {
  18.       dir = 1;
  19.       offSet = 0;
  20.     } else {
  21.       dir =-1;
  22.       offSet = -PI/2;
  23.     }
  24.     color col = palette[y%3];
  25.     for (int x=0; x<cols; x++) {
  26.       float px = (x+1.5)*cellSize;
  27.       float py = (y+1.5)*cellSize;
  28.       aps[i] = new animatedPolygon(3+j, px, py, offSet, dir, col);
  29.       i++;
  30.     }
  31.     j++;
  32.   }
  33.   strokeWeight(3);
  34. }
  35.  
  36.  
  37. void draw() {
  38.   t = (frameCount%frames)/float(frames);
  39.   background(bg);
  40.   for (int i=0; i<aps.length; i++) {
  41.     aps[i].run();
  42.   }
  43.   if (frameCount<=frames) saveFrame("image-###.gif");
  44. }
  45.  
  46. class animatedPolygon {
  47.  
  48.   PVector[] vertices;
  49.   PVector currPos;
  50.   int counter, v, v1, v2, dir, totalSegments;
  51.   float currentPosBetweenVertices;
  52.   float angle, x, y, cx, cy, r, rot;
  53.   color col;
  54.  
  55.   animatedPolygon(int v, float _x, float _y, float offSet, int _dir, color _col) {
  56.     totalSegments = v;
  57.     vertices = new PVector[v];
  58.     r = width/(rows+2)*.4;
  59.     col = _col;
  60.     x = _x;
  61.     y = _y;
  62.     dir = _dir;
  63.     angle = offSet;
  64.     for (int i=0; i<vertices.length; i++) {
  65.       float px = cos(angle)*r;
  66.       float py = sin(angle)*r;
  67.       vertices[i] = new PVector(px, py);
  68.       angle += (TWO_PI/v);
  69.     }
  70.   }
  71.  
  72.   void run() {
  73.     update();
  74.     display();
  75.   }
  76.  
  77.   void update() {
  78.  
  79.     if (t<0.5) {
  80.       new_t = map(t, 0, 0.5, 0, 1);
  81.     } else {
  82.       new_t = map(t, 0.5, 1, 0, 1);
  83.     }
  84.  
  85.     fullSegments = floor(new_t * totalSegments);
  86.     partialSegment = (new_t * totalSegments) % totalSegments;
  87.     currentPosBetweenVertices = partialSegment-fullSegments;
  88.  
  89.     v1 = int(fullSegments);
  90.     v2 = int((fullSegments+1)%totalSegments);
  91.  
  92.     currPos = PVector.lerp(vertices[v1], vertices[v2], currentPosBetweenVertices);
  93.   }
  94.  
  95.   void display() {
  96.  
  97.     pushMatrix();
  98.     translate(x, y);
  99.     rotate(rot);
  100.     stroke(col);
  101.     if (t<0.5) {
  102.       for (int i=0; i<fullSegments; i++) {
  103.         line(vertices[i].x, vertices[i].y, vertices[(i+1)%totalSegments].x, vertices[(i+1)%totalSegments].y);
  104.       }
  105.       line(vertices[v1].x, vertices[v1].y, currPos.x, currPos.y);
  106.     } else {
  107.       for (int i= (v1+1); i<totalSegments; i++) {
  108.         line(vertices[i].x, vertices[i].y, vertices[(i+1)%totalSegments].x, vertices[(i+1)%totalSegments].y);
  109.       }
  110.       line(currPos.x, currPos.y, vertices[v2].x, vertices[v2].y);
  111.     }
  112.  
  113.     for (int i=0; i<totalSegments; i++) {
  114.       fill(bg);
  115.       ellipse(vertices[i].x, vertices[i].y, 10, 10);
  116.     }
  117.     rot += (TWO_PI/frames)*dir;
  118.     popMatrix();
  119.   }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement