Advertisement
xeromino

vertex

Nov 13th, 2014
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.89 KB | None | 0 0
  1. int frames=300, totalSegments=6;
  2. animatedPolygon ap1;
  3. float t, new_t, partialSegment, fullSegments;
  4.  
  5. void setup() {
  6.   size(500, 500);
  7.   ap1 = new animatedPolygon(totalSegments);
  8.   strokeWeight(40);
  9. }
  10.  
  11.  
  12. void draw() {
  13.   t = (frameCount%frames)/float(frames);
  14.   background(#35465c);
  15.   ap1.run();
  16.   //if (frameCount<=frames) saveFrame("image-###.gif");
  17. }
  18.  
  19. class animatedPolygon {
  20.  
  21.   PVector[] vertices;
  22.   PVector currPos;
  23.   int counter, v, v1, v2;
  24.   float currentPosBetweenVertices;
  25.   float angle, cx, cy, r;
  26.  
  27.   animatedPolygon(int v) {
  28.     vertices = new PVector[v];
  29.     r = 200;
  30.     cx = width/2;
  31.     cy = height/2;
  32.     for (int i=0; i<vertices.length; i++) {
  33.       float x = cx + cos(angle)*r;
  34.       float y = cy + sin(angle)*r;
  35.       vertices[i] = new PVector(x, y);
  36.       angle += TWO_PI/v;
  37.     }
  38.   }
  39.  
  40.   void run() {
  41.     update();
  42.     display();
  43.   }
  44.  
  45.   void update() {
  46.  
  47.     if (t<0.5) {
  48.       new_t = map(t, 0, 0.5, 0, 1);
  49.     } else {
  50.       new_t = map(t, 0.5, 1, 0, 1);
  51.     }
  52.  
  53.     fullSegments = floor(new_t * totalSegments);
  54.     partialSegment = (new_t * totalSegments) % totalSegments;
  55.     currentPosBetweenVertices = partialSegment-fullSegments;
  56.  
  57.     v1 = int(fullSegments);
  58.     v2 = int((fullSegments+1)%totalSegments);
  59.  
  60.     currPos = PVector.lerp(vertices[v1], vertices[v2], currentPosBetweenVertices);
  61.   }
  62.  
  63.   void display() {
  64.     stroke(255);
  65.     if (t<0.5) {
  66.       for (int i=0; i<fullSegments; i++) {
  67.         line(vertices[i].x, vertices[i].y, vertices[(i+1)%totalSegments].x, vertices[(i+1)%totalSegments].y);
  68.       }
  69.       line(vertices[v1].x, vertices[v1].y, currPos.x, currPos.y);
  70.     } else {
  71.       for (int i= (v1+1); i<totalSegments; i++) {
  72.         line(vertices[i].x, vertices[i].y, vertices[(i+1)%totalSegments].x, vertices[(i+1)%totalSegments].y);
  73.       }
  74.       line(currPos.x, currPos.y, vertices[v2].x, vertices[v2].y);
  75.     }
  76.   }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement