-
Notifications
You must be signed in to change notification settings - Fork 0
/
L_System_Fractal_Tree.pde
81 lines (68 loc) · 1.54 KB
/
L_System_Fractal_Tree.pde
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*variables : X F
constants : + − [ ]
axiom : X
rules : (X → F−[[X]+X]+F[+FX]−X), (F → FF)
angle : 25°*/
String prev;
String last;
void setup() {
size(640, 480);
prev = "X";
last = prev;
}
int count = 0;
float size = 128;
void draw() {
background(255);
translate(width / 4, height);
rotate(radians(10));
for (int i = 0; i < last.length(); i++) {
if (last.charAt(i) == 'F') {
line(0, 0, 0, -size);
translate(0, -size);
//size = size / 1.01;
} else if (last.charAt(i) == '-') {
rotate(radians(-25));
} else if (last.charAt(i) == '+') {
rotate(radians(25));
} else if (last.charAt(i) == '[') {
pushMatrix();
} else if (last.charAt(i) == ']') {
popMatrix();
}
}
prev = last;
last = "";
for (int i = 0; i < prev.length(); i++) {
if (prev.charAt(i) == 'X') {
last += "F−[[X]+X]+F[+FX]−X";
} else if (prev.charAt(i) == 'F') {
last += "FF";
} else last += prev.charAt(i);
}
size = (float) Math.pow(2, 7 - count);
count++;
println(count);
}
void generate() {
prev = last;
last = "";
for (int i = 0; i < prev.length(); i++) {
if (prev.charAt(i) == 'X') {
last += "F−[[X]+X]+F[+FX]−X";
} else if (prev.charAt(i) == 'F') {
last += "FF";
} else last += prev.charAt(i);
}
size = (float) Math.pow(2, 7 - count);
count++;
println(prev);
println(count);
}
void keyPressed() {
if (key == ' ') {
generate();
} else if (key == 's') {
saveFrame("Fractal_tree_###.png");
}
}