Advertisement
xeromino

vertex2

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