-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsystem.js
83 lines (74 loc) · 1.94 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
function LSystem(axiom, rules) {
this.axiom = axiom;
this.ruleMap = [];
for (var i=0 ; i<rules.length ; ++i) {
var s = rules[i].split("->");
this.ruleMap[s[0]] = s[1];
}
this.iterate = function(times) {
if (!times) times = 1;
var value = axiom;
for (var i=0 ; i<times ; ++i) {
var array = stringToArray(value);
for (var key in this.ruleMap) {
// note - a regex won't work, as we don't want to alter values changed this turn.
//value = value.replace( new RegExp(key, "g"), this.ruleMap[key]);
for (var c=0 ; c<array.length ; ++c) {
var newValue = this.ruleMap[ array[c] ];
if (newValue != null) {
array[c] = newValue;
}
}
}
value = array.join("");
}
return value;
}
}
function stringToArray(s) {
var array = [];
for (var i=0 ; i<s.length ; ++i) {
array[i] = s.charAt(i);
}
return array;
}
function getDefaultLSystemHandler() {
var goForward = function(args) {
args.ctx.translate(0, args.distance);
};
var drawForward = function(args) {
args.ctx.beginPath();
args.ctx.moveTo(0, 0);
args.ctx.lineTo(0, args.distance);
args.ctx.stroke();
goForward(args);
};
return {
// Turn right
'+': function(args) { args.ctx.rotate(vary( args.angle, args.angle * args.angleVariance)); },
// Turn left
'-': function(args) { args.ctx.rotate(vary(-args.angle, args.angle * args.angleVariance)); },
// Push
'[': function(args) { args.ctx.save(); args.depth++; },
// Pop
']': function(args) { args.ctx.restore(); args.depth--; },
// Draw forward by length computed by recursion depth
'|': function(args) { args.distance /= Math.pow(2.0, args.depth); drawForward() },
'F': drawForward,
'A': drawForward,
'B': drawForward,
'G': goForward
};
}
function drawLSystem(args, handler) {
if (!handler) {
handler = getDefaultLSystemHandler();
}
args.depth = 0;
for(var i=0 ; i<args.str.length ; ++i) {
var c = args.str.charAt(i);
if (handler[c]) {
handler[c](args);
}
}
}