Skip to content

Instantly share code, notes, and snippets.

@avdotion
Created June 11, 2018 14:29
Show Gist options
  • Save avdotion/1c08af1d2d479a3b65fda4ed8099de1a to your computer and use it in GitHub Desktop.
Save avdotion/1c08af1d2d479a3b65fda4ed8099de1a to your computer and use it in GitHub Desktop.
Fractal Trees - Recursive (p5.js)
const maxRecurtionDepth = 15;
let slider, angle;
const len = 200;
function setup() {
createCanvas(600, 600);
slider = createSlider(0, PI, PI/4, PI/16);
}
function draw() {
background(51);
angle = slider.value();
stroke(255);
translate(width/2, height);
branch(len, maxRecurtionDepth);
}
function branch(len, depth) {
if (depth > 0) {
line(0, 0, 0, -len);
translate(0, -len);
push();
rotate(angle);
branch(len * 2 / 3, depth - 1);
pop();
push();
rotate(-angle);
branch(len * 2 / 3, depth - 1);
pop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment