-
Notifications
You must be signed in to change notification settings - Fork 0
/
dragon.js
261 lines (239 loc) · 7.31 KB
/
dragon.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
var initial = {
start: { x: 0, y: 0 },
end: { x: 300, y: 0}
};
// goldenDragon calculations based on http://ecademy.agnesscott.edu/~lriddle/ifs/heighway/goldenDragon.htm
var goldenRatio = (1 + Math.sqrt(5)) / 2,
gdR = Math.pow(1/goldenRatio, 1/goldenRatio),
gdR2 = Math.pow(gdR, 2),
gdA = length(initial),
gdB = gdR * gdA,
gdC = gdR2 * gdA,
gdX = (Math.pow(gdA,2) + Math.pow(gdB,2) - Math.pow(gdC, 2)) / (2*gdA),
gdY = Math.sqrt(Math.pow(gdB,2) - Math.pow(gdX,2));
var rules = [
{
name: "Heighway Dragon",
rule: [{
start: { x: 0, y: 0 },
end: { x: 150, y: -150 }
},{
start: { x: 300, y: 0 },
end: { x: 150, y: -150 }
}]
},{
name: "Dragon of Eve",
rule: [{
start: { x: 0, y: 0 },
end: { x: 0, y: -150 }
},{
start: { x: 0, y: -150 },
end: { x: 150, y: 0 }
},{
start: { x: 300, y: 0 },
end: { x: 150, y: 0 }
}]
},{
name: "Terdragon",
rule: [{
start: { x: 0, y: 0 },
end: { x: 150, y: -150*Math.tan(Math.PI/6) }
},{
start: { x: 150, y: -150*Math.tan(Math.PI/6) },
end: { x: 150, y: 150*Math.tan(Math.PI/6) }
},{
start: { x: 150, y: 150*Math.tan(Math.PI/6) },
end: { x: 300, y: 0 }
}]
},{
name: "Golden Dragon",
rule: [{
start: { x: 0, y: 0 },
end: { x: gdX, y: -gdY }
},{
start: { x: 300, y: 0 },
end: { x: gdX, y: -gdY }
}]
},{
name: "Koch",
rule: [{
start: { x: 0, y: 0 },
end: { x: 100, y: 0 }
},{
start: { x: 100, y: 0 },
end: { x: 150, y: -100*Math.sin(Math.PI/3) }
},{
start: { x: 150, y: -100*Math.sin(Math.PI/3) },
end: { x: 200, y: 0 }
},{
start: { x: 200, y: 0 },
end: { x: 300, y: 0 }
}]
}
];
var transforms, lines;
// KK, plans:
// - curvy rendering so the path is more apparent
// - interactive rule editor
// - optional grid snapping
// - grid always shows (so they know its edit mode)
// - make them click-drag to draw new lines WITH A DIRECTION
// - direction of original line is indicated by gradient, mayhaps
// - might need a way to fix particular angles? eh, maybe just a text input that lets
// you add lines and specify start/end points for them. let them do the math themselves.
// it's for power users.
// - d3 animation stuff maybe? if not the "stretch from original line" type thing
// that dragon of eve does, then maybe like fade in as red, then fade to white while
// original fades to nothingness
// - explore performance issues. drawing one long path rather than thousands of
// short ones, more efficient storage of `lines` array, maybe
// singleTransform/matrixVecMultiply during iterations can be faster (e.g.
// cuz vector's z is always 1), etc.
var selector = '#draw-here',
element = d3.select(selector);
var svg = element.append('svg')
.attr({
width: '100%',
height: '100%'
});
var thing = svg.append('g')
.attr('transform', 'translate(150, 350)');
// mat:
// [[A, B, C],
// [D, E, F],
// [G, H, I]]
// vec:
// [x,
// y,
// z]
function matrixMult(m1, m2) {
var res = [];
var m2Cols = transverse(m2);
res[0] = matrixVecMult(m1, m2Cols[0]);
res[1] = matrixVecMult(m1, m2Cols[1]);
res[2] = matrixVecMult(m1, m2Cols[2]);
return transverse(res);
}
function transverse(m) {
return [[m[0][0], m[1][0], m[2][0]],
[m[0][1], m[1][1], m[2][1]],
[m[0][2], m[1][2], m[2][2]]];
}
function matrixVecMult(m, v) {
return [m[0][0]*v[0] + m[0][1]*v[1] + m[0][2]*v[2],
m[1][0]*v[0] + m[1][1]*v[1] + m[1][2]*v[2],
m[2][0]*v[0] + m[2][1]*v[1] + m[2][2]*v[2]];
}
function angleOf(line) {
var dx = line.end.x - line.start.x,
dy = line.end.y - line.start.y;
return Math.atan2(dy, dx);
}
function ruleToTransform(rule) {
// what's its length compared to initial -- that's Scale
// where is start compared to initial (0,0) -- that's Translate
// how does it point compared to initial (0rad) -- that's Rotate
// then do T*S*R
var s = length(rule) / length(initial),
S = [[s, 0, 0],
[0, s, 0],
[0, 0, 1]],
r = angleOf(rule),
R = [[Math.cos(r), -Math.sin(r), 0],
[Math.sin(r), Math.cos(r), 0],
[0, 0, 1]],
dx = rule.start.x - initial.start.x,
dy = rule.start.y - initial.start.y,
T = [[1, 0, dx],
[0, 1, dy],
[0, 0, 1]];
return matrixMult(T, matrixMult(S, R));
}
function singleTransform(line, matrix) {
var start = matrixVecMult(matrix, [line.start.x, line.start.y, 1]),
end = matrixVecMult(matrix, [line.end.x, line.end.y, 1]);
return {
start: { x: start[0], y: start[1] },
end: { x: end[0], y: end[1] }
};
}
function applyTransforms(xforms, line) {
return xforms.map(function(xform) {
return singleTransform(line, xform);
});
}
function length(line) {
return Math.sqrt(Math.pow(line.end.x - line.start.x, 2) + Math.pow(line.end.y - line.start.y, 2));
}
function render(lines) {
var drawnLines = thing.selectAll('line').data(lines);
drawnLines.enter()
.append('line');
drawnLines.attr({
'x1': function(d) { return d.start.x; },
'y1': function(d) { return d.start.y; },
'x2': function(d) { return d.end.x; },
'y2': function(d) { return d.end.y; },
'stroke': '#fff'
});
drawnLines.exit().remove();
}
function iterate() {
var newLines = lines.reduce(function(result, line) {
applyTransforms(transforms, line).forEach(function(l) { result.push(l); });
return result;
}, []);
lines = newLines;
render(lines);
}
function resetWithRule(rule) {
transforms = rule.rule.map(ruleToTransform);
lines = [initial];
render(lines);
}
function firstIterationFor(rule) {
return applyTransforms(rule.rule.map(ruleToTransform), initial);
}
svg.append('text')
.text('Click inside this box to grow the dragon') // TODO: make this say "...grow the <CURRENT DRAGON NAME>" instead
.attr('fill', '#2a2')
.attr('transform', 'translate(5 20)');
svg.on('mousedown', function() {
d3.event.preventDefault(); // so the text doesn't get selected on double-clicks
iterate();
});
// TODO: this be a big mess.
var sampleArea = d3.select('#samples');
var samples = sampleArea.selectAll('div').data(rules);
var samplesFrd = samples.enter()
.append('div')
.classed('sample', true)
.on('click', resetWithRule);
var sampleSvgs = samplesFrd.append('svg')
.classed('sample-svg', true)
.on('mouseover', function() {
d3.select(this).classed('sample-svg--hovered', true);
})
.on('mouseout', function() {
d3.select(this).classed('sample-svg--hovered', false);
});
sampleSvgs.append('g').attr('transform', 'translate(45 50) scale(0.3)')
.selectAll('line').data(function(d) { return firstIterationFor(d); })
.enter()
.append('line')
.attr({
'x1': function(d) { return d.start.x; },
'y1': function(d) { return d.start.y; },
'x2': function(d) { return d.end.x; },
'y2': function(d) { return d.end.y; },
'stroke': '#fff'
});
sampleSvgs.append('text')
.text(function(d) { return d.name; })
.attr({
x: '100%',
y: '100%',
transform: 'translate(-5 -5)'
});
// samplesFrd.append('span').text(function(d) { return d.name; }).classed('sample-name', true);
resetWithRule(rules[0]);