Advertisement
xeromino

ellipses

Jan 19th, 2016
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.37 KB | None | 0 0
  1. int cols = 10, 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);
  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.   randomSeed(1234);
  21.   background(255);
  22.   v = new PVector(width/2+sin(theta)*270, height/2+cos(theta)*270);
  23.   for (int i=0; i<(cols-2)*(rows-2); i++) {
  24.     squares[i].update();
  25.     squares[i].display();
  26.   }
  27.   theta += TWO_PI/frms;
  28. }
  29.  
  30. class Square {
  31.  
  32.   PGraphics square;
  33.   float x, y;
  34.  
  35.   Square(float _x, float _y) {
  36.     x = _x;
  37.     y = _y;
  38.     square = createGraphics(w, h);
  39.   }
  40.  
  41.   void update() {
  42.     float distance = dist(v.x, v.y, x+w/2, y+h/2);
  43.     float r = map(distance, 0, sqrt(sq(width)+sq(height)), 0, HALF_PI);
  44.     float x = map(distance, 0, sqrt(sq(width)+sq(height)), -30, 30);
  45.     float sz = map(distance, 0, sqrt(sq(width)+sq(height)), w*.9, w*1.2);
  46.     square.beginDraw();
  47.     square.rectMode(CENTER);
  48.     square.background(255);
  49.     square.pushMatrix();
  50.     square.translate(w/2, h/2);
  51.     square.rotate(r);
  52.     square.fill(255);
  53.     square.stroke(0);
  54.     square.ellipse(x, 0, sz, sz);
  55.     square.popMatrix();
  56.     square.endDraw();
  57.   }
  58.  
  59.   void display() {
  60.     image(square, x, y);
  61.   }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement