-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlabyrinth-03.maker.ts
428 lines (386 loc) · 12.1 KB
/
labyrinth-03.maker.ts
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/// <reference path="node_modules/makerjs/index.d.ts" />
/*
↑ ↑ ← ↓ ↓ ↓ ↓ ↓ ← ↑ ↑ ↑ ← ↓ ↓ ↓ ← ↑ ↑ ↑ ← ↓ ↓ ↓ ← ↑ ↑ ↑ ↑ → ↑ ← ↑ → ↑ ↑ ← ↑ → → ↓ ↓ ↓ ↓ ↓ → → ↑ ← ↑ → ↑ → ↓ → ↑ ↑ ← ← ← ↑ ↑ ← ← ← ↑ → → → → ↓ ↓ → ↑ ↑ → ↓ ↓ → ↑ → ↑ → ↓ → → ↑ → ↓ → ↑ → ↓ ↓ ← ← ← ← ← ← ↓ ← ↓ → ↓ ← ↓ → ↓ ↓ → ↑ ↑ ↑ ↑ ↑ → ↓ ↓ ↓ → ↑ ↑ ↑ → ↓ ↓ ↓ → ↑ ↑ ↑ → ↓ ↓ ↓ ↓ ← ↓ → ↓ ← ↓ ↓ → ↓ ← ← ↑ ↑ ↑ ↑ ↑ ← ← ↓ → ↓ ← ↓ ← ↑ ← ↓ ↓ → → → ↓ ↓ → → → ↓ ← ← ← ← ↑ ↑ ← ↓ ↓ ← ↑ ↑ ← ↓ ← ↓ ← ↑ ← ← ↓ ← ↑ ← ↓ ← ↑ ↑ → → → → → → ↑ → ↑ ← ↑ → ↑ ←
→ → → → ↓ → ↓ . → ↓ . → ↓ → ↓
↑ ← ← ← ↓ ↑ ↓ → ↑ → → ↑ → ↑ ↓
→ → ↓ ↑ → ↑ → ↑ ↓ ← ← ← ← ← ←
↑ ← ↓ ↑ ← ← ← ↓ ← → ↓ → ↓ → ↓
. ↑ ↓ . → ↓ ↑ → ↓ ↑ ↓ ↑ ↓ ↑ ↓
→ ↑ ↓ → ↑ → ↑ ↓ ← ↑ ↓ ↑ ↓ ↑ ↓
↑ ← ↓ ↑ ← ↓ ← → ↓ ↑ → ↑ → ↑ ↓
→ ↑ → → ↑ ↓ ↑ . ↓ ↑ ↓ ← ← ↓ ←
↑ ↓ ← ↓ ← ↓ ↑ ← → ↑ → ↓ ↑ → ↓
↑ ↓ ↑ ↓ ↑ ↓ → ↑ ↓ ← ↓ ← ↑ ↓ ←
↑ ↓ ↑ ↓ ↑ ↓ ↑ ← ↓ ↑ ← . ↑ ↓ .
↑ ← ↑ ← ↑ ← → ↑ → → → ↓ ↑ → ↓
→ → → → → → ↑ ↓ ← ↓ ← ↓ ↑ ← ←
↑ ↓ ← ↓ ← ← ↓ ← ↑ ↓ ↑ → → → ↓
↑ ← ↑ ← . ↑ ← . ↑ ← ↑ ← ← ← ←
*/
import * as MakerJs from 'makerjs';
type Point = [number, number];
const enum Direction {
Up,
Down,
Left,
Right
}
type Path = Array<Direction>;
type AbsPath = Array<Point>;
type BoardSpace = {
direction: Direction;
point: Point;
index: number;
next: number;
} | null;
type Board = Array<BoardSpace>;
type Mutation = [number, Direction];
type Mutations = Array<Mutation>;
const SIZE = 15;
const MAX = Math.floor(SIZE / 2);
const MIN = -1 * MAX;
const SCALE = 48;
const BOARD = (SIZE + 1) * SCALE;
const RADIUS = 18;
const STROKE = 36;
const SYMMETRY = true;
const DEBUG = true;
const THICKNESS = 2;
const DEPTH = STROKE / 2;
const PAGES = DEPTH / THICKNESS;
const LEFT: Point = [-1, 0];
const RIGHT: Point = [1, 0];
const UP: Point = [0, -1];
const DOWN: Point = [0, 1];
const DIR = [UP, DOWN, LEFT, RIGHT];
const Opposite = [
Direction.Down,
Direction.Up,
Direction.Right,
Direction.Left
];
// const OPP = [DOWN, UP, RIGHT, LEFT];
const PRINT = ['↑', '↓', '←', '→'];
const X = 0;
const Y = 1;
const START: Point = [Math.floor(SIZE / 2) - 1, Math.floor(SIZE / 2) + 1];
const initial: Path = [
Direction.Up,
Direction.Up,
Direction.Right,
Direction.Right,
Direction.Down,
Direction.Down,
Direction.Left,
Direction.Left
];
function makeBoard(
path: Path,
start: Point = START,
size: number = SIZE
): Board {
let board = [];
let length = size * size;
for (let i = 0; i < length; i++) {
board[i] = null;
}
let x = start[X];
let y = start[Y];
return path.reduce(function(board, direction, index, path) {
const next = (index + 1) % path.length;
board[y * size + x] = {direction, point: [x, y], index, next};
x += DIR[direction][X];
y += DIR[direction][Y];
return board;
}, board);
}
function getBoardSpace(
board: Board,
x: number,
y: number,
size: number = SIZE
): BoardSpace {
const index = y * size + x;
return board[index];
}
function boardToString(board, size: number = SIZE) {
return board.reduce(function(output, current, index, board) {
let char = '. ';
if (current != null) {
char = PRINT[current.direction] + ' ';
}
if (index > 0 && (index + 1) % SIZE === 0) {
char += '\n';
}
return output + char;
}, '');
}
// const mutations: Mutations = [
// [0, Direction.Left],
// [1, Direction.Left],
// [7, Direction.Up],
// [8, Direction.Up]
// ];
function pathWithMutations(path: Path, mutations: Mutations): Path {
return mutations.reduce(function(
last: Path,
current: Mutation,
mutationIndex: number,
array: Mutations
) {
const [index, direction] = current;
last.splice(index, 0, direction);
const opposite = Opposite[direction];
const index2 = (index + 2) % (last.length + 1);
last.splice(index2, 0, opposite);
if (SYMMETRY) {
// TODO wrap bugs
const len = last.length;
let symIndex = (index + len / 2 + 1) % len;
if (symIndex === 0) {
symIndex = len;
}
last.splice(symIndex, 0, opposite);
let symIndex2 = (symIndex + 2) % last.length;
if (symIndex2 === 0) {
symIndex2 = last.length;
}
last.splice(symIndex2, 0, direction);
}
return last;
}, path.slice());
}
function makeAbsPath(path: Path, start: Point = START): AbsPath {
let x = start[X];
let y = start[Y];
return path.map(function(direction) {
const absolutePoint: Point = [x, y];
x += DIR[direction][X];
y += DIR[direction][Y];
return absolutePoint;
});
}
function getPossibleMutations(path: Path, size: number = SIZE): Mutations {
const board = makeBoard(path);
return board.reduce(function(
possible: Mutations,
boardSpace: BoardSpace,
boardIndex,
board
) {
if (!boardSpace) return possible;
const {direction, point, index, next} = boardSpace;
const nextDirection = path[next];
const [x, y] = point;
switch (direction) {
case Direction.Up:
if (
(nextDirection === Direction.Up ||
nextDirection === Direction.Right) &&
x > 0 &&
y > 0
) {
const bump1 = getBoardSpace(board, x - 1, y);
const bump2 = getBoardSpace(board, x - 1, y - 1);
if (!bump1 && !bump2) {
possible.push([index, Direction.Left]);
}
}
break;
case Direction.Down:
if (
(nextDirection === Direction.Down ||
nextDirection === Direction.Left) &&
x < size - 1 &&
y < size - 1
) {
const bump1 = getBoardSpace(board, x + 1, y);
const bump2 = getBoardSpace(board, x + 1, y + 1);
if (!bump1 && !bump2) {
possible.push([index, Direction.Right]);
}
}
break;
case Direction.Left:
if (
(nextDirection === Direction.Left ||
nextDirection === Direction.Up) &&
x > 0 &&
y < size - 1
) {
const bump1 = getBoardSpace(board, x, y + 1);
const bump2 = getBoardSpace(board, x - 1, y + 1);
if (!bump1 && !bump2) {
possible.push([index, Direction.Down]);
}
}
break;
case Direction.Right:
if (
(nextDirection === Direction.Right ||
nextDirection === Direction.Down) &&
x < size - 1 &&
y > 0
) {
const bump1 = getBoardSpace(board, x, y - 1);
const bump2 = getBoardSpace(board, x + 1, y - 1);
if (!bump1 && !bump2) {
possible.push([index, Direction.Up]);
}
}
break;
default:
break;
}
return possible;
}, []);
}
function logPath(path) {
console.log(path.map(i => PRINT[i]).join(' '));
console.log(boardToString(makeBoard(path)));
}
function randomMutations(path: Path): Mutations {
const mutations = [];
if (DEBUG) {
logPath(path);
}
let possible = getPossibleMutations(path);
while (possible.length > 0) {
const mutation = possible[Math.floor(Math.random() * possible.length)];
mutations.push(mutation);
const currentPath = pathWithMutations(path, mutations);
if (DEBUG) {
logPath(currentPath);
}
possible = getPossibleMutations(currentPath);
if (mutations.length > SIZE * SIZE) {
console.error('oops');
return;
}
}
return mutations;
}
const mutations = randomMutations(initial);
const laby = makeAbsPath(pathWithMutations(initial, mutations));
// console.log(laby);
function pathToMaker(path: AbsPath): MakerJs.IModel {
const length = path.length;
const lines = path.reduce(function(last, current, index, array) {
const line = new MakerJs.paths.Line(current, array[(index + 1) % length]);
const scaled = MakerJs.path.scale(line, SCALE);
last['line' + index] = scaled;
return last;
}, {});
const output: MakerJs.IModel = {paths: lines};
MakerJs.model.findChains(output, function(chains, loose, layer) {
const chain = chains[0];
const fillets = MakerJs.chain.fillet(chain, RADIUS);
output.models = {fillets};
});
// MakerJs.model.move(output, [SCALE / 2, SCALE / 2]);
// Mirror Y to normalize coordinate system
// const mirrored = MakerJs.model.mirror(output, false, true);
// mirrored.units = MakerJs.unitType.Millimeter;
// MakerJs.model.move(mirrored, [0, 0 - BOARD]);
return output;
}
const makerPath = pathToMaker(laby);
function simpleExpand(model, stroke = STROKE): MakerJs.IModel {
const paths = {};
const lines = model.paths;
const stroke2 = stroke / 2;
for (let key in lines) {
if (lines.hasOwnProperty(key)) {
let line: MakerJs.paths.Line = lines[key];
if (line.end[X] === line.origin[X]) {
// Vertical line segment
paths[key + 'L'] = MakerJs.path.moveRelative(
MakerJs.cloneObject(line),
[-stroke2, 0]
);
paths[key + 'R'] = MakerJs.path.moveRelative(
MakerJs.cloneObject(line),
[stroke2, 0]
);
} else {
// Horizontal line segment
paths[key + 'U'] = MakerJs.path.moveRelative(
MakerJs.cloneObject(line),
[0, stroke2]
);
paths[key + 'D'] = MakerJs.path.moveRelative(
MakerJs.cloneObject(line),
[0, -stroke2]
);
}
}
}
const fillets = model.models.fillets.paths;
for (let key in fillets) {
if (fillets.hasOwnProperty(key)) {
let fillet = fillets[key];
paths[key + 'In'] = {...fillet, radius: fillet.radius - stroke2};
paths[key + 'Out'] = {...fillet, radius: fillet.radius + stroke2};
}
}
return {paths};
}
// const expanded = MakerJs.model.expandPaths(makerPath, STROKE);
const points = [
[0, 0],
[0, BOARD - SCALE],
[BOARD - SCALE, 0],
[BOARD - SCALE, BOARD - SCALE],
[(BOARD - SCALE) / 2, (BOARD - SCALE) / 2]
// [-SCALE / 2, SCALE / 2],
// [BOARD - SCALE * 3/2, SCALE / 2],
// [-SCALE/2, -BOARD + SCALE * 3/2],
// [BOARD - SCALE * 3/2, -BOARD + SCALE * 3/2],
];
const holes: MakerJs.IModel = new MakerJs.models.Holes(2, points);
MakerJs.model.move(holes, [0 - SCALE / 2, 0 - SCALE / 2]);
/*
The Stack To Cut
stacked paper with approx semicircular path
*/
const expanded = {};
const layerOptions = {};
const MARGIN = 12;
const COLS = 3;
for (let i = 0; i < PAGES; i++) {
const depth = THICKNESS * (PAGES - i - 1);
// Length of chord, like Pythagoras
const stroke = 2 * Math.sqrt(DEPTH * DEPTH - depth * depth);
console.log(i, depth, stroke);
const name = 'expanded' + i;
const layer = simpleExpand(makerPath, stroke);
// MakerJs.model.moveRelative(layer, [SCALE, -BOARD])
const border = MakerJs.model.move(
new MakerJs.models.RoundRectangle(BOARD, BOARD, RADIUS),
[0 - SCALE, 0 - SCALE]
);
layer.models = {border, holes};
layer.layer = name;
// layerOptions[name] = {fill: `hsla(${360 / PAGES * i}, 100%, 50%, 1)`};
// layerOptions[name] = {fill: i%2===0 ? 'grey' : 'black'};
const x = i % COLS * (BOARD + MARGIN);
const y = Math.floor(i / COLS) * (BOARD + MARGIN);
MakerJs.model.move(layer, [x, -y]);
expanded[name] = layer;
}
const cut = {
models: {
// line: makerPath,
expanded: {models: expanded}
}
};
console.log(cut);
const svg = MakerJs.exporter.toSVG(cut, {
// useSvgPathOnly: !DEBUG,
// annotate: DEBUG,
fill: 'white',
// stroke: 'none',
layerOptions
});
document.write(svg);
document.body.style.backgroundColor = 'hsla(0, 0%, 90%, 1)';