Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- animatedPolygon ap1;
- void setup() {
- size(500,500);
- ap1 = new animatedPolygon(4);
- }
- void draw() {
- background(255);
- ap1.run();
- }
- class animatedPolygon {
- PVector[] vertices;
- //PVector[] vertices2;
- PVector currPos;
- int counter, currentVertex=0;
- float currentPosBetweenVertices;
- float angle, cx, cy, r;
- float stepSize=.03;
- 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;
- }
- //vertices2 = vertices.copy();
- }
- void run() {
- update();
- display();
- }
- void update() {
- if (currentVertex==vertices.length-1) {
- currPos = PVector.lerp(vertices[currentVertex], vertices[0], currentPosBetweenVertices);
- } else {
- currPos = PVector.lerp(vertices[currentVertex], vertices[currentVertex+1], currentPosBetweenVertices);
- }
- if (currentPosBetweenVertices>1) {
- currentPosBetweenVertices=0;
- if (currentVertex==vertices.length-1) {
- currentVertex=0;
- counter++;
- } else {
- currentVertex++;
- }
- }
- currentPosBetweenVertices += stepSize ;
- }
- void display() {
- stroke(0);
- for (int i=0; i<currentVertex; i++) {
- line(vertices[i].x, vertices[i].y, vertices[i+1].x, vertices[i+1].y);
- }
- line(vertices[currentVertex].x, vertices[currentVertex].y, currPos.x, currPos.y);
- /*
- stroke(255);
- for (int i=0; i<currentVertex; i++) {
- line(vertices2[i].x, vertices2[i].y, vertices2[i+1].x, vertices2[i+1].y);
- }
- line(vertices2[currentVertex].x, vertices2[currentVertex].y, currPos.x, currPos.y);
- */
- for (int i=0; i<vertices.length; i++) {
- ellipse(vertices[i].x, vertices[i].y, 10, 10);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement