Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // based upon:
- // simple and fast Voronoi diagram with Hoff's algorithm
- // sandorlevi, 2016
- float r, angle, d=150;
- int n = 80, frms = 160;
- Cell cells[] = new Cell[n];
- void setup() {
- size(500, 500, P3D);
- noStroke();
- r = dist(0, 0, width, height);
- ortho(-width / 2, width / 2, -height / 2, height / 2, 0, r);
- for (int i = 0; i < n; i++) {
- cells[i] = new Cell(
- random(width), random(height),
- random(-1, 1), random(-1, 1));
- }
- }
- void draw() {
- for (int i = 0; i < n; i++) {
- fill(map(i,0,n-1,50,200));
- cells[i].show();
- cells[i].advance();
- }
- fill(125, 250, 80);
- float x = width/2 + cos(angle)*d ;
- float y = height/2 + sin(angle)*d;
- Cell cell = new Cell(x, y, 0, 0);
- cell.show();
- angle += TWO_PI/frms;
- if (frameCount<=frms) saveFrame("image-###.gif");
- }
- class Cell {
- float x, y, sx, sy;
- Cell(float x, float y, float sx, float sy) {
- this.x = x;
- this.y = y;
- this.sx = sx;
- this.sy = sy;
- }
- void show() {
- pushMatrix();
- translate(this.x, this.y, 0);
- float a = 0;
- float s = frms;
- float p = TWO_PI / s;
- beginShape(TRIANGLE_STRIP);
- for (int i = 0; i < s; i++) {
- vertex(0, 0, 0);
- vertex(r * cos(a), r * sin(a), -r);
- vertex(r * cos(a + p), r * sin(a + p), -r);
- a += p;
- }
- endShape();
- popMatrix();
- }
- void advance() {
- this.x += this.sx;
- this.y += this.sy;
- if (this.x < 0 || this.x > width) {
- this.sx = -this.sx;
- }
- if (this.y < 0 || this.y > height) {
- this.sy = -this.sy;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement