-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBranch.js
More file actions
51 lines (44 loc) · 1.6 KB
/
Copy pathBranch.js
File metadata and controls
51 lines (44 loc) · 1.6 KB
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
function Branch (begin, end) {
// Declare variables
this.begin = begin;
this.end = end;
this.finish = false
this.len = 0.68;
this.angle = PI/6;
// Show the branches
this.show = function () {
stroke(88,71,57);
line(this.begin.x, this.begin.y, this.end.x, this.end.y);
}
//---------------------------------------
// Create the left and right branches
this.branchR = function () {
// Create a direction vector that points
// from the botton to the top of a branch
var dir = p5.Vector.sub(this.end, this.begin);
// Rotate that branch and shrink it
dir.rotate(this.angle);
dir.mult(this.len);
// Create a new end point by adding the end point with the direction vector
var newEnd = p5.Vector.add(this.end, dir);
// Create the new branch on the right
// using the last point in the last branch
var r = new Branch(this.end, newEnd);
return r;
}
//---------------------------------------
this.branchL = function () {
// Create a direction vector that points
// from the botton to the top of a branch
var dir = p5.Vector.sub(this.end, this.begin);
// Rotate that branch and shrink it
dir.rotate(-this.angle);
dir.mult(this.len);
// Create a new end point by adding the end point with the direction vector
var newEnd = p5.Vector.add(this.end, dir);
// Create the new branch on the right
// using the last point in the last branch
var l = new Branch(this.end, newEnd);
return l;
}
}