@altnovatrail/picture-kit 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +7 -0
- package/README.md +27 -2
- package/lib/emoji.d.ts +16 -0
- package/lib/emoji.js +64 -0
- package/lib/index.d.ts +9 -0
- package/lib/index.js +10 -0
- package/lib/kind.d.ts +22 -0
- package/lib/kind.js +2 -0
- package/lib/kinds/balance.d.ts +34 -0
- package/lib/kinds/balance.js +217 -0
- package/lib/kinds/branches.d.ts +33 -0
- package/lib/kinds/branches.js +473 -0
- package/lib/kinds/calendar.d.ts +55 -0
- package/lib/kinds/calendar.js +532 -0
- package/lib/kinds/chart.d.ts +48 -0
- package/lib/kinds/chart.js +645 -0
- package/lib/kinds/choices.d.ts +14 -0
- package/lib/kinds/choices.js +53 -0
- package/lib/kinds/clock.d.ts +36 -0
- package/lib/kinds/clock.js +219 -0
- package/lib/kinds/coins.d.ts +37 -0
- package/lib/kinds/coins.js +550 -0
- package/lib/kinds/column.d.ts +37 -0
- package/lib/kinds/column.js +410 -0
- package/lib/kinds/commands-parts.d.ts +12 -0
- package/lib/kinds/commands-parts.js +24 -0
- package/lib/kinds/commands.d.ts +66 -0
- package/lib/kinds/commands.js +789 -0
- package/lib/kinds/containers.d.ts +52 -0
- package/lib/kinds/containers.js +758 -0
- package/lib/kinds/countfig.d.ts +41 -0
- package/lib/kinds/countfig.js +372 -0
- package/lib/kinds/cubes.d.ts +70 -0
- package/lib/kinds/cubes.js +371 -0
- package/lib/kinds/die.d.ts +32 -0
- package/lib/kinds/die.js +301 -0
- package/lib/kinds/domino.d.ts +37 -0
- package/lib/kinds/domino.js +450 -0
- package/lib/kinds/dotgrid.d.ts +38 -0
- package/lib/kinds/dotgrid.js +411 -0
- package/lib/kinds/figure.d.ts +62 -0
- package/lib/kinds/figure.js +687 -0
- package/lib/kinds/fold-parts.d.ts +73 -0
- package/lib/kinds/fold-parts.js +315 -0
- package/lib/kinds/fold.d.ts +52 -0
- package/lib/kinds/fold.js +650 -0
- package/lib/kinds/fraction.d.ts +50 -0
- package/lib/kinds/fraction.js +568 -0
- package/lib/kinds/gauges.d.ts +53 -0
- package/lib/kinds/gauges.js +880 -0
- package/lib/kinds/gears.d.ts +40 -0
- package/lib/kinds/gears.js +787 -0
- package/lib/kinds/grid.d.ts +45 -0
- package/lib/kinds/grid.js +591 -0
- package/lib/kinds/groups.d.ts +23 -0
- package/lib/kinds/groups.js +335 -0
- package/lib/kinds/index.d.ts +3 -0
- package/lib/kinds/index.js +87 -0
- package/lib/kinds/lanes.d.ts +36 -0
- package/lib/kinds/lanes.js +220 -0
- package/lib/kinds/layers.d.ts +31 -0
- package/lib/kinds/layers.js +419 -0
- package/lib/kinds/line.d.ts +38 -0
- package/lib/kinds/line.js +739 -0
- package/lib/kinds/magic-parts.d.ts +72 -0
- package/lib/kinds/magic-parts.js +186 -0
- package/lib/kinds/magic.d.ts +66 -0
- package/lib/kinds/magic.js +664 -0
- package/lib/kinds/map.d.ts +32 -0
- package/lib/kinds/map.js +370 -0
- package/lib/kinds/matchsticks.d.ts +26 -0
- package/lib/kinds/matchsticks.js +434 -0
- package/lib/kinds/necklace.d.ts +27 -0
- package/lib/kinds/necklace.js +342 -0
- package/lib/kinds/net.d.ts +47 -0
- package/lib/kinds/net.js +241 -0
- package/lib/kinds/overlay.d.ts +50 -0
- package/lib/kinds/overlay.js +519 -0
- package/lib/kinds/pieces.d.ts +41 -0
- package/lib/kinds/pieces.js +420 -0
- package/lib/kinds/pipes.d.ts +46 -0
- package/lib/kinds/pipes.js +657 -0
- package/lib/kinds/plane-parts.d.ts +105 -0
- package/lib/kinds/plane-parts.js +372 -0
- package/lib/kinds/plane.d.ts +35 -0
- package/lib/kinds/plane.js +755 -0
- package/lib/kinds/pyramid.d.ts +26 -0
- package/lib/kinds/pyramid.js +228 -0
- package/lib/kinds/ropes.d.ts +30 -0
- package/lib/kinds/ropes.js +571 -0
- package/lib/kinds/shapes.d.ts +59 -0
- package/lib/kinds/shapes.js +490 -0
- package/lib/kinds/solids.d.ts +78 -0
- package/lib/kinds/solids.js +1058 -0
- package/lib/kinds/tangram.d.ts +52 -0
- package/lib/kinds/tangram.js +713 -0
- package/lib/kinds/target.d.ts +39 -0
- package/lib/kinds/target.js +761 -0
- package/lib/kinds/tiles.d.ts +55 -0
- package/lib/kinds/tiles.js +606 -0
- package/lib/kinds/venn.d.ts +38 -0
- package/lib/kinds/venn.js +710 -0
- package/lib/noto.d.ts +2 -0
- package/lib/noto.js +212 -0
- package/lib/objects.d.ts +7 -0
- package/lib/objects.js +226 -0
- package/lib/pen.d.ts +32 -0
- package/lib/pen.js +60 -0
- package/lib/read.d.ts +9 -0
- package/lib/read.js +49 -0
- package/lib/render.d.ts +24 -0
- package/lib/render.js +68 -0
- package/lib/solve/balance.d.ts +46 -0
- package/lib/solve/balance.js +118 -0
- package/lib/style.d.ts +70 -0
- package/lib/style.js +39 -0
- package/lib/svg.d.ts +22 -0
- package/lib/svg.js +48 -0
- package/lib/things.d.ts +40 -0
- package/lib/things.js +269 -0
- package/package.json +28 -4
|
@@ -0,0 +1,650 @@
|
|
|
1
|
+
import { margin } from "../kind.js";
|
|
2
|
+
import { int, isObj, list, oneOf, only } from "../read.js";
|
|
3
|
+
import { colorNames, colors, ink, isColor, outline } from "../style.js";
|
|
4
|
+
import { n, poly } from "../svg.js";
|
|
5
|
+
import { shapePath } from "../things.js";
|
|
6
|
+
import { andThen, article, atomsOf, boxOf, centreOf, identity, innerLines, key, loopsOf, mapAtom, partQuarters, parts, partWords, pieceCount, sheetAtoms, transposed, turnMat, turnOf, ways, } from "./fold-parts.js";
|
|
7
|
+
/**
|
|
8
|
+
* Fold: a sheet of paper, a rectangle of squares, folded in half one to three
|
|
9
|
+
* times, then punched with holes or cut, drawn as steps in a row: the sheet
|
|
10
|
+
* with the dashed line of its first fold and a curved arrow from the half that
|
|
11
|
+
* moves to the half it lies on, the folded sheet with its next fold, and so on
|
|
12
|
+
* to the folded sheet with its holes and its cuts, the parts cut off dashed.
|
|
13
|
+
* After an arrow the picture may end with the sheet unfolded again, or with a
|
|
14
|
+
* "?" in its place; or it may show the unfolded sheet alone, as an option of a
|
|
15
|
+
* choice. The program unfolds the sheet itself, quarter of a square by
|
|
16
|
+
* quarter, so the unfolded sheet is exactly what the folds, holes and cuts
|
|
17
|
+
* make, every hole turned or mirrored as the folds turn it.
|
|
18
|
+
*
|
|
19
|
+
* A fold is named by the half that moves and the half it lies on — "left
|
|
20
|
+
* onto right", "top-right onto bottom-left" — and folds the sheet as folded so
|
|
21
|
+
* far along its middle line of that direction. A place on the folded sheet is
|
|
22
|
+
* [row, column], counted from 1 at the top left of the folded sheet. A hole is
|
|
23
|
+
* punched in the middle of a whole square of it; a cut takes off a square or a
|
|
24
|
+
* part of one: "nw", "ne", "se", "sw", the half at that corner of a square cut
|
|
25
|
+
* along a diagonal, or "top", "right", "bottom", "left", the quarter between
|
|
26
|
+
* that side and the centre.
|
|
27
|
+
*/
|
|
28
|
+
export const foldNames = [
|
|
29
|
+
"left onto right",
|
|
30
|
+
"right onto left",
|
|
31
|
+
"top onto bottom",
|
|
32
|
+
"bottom onto top",
|
|
33
|
+
"top-right onto bottom-left",
|
|
34
|
+
"bottom-left onto top-right",
|
|
35
|
+
"top-left onto bottom-right",
|
|
36
|
+
"bottom-right onto top-left",
|
|
37
|
+
];
|
|
38
|
+
export const holeShapes = [
|
|
39
|
+
"circle",
|
|
40
|
+
"square",
|
|
41
|
+
"triangle",
|
|
42
|
+
"diamond",
|
|
43
|
+
"star",
|
|
44
|
+
"heart",
|
|
45
|
+
"moon",
|
|
46
|
+
"cross",
|
|
47
|
+
];
|
|
48
|
+
export const shows = ["steps", "result", "ask", "unfolded"];
|
|
49
|
+
export const holeTurns = [0, 90, 180, 270];
|
|
50
|
+
const most = { side: 8, folds: 3, holes: 6, cuts: 8 };
|
|
51
|
+
const biggest = 40;
|
|
52
|
+
const smallest = 22;
|
|
53
|
+
const room = 640 - 2 * margin - 2 * outline;
|
|
54
|
+
const arrowW = 28;
|
|
55
|
+
const gap = 9;
|
|
56
|
+
const holeSize = 0.6;
|
|
57
|
+
const foldDir = {
|
|
58
|
+
"left onto right": "vertical",
|
|
59
|
+
"right onto left": "vertical",
|
|
60
|
+
"top onto bottom": "horizontal",
|
|
61
|
+
"bottom onto top": "horizontal",
|
|
62
|
+
"top-right onto bottom-left": "diagonal",
|
|
63
|
+
"bottom-left onto top-right": "diagonal",
|
|
64
|
+
"top-left onto bottom-right": "anti",
|
|
65
|
+
"bottom-right onto top-left": "anti",
|
|
66
|
+
};
|
|
67
|
+
/** Whether a point lies on the side of the line that moves; k places the line. */
|
|
68
|
+
const moves = {
|
|
69
|
+
"left onto right": (p, k) => p.x < k,
|
|
70
|
+
"right onto left": (p, k) => p.x > k,
|
|
71
|
+
"top onto bottom": (p, k) => p.y < k,
|
|
72
|
+
"bottom onto top": (p, k) => p.y > k,
|
|
73
|
+
"top-right onto bottom-left": (p, k) => p.x - p.y > k,
|
|
74
|
+
"bottom-left onto top-right": (p, k) => p.x - p.y < k,
|
|
75
|
+
"top-left onto bottom-right": (p, k) => p.x + p.y < k,
|
|
76
|
+
"bottom-right onto top-left": (p, k) => p.x + p.y > k,
|
|
77
|
+
};
|
|
78
|
+
const lineWords = {
|
|
79
|
+
vertical: "",
|
|
80
|
+
horizontal: "",
|
|
81
|
+
diagonal: ", along the slanting line from its top left down to its bottom right",
|
|
82
|
+
anti: ", along the slanting line from its bottom left up to its top right",
|
|
83
|
+
};
|
|
84
|
+
/** The way each hole points when it is punched upright, as an index of ways; none for a round one. */
|
|
85
|
+
const holeWay = {
|
|
86
|
+
circle: undefined,
|
|
87
|
+
square: undefined,
|
|
88
|
+
diamond: undefined,
|
|
89
|
+
cross: undefined,
|
|
90
|
+
triangle: 0,
|
|
91
|
+
star: 0,
|
|
92
|
+
heart: 2,
|
|
93
|
+
moon: 1,
|
|
94
|
+
};
|
|
95
|
+
function lineOf(name, box) {
|
|
96
|
+
const dir = foldDir[name];
|
|
97
|
+
const cx = (box.x0 + box.x1) / 2;
|
|
98
|
+
const cy = (box.y0 + box.y1) / 2;
|
|
99
|
+
if (dir === "vertical")
|
|
100
|
+
return { dir, k: cx, mirror: [-1, 0, 0, 1, 2 * cx, 0] };
|
|
101
|
+
if (dir === "horizontal")
|
|
102
|
+
return { dir, k: cy, mirror: [1, 0, 0, -1, 0, 2 * cy] };
|
|
103
|
+
if (dir === "diagonal") {
|
|
104
|
+
const k = cx - cy;
|
|
105
|
+
return { dir, k, mirror: [0, 1, 1, 0, k, -k] };
|
|
106
|
+
}
|
|
107
|
+
const k = cx + cy;
|
|
108
|
+
return { dir, k, mirror: [0, -1, -1, 0, k, k] };
|
|
109
|
+
}
|
|
110
|
+
/** foldSheet folds the sheet; it records a problem and stops at the first fold it cannot make. */
|
|
111
|
+
function foldSheet(sheet, folds, problems) {
|
|
112
|
+
const pos = sheetAtoms(sheet.rows, sheet.cols);
|
|
113
|
+
const maps = pos.map(() => identity);
|
|
114
|
+
const steps = [];
|
|
115
|
+
let region = pos.map((a) => ({ ...a }));
|
|
116
|
+
for (const [i, name] of folds.entries()) {
|
|
117
|
+
const box = boxOf(region);
|
|
118
|
+
const line = lineOf(name, box);
|
|
119
|
+
const w = box.x1 - box.x0;
|
|
120
|
+
const h = box.y1 - box.y0;
|
|
121
|
+
if (!Number.isInteger(line.k)) {
|
|
122
|
+
problems?.push(line.dir === "vertical"
|
|
123
|
+
? `folds[${i}]: the sheet is ${w} squares wide here, so its middle line runs through squares; fold it in half from left to right only when its width is even`
|
|
124
|
+
: line.dir === "horizontal"
|
|
125
|
+
? `folds[${i}]: the sheet is ${h} squares high here, so its middle line runs through squares; fold it in half from top to bottom only when its height is even`
|
|
126
|
+
: `folds[${i}]: the slanting line through the middle of the sheet does not run along the diagonals of its squares`);
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
const inRegion = new Set(region.map(key));
|
|
130
|
+
if (!region.every((a) => inRegion.has(key(mapAtom(a, line.mirror))))) {
|
|
131
|
+
problems?.push(`folds[${i}]: the sheet as folded so far, ${w} by ${h} squares, has no halves that lie on each other along that line; fold a rectangle across its middle, a square also along a diagonal, a triangle along its middle line`);
|
|
132
|
+
return undefined;
|
|
133
|
+
}
|
|
134
|
+
const moving = region.filter((a) => moves[name](centreOf(a), line.k));
|
|
135
|
+
const staying = region.filter((a) => !moves[name](centreOf(a), line.k));
|
|
136
|
+
steps.push({ region, name, line, moving, staying });
|
|
137
|
+
pos.forEach((a, j) => {
|
|
138
|
+
if (!moves[name](centreOf(a), line.k))
|
|
139
|
+
return;
|
|
140
|
+
pos[j] = mapAtom(a, line.mirror);
|
|
141
|
+
maps[j] = andThen(maps[j], line.mirror);
|
|
142
|
+
});
|
|
143
|
+
region = staying;
|
|
144
|
+
}
|
|
145
|
+
return { steps, final: region, pos, maps };
|
|
146
|
+
}
|
|
147
|
+
/** origin is the top-left square of the folded sheet, from which its places count. */
|
|
148
|
+
function originOf(final) {
|
|
149
|
+
const box = boxOf(final);
|
|
150
|
+
const r = Math.floor(box.y0);
|
|
151
|
+
const c = Math.floor(box.x0);
|
|
152
|
+
return { r, c, rows: Math.ceil(box.y1) - r, cols: Math.ceil(box.x1) - c };
|
|
153
|
+
}
|
|
154
|
+
/** unfold works out the flat sheet: the atoms that stay, and every image of every hole. */
|
|
155
|
+
function unfold(scene, folded) {
|
|
156
|
+
const o = originOf(folded.final);
|
|
157
|
+
const cut = new Set();
|
|
158
|
+
for (const c of scene.cuts ?? [])
|
|
159
|
+
for (const a of atomsOf(o.r + c[0] - 1, o.c + c[1] - 1, c[2] ?? "whole"))
|
|
160
|
+
cut.add(key(a));
|
|
161
|
+
const flat = sheetAtoms(scene.sheet.rows, scene.sheet.cols);
|
|
162
|
+
const atoms = flat.filter((_, i) => !cut.has(key(folded.pos[i])));
|
|
163
|
+
const holes = [];
|
|
164
|
+
for (let r = 0; r < scene.sheet.rows; r++)
|
|
165
|
+
for (let c = 0; c < scene.sheet.cols; c++) {
|
|
166
|
+
const i = (r * scene.sheet.cols + c) * 4;
|
|
167
|
+
const at = folded.pos[i];
|
|
168
|
+
const whole = [1, 2, 3].every((q) => {
|
|
169
|
+
const p = folded.pos[i + q];
|
|
170
|
+
return p.r === at.r && p.c === at.c;
|
|
171
|
+
});
|
|
172
|
+
if (!whole)
|
|
173
|
+
continue;
|
|
174
|
+
const hole = (scene.holes ?? []).find((h) => o.r + h.at[0] - 1 === at.r && o.c + h.at[1] - 1 === at.c);
|
|
175
|
+
if (!hole)
|
|
176
|
+
continue;
|
|
177
|
+
// The hole was punched upright, turned by its turn, on the folded sheet;
|
|
178
|
+
// on the flat sheet it lies as the folds that took this square there,
|
|
179
|
+
// undone, make it.
|
|
180
|
+
const pose = andThen(turnMat(hole.turn ?? 0), transposed(folded.maps[i]));
|
|
181
|
+
const way = holeWay[hole.shape];
|
|
182
|
+
holes.push({ r, c, shape: hole.shape, turn: way === undefined ? 0 : turnOf(pose, way) });
|
|
183
|
+
}
|
|
184
|
+
return { atoms, holes };
|
|
185
|
+
}
|
|
186
|
+
function cutAtoms(scene, final) {
|
|
187
|
+
const o = originOf(final);
|
|
188
|
+
return (scene.cuts ?? []).flatMap((c) => atomsOf(o.r + c[0] - 1, o.c + c[1] - 1, c[2] ?? "whole"));
|
|
189
|
+
}
|
|
190
|
+
// Checking.
|
|
191
|
+
function checkSheet(sheet, problems) {
|
|
192
|
+
if (!isObj(sheet)) {
|
|
193
|
+
problems.push("sheet: must be the sheet's rows and cols, and its color");
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
only(sheet, ["rows", "cols", "color", "grid"], "sheet", problems);
|
|
197
|
+
int(sheet.rows, "sheet.rows", problems, 1, most.side);
|
|
198
|
+
int(sheet.cols, "sheet.cols", problems, 1, most.side);
|
|
199
|
+
if (sheet.color !== undefined && (!isColor(sheet.color) || sheet.color === "white"))
|
|
200
|
+
problems.push(`sheet.color: must be a colour other than white, which would hide the white holes: ${colorNames.filter((c) => c !== "white").join(", ")}`);
|
|
201
|
+
if (sheet.grid !== undefined && typeof sheet.grid !== "boolean")
|
|
202
|
+
problems.push("sheet.grid: must be true or false");
|
|
203
|
+
}
|
|
204
|
+
function checkHoles(scene, final, problems) {
|
|
205
|
+
if (scene.holes === undefined)
|
|
206
|
+
return;
|
|
207
|
+
const o = originOf(final);
|
|
208
|
+
const inFinal = new Set(final.map(key));
|
|
209
|
+
const seen = new Map();
|
|
210
|
+
list(scene.holes, "holes", problems, 0, most.holes).forEach((h, i) => {
|
|
211
|
+
const path = `holes[${i}]`;
|
|
212
|
+
if (!isObj(h)) {
|
|
213
|
+
problems.push(`${path}: must be a hole: at and shape`);
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
only(h, ["at", "shape", "turn"], path, problems);
|
|
217
|
+
oneOf(h.shape, holeShapes, `${path}.shape`, problems);
|
|
218
|
+
if (h.turn !== undefined && !holeTurns.includes(h.turn))
|
|
219
|
+
problems.push(`${path}.turn: must be one of ${holeTurns.join(", ")}, clockwise`);
|
|
220
|
+
const at = placeOf(h.at, `${path}.at`, o, problems);
|
|
221
|
+
if (!at)
|
|
222
|
+
return;
|
|
223
|
+
const whole = atomsOf(o.r + at[0] - 1, o.c + at[1] - 1, "whole").every((a) => inFinal.has(key(a)));
|
|
224
|
+
if (!whole)
|
|
225
|
+
problems.push(`${path}.at: the square at row ${at[0]}, column ${at[1]} is not a whole square of the folded sheet; punch a hole in a whole one`);
|
|
226
|
+
const name = `${at}`;
|
|
227
|
+
const other = seen.get(name);
|
|
228
|
+
if (other !== undefined)
|
|
229
|
+
problems.push(`${path}.at: holes[${other}] is in that square already`);
|
|
230
|
+
seen.set(name, i);
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
function placeOf(value, path, o, problems) {
|
|
234
|
+
if (!Array.isArray(value) || value.length !== 2) {
|
|
235
|
+
problems.push(`${path}: must be [row, column], counted from 1 at the top left of the folded sheet`);
|
|
236
|
+
return undefined;
|
|
237
|
+
}
|
|
238
|
+
const before = problems.length;
|
|
239
|
+
const r = int(value[0], `${path}[0]`, problems, 1, o.rows);
|
|
240
|
+
const c = int(value[1], `${path}[1]`, problems, 1, o.cols);
|
|
241
|
+
return problems.length > before ? undefined : [r, c];
|
|
242
|
+
}
|
|
243
|
+
function checkCuts(scene, final, problems) {
|
|
244
|
+
if (scene.cuts === undefined)
|
|
245
|
+
return;
|
|
246
|
+
const o = originOf(final);
|
|
247
|
+
const inFinal = new Set(final.map(key));
|
|
248
|
+
const taken = new Map();
|
|
249
|
+
const holes = (Array.isArray(scene.holes) ? scene.holes : []);
|
|
250
|
+
list(scene.cuts, "cuts", problems, 0, most.cuts).forEach((cut, i) => {
|
|
251
|
+
const path = `cuts[${i}]`;
|
|
252
|
+
if (!Array.isArray(cut) || cut.length < 2 || cut.length > 3) {
|
|
253
|
+
problems.push(`${path}: must be [row, column] for a whole square, or [row, column, part] with a part: ${parts.slice(1).join(", ")}`);
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
const before = problems.length;
|
|
257
|
+
const at = placeOf(cut.slice(0, 2), path, o, problems);
|
|
258
|
+
const part = cut.length === 3 ? oneOf(cut[2], parts, `${path}[2]`, problems) : "whole";
|
|
259
|
+
if (!at || problems.length > before)
|
|
260
|
+
return;
|
|
261
|
+
const atoms = atomsOf(o.r + at[0] - 1, o.c + at[1] - 1, part);
|
|
262
|
+
if (!atoms.every((a) => inFinal.has(key(a)))) {
|
|
263
|
+
problems.push(`${path}: that part of the square at row ${at[0]}, column ${at[1]} is not on the folded sheet`);
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
for (const a of atoms) {
|
|
267
|
+
const other = taken.get(key(a));
|
|
268
|
+
if (other !== undefined) {
|
|
269
|
+
problems.push(`${path}: overlaps cuts[${other}]; each part is cut once`);
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
for (const a of atoms)
|
|
274
|
+
taken.set(key(a), i);
|
|
275
|
+
const hole = holes.findIndex((h) => isObj(h) && Array.isArray(h.at) && h.at[0] === at[0] && h.at[1] === at[1]);
|
|
276
|
+
if (hole >= 0)
|
|
277
|
+
problems.push(`${path}: holes[${hole}] is in that square; a hole needs a whole square`);
|
|
278
|
+
});
|
|
279
|
+
if (problems.length === 0 && final.every((a) => taken.has(key(a))))
|
|
280
|
+
problems.push("cuts: they cut away the whole folded sheet; leave some of it");
|
|
281
|
+
}
|
|
282
|
+
function framesOf(scene, folded) {
|
|
283
|
+
const flat = { x0: 0, y0: 0, x1: scene.sheet.cols, y1: scene.sheet.rows };
|
|
284
|
+
const show = scene.show ?? "steps";
|
|
285
|
+
if (show === "unfolded")
|
|
286
|
+
return [{ what: "flat", box: flat }];
|
|
287
|
+
const frames = folded.steps.map((step) => ({
|
|
288
|
+
what: "fold",
|
|
289
|
+
step,
|
|
290
|
+
box: boxOf(step.region),
|
|
291
|
+
}));
|
|
292
|
+
frames.push({ what: "folded", atoms: folded.final, box: boxOf(folded.final) });
|
|
293
|
+
if (show === "result")
|
|
294
|
+
frames.push({ what: "flat", box: flat });
|
|
295
|
+
if (show === "ask")
|
|
296
|
+
frames.push({ what: "ask", box: flat });
|
|
297
|
+
return frames;
|
|
298
|
+
}
|
|
299
|
+
/** sizeOf is the side of a square in pixels, so that the frames fit side by side. */
|
|
300
|
+
function sizeOf(frames) {
|
|
301
|
+
const squares = frames.reduce((sum, f) => sum + f.box.x1 - f.box.x0, 0);
|
|
302
|
+
const joins = (frames.length - 1) * (arrowW + 2 * gap);
|
|
303
|
+
return Math.min(biggest, (room - joins) / squares);
|
|
304
|
+
}
|
|
305
|
+
// Drawing.
|
|
306
|
+
function drawAtoms(pen, atoms, at, fill, stroke) {
|
|
307
|
+
pen.el("path", {
|
|
308
|
+
d: loopsOf(atoms)
|
|
309
|
+
.map((loop) => poly(loop.map(at)))
|
|
310
|
+
.join(""),
|
|
311
|
+
fill,
|
|
312
|
+
"fill-rule": "evenodd",
|
|
313
|
+
...stroke,
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
/** drawPaper draws paper: its fill, the lines of its squares when asked, its outline. */
|
|
317
|
+
function drawPaper(pen, atoms, at, scene) {
|
|
318
|
+
const color = colors[scene.sheet.color ?? "yellow"];
|
|
319
|
+
drawAtoms(pen, atoms, at, color.fill, {});
|
|
320
|
+
if (scene.sheet.grid) {
|
|
321
|
+
const lines = innerLines(atoms, (a, b) => Number.isInteger(a.x) &&
|
|
322
|
+
Number.isInteger(a.y) &&
|
|
323
|
+
Number.isInteger(b.x) &&
|
|
324
|
+
Number.isInteger(b.y));
|
|
325
|
+
if (lines.length > 0)
|
|
326
|
+
pen.el("path", {
|
|
327
|
+
d: lines.map(([a, b]) => poly([at(a), at(b)], false)).join(""),
|
|
328
|
+
stroke: color.dark,
|
|
329
|
+
"stroke-width": 1.5,
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
drawAtoms(pen, atoms, at, "none", {
|
|
333
|
+
stroke: ink,
|
|
334
|
+
"stroke-width": outline,
|
|
335
|
+
"stroke-linejoin": "round",
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
function drawHole(pen, shape, turn, x, y, s) {
|
|
339
|
+
pen.el("path", {
|
|
340
|
+
class: "hole",
|
|
341
|
+
// A radius of whole tenths: shapePath writes a circle as two arcs whose
|
|
342
|
+
// radius and chord it rounds apart, and a radius rounded up bulges them.
|
|
343
|
+
d: shapePath(shape, x, y, Math.round(s * holeSize * 5) / 5),
|
|
344
|
+
fill: "#ffffff",
|
|
345
|
+
stroke: ink,
|
|
346
|
+
"stroke-width": 2,
|
|
347
|
+
"stroke-linejoin": "round",
|
|
348
|
+
transform: turn === 0 ? undefined : `rotate(${turn} ${n(x)} ${n(y)})`,
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
/** drawFoldSign draws a fold's dashed crease and a curved arrow from the half that moves onto the other. */
|
|
352
|
+
function drawFoldSign(pen, step, at) {
|
|
353
|
+
const crease = innerLines(step.region, (a, b) => onLine(step.line, a) && onLine(step.line, b));
|
|
354
|
+
pen.el("path", {
|
|
355
|
+
class: "crease",
|
|
356
|
+
d: crease.map(([a, b]) => poly([at(a), at(b)], false)).join(""),
|
|
357
|
+
stroke: ink,
|
|
358
|
+
"stroke-width": 2,
|
|
359
|
+
"stroke-dasharray": "6 5",
|
|
360
|
+
"stroke-linecap": "round",
|
|
361
|
+
});
|
|
362
|
+
const mean = (atoms) => {
|
|
363
|
+
const ps = atoms.map(centreOf);
|
|
364
|
+
return at({
|
|
365
|
+
x: ps.reduce((s, p) => s + p.x, 0) / ps.length,
|
|
366
|
+
y: ps.reduce((s, p) => s + p.y, 0) / ps.length,
|
|
367
|
+
});
|
|
368
|
+
};
|
|
369
|
+
const from = mean(step.moving);
|
|
370
|
+
const to = mean(step.staying);
|
|
371
|
+
const d = { x: to.x - from.x, y: to.y - from.y };
|
|
372
|
+
const a = { x: from.x + d.x * 0.08, y: from.y + d.y * 0.08 };
|
|
373
|
+
const b = { x: to.x - d.x * 0.08, y: to.y - d.y * 0.08 };
|
|
374
|
+
// The arrow bows away from the paper's bottom right, so that it reads as a
|
|
375
|
+
// flap lifted over the line, the same way for every fold.
|
|
376
|
+
let side = { x: d.y, y: -d.x };
|
|
377
|
+
if (side.y > 0 || (side.y === 0 && side.x > 0))
|
|
378
|
+
side = { x: -side.x, y: -side.y };
|
|
379
|
+
const c = { x: (a.x + b.x) / 2 + side.x * 0.45, y: (a.y + b.y) / 2 + side.y * 0.45 };
|
|
380
|
+
pen.el("path", {
|
|
381
|
+
class: "fold-arrow",
|
|
382
|
+
d: `M${n(a.x)} ${n(a.y)}Q${n(c.x)} ${n(c.y)} ${n(b.x)} ${n(b.y)}`,
|
|
383
|
+
fill: "none",
|
|
384
|
+
stroke: ink,
|
|
385
|
+
"stroke-width": outline,
|
|
386
|
+
"stroke-linecap": "round",
|
|
387
|
+
});
|
|
388
|
+
const t = { x: b.x - c.x, y: b.y - c.y };
|
|
389
|
+
const len = Math.hypot(t.x, t.y);
|
|
390
|
+
const u = { x: t.x / len, y: t.y / len };
|
|
391
|
+
pen.el("path", {
|
|
392
|
+
d: poly([
|
|
393
|
+
{ x: b.x + u.x * 4, y: b.y + u.y * 4 },
|
|
394
|
+
{ x: b.x - u.x * 9 - u.y * 7, y: b.y - u.y * 9 + u.x * 7 },
|
|
395
|
+
{ x: b.x - u.x * 9 + u.y * 7, y: b.y - u.y * 9 - u.x * 7 },
|
|
396
|
+
]),
|
|
397
|
+
fill: ink,
|
|
398
|
+
stroke: ink,
|
|
399
|
+
"stroke-width": 1.5,
|
|
400
|
+
"stroke-linejoin": "round",
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
function onLine(line, p) {
|
|
404
|
+
const v = line.dir === "vertical"
|
|
405
|
+
? p.x
|
|
406
|
+
: line.dir === "horizontal"
|
|
407
|
+
? p.y
|
|
408
|
+
: line.dir === "diagonal"
|
|
409
|
+
? p.x - p.y
|
|
410
|
+
: p.x + p.y;
|
|
411
|
+
return Math.abs(v - line.k) < 1e-9;
|
|
412
|
+
}
|
|
413
|
+
/** drawArrow draws the grey arrow from one step to the next. */
|
|
414
|
+
function drawArrow(pen, x, mid) {
|
|
415
|
+
const b = x + arrowW;
|
|
416
|
+
pen.el("path", {
|
|
417
|
+
d: poly([
|
|
418
|
+
{ x, y: mid - 4 },
|
|
419
|
+
{ x: b - 12, y: mid - 4 },
|
|
420
|
+
{ x: b - 12, y: mid - 11 },
|
|
421
|
+
{ x: b, y: mid },
|
|
422
|
+
{ x: b - 12, y: mid + 11 },
|
|
423
|
+
{ x: b - 12, y: mid + 4 },
|
|
424
|
+
{ x, y: mid + 4 },
|
|
425
|
+
]),
|
|
426
|
+
fill: colors.gray.fill,
|
|
427
|
+
stroke: ink,
|
|
428
|
+
"stroke-width": 2,
|
|
429
|
+
"stroke-linejoin": "round",
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
// Words.
|
|
433
|
+
function holeWords(shape, turn) {
|
|
434
|
+
const way = holeWay[shape];
|
|
435
|
+
const pointing = way === undefined ? "" : ways[(way + turn / 90) % 4];
|
|
436
|
+
switch (shape) {
|
|
437
|
+
case "circle":
|
|
438
|
+
return "round hole";
|
|
439
|
+
case "square":
|
|
440
|
+
return "square hole";
|
|
441
|
+
case "diamond":
|
|
442
|
+
return "diamond-shaped hole";
|
|
443
|
+
case "cross":
|
|
444
|
+
return "cross-shaped hole";
|
|
445
|
+
case "triangle":
|
|
446
|
+
return `triangular hole pointing ${pointing}`;
|
|
447
|
+
case "star":
|
|
448
|
+
return `star-shaped hole with a point ${pointing}`;
|
|
449
|
+
case "heart":
|
|
450
|
+
return `heart-shaped hole with its tip ${pointing}`;
|
|
451
|
+
case "moon":
|
|
452
|
+
return `crescent-shaped hole with its tips ${pointing}`;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
function sheetWords(sheet) {
|
|
456
|
+
const color = colors[sheet.color ?? "yellow"].word;
|
|
457
|
+
const form = sheet.rows === sheet.cols ? "square " : "";
|
|
458
|
+
return `${article(`${color} ${form}sheet`)} of paper, ${sheet.cols} square${sheet.cols === 1 ? "" : "s"} wide and ${sheet.rows} high${sheet.grid ? ", with the lines of its squares drawn on it" : ""}`;
|
|
459
|
+
}
|
|
460
|
+
/** shapeWords says what shape the folded sheet is. */
|
|
461
|
+
function shapeWords(atoms) {
|
|
462
|
+
const box = boxOf(atoms);
|
|
463
|
+
const w = box.x1 - box.x0;
|
|
464
|
+
const h = box.y1 - box.y0;
|
|
465
|
+
const size = `${w} square${w === 1 ? "" : "s"} wide and ${h} high`;
|
|
466
|
+
const loops = loopsOf(atoms);
|
|
467
|
+
const pts = loops[0];
|
|
468
|
+
if (loops.length === 1 && pts.length === 4)
|
|
469
|
+
return `${w === h ? "a square" : "a rectangle"}, ${size}`;
|
|
470
|
+
if (loops.length === 1 && pts.length === 3) {
|
|
471
|
+
const right = pts.find((p, i) => {
|
|
472
|
+
const a = pts[(i + 1) % 3];
|
|
473
|
+
const b = pts[(i + 2) % 3];
|
|
474
|
+
return Math.abs((a.x - p.x) * (b.x - p.x) + (a.y - p.y) * (b.y - p.y)) < 1e-9;
|
|
475
|
+
});
|
|
476
|
+
const across = right.x === box.x0 ? "left" : right.x === box.x1 ? "right" : "";
|
|
477
|
+
const down = right.y === box.y0 ? "top" : right.y === box.y1 ? "bottom" : "";
|
|
478
|
+
if (across && down)
|
|
479
|
+
return `a triangle with its right angle at the ${down} ${across}, ${size}`;
|
|
480
|
+
const way = across || (down === "top" ? "up" : "down");
|
|
481
|
+
const long = { left: "right", right: "left", up: "bottom", down: "top" }[way] ?? "";
|
|
482
|
+
return `a triangle pointing ${way}, its long side on the ${long}, ${size}`;
|
|
483
|
+
}
|
|
484
|
+
return `a shape ${size}`;
|
|
485
|
+
}
|
|
486
|
+
function sayPlace(r, c) {
|
|
487
|
+
return `row ${r}, column ${c}`;
|
|
488
|
+
}
|
|
489
|
+
/** flatWords says the sheet unfolded: its holes, the parts cut away, the pieces it falls into. */
|
|
490
|
+
function flatWords(scene, folded) {
|
|
491
|
+
const { atoms, holes } = unfold(scene, folded);
|
|
492
|
+
const out = [];
|
|
493
|
+
const groups = new Map();
|
|
494
|
+
for (const h of holes) {
|
|
495
|
+
const name = holeWords(h.shape, h.turn);
|
|
496
|
+
const g = groups.get(name) ?? { count: 0, places: [] };
|
|
497
|
+
g.count++;
|
|
498
|
+
g.places.push(sayPlace(h.r + 1, h.c + 1));
|
|
499
|
+
groups.set(name, g);
|
|
500
|
+
}
|
|
501
|
+
if (holes.length > 0)
|
|
502
|
+
out.push(`with ${holes.length} hole${holes.length === 1 ? "" : "s"}: ${[...groups.entries()]
|
|
503
|
+
.map(([name, g]) => `${g.count === 1 ? article(name) : `${g.count} ${name.replace("hole", "holes")}`} at ${g.places.join("; ")}`)
|
|
504
|
+
.join("; and ")}`);
|
|
505
|
+
const kept = new Set(atoms.map(key));
|
|
506
|
+
const gone = [];
|
|
507
|
+
for (let r = 0; r < scene.sheet.rows; r++)
|
|
508
|
+
for (let c = 0; c < scene.sheet.cols; c++) {
|
|
509
|
+
const qs = [0, 1, 2, 3].filter((q) => !kept.has(key({ r, c, q })));
|
|
510
|
+
if (qs.length === 0)
|
|
511
|
+
continue;
|
|
512
|
+
const part = Object.keys(partQuarters).find((p) => partQuarters[p].length === qs.length && partQuarters[p].every((q) => qs.includes(q)));
|
|
513
|
+
gone.push(part
|
|
514
|
+
? partWords(part, r + 1, c + 1)
|
|
515
|
+
: `the ${qs.map((q) => ["top", "right", "bottom", "left"][q]).join(", ")} quarters of the square at ${sayPlace(r + 1, c + 1)}`);
|
|
516
|
+
}
|
|
517
|
+
if (gone.length > 0)
|
|
518
|
+
out.push(`with cut away: ${gone.join("; ")}`);
|
|
519
|
+
const pieces = pieceCount(atoms);
|
|
520
|
+
const words = `${sheetWords(scene.sheet)}${out.length > 0 ? `, ${out.join(", and ")}` : ""}`;
|
|
521
|
+
return pieces > 1 ? `${words}; it is in ${pieces} pieces` : words;
|
|
522
|
+
}
|
|
523
|
+
function cap(words) {
|
|
524
|
+
return `${words[0].toUpperCase()}${words.slice(1)}`;
|
|
525
|
+
}
|
|
526
|
+
export const fold = {
|
|
527
|
+
kind: "fold",
|
|
528
|
+
members: ["kind", "sheet", "folds", "holes", "cuts", "show"],
|
|
529
|
+
check(scene, problems) {
|
|
530
|
+
only(scene, this.members, "scene", problems);
|
|
531
|
+
checkSheet(scene.sheet, problems);
|
|
532
|
+
if (scene.folds !== undefined)
|
|
533
|
+
list(scene.folds, "folds", problems, 0, most.folds).forEach((f, i) => {
|
|
534
|
+
oneOf(f, foldNames, `folds[${i}]`, problems);
|
|
535
|
+
});
|
|
536
|
+
if (scene.show !== undefined)
|
|
537
|
+
oneOf(scene.show, shows, "show", problems);
|
|
538
|
+
if (problems.length > 0)
|
|
539
|
+
return;
|
|
540
|
+
const typed = scene;
|
|
541
|
+
const folded = foldSheet(typed.sheet, typed.folds ?? [], problems);
|
|
542
|
+
if (!folded)
|
|
543
|
+
return;
|
|
544
|
+
checkHoles(scene, folded.final, problems);
|
|
545
|
+
checkCuts(scene, folded.final, problems);
|
|
546
|
+
if (problems.length > 0)
|
|
547
|
+
return;
|
|
548
|
+
if (sizeOf(framesOf(typed, folded)) < smallest)
|
|
549
|
+
problems.push(`sheet: the steps do not fit side by side with squares of at least ${smallest} pixels; use a sheet of fewer squares, fewer folds, or show less`);
|
|
550
|
+
},
|
|
551
|
+
draw(scene, pen) {
|
|
552
|
+
const typed = scene;
|
|
553
|
+
const folded = foldSheet(typed.sheet, typed.folds ?? []);
|
|
554
|
+
const frames = framesOf(typed, folded);
|
|
555
|
+
const s = sizeOf(frames);
|
|
556
|
+
const high = Math.max(...frames.map((f) => f.box.y1 - f.box.y0)) * s;
|
|
557
|
+
const mid = margin + outline + high / 2;
|
|
558
|
+
let x = margin + outline;
|
|
559
|
+
frames.forEach((frame, i) => {
|
|
560
|
+
const { box } = frame;
|
|
561
|
+
const w = (box.x1 - box.x0) * s;
|
|
562
|
+
const top = mid - ((box.y1 - box.y0) * s) / 2;
|
|
563
|
+
const at = (p) => ({ x: x + (p.x - box.x0) * s, y: top + (p.y - box.y0) * s });
|
|
564
|
+
if (frame.what === "fold") {
|
|
565
|
+
drawPaper(pen, frame.step.region, at, typed);
|
|
566
|
+
drawFoldSign(pen, frame.step, at);
|
|
567
|
+
}
|
|
568
|
+
else if (frame.what === "folded") {
|
|
569
|
+
const cut = cutAtoms(typed, folded.final);
|
|
570
|
+
const gone = new Set(cut.map(key));
|
|
571
|
+
drawPaper(pen, folded.final.filter((a) => !gone.has(key(a))), at, typed);
|
|
572
|
+
if (cut.length > 0)
|
|
573
|
+
drawAtoms(pen, cut, at, "none", {
|
|
574
|
+
class: "cut",
|
|
575
|
+
stroke: ink,
|
|
576
|
+
"stroke-width": 2,
|
|
577
|
+
"stroke-dasharray": "5 4",
|
|
578
|
+
"stroke-linejoin": "round",
|
|
579
|
+
});
|
|
580
|
+
const o = originOf(folded.final);
|
|
581
|
+
for (const h of typed.holes ?? []) {
|
|
582
|
+
const c = at({ x: o.c + h.at[1] - 0.5, y: o.r + h.at[0] - 0.5 });
|
|
583
|
+
drawHole(pen, h.shape, h.turn ?? 0, c.x, c.y, s);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
else if (frame.what === "flat") {
|
|
587
|
+
const { atoms, holes } = unfold(typed, folded);
|
|
588
|
+
drawPaper(pen, atoms, at, typed);
|
|
589
|
+
for (const h of holes) {
|
|
590
|
+
const c = at({ x: h.c + 0.5, y: h.r + 0.5 });
|
|
591
|
+
drawHole(pen, h.shape, h.turn, c.x, c.y, s);
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
else {
|
|
595
|
+
pen.el("rect", {
|
|
596
|
+
x,
|
|
597
|
+
y: top,
|
|
598
|
+
width: w,
|
|
599
|
+
height: (box.y1 - box.y0) * s,
|
|
600
|
+
fill: "#ffffff",
|
|
601
|
+
stroke: ink,
|
|
602
|
+
"stroke-width": outline,
|
|
603
|
+
"stroke-dasharray": "7 6",
|
|
604
|
+
});
|
|
605
|
+
pen.text(x + w / 2, mid, "?", {
|
|
606
|
+
size: Math.max(28, Math.min(64, Math.min(w, high) * 0.5)),
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
x += w;
|
|
610
|
+
if (i < frames.length - 1) {
|
|
611
|
+
drawArrow(pen, x + gap, mid);
|
|
612
|
+
x += arrowW + 2 * gap;
|
|
613
|
+
}
|
|
614
|
+
});
|
|
615
|
+
return { width: x + outline + margin, height: mid + high / 2 + outline + margin };
|
|
616
|
+
},
|
|
617
|
+
describe(scene) {
|
|
618
|
+
const typed = scene;
|
|
619
|
+
const folded = foldSheet(typed.sheet, typed.folds ?? []);
|
|
620
|
+
const show = typed.show ?? "steps";
|
|
621
|
+
const folds = typed.folds ?? [];
|
|
622
|
+
if (show === "unfolded" || folds.length === 0) {
|
|
623
|
+
const words = `${cap(flatWords(typed, folded))}.`;
|
|
624
|
+
return show === "ask" ? `${words} After an arrow, a question mark.` : words;
|
|
625
|
+
}
|
|
626
|
+
const out = [`${cap(sheetWords(typed.sheet))}.`];
|
|
627
|
+
const times = ["once", "twice", "three times"][folds.length - 1];
|
|
628
|
+
const each = folded.steps.map((step) => {
|
|
629
|
+
const [moving, staying] = step.name.split(" onto ");
|
|
630
|
+
return `the ${moving} half onto the ${staying} half${lineWords[step.line.dir]}`;
|
|
631
|
+
});
|
|
632
|
+
out.push(`It is folded in half ${times}: ${each.length === 1
|
|
633
|
+
? each[0]
|
|
634
|
+
: `first ${each.slice(0, -1).join(", then ")}, then ${each[each.length - 1]}`}.`);
|
|
635
|
+
out.push(`Folded, it is ${shapeWords(folded.final)}, with ${2 ** folds.length} layers of paper.`);
|
|
636
|
+
const holes = typed.holes ?? [];
|
|
637
|
+
if (holes.length > 0)
|
|
638
|
+
out.push(`${holes.length === 1 ? "A hole is" : `${holes.length} holes are`} punched through all the layers, its squares counted from the top left of the folded sheet: ${holes
|
|
639
|
+
.map((h) => `${article(holeWords(h.shape, h.turn ?? 0))} in the square at ${sayPlace(h.at[0], h.at[1])}`)
|
|
640
|
+
.join("; ")}.`);
|
|
641
|
+
const cuts = typed.cuts ?? [];
|
|
642
|
+
if (cuts.length > 0)
|
|
643
|
+
out.push(`${holes.length > 0 ? "Then " : ""}${cuts.map((c) => partWords(c[2] ?? "whole", c[0], c[1])).join("; ")} ${cuts.length === 1 ? "is" : "are"} cut off through all the layers${holes.length > 0 ? "" : ", its squares counted from the top left of the folded sheet"}.`.replace(/^t/, "T"));
|
|
644
|
+
if (show === "ask")
|
|
645
|
+
out.push("Then the sheet is unfolded again: a question mark stands for what it looks like.");
|
|
646
|
+
if (show === "result")
|
|
647
|
+
out.push(`Unfolded again, it is ${flatWords(typed, folded)}.`);
|
|
648
|
+
return out.join(" ");
|
|
649
|
+
},
|
|
650
|
+
};
|