Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int frames=300, totalSegments=6;
- animatedPolygon ap1;
- float t, new_t, partialSegment, fullSegments;
- void setup() {
- size(500, 500);
- ap1 = new animatedPolygon(totalSegments);
- strokeWeight(40);
- }
- void draw() {
- t = (frameCount%frames)/float(frames);
- background(#35465c);
- ap1.run();
- //if (frameCount<=frames) saveFrame("image-###.gif");
- }
- class animatedPolygon {
- PVector[] vertices;
- PVector currPos;
- int counter, v, v1, v2;
- float currentPosBetweenVertices;
- float angle, cx, cy, r;
- animatedPolygon(int v) {
- vertices = new PVector[v];
- r = 200;
- cx = width/2;
- cy = height/2;
- for (int i=0; i<vertices.length; i++) {
- float x = cx + cos(angle)*r;
- float y = cy + sin(angle)*r;
- vertices[i] = new PVector(x, y);
- angle += TWO_PI/v;
- }
- }
- void run() {
- update();
- display();
- }
- void update() {
- if (t<0.5) {
- new_t = map(t, 0, 0.5, 0, 1);
- } else {
- new_t = map(t, 0.5, 1, 0, 1);
- }
- fullSegments = floor(new_t * totalSegments);
- partialSegment = (new_t * totalSegments) % totalSegments;
- currentPosBetweenVertices = partialSegment-fullSegments;
- v1 = int(fullSegments);
- v2 = int((fullSegments+1)%totalSegments);
- currPos = PVector.lerp(vertices[v1], vertices[v2], currentPosBetweenVertices);
- }
- void display() {
- stroke(255);
- if (t<0.5) {
- for (int i=0; i<fullSegments; i++) {
- line(vertices[i].x, vertices[i].y, vertices[(i+1)%totalSegments].x, vertices[(i+1)%totalSegments].y);
- }
- line(vertices[v1].x, vertices[v1].y, currPos.x, currPos.y);
- } else {
- for (int i= (v1+1); i<totalSegments; i++) {
- line(vertices[i].x, vertices[i].y, vertices[(i+1)%totalSegments].x, vertices[(i+1)%totalSegments].y);
- }
- line(currPos.x, currPos.y, vertices[v2].x, vertices[v2].y);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement