-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
371 lines (326 loc) · 14 KB
/
index.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
364
365
366
367
368
369
370
371
<!DOCTYPE html>
<html>
<head>
<!-- HTML and several functions based or copied from https://github.com/Draradech/jigsaw -->
<meta charset="UTF-8">
<title>Gosper curve jigsaw puzzle generator</title>
<script type="text/javascript">
var seed = 1;
var corder, psize, rad, stepsize, framecorner, minp, maxp;
var jig;
function $(id) { return document.getElementById(id); }
function random() {
if($("ndr").checked){
return Math.random();
}else{
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 updateseed() { $("_seed").value = $("seed").value; update(); }
function updatecurveorder() { $("_corder").value = $("corder").value; update(); }
function updatepsize() { $("_psize").value = $("psize").value; update(); }
function update_seed() { var val = parseInt($("_seed").value); if (!isNaN(val)) { $("seed").value = val; } updateseed(); }
function update_corder() { var val = parseInt($("_corder").value); if (!isNaN(val)) { $("corder").value = val; } updatecurveorder(); }
function update_size() { var val = parseInt($("_psize").value); if (!isNaN(val)) { $("psize").value = val; } updatepsize(); }
class Point {
constructor(x, y) {
this._x = x;
this._y = y;
this.break = false;
}
get x() {
return this._x;
}
get y() {
return this._y;
}
eq(p) {
return (this.x == p.x && this.y == p.y);
}
}
class Gosper {
constructor(order, size) {
this.order = order;
this.size = size;
this.points = [new Point(0, 0)];
this.lp = this.points[0];
this.dir = 0;
this.sizes = size * 0.86602540378443864676372317075294;
this.sizeh = size / 2;
this.incs = [[0, this.size], [this.sizes, this.sizeh], [this.sizes, -this.sizeh], [0, -this.size], [-this.sizes, -this.sizeh], [-this.sizes, this.sizeh]]
this.pieces = [];
this.cutlines = [];
const xpts_ord = [1, 3, 9, 25, 63, 173, 471];
const ypts_ord = [3, 8, 16, 41, 115, 303, 775];
this.xpts = xpts_ord[this.order];
this.ypts = ypts_ord[this.order];
this.occupancy = new Array(this.xpts * this.ypts).fill(-1);
}
left() {
this.dir = this.dir - 1;
if (this.dir < 0) {
this.dir = 5;
}
}
right() {
this.dir = this.dir + 1;
this.dir %= 6;
}
move() {
this.lp = new Point(this.lp.x + this.incs[this.dir][0], this.lp.y + this.incs[this.dir][1]);
this.points.push(this.lp);
}
gosper_step(order, is_A) {
if (order == 0) {
this.move();
return;
}
if (is_A) {
var seq = [0, 3, 1, 3, 3, 1, 2, 0, 2, 2, 0, 0, 2, 1, 3];
} else {
var seq = [2, 0, 3, 1, 1, 3, 3, 1, 3, 0, 2, 2, 0, 2, 1];
}
seq.forEach((c) => {
switch (c) {
case 0:
this.gosper_step(order - 1, true);
break;
case 1:
this.gosper_step(order - 1, false);
break;
case 2:
this.left();
break;
case 3:
this.right();
break;
}
});
}
generate() {
this.gosper_step(this.order, true);
}
extents() {
var minx = Math.min(...this.points.map((p) => p.x));
var maxx = Math.max(...this.points.map((p) => p.x));
var miny = Math.min(...this.points.map((p) => p.y));
var maxy = Math.max(...this.points.map((p) => p.y));
return [minx, maxx, miny, maxy]
}
normalize() {
var extents = this.extents();
var newpoints = []
this.points.forEach((p) => {
newpoints.push(new Point(p.x - extents[0], p.y - extents[2]));
});
this.points = newpoints;
}
width() {
return this.extents()[1] - this.extents()[0];
}
height() {
return this.extents()[3] - this.extents()[2];
}
svgpoints() {
var data = "";
this.points.forEach((p) => {
data += p.x + "," + p.y + " ";
});
data = data.slice(0, -1);
return data;
}
gen_occupancy(self) {
for (var i = 0; i < this.points.length; i++) {
var p = this.points[i];
var x = Math.round(p.x / this.sizes);
var y = Math.round(p.y / this.sizeh);
this.occupancy[x * this.ypts + y] = i;
}
}
makepieces() {
var piece = [];
var start = Math.floor(uniform(0, this.points.length))
var i = 0;
var k = 0;
while (k < this.points.length) {
i = (k + start) % this.points.length;
var p = this.points[i]
piece = []
if (true) {
var x = Math.round(p.x / this.sizes);
var y = Math.round(p.y / this.sizeh);
var neighbors = [[x + 1, y + 1], [x + 1, y - 1], [x - 1, y + 1], [x - 1, y - 1],[x , y - 2],[x, y +2]];
var validneighbors = [];
neighbors.forEach((n) => {
var index = n[0] * this.ypts + n[1];
if (index < this.occupancy.length) {
var noccu = this.occupancy[index];
if (noccu > i + 2) {
validneighbors.push(noccu);
}
}
});
if (validneighbors.length) {
piece.push(p);
var cn = validneighbors[Math.floor(uniform(0, validneighbors.length))];
var btindex = cn;
while (btindex > i) {
piece.push(this.points[btindex]);
btindex--;
}
if (piece.length > psize) {
var data = "";
piece.forEach((p) => {
data += p.x + "," + p.y + " ";
});
data = data.slice(0, -1);
var newpath = document.createElementNS($("puzzlecontainer").namespaceURI, "polyline")
newpath.setAttribute("points", data);
newpath.setAttribute("stroke", "black");
newpath.setAttribute("stroke-width", 1);
newpath.setAttribute("fill", "#" + Math.floor(uniform(0, 16777216)).toString(16));
$("puzzlecontainer").appendChild(newpath);
this.cutlines.push([piece[0], piece[1]]);
this.pieces.push(piece);
}
}
}
k = k + Math.min(10, piece.length + 1);
}
this.cutlines.forEach((c) => {
var newpath = document.createElementNS($("puzzlecontainer").namespaceURI, "line")
newpath.setAttribute("x1", c[0].x);
newpath.setAttribute("y1", c[0].y);
newpath.setAttribute("x2", c[1].x);
newpath.setAttribute("y2", c[1].y);
newpath.setAttribute("stroke", "red");
newpath.setAttribute("stroke-width", 2);
$("puzzlecontainer").appendChild(newpath);
});
}
exportsvg() {
var width = this.width();
var height = this.height();
var data = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><svg baseProfile=\"full\" height=\"" + height + "mm\" version=\"1.1\" viewBox=\"0 0 " + width + " " + height + "\" width=\"" + width + "mm\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:ev=\"http://www.w3.org/2001/xml-events\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><defs />"
data += "<polyline points=\"" + this.svgpoints() + "\" style=\"fill:none;stroke:black;stroke-width:1\" />"
this.cutlines.forEach((c) => {
data += "<line x1=\"" + c[0].x + "\" y1=\"" + c[0].y + "\" x2=\"" + c[1].x + "\" y2=\"" + c[1].y + "\" style=\"stroke:rgb(255,0,0);stroke-width:1\" />";
});
//data += "<path fill=\"none\" stroke=\"black\" stroke-width=\"0.1\" d=\"" + createframe() + "\"></path>";
data += "</svg>";
return data;
}
exportsvg_colored() {
var width = this.width();
var height = this.height();
var data = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><svg baseProfile=\"full\" height=\"" + height + "mm\" version=\"1.1\" viewBox=\"0 0 " + width + " " + height + "\" width=\"" + width + "mm\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:ev=\"http://www.w3.org/2001/xml-events\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><defs />"
this.pieces.forEach((pc) => {
data += "<polyline points=\"";
pc.forEach((p) => {
data += p.x + "," + p.y + " ";
});
data += "Z\" style=\"fill:#" + Math.floor(uniform(0, 16777216)).toString(16) + ";stroke:black;stroke-width:1\" />"
});
data += "<polyline points=\"" + this.svgpoints() + "\" style=\"fill:none;stroke:black;stroke-width:1\" />"
this.cutlines.forEach((c) => {
data += "<line x1=\"" + c[0].x + "\" y1=\"" + c[0].y + "\" x2=\"" + c[1].x + "\" y2=\"" + c[1].y + "\" style=\"stroke:rgb(255,0,0);stroke-width:1\" />";
});
//data += "<path fill=\"none\" stroke=\"black\" stroke-width=\"0.1\" d=\"" + createframe() + "\"></path>";
data += "</svg>";
return data;
}
}
// 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);
}
}
function parse_input() {
seed = parseInt($("seed").value);
corder = parseInt($("corder").value);
psize = parseInt($("psize").value);
stepsize = parseFloat($("stepsize").value);
}
function update() {
parse_input();
}
function savesvg(mode) {
if (mode) {
save("jigsaw_color.svg", jig.exportsvg_colored());
} else {
save("jigsaw.svg", jig.exportsvg());
}
}
function generate() {
parse_input();
jig = new Gosper(corder, stepsize);
jig.generate();
jig.normalize();
jig.gen_occupancy();
$("puzzlecontainer").setAttribute("width", jig.width());
$("puzzlecontainer").setAttribute("height", jig.height());
while ($("puzzlecontainer").firstChild) {
$("puzzlecontainer").removeChild($("puzzlecontainer").firstChild);
}
var newpath = document.createElementNS($("puzzlecontainer").namespaceURI, "polyline")
newpath.setAttribute("points", jig.svgpoints());
newpath.setAttribute("stroke", "black");
newpath.setAttribute("stroke-width", 1);
newpath.setAttribute("fill", "none");
$("puzzlecontainer").appendChild(newpath);
jig.makepieces();
$("width").innerHTML = jig.width();
$("height").innerHTML = jig.height();
}
</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>
<td>Use non-deterministic randomness:</td>
<td><input id="ndr" type="checkbox"/></td>
</tr>
<tr>
<td>Curve Order:</td>
<td><input id="_corder" type="text" value="4" onchange="update_corder()" /></td>
<td><input id="corder" type="range" value="4" min="2" max="6" step="1" onchange="updatecurveorder()" /></td>
</tr>
<tr>
<td>Value related to piece "size":</td>
<td><input id="_psize" type="text" value="100" onchange="update_size()" /></td>
<td><input id="psize" type="range" value="100" min="20" max="250" step="1" onchange="updatepsize()" /></td>
</tr>
<tr>
<td>Piece segment length:</td>
<td><input id="stepsize" type="text" value="6.0" size="4" onchange="update()" /> mm</td>
<td></td>
</tr>
<tr>
<td>Jigsaw size: <label id="width">w</label> x <label id="height">h</label> mm</td>
</tr>
<tr>
<td><button onclick="generate()">Generate Jigsaw</button></td>
<td><button onclick="savesvg(0)">Download SVG</button></td>
<td><button onclick="savesvg(1)">Download SVG colored (VERY BROKEN)</button></td>
</tr>
</table>
<svg id="puzzlecontainer">
<!-- <path id="puzzlepath" fill="none" stroke="Black"></path> -->
</svg>
</body>
</html>