-
Notifications
You must be signed in to change notification settings - Fork 1
/
lsystem.js
261 lines (222 loc) · 6.54 KB
/
lsystem.js
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
lsystem.js --- Basic L-System parser in JavaScript.
Copyright (C) 2014 Jan CW Kroeze
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
function LSystem()
{
this.axiom = "";
this.string = "";
this.rules = [];
}
LSystem.prototype.setAxiom = function(axiom)
{
this.axiom = axiom;
this.string = axiom;
}
LSystem.prototype.addRule = function(rule)
{
this.rules.push(rule);
}
LSystem.prototype.step = function()
{
var string = this.string;
var newString = this.string;
$.each(this.rules, function(idx, rule)
{
var last_pos = 0;
var pos = string.slice(last_pos).search(rule.predecessor);
while (pos !== -1)
{
var match_length = rule.predecessor.length;
var match = rule.predecessor;
var matched = true;
var attribute = "";
var successor = rule.successor;
if (string.charAt(pos + match_length) == "(")
{
var rbracket = string.slice(pos).search(/\)/);
var token = string.substr(pos, rbracket + 1);
var parts = extractBrackets(token);
match = parts.string;
attribute = parts.contents;
match_length = rbracket - pos + 1;
matched = matchAttributeConditional(attribute, rule.conditional);
successor = rule.successor + "(" + applyAttributeArithmetic(attribute, rule) + ")";
}
if (matched === true)
{
var place_holder;
if (rule.predecessor.length > successor.length)
{
place_holder = spaces(rule.predecessor.length);
}
else
{
place_holder = spaces(successor.length);
}
if (rule.predecessor.length > successor.length)
{
successor = pad(successor, rule.predecessor.length);
}
string = string.slice(0, pos) +
place_holder +
string.slice(pos + match_length, string.length);
newString = newString.slice(0, pos) +
successor +
newString.slice(pos + match_length, string.length);
newString = newString.trim().replace(" ", "");
}
last_pos = pos + match_length;
pos = string.slice(last_pos).search(rule.predecessor);
if (pos != -1)
{
pos += last_pos;
}
}
});
this.string = newString.trim().replace(" ", "");
}
/**
* A rule consists of several parts. Most important is the successor and the
* predecessor. The former describes the sequence of characters that triggers
* this rule, the latter describes the sequence of characters that it is
* replaced with.
*
* Next is the conditional, this is an optional attribute placed after the
* predecessor to indicate a range of acceptable values for some attribute.
* If an input matches both the predecessor and the conditional, the rule is
* triggered.
*/
function Rule(predecessor, successor)
{
var predecessorParts = extractBrackets(predecessor);
this.predecessor = predecessorParts.string;
this.conditional = predecessorParts.contents;
var successorParts = extractBrackets(successor);
this.successor = successorParts.string;
this.attributeArithmetic = successorParts.contents;
}
function applyAttributeArithmetic(attribute, rule)
{
var type = "=";
var aaParts = rule.attributeArithmetic.split(type);
if (aaParts.length < 2)
{
type = "+";
aaParts = rule.attributeArithmetic.split(type);
}
var name = aaParts[0];
var value = aaParts[1];
var attributeValue = attribute.split("=")[1];
if (type === "=")
{
return name + "=" + value;
}
else if (type === "+")
{
var newValue = parseFloat(attributeValue) + parseFloat(value);
return name + "=" + newValue;
}
}
function matchAttributeConditional(attribute, conditional)
{
var attributeParts = attribute.split("=");
var attributeName = attributeParts[0];
var attributeValue = attributeParts[1];
var conditionalType = ">";
var conditionalParts = conditional.split(conditionalType);
if (conditionalParts.length < 2)
{
conditionalType = "=";
conditionalParts = conditional.split(conditionalType);
}
var conditionalName = conditionalParts[0];
var conditionalValue = conditionalParts[1];
if (attributeName !== conditionalName)
{
return false;
}
else
{
attributeValue = parseFloat(attributeValue);
conditionalValue = parseFloat(conditionalValue);
if (conditionalType === ">")
{
return attributeValue > conditionalValue
}
else if (conditionalType === "=")
{
return attributeValue === conditionalValue;
}
}
}
/**
* Removes brackets from a string and returns the altered string. Also returns
* the contents of the brackets in a parameter.
*
* @param string A string.
* @return An object with two attributes, 'string' and 'contents'. The former
* contains the altered string, the latter the contents of the brackets.
*/
function extractBrackets(string)
{
function Result()
{
this.string = "";
this.contents = "";
}
var result = new Result();
var pos = string.search(/\(/);
if (pos !== -1)
{
var rpos = string.search(/\)/);
result.string = string.slice(0, pos) + string.slice(rpos + 1, string.length);
result.contents = string.slice(pos + 1, rpos);
}
else
{
result.string = string;
result.contents = "";
}
return result;
}
/**
* Creates a string containing the specified amount of spaces.
*
* @param count A positive integer.
*/
function spaces(count)
{
var r = "";
for (var i = 0; i < count; i++)
{
r += " ";
}
return r;
}
/**
* Pads a string with spaces so it has a certain length.
*
* @param str A string.
* @param count A positive integer.
*/
function pad(str, count)
{
var padding = count - str.length;
var r = str;
for (var i = 0; i < padding; i++)
{
r += " ";
}
return r;
}