xxxxxxxxxx
52
function setup() {
createCanvas(600, 500);
background(20);
angleMode(DEGREES);
noLoop();
}
function draw() {
// stroke(150, 150, 0, 100);
strokeWeight(6);
for (let i = 0; i < 20; i++) {
let x = random(50, 500);
let y = random(240, 490);
tree(x, y, 0, 8);
}
}
function tree(x1, y1, angle, depth) {
stroke(depth * 5, depth * 40, depth * 5, 200);
strokeWeight(depth / 2 + 0.2);
let r = int(random(4));
let length = 40 * Math.tanh(depth / 4) * r/2;
let x2 = x1 - length * sin(angle);
let y2 = y1 - length * cos(angle);
let x3 = x1 - 0.7 * length * sin(angle);
let y3 = y1 - 0.7 * length * cos(angle);
let x4 = x1 - 0.4 * length * sin(angle);
let y4 = y1 - 0.4 * length * cos(angle);
line(x1, y1, x2, y2);
let angle1 = angle + 20 + random(-15, 15);
let angle2 = angle - 20 + random(-15, 15);
if (depth > 1) {
tree(x2, y2, angle1, depth - 1);
tree(x2, y2, angle2, depth - 1);
if (r > 2) {
tree(x3, y3, angle1, depth - 1);
}
if (r > 1) {
tree(x4, y4, angle2, depth - 1);
}
}
}