Advertisement
SneakySquid

Brownian Tree Snowflake

Feb 2nd, 2021
1,283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | None | 0 0
  1. int iterations = 5000;
  2. PVector[] snowflake = new PVector[iterations];
  3.  
  4. void setup() {
  5.   size(720, 720);
  6.   background(0);
  7.  
  8.   for (int i = 0; i < iterations; i++) {
  9.     boolean stopped = false;
  10.     PVector current = new PVector(width / 2, 0);
  11.  
  12.     while (!stopped) {
  13.       current.x -= 1;
  14.       current.y += random(-10, 10);
  15.  
  16.       if (current.x <= 0) {
  17.         stopped = true;
  18.       } else {
  19.         for (int j = 0; j < i; j++) {
  20.           PVector point = snowflake[j];
  21.           float d = dist(point.x, point.y, current.x, current.y);
  22.  
  23.           if (d <= 2) {
  24.             stopped = true;
  25.             break;
  26.           }
  27.         }
  28.       }
  29.     }
  30.  
  31.     snowflake[i] = current;
  32.   }
  33. }
  34.  
  35. void draw() {
  36.   translate(width / 2, height / 2);
  37.  
  38.   noFill();
  39.   stroke(255);
  40.  
  41.   for (int i = 0; i <= 360; i += 60) {
  42.     push();
  43.     rotate(radians(i));
  44.  
  45.     for (PVector part : snowflake) {
  46.       point(part.x, part.y);
  47.     }
  48.    
  49.     scale(1, -1);
  50.  
  51.     for (PVector part : snowflake) {
  52.       point(part.x, part.y);
  53.     }
  54.     pop();
  55.   }
  56.  
  57.   noLoop();
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement