summaryrefslogtreecommitdiff
path: root/lsys/lsystem.js
blob: c3896e5345b1b403553422acff568bb7ce377a90 (plain)
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
/* LSystem */

function LSystem(axiom, ruleset) {
	this.alphabet = ['F', '+', '-', '[', ']'];
	this.axiom = axiom;
	this.rule = ["", "+", "-", "[", "]"];
	this.tree = "";

	this.rule[0] = ruleset;

	this.draw = function(x, y, zoom, angle, drawtype) {
		a.fillStyle = "#000";
		a.strokeStyle = "#000";

		a.save();
		a.translate(x, y);

		var i;

		for (i=0; i<this.tree.length; i++) {
			switch (this.tree.charAt(i)) {
				case 'F':
					switch (drawtype) {
						case 'line':
							a.beginPath();
							a.moveTo(0, 0);
							a.lineTo(zoom, 0);
							a.closePath();
							a.stroke();
							break;
						case 'hollow_circle':
							a.beginPath();
							a.arc(0, 0, zoom / 10, 0, Math.PI*2, true);
							a.closePath();
							a.stroke();
							break;
						case 'filled_circle':
							a.beginPath();
							a.arc(0, 0, zoom / 10, 0, Math.PI*2, true);
							a.closePath();
							a.fill();
							break;
						case 'faint':
							a.fillStyle = "rgba(0, 0, 0, " + (i / this.tree.length) + ")";
							a.beginPath();
							a.moveTo(Math.random() - .5, Math.random() - .5);
							a.lineTo(Math.sin(zoom) + (zoom / 2), Math.random() - .5);
							a.lineTo(zoom, 0);
							a.closePath();
							a.fill();
							break;
						case 'small_circles':
							a.fillStyle = "rgba(0, 0, 0, " + (i / this.tree.length) + 0.2 + ")";
							a.beginPath();
							a.moveTo(Math.random() - .5, Math.random() - .5);
							a.lineTo(Math.sin(zoom) + (zoom / 2), Math.random() - .5);
							a.arc(Math.sin(zoom) + (zoom / 2), Math.random() - .5, zoom / 5, 0, Math.PI*2, true);
							a.lineTo(zoom, 0);
							a.closePath();
							a.fill();
							break;
						case 'triangles':
							a.fillStyle = "rgba(0, 0, 0, " + (i / this.tree.length) + ")";
							a.beginPath();

							var dist = zoom / 2;
							var halfdist = dist / 2;
							a.moveTo(0, dist);
							a.lineTo(halfdist, halfdist);
							a.lineTo(dist, 0);
							a.lineTo(halfdist, -halfdist);
							a.lineTo(0, -dist);
							a.closePath();
							a.fill();
							break;
						case 'more_circles':
							a.fillStyle = "rgba(0, 0, 0, " + Math.random() + ")";
							a.beginPath();
							a.arc(Math.random() + .5, Math.random() - .5, (i / this.tree.length) * 5, 0, Math.PI*2, true);
							a.closePath();
							a.fill();

							a.fillStyle = "rgba(0, 0, 0, " + Math.random() + ")";
							a.beginPath();
							a.arc(Math.random() - .5, Math.random() + .5, (i / this.tree.length) * 3, 0, Math.PI*2, true);
							a.closePath();
							a.fill();

							break;
						case 'hatch':
							var red = Math.random() * 255;
							var green = Math.random() * 255;
							var blue = Math.random() * 255;
							a.strokeStyle = "rgba(" + red + ", " + green + ", " + blue + ", " + (i / this.tree.length) + ")";
							a.beginPath();

							var dist = zoom / 2;
							a.moveTo(-dist, -dist);
							a.lineTo(dist, -dist);
							a.moveTo(-dist, 0);
							a.lineTo(dist, 0);
							a.moveTo(-dist, dist);
							a.lineTo(dist, dist);
							a.closePath();
							a.stroke();
							break;
					}
					a.translate(zoom, 0);
					break;

				case '+':
					a.rotate(angle);
					break;

				case '-':
					a.rotate(-angle);
					break;

				case '[':
					a.save();
					break;

				case ']':
					a.restore();
					break;
			}
		}
		a.restore();
	}

	this.iterate = function(max_length) {
		this.tree = this.axiom;
		var rule_length = [];
		var i;

		for (i=0; i<this.alphabet.length; i++) {
			rule_length[i] = this.rule[i].length;
		}

		for (i=0; i<max_length; i++) {
			var new_length = 0;
			var j;

			for (j=0; j<this.tree.length; j++) {
				var c = this.tree.charAt(j);
				var k;

				for (k=0; k<this.alphabet.length; k++) {
					if (c == this.alphabet[k]) {
						new_length += rule_length[k];
						break;
					}
				}
			}

			var new_tree = [];
		
			for (j=0; j<this.tree.length; j++) {
				var c = this.tree.charAt(j);
				var k;

				for (k=0; k<this.alphabet.length; k++) {
					if (c == this.alphabet[k]) {
						new_tree.push(this.rule[k]);
						break;
					}
				}
			}
			this.tree = new_tree.join("");
		}
	}
}