-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBarnsley.java
62 lines (51 loc) · 1.83 KB
/
Barnsley.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
/* ****************************************************************************
* Compilation: javac-introcs Barnsley.java
* Execution: java-introcs Barnsley n
* Dependencies: StdDraw.java
*
* Play chaos game to produce Barnsley's fern.
*
* % java-introcs Barnsley 10000
*
* ****************************************************************************/
public class Barnsley {
public static void main(String[] args) {
int n = Integer.parseInt(args[0]); // number of points to draw
StdDraw.setScale(-0.1, 1.1); // leave a 10% border
StdDraw.clear(StdDraw.BOOK_LIGHT_BLUE); // background color
StdDraw.setPenColor(0, 114, 0); // a shade of green
// starting point
double x = 0.5;
double y = 0.0;
// repeated choose one of four update rules at random
for (int i = 0; i < n; i++) {
double tempx;
double tempy;
double r = StdRandom.uniformDouble(0.0, 1.0);
// stem
if (r <= 0.01) {
tempx = 0.50;
tempy = 0.16 * y;
}
// largest left-hand leaflet
else if (r <= 0.08) {
tempx = 0.20 * x - 0.26 * y + 0.400;
tempy = 0.23 * x + 0.22 * y - 0.045;
}
// largest right-hand leaflet
else if (r <= 0.15) {
tempx = -0.15 * x + 0.28 * y + 0.575;
tempy = 0.26 * x + 0.24 * y - 0.086;
}
// successively smaller leaflets
else {
tempx = 0.85 * x + 0.04 * y + 0.075;
tempy = -0.04 * x + 0.85 * y + 0.180;
}
// update (x, y) and draw point
x = tempx;
y = tempy;
StdDraw.point(x, y);
}
}
}