forked from Draradech/jigsaw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jigsaw-hex.html
363 lines (330 loc) · 13.5 KB
/
jigsaw-hex.html
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Jigsaw puzzle (hex)</title>
<script type="text/javascript">
// save function pieced together from here: https://stackoverflow.com/questions/19327749/javascript-blob-filename-without-link
function save(filename, data)
{
var blob = new Blob([data], {type: "text/csv"});
if (window.navigator.msSaveOrOpenBlob)
{
window.navigator.msSaveBlob(blob, filename);
}
else
{
var elem = window.document.createElement('a');
elem.href = window.URL.createObjectURL(blob);
elem.download = filename;
document.body.appendChild(elem);
elem.click();
document.body.removeChild(elem);
}
}
var seed = 1;
function random() { var x = Math.sin(seed) * 10000; seed += 1; return x - Math.floor(x); }
function uniform(min, max) { var r = random(); return min + r * (max - min); }
function rbool() { return random() > 0.5; }
function $(id) { return document.getElementById(id); }
function updateseed() { $("_seed").value = $("seed").value; update(); }
function updatetabsize() { $("_tabsize").value = $("tabsize").value + "%"; update(); }
function updatejitter() { $("_jitter").value = $("jitter").value + "%"; update(); }
function update_seed() { var val = parseFloat($("_seed").value); if (!isNaN(val)) { $("seed").value = val; } updateseed(); }
function update_tabsize() { var val = parseFloat($("_tabsize").value); if (!isNaN(val)) { $("tabsize").value = val; } updatetabsize(); }
function update_jitter() { var val = parseFloat($("_jitter").value); if (!isNaN(val)) { $("jitter").value = val; } updatejitter(); }
function next() { flip = rbool(); a = uniform(-j, j); b = uniform(-j, j); c = uniform(-j, j); d = uniform(-j, j); e = uniform(-j, j);}
function l(v) { return v; }
function w(v) { return v * (flip ? -1.0 : 1.0); }
function p0() { return {l: l(0.0), w: w(0.0),}; }
function p1() { return {l: l(0.2), w: w(a),}; }
function p2() { return {l: l(0.5 + b + d), w: w(-t + c),}; }
function p3() { return {l: l(0.5 - t + b), w: w(t + c),}; }
function p4() { return {l: l(0.5 - 2.0 * t + b - d), w: w(3.0 * t + c),}; }
function p5() { return {l: l(0.5 + 2.0 * t + b - d), w: w(3.0 * t + c),}; }
function p6() { return {l: l(0.5 + t + b), w: w(t + c),}; }
function p7() { return {l: l(0.5 + b + d), w: w(-t + c),}; }
function p8() { return {l: l(0.8), w: w(e),}; }
function p9() { return {l: l(1.0), w: w(0.0),}; }
function parse_input()
{
seed = parseInt($("seed").value);
t = parseFloat($("tabsize").value) / 200.0;
j = parseFloat($("jitter").value) / 100.0;
n = parseInt($("rings").value);
do_warp = $("warp").checked;
do_trunc = $("trunc").checked;
}
function scale(x, y)
{
return {
x: x * (1.0 / (2 * n - 4.0 / 3)) * radius,
y: y * (1.0 / (2 * n - 4.0 / 3)) * radius * Math.sqrt(0.75),
};
}
function rotate(vec, rot)
{
var cs = Math.cos(rot);
var sn = Math.sin(rot);
return {
x: vec.x * cs - vec.y * sn,
y: vec.x * sn + vec.y * cs,
};
}
function warp(vec)
{
if (!do_warp)
{
return vec;
}
var angl = Math.atan2(vec.y, vec.x) + Math.PI;
var angl60 = angl % (Math.PI / 3);
var angl30 = Math.abs((Math.PI / 6) - angl60);
var l = Math.sqrt(0.75) / Math.cos(angl30);
return {
x: vec.x / l,
y: vec.y / l,
};
}
function translate(vec)
{
return {
x: vec.x + radius + offset,
y: vec.y + radius + offset,
};
}
function process_r(x, y, rot)
{
return translate(warp(rotate(scale(x, y), rot)));
}
function process(x, y)
{
return process_r(x, y, 0);
}
function sub(v1, v2)
{
return {
x: v1.x - v2.x,
y: v1.y - v2.y,
};
}
function rot90(v)
{
return {
x: -v.y,
y: v.x,
};
}
function add(v1, v2)
{
return {
x: v1.x + v2.x,
y: v1.y + v2.y,
};
}
function mul(s, v)
{
return {
x: s * v.x,
y: s * v.y,
};
}
function lerp(p, v1, v2, op)
{
dl = sub(v2, v1);
dw = rot90(dl);
var vec = add(v1, mul(p.l, dl));
vec = add(vec, mul(p.w, dw));
return op + vec.x + " " + vec.y + " ";
}
function gentab(v1, v2, isnew)
{
var str = "";
next();
if (isnew)
{
str += lerp(p0(), v1, v2, "M ");
}
str += lerp(p1(), v1, v2, "C ");
str += lerp(p2(), v1, v2, "");
str += lerp(p3(), v1, v2, "");
str += lerp(p4(), v1, v2, "C ");
str += lerp(p5(), v1, v2, "");
str += lerp(p6(), v1, v2, "");
str += lerp(p7(), v1, v2, "C ");
str += lerp(p8(), v1, v2, "");
str += lerp(p9(), v1, v2, "");
return str;
}
function hlineseg(x, y, isnew)
{
var yeven = ((y + 1) % 4 == 0);
var xeven = (x % 2 == 0);
var yoff = (yeven ? xeven : !xeven) ? (-1.0 / 3) : (1.0 / 3);
return gentab(process(x, y + yoff), process(x + 1, y - yoff), isnew);
}
function vlineseg(x, y)
{
return gentab(process(x, y + (1.0 / 3)), process(x, y + (5.0 / 3)), true);
}
function blineseg(x, y, isnew, rot)
{
var yeven = ((y + 1) % 4 == 0);
var xeven = (x % 2 == 0);
var yoff = (yeven ? xeven : !xeven) ? (-1.0 / 3) : (1.0 / 3);
var str = "";
var vec;
if (isnew)
{
vec = process_r(x, y + yoff, rot);
str += "M " + vec.x + " " + vec.y + " ";
}
vec = process_r(x + 1, y - yoff, rot);
str += "L " + vec.x + " " + vec.y + " ";
return str;
}
function gen_dh()
{
var str = "";
var yl = 2 * n - 1;
for (var yi = -yl + 2; yi <= yl - 2; yi = yi + 2)
{
var isnew = true;
var xl = 2 * n - 1 - (Math.abs(yi) - 1) / 2;
for (var xi = -xl + 1; xi < xl - 1; xi = xi + 1)
{
str += hlineseg(xi, yi, isnew);
isnew = false;
}
}
return str;
}
function gen_dv()
{
var str = "";
var yl = 2 * n - 1;
for (var yi = -yl; yi < yl; yi = yi + 2)
{
var xl = 2 * n - 1 - (Math.abs(yi + 1)) / 2;
for (var xi = -xl + 2; xi <= xl - 2; xi = xi + 2)
{
str += vlineseg(xi, yi);
}
}
return str;
}
function gen_db()
{
var str = "";
if (!do_trunc)
{
var yi = 1 - 2 * n;
var isnew = true;
for (var rot = 0.0; rot < 2 * Math.PI - 0.1; rot = rot + Math.PI / 3)
{
for (var xi = -n; xi < n - 1; xi = xi + 1)
{
str += blineseg(xi, yi, isnew, rot);
isnew = false;
}
}
}
else
{
if (do_warp)
{
str += "M " + offset + " " + (radius + offset) + " ";
str += "a " + radius + " " + radius + " 0 1 0 " + ( 2 * radius) + " 0 ";
str += "a " + radius + " " + radius + " 0 1 0 " + (-2 * radius) + " 0 ";
}
else
{
str += "M " + offset + " " + (radius + offset) + " ";
str += "L " + (radius / 2 + offset) + " " + (radius + offset - radius * Math.sqrt(0.75)) + " ";
str += "L " + (radius * 1.5 + offset) + " " + (radius + offset - radius * Math.sqrt(0.75)) + " ";
str += "L " + (radius * 2 + offset) + " " + (radius + offset) + " ";
str += "L " + (radius * 1.5 + offset) + " " + (radius + offset + radius * Math.sqrt(0.75)) + " ";
str += "L " + (radius / 2 + offset) + " " + (radius + offset + radius * Math.sqrt(0.75)) + " ";
str += "L " + offset + " " + (radius + offset) + " ";
}
}
return str;
}
function update()
{
radius = 400;
offset = 40;
$("puzzlecontainer").setAttribute("width", 880);
$("puzzlecontainer").setAttribute("height", 880);
parse_input();
$("puzzlepath_h").setAttribute("d", gen_dh());
$("puzzlepath_v").setAttribute("d", gen_dv());
$("puzzlepath_b").setAttribute("d", gen_db());
}
function generate()
{
var diameter = parseFloat($("diameter").value);
radius = diameter / 2.0;
offset = radius * 0.2;
var width = 2.0 * (radius + offset);
var height = 2.0 * (radius + offset);
parse_input();
var data = "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.0\" ";
data += "width=\"" + width + "mm\" height=\"" + height + "mm\" viewBox=\"0 0 " + width + " " + height + "\">";
data += "<path fill=\"none\" stroke=\"DarkBlue\" stroke-width=\"0.2\" d=\"";
data += gen_dh();
data += "\"></path>";
data += "<path fill=\"none\" stroke=\"DarkRed\" stroke-width=\"0.2\" d=\"";
data += gen_dv();
data += "\"></path>";
data += "<path fill=\"none\" stroke=\"Black\" stroke-width=\"0.2\" d=\"";
data += gen_db();
data += "\"></path>";
data += "</svg>";
save("jigsaw.svg", data);
}
</script>
</head>
<body onload="$('seed').value = Math.random() * 10000; updateseed();">
<table>
<tr>
<td>Seed:</td>
<td><input id="_seed" type="text" value="0" onchange="update_seed()"/></td>
<td><input id="seed" type="range" value="0" min="0" max="9999" step="1" onchange="updateseed()"/></td>
</tr>
<tr>
<td>Tab Size:</td>
<td><input id="_tabsize" type="text" value="27%" onchange="update_tabsize()"/></td>
<td><input id="tabsize" type="range" value="27" min="15" max="35" step="0.1" onchange="updatetabsize()"/></td>
</tr>
<tr>
<td>Jitter:</td>
<td><input id="_jitter" type="text" value="5%" onchange="update_jitter()"/></td>
<td><input id="jitter" type="range" value="5" min="0" max="13" step="0.1" onchange="updatejitter()"/></td>
</tr>
<tr>
<td>Diameter:</td>
<td><input id="diameter" type="text" value="240" size="4" onchange="update()"/> mm</td>
<td></td>
</tr>
<tr>
<td>Rings:</td>
<td><input id="rings" type="text" value="6" size="4" onchange="update()"/></td>
<td></td>
</tr>
<tr>
<td>Circle Warp:</td>
<td><input id="warp" type="checkbox" onchange="update()"/></td>
<td></td>
</tr>
<tr>
<td>Truncate Edge Pieces:</td>
<td><input id="trunc" type="checkbox" onchange="update()"/></td>
<td><button onclick="generate()">Download SVG</button></td>
</tr>
</table>
<svg id="puzzlecontainer"><path id="puzzlepath_h" fill="none" stroke="DarkBlue"></path><path id="puzzlepath_v" fill="none" stroke="DarkRed"></path><path id="puzzlepath_b" fill="none" stroke="Black"></path></svg>
<p><a href="index.html">Back</a></p>
<a href="https://github.com/Draradech/jigsaw" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#FD6C6C; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
</body>
</html>