# ellipses2

Jan 19th, 2016
263
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. int cols = 12, rows = cols, w, h, frms = 120;
2. Square[] squares = new Square[rows*cols];
3. float theta;
4. PVector v;
5.
6. void setup() {
7.   size(540, 540, P2D);
8.   w = 540/cols;
9.   h = 540/rows;
10.   int i=0;
11.   for (int x=1; x<(rows-1); x++) {
12.     for (int y=1; y<(cols-1); y++) {
13.       squares[i] = new Square(x*w, y*h);
14.       i++;
15.     }
16.   }
17. }
18.
19. void draw() {
20.   background(0);
21.   v = new PVector(width/2+sin(theta)*width/2, height/2+cos(theta)*height/2);
22.   for (int i=0; i<(cols-2)*(rows-2); i++) {
23.     println(i);
24.     squares[i].update();
25.     squares[i].display();
26.   }
27.   theta += TWO_PI/frms;
28.   //if (frameCount<=frms) saveFrame("image-###.gif");
29. }
30.
31. class Square {
32.
33.   PGraphics square;
34.   float x, y;
35.
36.   Square(float _x, float _y) {
37.     x = _x;
38.     y = _y;
39.     square = createGraphics(w, h);
40.   }
41.
42.   void update() {
43.     float distance = dist(v.x, v.y, x+w/2, y+h/2);
44.     float r = map(distance, 0, sqrt(sq(width)+sq(height)), 0, HALF_PI);
45.     float x = map(distance, 0, sqrt(sq(width)+sq(height)), -30, 30);
46.     float sz = map(distance, 0, sqrt(sq(width)+sq(height)), w*.9, w*1.2);
47.     square.beginDraw();
48.     square.rectMode(CENTER);
49.     square.background(0);
50.     square.pushMatrix();
51.     square.translate(w/2, h/2);
52.     square.rotate(r);
53.     square.fill(0);
54.     square.stroke(255);
55.     square.ellipse(x, 0, sz, sz);
56.     square.ellipse(x, x/2, sz/2, sz/2);
57.     square.popMatrix();
58.     square.endDraw();
59.   }
60.
61.   void display() {
62.     image(square, x, y);
63.   }
64. }