xxxxxxxxxx
53
const maxIterations = 10000;
const oddAngle = -0.33;
const evenAngle = 0.17;
const trunkWeight = 10;
function setup() {
console.log('Build tree');
const tree = Tree.create(maxIterations);
console.log('Draw tree');
drawTree(tree);
console.log('Finished');
}
function drawTree(tree) {
setupCanvas();
moveToStart();
drawBranch(tree.getRoot());
}
function setupCanvas() {
createCanvas(innerWidth, innerHeight);
background(0);
}
function moveToStart() {
resetMatrix();
translate(width / 2, height);
}
function drawBranch(node) {
if (!node)
return;
const isEven = node.value.isEven();
const len = height / 200.0;
const angle = isEven ? evenAngle : oddAngle;
const weight = 0.3 + node.count * trunkWeight / maxIterations;
stroke(255);
strokeWeight(weight);
rotate(angle);
line(0, 0, 0, -len);
translate(0, -len);
drawBranch(node.even);
drawBranch(node.odd);
translate(0, len);
rotate(-angle);
}