-
Notifications
You must be signed in to change notification settings - Fork 1
/
LSystem.java
219 lines (190 loc) · 6.21 KB
/
LSystem.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import java.util.ArrayList;
/**
* Generates a production with a context-free L-system.
*
* @author Harry Allen
*/
public class LSystem
{
private ArrayList<String> m_alphabet;
private String m_axiom;
private ArrayList<String> m_rule;
private ArrayList<String[]> defaultAlphabets;
private ArrayList<String> defaultAxioms;
private ArrayList<String[]> defaultRules;
private String m_tree;
/**
* Default Constructor. Defaults to Preset L-System #1.
*/
public LSystem()
{
defaultAlphabets = new ArrayList<String[]>();
defaultAxioms = new ArrayList<String>();
defaultRules = new ArrayList<String[]>();
m_alphabet = new ArrayList<String>();
m_rule = new ArrayList<String>();
defaultAlphabets.add(new String[] { "A", "B" });
defaultAlphabets.add(new String[] { "A", "B" });
defaultAlphabets.add(new String[] { "A", "B" });
defaultAlphabets.add(new String[] { "A", "B", "C", "D" });
defaultAlphabets.add(new String[] { "B", "L", "R", "T" });
defaultAlphabets.add(new String[] { "A", "B" });
defaultAlphabets.add(new String[] { "A", "B", "g" });
defaultAlphabets.add(new String[] { "A", "B", "g" });
defaultAxioms.add("A");
defaultAxioms.add("A B");
defaultAxioms.add("A");
defaultAxioms.add("A C A");
defaultAxioms.add("R @ @ B");
defaultAxioms.add("A");
defaultAxioms.add("A");
defaultAxioms.add("A");
defaultRules.add(new String[] { "g + g - g B", "+ g - g B" });
defaultRules.add(new String[] { "- B g + A g A + g B -", "+ A g - B g B - g A +" });
defaultRules.add(new String[] { "A + g - g B - g + g + g -", "- g + g A - g +" });
defaultRules.add(new String[] { "g - g B g + A g A + g B g - g", "+ A g - B g B - g A +", "+ C g + g - g - @ @ D g D - g - g + g C +", "- g + g @ A - g +" });
defaultRules.add(new String[] { "@ @ T L - B + + B", "- g + g - g", "@ @ R", "T g" });
defaultRules.add(new String[] { "- g g g g g + g g f g g B", " - g g g g + g + g g - g + g g g - g A" });
defaultRules.add(new String[] { "- g g g g g + g g f g g B", " - g g g g + g + g g - g + g g g - g A", "- g" });
defaultRules.add(new String[] { "g g - g + g + g - g - g + g g - g g + B", "g g - g + g g + g - g g f g + g - g g A", "- g" });
for (String x : defaultAlphabets.get(0))
m_alphabet.add(x);
m_axiom = defaultAxioms.get(0);
for (String x : defaultRules.get(0))
m_rule.add(x);
m_tree = "";
}
/**
* Generate the tree by iterating through the specified number of times, expanding symbols using the rules where applicable.
*
* @param maxLength maximum number of iterations
*/
public void iterate(int maxLength)
{
m_tree = new String(m_axiom);
for (int k = 0; k < maxLength; ++k)
{
String[] Tree = m_tree.split("");
StringBuffer newTree = new StringBuffer();
for (int i = 0; i < Tree.length; ++i)
{
for (int j = 0; j < m_alphabet.size(); ++j)
{
String alpha = m_alphabet.get(j);
if (Tree[i].equals(alpha))
Tree[i] = m_rule.get(j);
}
}
for (String str : Tree)
newTree.append(str);
m_tree = newTree.toString();
}
}
/**
* @return The current state of the L-System.
*/
public String getTree()
{
return m_tree;
}
/**
* @return The set of production rules of the L-System as an ArrayList of Strings.
*/
public ArrayList<String> getRules()
{
return m_rule;
}
/**
* @return The alphabet of the L-System as an ArrayList of Strings.
*/
public ArrayList<String> getAlphabet()
{
return m_alphabet;
}
/**
* @return The axiom for the L-System.
*/
public String getAxiom()
{
return m_axiom;
}
/**
* @return An ArrayList of all preset L-System alphabets as arrays of Strings
*/
public ArrayList<String[]> getDefaultAlphabets()
{
return defaultAlphabets;
}
/**
* @return An ArrayList of all preset L-System axioms as Strings.
*/
public ArrayList<String> getDefaultAxioms()
{
return defaultAxioms;
}
/**
* @return An ArrayList of all preset L-System production rules as arrays of Strings.
*/
public ArrayList<String[]> getDefaultRules()
{
return defaultRules;
}
/**
* Set the accepted String as the L-System's axiom.
*
* @param axiom new axiom
*/
public void setAxiom(String axiom)
{
m_axiom = axiom;
}
/**
* Set the accepted ArrayList of Strings as the L-System's alphabet.
*
* @param newalphabet new alphabet
*/
public void setAlphabet(ArrayList<String> newalphabet)
{
m_alphabet = newalphabet;
}
/**
* Set the accepted ArrayList of Strings as the L-System's production rules.
*
* @param newrules new set of rules
*/
public void setRules(ArrayList<String> newrules)
{
m_rule = newrules;
}
/**
* Set the L-System's alphabet to one of the preset L-Systems' based on the parameter.
*
* @param defnum the number of the default to be set
*/
public void setAlphabetDef(int defnum)
{
m_alphabet.clear();
for (String x : defaultAlphabets.get(defnum))
m_alphabet.add(x);
}
/**
* Set the L-System's axiom to one of the preset L-Systems' based on the parameter.
*
* @param defnum the number of the default to be set
*/
public void setAxiomDef(int defnum)
{
m_axiom = defaultAxioms.get(defnum);
}
/**
* Set the L-System's production rules to one of the preset L-Systems' based on the parameter.
*
* @param defnum the number of the default to be set
*/
public void setRulesDef(int defnum)
{
m_rule.clear();
for (String x : defaultRules.get(defnum))
m_rule.add(x);
}
}