Advertisement
xeromino

voronoi

May 6th, 2016
437
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // based upon:
  2. // simple and fast Voronoi diagram with Hoff's algorithm
  3. // sandorlevi, 2016
  4.  
  5. float r, angle, d=150;
  6. int n = 80, frms = 160;
  7. Cell cells[] = new Cell[n];
  8.  
  9. void setup() {
  10.   size(500, 500, P3D);
  11.   noStroke();
  12.   r = dist(0, 0, width, height);
  13.   ortho(-width / 2, width / 2, -height / 2, height / 2, 0, r);
  14.   for (int i = 0; i < n; i++) {
  15.     cells[i] = new Cell(
  16.       random(width), random(height),
  17.       random(-1, 1), random(-1, 1));
  18.   }
  19. }
  20.  
  21. void draw() {
  22.   for (int i = 0; i < n; i++) {
  23.     fill(map(i,0,n-1,50,200));
  24.     cells[i].show();
  25.     cells[i].advance();
  26.   }
  27.   fill(125, 250, 80);
  28.   float x = width/2 + cos(angle)*d ;
  29.   float y = height/2 + sin(angle)*d;
  30.   Cell cell = new Cell(x, y, 0, 0);
  31.   cell.show();
  32.  
  33.   angle += TWO_PI/frms;
  34.  
  35.   if (frameCount<=frms) saveFrame("image-###.gif");
  36. }
  37.  
  38. class Cell {
  39.   float x, y, sx, sy;
  40.  
  41.   Cell(float x, float y, float sx, float sy) {
  42.     this.x = x;
  43.     this.y = y;
  44.     this.sx = sx;
  45.     this.sy = sy;
  46.   }
  47.  
  48.   void show() {
  49.     pushMatrix();
  50.     translate(this.x, this.y, 0);
  51.     float a = 0;
  52.     float s = frms;
  53.     float p = TWO_PI / s;
  54.     beginShape(TRIANGLE_STRIP);
  55.     for (int i = 0; i < s; i++) {
  56.       vertex(0, 0, 0);
  57.       vertex(r * cos(a), r * sin(a), -r);
  58.       vertex(r * cos(a + p), r * sin(a + p), -r);
  59.       a += p;
  60.     }
  61.     endShape();
  62.     popMatrix();
  63.   }
  64.  
  65.   void advance() {
  66.     this.x += this.sx;
  67.     this.y += this.sy;
  68.     if (this.x < 0 || this.x > width) {
  69.       this.sx = -this.sx;
  70.     }    
  71.     if (this.y < 0 || this.y > height) {
  72.       this.sy = -this.sy;
  73.     }
  74.   }
  75. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement