/
branch.js
51 lines (49 loc) · 1.29 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Branch
{
constructor(start, end)
{
this.start = start;
this.end = end;
this.grown = false;
this.flower = null;
this.thickness = 2.5;
}
setFlower(flower)
{
this.flower = flower;
}
show()
{
stroke(255);
strokeWeight(this.thickness);
line(this.start.x, this.start.y, this.end.x, this.end.y);
}
spawnA(size, angle)
{
var dir = p5.Vector.sub(this.end, this.start);
dir.mult(size);
dir.rotate(angle);
var newEnd = p5.Vector.add(dir, this.end);
var right = new Branch(this.end, newEnd);
return right;
}
spawnB(size, angle)
{
var dir = p5.Vector.sub(this.end, this.start);
dir.mult(size);
dir.rotate(-angle);
var newEnd = p5.Vector.add(dir, this.end);
var left = new Branch(this.end, newEnd);
return left;
}
growFlower()
{
this.flower.draw();
this.flower.update();
}
shake(intensity)
{
this.end.x = constrain(this.end.x + random(-intensity, intensity), this.end.x - 2, this.end.x + 2);
this.end.y = constrain(this.end.y + random(-intensity, intensity), this.end.y - 2, this.end.y + 2);
}
}