-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tree.java
37 lines (31 loc) · 1.22 KB
/
Tree.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
/******************************************************************************
* Compilation: javac Tree.java
* Execution: java Tree n
* Dependencies: StdDraw.java
*
* Plot a tree fractal.
*
* % java Tree 9
*
******************************************************************************/
public class Tree {
public static void tree(int n, double x, double y, double a, double branchRadius) {
double bendAngle = Math.toRadians(15);
double branchAngle = Math.toRadians(37);
double branchRatio = 0.65;
double cx = x + Math.cos(a) * branchRadius;
double cy = y + Math.sin(a) * branchRadius;
StdDraw.setPenRadius(0.001 * Math.pow(n, 1.2));
StdDraw.line(x, y, cx, cy);
if (n == 0) return;
tree(n-1, cx, cy, a + bendAngle - branchAngle, branchRadius * branchRatio);
tree(n-1, cx, cy, a + bendAngle + branchAngle, branchRadius * branchRatio);
tree(n-1, cx, cy, a + bendAngle, branchRadius * (1 - branchRatio));
}
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
StdDraw.enableDoubleBuffering();
tree(n, 0.5, 0, Math.PI/2, 0.3);
StdDraw.show();
}
}