Skip to content

Instantly share code, notes, and snippets.

@davidrusu
Created June 16, 2016 00:43
Show Gist options
  • Save davidrusu/cef7cb7d843c81dd4b0223edcb293419 to your computer and use it in GitHub Desktop.
Save davidrusu/cef7cb7d843c81dd4b0223edcb293419 to your computer and use it in GitHub Desktop.
int count = 1;
void setup() {
size(500, 500);
}
void keyPressed() {
if (keyCode == UP) {
count += 1;
} else if (keyCode == DOWN) {
count -= 1;
}
}
void tree(float x, float y, float length, float angle) {
if (length < 5) {
fill(0, 50);
noStroke();
float rad = 50 * random(0,1) * random(0, 1);
ellipse(x, y, rad, rad);
return;
}
float ny = y - length * sin(angle);
float nx = x + length * cos(angle);
stroke(50);
strokeWeight(length * 0.1);
line(x, y, nx, ny);
float dangle = PI/2 * ((float) mouseX / width) * random(0, 1);
float bias = PI/2 * ((float) mouseY / height - 0.5) * random(0, 1);
float new_l = length * random(0.5, 0.9);
tree(nx, ny, new_l, angle + dangle + bias);
tree(nx, ny, new_l, angle - dangle + bias);
}
void draw() {
background(255);
float horizon = 50;
fill(0);
rect(0, height - horizon, width, height - horizon);
randomSeed(0);
for (int i = 0; i < count; i++) {
float dist = random(horizon) * random(horizon) / horizon;
tree(random(width), height - dist, height / 5 / max(1, dist * 0.1), PI/2 * random(0.9, 1.1));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment