xxxxxxxxxx
59
//https://justinpoliachik.com/posts/2021-09-13-generativetrees01/
let size = 800;
let trees = [];
function setup() {
createCanvas(size, size);//, WEBGL);
background(220);
// for (let _ = 0; _ < 5; _++) {
let startPoint = [width/2,height-1]//[random(size * 0.25, size*0.75), random(size*0.25,size*0.75)];
let length = randomBetween(90, 120);
let weight = randomBetween(20, 35);
strokeWeight(weight);
stroke(30);
let branchAngle = PI / 2;
branch(startPoint, weight, length, branchAngle);
// }
// Scale brushes to adapt to canvas size
// brush.scaleBrushes(1.5)
// Activate the flowfield we're going to use
// brush.field("seabed")
}
function draw() {
// orbitControl();
}
function branch(startPoint, weight, length, angle) {
// 𝑥1=𝑥+𝑛cos𝜃
// 𝑦1=𝑦+𝑛sin𝜃
let x1 = startPoint[0] + length * cos(angle);
let y1 = startPoint[1] - length * sin(angle);
let endpoint = [x1, y1];
strokeWeight(weight);
line(startPoint[0], startPoint[1], endpoint[0], endpoint[1]);
let angleMax = angle + PI / 4;
let angleMin = angle - PI / 4;
let angleDiff = randomBetween(0, angleMax - angleMin - PI / 16);
let angle1 = angleMax - angleDiff / 2;
let angle2 = angleMin + angleDiff / 2;
let newWeight = randomBetween(weight * 0.6, weight * 0.8);
let newLength = randomBetween(length * 0.7, length * 0.9);
if (newLength < 3) {
return;
}
branch(endpoint, newWeight, newLength, angle1);
branch(endpoint, newWeight, newLength, angle2);
}
function randomBetween(low, high) {
return random(high - low) + low;
}