-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsystems.pde
195 lines (151 loc) · 3.96 KB
/
lsystems.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
Lsystem lsys;
Turtle turtle;
float generations, done;
HashMap rules, alpha;
String axiom;
void setup()
{
size(7500,800);
background(0);
stroke(255);
smooth();
generations = 12;
done = 0;
Dragon(); // Call L-System setup function
}
void draw()
{
while(done<generations) {
DragonDraw();
done++;
}
}
void Cantor()
{
HashMap rules = new HashMap();
rules.put("A", "A B A");
rules.put("B","B B B");
HashMap alpha = new HashMap();
alpha.put("A", "forward 15");
alpha.put("B", "jumpf 15");
String axiom = "A";
turtle = new Turtle(5, height/2);
lsys = new Lsystem(alpha, rules, axiom);
}
void CantorDraw()
{
lsys.generate();
lsys.renderWith(turtle);
turtle.moveTo(5.0, turtle.Y-20, 0.0);
}
void KochCurve()
{
rules = new HashMap();
rules.put("F", "F + F - F - F + F");
alpha = new HashMap();
alpha.put("F", "forward 10");
alpha.put("+" , "right 90");
alpha.put("-", "left 90");
axiom = "F";
turtle = new Turtle(0, height*.95);
lsys = new Lsystem(alpha, rules, axiom);
}
void KochCurveDraw()
{
lsys.generate();
lsys.renderWith(turtle);
float newStart = height*(1-((done+1)/generations));
turtle.moveTo(0.0, newStart, 0.0);
}
void Sierpinski()
{
rules = new HashMap();
rules.put("A", "B - A - B");
rules.put("B", "A + B + A");
alpha = new HashMap();
alpha.put("A", "forward 2");
alpha.put("B", "forward 2");
alpha.put("+" , "right 60");
alpha.put("-", "left 60");
axiom = "A";
turtle = new Turtle(width/3, 0);
lsys = new Lsystem(alpha, rules, axiom);
}
void SierpinskiDraw()
{
lsys.generate();
if(done%2 == 1) {
lsys.renderWith(turtle);
float newStart = height*((1+done)/generations);
turtle.moveTo(done*10, newStart, 0.0);
}
}
void Penrose()
{
rules = new HashMap();
rules.put("1", "");
rules.put("6","8 1 + + 9 1 − − − − 7 1 [ − 8 1 − − − − 6 1 ] + +");
rules.put("7","+ 8 1 − − 9 1 [ − − − 6 1 − − 7 1 ] +");
rules.put("8", "− 6 1 + + 7 1 [ + + + 8 1 + + 9 1 ] −");
rules.put("9","− − 8 1 + + + + 6 1 [ + 9 1 + + + + 7 1 ] − − 7 1");
alpha = new HashMap();
alpha.put("1", "forward 25");
alpha.put("+", "left 36");
alpha.put("-", "right 36");
alpha.put("[","push");
alpha.put("]", "pop");
axiom = "[ 7 ] + + [ 7 ] + + [ 7 ] + + [ 7 ] + + [ 7 ]";
turtle = new Turtle(400,height/2);
lsys = new Lsystem(alpha, rules, axiom);
}
void PenroseDraw()
{
float next = done + 1;
lsys.generate();
lsys.renderWith(turtle);
save("output/penrose"+next+".png");
turtle.moveTo((next*next*100), height/2,0.0);
stroke(255*(1-next/generations),255*(next/generations),255*(1-next/generations));
}
void Levy()
{
rules = new HashMap();
rules.put("F", "+ F - - F +");
alpha = new HashMap();
alpha.put("F","forward 10");
alpha.put("+","right 45");
alpha.put("-", "left 45");
axiom = "F";
turtle = new Turtle(0, 0.8*height);
lsys = new Lsystem(alpha,rules,axiom);
}
void LevyDraw()
{
float next = done +1;
lsys.generate();
lsys.renderWith(turtle);
save("output/levt"+next+".png");
turtle.moveTo((next*next*50), height*.8, 0.0);
}
void Dragon()
{
rules = new HashMap();
rules.put("X", "X + Y F +");
rules.put("Y", "- F X - Y");
alpha = new HashMap();
alpha.put("F", "forward 10");
alpha.put("-", "left 90");
alpha.put("+", "right 90");
axiom = "F X";
turtle = new Turtle(0, 0.7*height);
lsys = new Lsystem(alpha,rules,axiom);
}
void DragonDraw()
{
float next = done +1;
lsys.generate();
lsys.renderWith(turtle);
save("output/dragon"+next+".png");
turtle.moveTo((next*next*50), height*.7, 0.0);
stroke(255*(next/generations),255*(1-next/generations),186*(next/generations));
}