-
Notifications
You must be signed in to change notification settings - Fork 0
/
Branch.java
62 lines (51 loc) · 1.49 KB
/
Branch.java
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
52
53
54
55
56
57
58
59
60
61
62
import java.awt.Graphics;
import java.awt.Point;
public class Branch {
int x;
int y;
double angle;
int depth;
double length;
double dTheta;
/** - x and y are the points of origin
- If depth 0, reached end of tree. The depth will tell information about
how long to make the branch
- Angle is the angle of the current branch
- dTheta is used to increase/decrease the angle the children branch off into
- length is the length of the line drawn
*/
public Branch(int x, int y, double angle, int depth, double length, double dTheta) {
this.x = x;
this.y = y;
this.angle = angle;
this.depth = depth;
this.length = length;
this.dTheta = dTheta;
}
public void setdTheta(double val) {
this.dTheta = val;
}
public void setLength(double val) {
this.length = val;
}
public void setDepth(int val) {
this.depth = val;
}
// Returns the end point of the line based on the angle and length
public Point getNewPoint() {
int newX = (int) ((Math.cos(angle) * length) + x);
int newY = (int) ((Math.sin(angle) * length) + y);
return new Point(newX, newY);
}
// Draws this branch and then draw all others
public void draw(Graphics g) {
Point p = this.getNewPoint();
g.drawLine(x, y, p.x, p.y);
if (depth != 0) {
Branch left = new Branch(p.x, p.y, angle - (Math.PI) / dTheta, depth-1, length * 0.67, dTheta);
Branch right = new Branch(p.x, p.y, angle + (Math.PI) / dTheta, depth-1, length * 0.67, dTheta);
left.draw(g);
right.draw(g);
}
}
}