-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbranch.js
36 lines (29 loc) · 1.28 KB
/
branch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
function Branch(start, end, thickness, initcolor, endcolor) {
this.start = start;
this.end = end;
this.drawn = false;
this.thickness = thickness;
this.initcolor = initcolor;
this.endcolor = endcolor;
this.show = function() {
stroke(this.initcolor); //desenha o ramo
strokeWeight(this.thickness);
line(this.start.x, this.start.y, this.end.x, this.end.y);
}
this.branchLeft = function() {
let prevVector = p5.Vector.sub(this.end, this.start);
prevVector.rotate(random(PI/6, PI/4));
prevVector.mult(random(0.4, 0.8)); //ramo anterior
let newEnd = p5.Vector.add(this.end, prevVector);
let newB = new Branch(this.end, newEnd, this.thickness * 0.8, lerpColor(this.initcolor, this.endcolor, 0.2), color(36, 92, 93)); //novo ramo com base no anterior
return newB;
}
this.branchRight = function() {
let prevVector = p5.Vector.sub(this.end, this.start);
prevVector.rotate(random(-PI/4, -PI/6));
prevVector.mult(random(0.4, 0.8)); //ramo anterior
let newEnd = p5.Vector.add(this.end, prevVector);
let newB = new Branch(this.end, newEnd, this.thickness * 0.8, lerpColor(this.initcolor, this.endcolor, 0.2), color(36, 92, 93)); //novo ramo com base no anterior
return newB;
}
}