Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int frames=360, cols, rows, dir;
- animatedPolygon[] aps;
- float t, new_t, offSet, partialSegment, fullSegments;
- //color[] palette = { #ECD27C, #BCD38D, #689D7E };
- color[] palette = { #FCBF05, #E00072, #007D98 };
- color bg = #120930;
- void setup() {
- size(750, 750,P2D);
- int i = 0;
- int j = 0;
- int cellSize = 150;
- cols = int(width/cellSize)-2;
- rows = int(height/cellSize)-2;
- aps = new animatedPolygon[rows*cols];
- for (int y=0; y<rows; y++) {
- if (y%2==0) {
- dir = 1;
- offSet = 0;
- } else {
- dir =-1;
- offSet = -PI/2;
- }
- color col = palette[y%3];
- for (int x=0; x<cols; x++) {
- float px = (x+1.5)*cellSize;
- float py = (y+1.5)*cellSize;
- aps[i] = new animatedPolygon(3+j, px, py, offSet, dir, col);
- i++;
- }
- j++;
- }
- strokeWeight(3);
- }
- void draw() {
- t = (frameCount%frames)/float(frames);
- background(bg);
- for (int i=0; i<aps.length; i++) {
- aps[i].run();
- }
- if (frameCount<=frames) saveFrame("image-###.gif");
- }
- class animatedPolygon {
- PVector[] vertices;
- PVector currPos;
- int counter, v, v1, v2, dir, totalSegments;
- float currentPosBetweenVertices;
- float angle, x, y, cx, cy, r, rot;
- color col;
- animatedPolygon(int v, float _x, float _y, float offSet, int _dir, color _col) {
- totalSegments = v;
- vertices = new PVector[v];
- r = width/(rows+2)*.4;
- col = _col;
- x = _x;
- y = _y;
- dir = _dir;
- angle = offSet;
- for (int i=0; i<vertices.length; i++) {
- float px = cos(angle)*r;
- float py = sin(angle)*r;
- vertices[i] = new PVector(px, py);
- 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() {
- pushMatrix();
- translate(x, y);
- rotate(rot);
- stroke(col);
- 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);
- }
- for (int i=0; i<totalSegments; i++) {
- fill(bg);
- ellipse(vertices[i].x, vertices[i].y, 10, 10);
- }
- rot += (TWO_PI/frames)*dir;
- popMatrix();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement