Good morning,
I started watching videos from The coding train channel and I’m trying to replicate his fractal tree. But in a slightly different way:
Sketch.js
var tree;
var width_canvas = 640;
var height_canvas = 400;
function setup() {
angle = PI/4;
coefficient = 0.67;
createCanvas(width_canvas,height_canvas);
tree = new Tree()
}
function draw() {
background(51);
tree.show();
}
function mousePressed(){
tree.add_branch(angle,coefficient);
}
tree.js
function Tree() {
len_branch = 100;
nb_branch= 0;
this.tab = [];
this.tab[0] = new Branch(createVector(width/2,height),createVector(width/2,height-len_branch));
this.add_branch = function(angle,coefficient) {
tab_end = this.tab.length;
for (var i = tab_end-pow(2,nb_branch); i <= tab_end-1; i++) {
this.tab[2*i+1] =this.tab[i].branch(angle,coefficient);
this.tab[2*i+2] =this.tab[i].branch(angle,coefficient);
}
nb_branch ++;
}
this.show=function() {
for (var i = 0; i <this.tab.length; i++) {
this.tab[i].show();
}
}
}
branch.js
function Branch(begin, end) {
this.branch=function(angle,coefficient){
dir = p5.Vector.sub(end,begin);
dir.rotate(angle);
dir.mult(coefficient);
new_end = p5.Vector.add(dir,end);
branch = new Branch(end, new_end,angle,coefficient);
return branch;
}
this.show=function () {
stroke(255);
line(begin.x, begin.y, end.x, end.y );
}
}
My problem is the following, I only manage to display a part of the lines making up my tree.
I think I miss something with the pop() and push() functions but I can’t understand how they work.