@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.
Files changed (122) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +7 -0
  3. package/README.md +27 -2
  4. package/lib/emoji.d.ts +16 -0
  5. package/lib/emoji.js +64 -0
  6. package/lib/index.d.ts +9 -0
  7. package/lib/index.js +10 -0
  8. package/lib/kind.d.ts +22 -0
  9. package/lib/kind.js +2 -0
  10. package/lib/kinds/balance.d.ts +34 -0
  11. package/lib/kinds/balance.js +217 -0
  12. package/lib/kinds/branches.d.ts +33 -0
  13. package/lib/kinds/branches.js +473 -0
  14. package/lib/kinds/calendar.d.ts +55 -0
  15. package/lib/kinds/calendar.js +532 -0
  16. package/lib/kinds/chart.d.ts +48 -0
  17. package/lib/kinds/chart.js +645 -0
  18. package/lib/kinds/choices.d.ts +14 -0
  19. package/lib/kinds/choices.js +53 -0
  20. package/lib/kinds/clock.d.ts +36 -0
  21. package/lib/kinds/clock.js +219 -0
  22. package/lib/kinds/coins.d.ts +37 -0
  23. package/lib/kinds/coins.js +550 -0
  24. package/lib/kinds/column.d.ts +37 -0
  25. package/lib/kinds/column.js +410 -0
  26. package/lib/kinds/commands-parts.d.ts +12 -0
  27. package/lib/kinds/commands-parts.js +24 -0
  28. package/lib/kinds/commands.d.ts +66 -0
  29. package/lib/kinds/commands.js +789 -0
  30. package/lib/kinds/containers.d.ts +52 -0
  31. package/lib/kinds/containers.js +758 -0
  32. package/lib/kinds/countfig.d.ts +41 -0
  33. package/lib/kinds/countfig.js +372 -0
  34. package/lib/kinds/cubes.d.ts +70 -0
  35. package/lib/kinds/cubes.js +371 -0
  36. package/lib/kinds/die.d.ts +32 -0
  37. package/lib/kinds/die.js +301 -0
  38. package/lib/kinds/domino.d.ts +37 -0
  39. package/lib/kinds/domino.js +450 -0
  40. package/lib/kinds/dotgrid.d.ts +38 -0
  41. package/lib/kinds/dotgrid.js +411 -0
  42. package/lib/kinds/figure.d.ts +62 -0
  43. package/lib/kinds/figure.js +687 -0
  44. package/lib/kinds/fold-parts.d.ts +73 -0
  45. package/lib/kinds/fold-parts.js +315 -0
  46. package/lib/kinds/fold.d.ts +52 -0
  47. package/lib/kinds/fold.js +650 -0
  48. package/lib/kinds/fraction.d.ts +50 -0
  49. package/lib/kinds/fraction.js +568 -0
  50. package/lib/kinds/gauges.d.ts +53 -0
  51. package/lib/kinds/gauges.js +880 -0
  52. package/lib/kinds/gears.d.ts +40 -0
  53. package/lib/kinds/gears.js +787 -0
  54. package/lib/kinds/grid.d.ts +45 -0
  55. package/lib/kinds/grid.js +591 -0
  56. package/lib/kinds/groups.d.ts +23 -0
  57. package/lib/kinds/groups.js +335 -0
  58. package/lib/kinds/index.d.ts +3 -0
  59. package/lib/kinds/index.js +87 -0
  60. package/lib/kinds/lanes.d.ts +36 -0
  61. package/lib/kinds/lanes.js +220 -0
  62. package/lib/kinds/layers.d.ts +31 -0
  63. package/lib/kinds/layers.js +419 -0
  64. package/lib/kinds/line.d.ts +38 -0
  65. package/lib/kinds/line.js +739 -0
  66. package/lib/kinds/magic-parts.d.ts +72 -0
  67. package/lib/kinds/magic-parts.js +186 -0
  68. package/lib/kinds/magic.d.ts +66 -0
  69. package/lib/kinds/magic.js +664 -0
  70. package/lib/kinds/map.d.ts +32 -0
  71. package/lib/kinds/map.js +370 -0
  72. package/lib/kinds/matchsticks.d.ts +26 -0
  73. package/lib/kinds/matchsticks.js +434 -0
  74. package/lib/kinds/necklace.d.ts +27 -0
  75. package/lib/kinds/necklace.js +342 -0
  76. package/lib/kinds/net.d.ts +47 -0
  77. package/lib/kinds/net.js +241 -0
  78. package/lib/kinds/overlay.d.ts +50 -0
  79. package/lib/kinds/overlay.js +519 -0
  80. package/lib/kinds/pieces.d.ts +41 -0
  81. package/lib/kinds/pieces.js +420 -0
  82. package/lib/kinds/pipes.d.ts +46 -0
  83. package/lib/kinds/pipes.js +657 -0
  84. package/lib/kinds/plane-parts.d.ts +105 -0
  85. package/lib/kinds/plane-parts.js +372 -0
  86. package/lib/kinds/plane.d.ts +35 -0
  87. package/lib/kinds/plane.js +755 -0
  88. package/lib/kinds/pyramid.d.ts +26 -0
  89. package/lib/kinds/pyramid.js +228 -0
  90. package/lib/kinds/ropes.d.ts +30 -0
  91. package/lib/kinds/ropes.js +571 -0
  92. package/lib/kinds/shapes.d.ts +59 -0
  93. package/lib/kinds/shapes.js +490 -0
  94. package/lib/kinds/solids.d.ts +78 -0
  95. package/lib/kinds/solids.js +1058 -0
  96. package/lib/kinds/tangram.d.ts +52 -0
  97. package/lib/kinds/tangram.js +713 -0
  98. package/lib/kinds/target.d.ts +39 -0
  99. package/lib/kinds/target.js +761 -0
  100. package/lib/kinds/tiles.d.ts +55 -0
  101. package/lib/kinds/tiles.js +606 -0
  102. package/lib/kinds/venn.d.ts +38 -0
  103. package/lib/kinds/venn.js +710 -0
  104. package/lib/noto.d.ts +2 -0
  105. package/lib/noto.js +212 -0
  106. package/lib/objects.d.ts +7 -0
  107. package/lib/objects.js +226 -0
  108. package/lib/pen.d.ts +32 -0
  109. package/lib/pen.js +60 -0
  110. package/lib/read.d.ts +9 -0
  111. package/lib/read.js +49 -0
  112. package/lib/render.d.ts +24 -0
  113. package/lib/render.js +68 -0
  114. package/lib/solve/balance.d.ts +46 -0
  115. package/lib/solve/balance.js +118 -0
  116. package/lib/style.d.ts +70 -0
  117. package/lib/style.js +39 -0
  118. package/lib/svg.d.ts +22 -0
  119. package/lib/svg.js +48 -0
  120. package/lib/things.d.ts +40 -0
  121. package/lib/things.js +269 -0
  122. package/package.json +28 -4
@@ -0,0 +1,687 @@
1
+ import { margin } from "../kind.js";
2
+ import { isObj, list, oneOf, only, text } from "../read.js";
3
+ import { colorNames, colors, ink, isColor, outline } from "../style.js";
4
+ import { n, poly } from "../svg.js";
5
+ import { checkThing, drawThing, wordOf } from "../things.js";
6
+ /**
7
+ * Figure: a square or round card with a pattern — its face cut into parts of
8
+ * colours, and marks on it: dots, things, arrows, letters — and what the card
9
+ * becomes when it is flipped over, turned and its colours switched. The
10
+ * program works the result out itself, part by part and mark by mark, and
11
+ * the result is again a card of the same form, so an option such as "the card
12
+ * turned a quarter turn" is exactly that card.
13
+ */
14
+ export const cardShapes = ["square", "circle"];
15
+ export const cuts = [
16
+ "none",
17
+ "cross",
18
+ "horizontal",
19
+ "vertical",
20
+ "diagonals",
21
+ "diagonal",
22
+ "anti-diagonal",
23
+ "stripes",
24
+ "bands",
25
+ "grid",
26
+ ];
27
+ export const positions = [
28
+ "top-left",
29
+ "top",
30
+ "top-right",
31
+ "left",
32
+ "centre",
33
+ "right",
34
+ "bottom-left",
35
+ "bottom",
36
+ "bottom-right",
37
+ ];
38
+ export const turns = [0, 90, 180, 270];
39
+ export const flips = ["none", "horizontal", "vertical"];
40
+ export const shows = ["result", "both", "ask"];
41
+ // The parts of each cut, in the square from -1 to 1, clockwise on the screen,
42
+ // each with its name in words.
43
+ const T = { x: -1, y: -1 };
44
+ const R = { x: 1, y: -1 };
45
+ const B = { x: 1, y: 1 };
46
+ const L = { x: -1, y: 1 };
47
+ const O = { x: 0, y: 0 };
48
+ const p = (x, y) => ({ x, y });
49
+ const third = 1 / 3;
50
+ const pieces = {
51
+ none: [{ name: "whole", points: [T, R, B, L] }],
52
+ cross: [
53
+ { name: "top-left quarter", points: [T, p(0, -1), O, p(-1, 0)] },
54
+ { name: "top-right quarter", points: [p(0, -1), R, p(1, 0), O] },
55
+ { name: "bottom-right quarter", points: [O, p(1, 0), B, p(0, 1)] },
56
+ { name: "bottom-left quarter", points: [p(-1, 0), O, p(0, 1), L] },
57
+ ],
58
+ horizontal: [
59
+ { name: "top half", points: [T, R, p(1, 0), p(-1, 0)] },
60
+ { name: "bottom half", points: [p(-1, 0), p(1, 0), B, L] },
61
+ ],
62
+ vertical: [
63
+ { name: "left half", points: [T, p(0, -1), p(0, 1), L] },
64
+ { name: "right half", points: [p(0, -1), R, B, p(0, 1)] },
65
+ ],
66
+ diagonals: [
67
+ { name: "top triangle", points: [T, R, O] },
68
+ { name: "right triangle", points: [R, B, O] },
69
+ { name: "bottom triangle", points: [B, L, O] },
70
+ { name: "left triangle", points: [L, T, O] },
71
+ ],
72
+ diagonal: [
73
+ { name: "top-right half", points: [T, R, B] },
74
+ { name: "bottom-left half", points: [T, B, L] },
75
+ ],
76
+ "anti-diagonal": [
77
+ { name: "top-left half", points: [T, R, L] },
78
+ { name: "bottom-right half", points: [R, B, L] },
79
+ ],
80
+ stripes: [
81
+ { name: "left stripe", points: [T, p(-third, -1), p(-third, 1), L] },
82
+ { name: "middle stripe", points: [p(-third, -1), p(third, -1), p(third, 1), p(-third, 1)] },
83
+ { name: "right stripe", points: [p(third, -1), R, B, p(third, 1)] },
84
+ ],
85
+ bands: [
86
+ { name: "top band", points: [T, R, p(1, -third), p(-1, -third)] },
87
+ { name: "middle band", points: [p(-1, -third), p(1, -third), p(1, third), p(-1, third)] },
88
+ { name: "bottom band", points: [p(-1, third), p(1, third), B, L] },
89
+ ],
90
+ grid: [-1, 0, 1].flatMap((row) => [-1, 0, 1].map((col) => {
91
+ const x = (2 * col - 1) / 3;
92
+ const y = (2 * row - 1) / 3;
93
+ const rowName = ["top", "middle", "bottom"][row + 1];
94
+ const colName = ["left", "middle", "right"][col + 1];
95
+ return {
96
+ name: `${rowName}-${colName} square`.replace("middle-middle", "centre"),
97
+ points: [p(x, y), p(x + 2 / 3, y), p(x + 2 / 3, y + 2 / 3), p(x, y + 2 / 3)],
98
+ };
99
+ })),
100
+ };
101
+ const cutWords = {
102
+ none: "",
103
+ cross: "cut into four quarters by a line down and a line across",
104
+ horizontal: "cut across the middle into a top half and a bottom half",
105
+ vertical: "cut down the middle into a left half and a right half",
106
+ diagonals: "cut along both diagonals into four triangles",
107
+ diagonal: "cut along the diagonal from the top-left corner to the bottom-right corner",
108
+ "anti-diagonal": "cut along the diagonal from the top-right corner to the bottom-left corner",
109
+ stripes: "cut into three stripes from top to bottom",
110
+ bands: "cut into three bands from side to side",
111
+ grid: "cut into nine squares, three rows of three",
112
+ };
113
+ function centroid(points) {
114
+ return {
115
+ x: points.reduce((s, q) => s + q.x, 0) / points.length,
116
+ y: points.reduce((s, q) => s + q.y, 0) / points.length,
117
+ };
118
+ }
119
+ const quarter = (q) => ({ x: -q.y, y: q.x });
120
+ const mirrorH = (q) => ({ x: -q.x, y: q.y });
121
+ const mirrorV = (q) => ({ x: q.x, y: -q.y });
122
+ const near = (a, b) => Math.abs(a.x - b.x) < 1e-9 && Math.abs(a.y - b.y) < 1e-9;
123
+ /** moveCut finds the cut a move makes of a cut, and where each part goes. */
124
+ function moveCut(cut, move) {
125
+ const from = pieces[cut].map((piece) => move(centroid(piece.points)));
126
+ for (const other of cuts) {
127
+ const at = pieces[other].map((piece) => centroid(piece.points));
128
+ if (at.length !== from.length)
129
+ continue;
130
+ const to = from.map((c) => at.findIndex((a) => near(a, c)));
131
+ if (to.every((i) => i >= 0))
132
+ return { cut: other, to };
133
+ }
134
+ throw new Error(`no cut is ${cut} moved`);
135
+ }
136
+ const grid3 = (cut) => ({
137
+ x: cut === "grid" || cut === "stripes" ? 2 / 3 : 0.5,
138
+ y: cut === "grid" || cut === "bands" ? 2 / 3 : 0.5,
139
+ });
140
+ function positionPoint(at) {
141
+ const i = positions.indexOf(at);
142
+ return { x: (i % 3) - 1, y: Math.floor(i / 3) - 1 };
143
+ }
144
+ function pointPosition(q) {
145
+ return positions[(Math.round(q.y) + 1) * 3 + Math.round(q.x) + 1];
146
+ }
147
+ const swapThing = (thing, c) => {
148
+ if (typeof thing === "string")
149
+ return thing;
150
+ if ("ball" in thing)
151
+ return { ...thing, ball: c(thing.ball) };
152
+ if ("shape" in thing)
153
+ return { ...thing, color: c(thing.color) };
154
+ if ("card" in thing && thing.color)
155
+ return { ...thing, color: c(thing.color) };
156
+ return thing;
157
+ };
158
+ /** look writes a mark's turn and mirror the short way: none when it is upright. */
159
+ function look(mark, turn, mirror) {
160
+ const out = { ...mark };
161
+ delete out.turn;
162
+ delete out.mirror;
163
+ if ("dot" in out)
164
+ return out;
165
+ const t = ((turn % 360) + 360) % 360;
166
+ // An arrow is its own mirror image.
167
+ const m = mirror && !("arrow" in out);
168
+ if (t !== 0)
169
+ out.turn = t;
170
+ if (m)
171
+ out.mirror = true;
172
+ return out;
173
+ }
174
+ /** moveCard moves a card: a quarter turn clockwise, or a flip. */
175
+ function moveCard(card, how) {
176
+ const move = how === "quarter" ? quarter : how === "horizontal" ? mirrorH : mirrorV;
177
+ const cut = card.cut ?? "none";
178
+ const moved = moveCut(cut, move);
179
+ const colors_ = [...card.colors];
180
+ moved.to.forEach((j, i) => {
181
+ colors_[j] = card.colors[i];
182
+ });
183
+ const out = { ...card, colors: colors_ };
184
+ if (card.cut !== undefined)
185
+ out.cut = moved.cut;
186
+ if (card.marks) {
187
+ out.marks = card.marks.map((m) => {
188
+ const turn = m.turn ?? 0;
189
+ const mirror = m.mirror === true;
190
+ const at = pointPosition(move(positionPoint(m.at)));
191
+ // A flip mirrors a mark and turns it the other way: H·R(t) = R(−t)·H,
192
+ // and a flip upside down is a flip across followed by a half turn.
193
+ const next = how === "quarter"
194
+ ? look(m, turn + 90, mirror)
195
+ : how === "horizontal"
196
+ ? look(m, -turn, !mirror)
197
+ : look(m, 180 - turn, !mirror);
198
+ return { ...next, at };
199
+ });
200
+ }
201
+ return out;
202
+ }
203
+ /** change works out what a card becomes: flipped first, then turned, then its colours switched. */
204
+ export function change(card, how) {
205
+ // A card is JSON data, so a copy through JSON needs no structuredClone, which only a host
206
+ // — a browser or Node — gives.
207
+ let out = JSON.parse(JSON.stringify(card));
208
+ if (how.flip && how.flip !== "none")
209
+ out = moveCard(out, how.flip);
210
+ for (let t = 0; t < (how.turn ?? 0); t += 90)
211
+ out = moveCard(out, "quarter");
212
+ if (how.swap && how.swap.length > 0) {
213
+ const pairs = how.swap;
214
+ const c = (x) => {
215
+ for (const [a, b] of pairs) {
216
+ if (x === a)
217
+ return b;
218
+ if (x === b)
219
+ return a;
220
+ }
221
+ return x;
222
+ };
223
+ out.colors = out.colors.map(c);
224
+ if (out.marks)
225
+ out.marks = out.marks.map((m) => {
226
+ if ("dot" in m)
227
+ return { ...m, dot: c(m.dot) };
228
+ if ("arrow" in m)
229
+ return { ...m, arrow: c(m.arrow) };
230
+ if ("letter" in m)
231
+ return m.color ? { ...m, color: c(m.color) } : m;
232
+ return { ...m, thing: swapThing(m.thing, c) };
233
+ });
234
+ }
235
+ return out;
236
+ }
237
+ // Drawing.
238
+ /** inDisc is the outline of a convex part of the square cut out of the round card. */
239
+ function inDisc(points) {
240
+ // The outer edges of the square go out past the circle, so that only the
241
+ // cuts cross it.
242
+ const out = points.map((q) => ({
243
+ x: Math.abs(q.x) === 1 ? 2 * q.x : q.x,
244
+ y: Math.abs(q.y) === 1 ? 2 * q.y : q.y,
245
+ }));
246
+ const segments = [];
247
+ for (let i = 0; i < out.length; i++) {
248
+ const a = out[i];
249
+ const b = out[(i + 1) % out.length];
250
+ const d = { x: b.x - a.x, y: b.y - a.y };
251
+ const qa = d.x * d.x + d.y * d.y;
252
+ const qb = 2 * (a.x * d.x + a.y * d.y);
253
+ const qc = a.x * a.x + a.y * a.y - 1;
254
+ const disc = qb * qb - 4 * qa * qc;
255
+ if (disc <= 0)
256
+ continue;
257
+ const t0 = Math.max(0, (-qb - Math.sqrt(disc)) / (2 * qa));
258
+ const t1 = Math.min(1, (-qb + Math.sqrt(disc)) / (2 * qa));
259
+ if (t1 - t0 < 1e-9)
260
+ continue;
261
+ segments.push([
262
+ { x: a.x + t0 * d.x, y: a.y + t0 * d.y },
263
+ { x: a.x + t1 * d.x, y: a.y + t1 * d.y },
264
+ ]);
265
+ }
266
+ return segments;
267
+ }
268
+ function piecePath(shape, piece, c, h) {
269
+ const at = (q) => ({ x: c.x + q.x * h, y: c.y + q.y * h });
270
+ if (shape === "square")
271
+ return poly(piece.map(at));
272
+ const segments = inDisc(piece);
273
+ if (segments.length === 0)
274
+ return circlePath(c, h);
275
+ let d = "";
276
+ let last;
277
+ const arc = (from, to) => {
278
+ const a0 = Math.atan2(from.y, from.x);
279
+ const a1 = Math.atan2(to.y, to.x);
280
+ const sweep = (((a1 - a0) % (2 * Math.PI)) + 2 * Math.PI) % (2 * Math.PI);
281
+ const q = at(to);
282
+ return `A${n(h)} ${n(h)} 0 ${sweep > Math.PI ? 1 : 0} 1 ${n(q.x)} ${n(q.y)}`;
283
+ };
284
+ for (const [a, b] of segments) {
285
+ if (!last)
286
+ d += `M${n(at(a).x)} ${n(at(a).y)}`;
287
+ else if (!near(last, a))
288
+ d += arc(last, a);
289
+ d += `L${n(at(b).x)} ${n(at(b).y)}`;
290
+ last = b;
291
+ }
292
+ const first = segments[0][0];
293
+ if (last && !near(last, first))
294
+ d += arc(last, first);
295
+ return `${d}Z`;
296
+ }
297
+ function circlePath(c, r) {
298
+ return `M${n(c.x - r)} ${n(c.y)}a${n(r)} ${n(r)} 0 1 0 ${n(2 * r)} 0a${n(r)} ${n(r)} 0 1 0 ${n(-2 * r)} 0Z`;
299
+ }
300
+ function outlinePath(shape, c, h) {
301
+ return shape === "square"
302
+ ? poly([
303
+ { x: c.x - h, y: c.y - h },
304
+ { x: c.x + h, y: c.y - h },
305
+ { x: c.x + h, y: c.y + h },
306
+ { x: c.x - h, y: c.y + h },
307
+ ])
308
+ : circlePath(c, h);
309
+ }
310
+ function drawMark(pen, mark, cut, c, h) {
311
+ const g = grid3(cut);
312
+ const q = positionPoint(mark.at);
313
+ const x = c.x + q.x * g.x * h;
314
+ const y = c.y + q.y * g.y * h;
315
+ if ("dot" in mark) {
316
+ pen.el("circle", {
317
+ cx: x,
318
+ cy: y,
319
+ r: h * 0.13,
320
+ fill: colors[mark.dot].fill,
321
+ stroke: ink,
322
+ "stroke-width": 2,
323
+ });
324
+ return;
325
+ }
326
+ const turn = mark.turn ?? 0;
327
+ const parts = [];
328
+ if (turn !== 0)
329
+ parts.push(`rotate(${turn} ${n(x)} ${n(y)})`);
330
+ if (mark.mirror)
331
+ parts.push(`translate(${n(2 * x)} 0) scale(-1 1)`);
332
+ if (parts.length > 0)
333
+ pen.add(`<g transform="${parts.join(" ")}">`);
334
+ if ("thing" in mark)
335
+ drawThing(pen, mark.thing, x, y, h * 0.56);
336
+ else if ("arrow" in mark) {
337
+ const s = h * 0.5;
338
+ pen.el("path", {
339
+ d: poly([
340
+ { x, y: y - s / 2 },
341
+ { x: x + s * 0.34, y: y - s * 0.05 },
342
+ { x: x + s * 0.12, y: y - s * 0.05 },
343
+ { x: x + s * 0.12, y: y + s / 2 },
344
+ { x: x - s * 0.12, y: y + s / 2 },
345
+ { x: x - s * 0.12, y: y - s * 0.05 },
346
+ { x: x - s * 0.34, y: y - s * 0.05 },
347
+ ]),
348
+ fill: colors[mark.arrow].fill,
349
+ stroke: ink,
350
+ "stroke-width": 2,
351
+ "stroke-linejoin": "round",
352
+ });
353
+ }
354
+ else
355
+ pen.text(x, y, mark.letter, {
356
+ size: h * 0.56,
357
+ weight: 800,
358
+ fill: mark.color ? colors[mark.color].fill : ink,
359
+ });
360
+ if (parts.length > 0)
361
+ pen.add("</g>");
362
+ }
363
+ /** drawCard draws a card centred at c, h its half width. */
364
+ export function drawCard(pen, card, c, h) {
365
+ const cut = card.cut ?? "none";
366
+ pieces[cut].forEach((piece, i) => {
367
+ pen.el("path", {
368
+ d: piecePath(card.shape, piece.points, c, h),
369
+ fill: colors[card.colors[i]].fill,
370
+ stroke: ink,
371
+ "stroke-width": 1.5,
372
+ "stroke-linejoin": "round",
373
+ });
374
+ });
375
+ for (const mark of card.marks ?? [])
376
+ drawMark(pen, mark, cut, c, h);
377
+ pen.el("path", {
378
+ d: outlinePath(card.shape, c, h),
379
+ fill: "none",
380
+ stroke: ink,
381
+ "stroke-width": outline,
382
+ "stroke-linejoin": "round",
383
+ });
384
+ }
385
+ /** drawMove draws the arrow from a card to what it becomes, with a sign for each move above it. */
386
+ function drawMove(pen, how, x, mid, w) {
387
+ const a = { x: x + 10, y: mid + 22 };
388
+ const b = { x: x + w - 10, y: mid + 22 };
389
+ pen.el("path", {
390
+ d: poly([
391
+ { x: a.x, y: a.y - 5 },
392
+ { x: b.x - 18, y: a.y - 5 },
393
+ { x: b.x - 18, y: a.y - 13 },
394
+ { x: b.x, y: a.y },
395
+ { x: b.x - 18, y: a.y + 13 },
396
+ { x: b.x - 18, y: a.y + 5 },
397
+ { x: a.x, y: a.y + 5 },
398
+ ]),
399
+ fill: colors.gray.fill,
400
+ stroke: ink,
401
+ "stroke-width": 2,
402
+ "stroke-linejoin": "round",
403
+ });
404
+ const signs = [];
405
+ const flip = how.flip ?? "none";
406
+ if (flip !== "none")
407
+ signs.push((cx, cy) => {
408
+ const across = flip === "horizontal";
409
+ pen.el("path", {
410
+ d: across ? `M${cx} ${cy - 15}V${cy + 15}` : `M${cx - 15} ${cy}H${cx + 15}`,
411
+ stroke: ink,
412
+ "stroke-width": 2,
413
+ "stroke-dasharray": "4 3",
414
+ });
415
+ const head = (tx, ty, dx, dy) => poly([
416
+ { x: tx, y: ty },
417
+ { x: tx - dx * 7 - dy * 5, y: ty - dy * 7 - dx * 5 },
418
+ { x: tx - dx * 7 + dy * 5, y: ty - dy * 7 + dx * 5 },
419
+ ]);
420
+ const d = across
421
+ ? `M${cx - 16} ${cy}H${cx + 16}${head(cx + 18, cy, 1, 0)}${head(cx - 18, cy, -1, 0)}`
422
+ : `M${cx} ${cy - 16}V${cy + 16}${head(cx, cy + 18, 0, 1)}${head(cx, cy - 18, 0, -1)}`;
423
+ pen.el("path", {
424
+ d,
425
+ stroke: ink,
426
+ "stroke-width": 2.5,
427
+ fill: ink,
428
+ "stroke-linejoin": "round",
429
+ });
430
+ });
431
+ const turn = how.turn ?? 0;
432
+ if (turn !== 0)
433
+ signs.push((cx, cy) => {
434
+ const back = turn === 270;
435
+ const r = 12;
436
+ const ox = cx - 14;
437
+ // A curved arrow, three quarters of a circle ending at the top, its
438
+ // head there pointing the way it turns: clockwise unless back.
439
+ const start = back ? { x: ox - r, y: cy } : { x: ox + r, y: cy };
440
+ const end = { x: ox, y: cy - r };
441
+ pen.el("path", {
442
+ d: `M${n(start.x)} ${n(start.y)}A${r} ${r} 0 1 ${back ? 0 : 1} ${n(end.x)} ${n(end.y)}`,
443
+ fill: "none",
444
+ stroke: ink,
445
+ "stroke-width": 2.5,
446
+ });
447
+ const dir = back ? -1 : 1;
448
+ pen.el("path", {
449
+ d: poly([
450
+ { x: end.x + dir * 7, y: end.y },
451
+ { x: end.x - dir * 2, y: end.y - 6 },
452
+ { x: end.x - dir * 2, y: end.y + 6 },
453
+ ]),
454
+ fill: ink,
455
+ stroke: ink,
456
+ "stroke-width": 1.5,
457
+ "stroke-linejoin": "round",
458
+ });
459
+ pen.text(cx + 18, cy, `${back ? 90 : turn}°`, { size: 16 });
460
+ });
461
+ for (const [c1, c2] of how.swap ?? [])
462
+ signs.push((cx, cy) => {
463
+ for (const [dx, color] of [
464
+ [-20, c1],
465
+ [20, c2],
466
+ ])
467
+ pen.el("circle", {
468
+ cx: cx + dx,
469
+ cy,
470
+ r: 9,
471
+ fill: colors[color].fill,
472
+ stroke: ink,
473
+ "stroke-width": 2,
474
+ });
475
+ pen.el("path", {
476
+ d: `M${cx - 7} ${cy - 3}H${cx + 7}M${cx + 3} ${cy - 7}L${cx + 7} ${cy - 3}M${cx - 7} ${cy + 3}H${cx + 7}M${cx - 3} ${cy + 7}L${cx - 7} ${cy + 3}`,
477
+ stroke: ink,
478
+ "stroke-width": 2,
479
+ fill: "none",
480
+ "stroke-linecap": "round",
481
+ });
482
+ });
483
+ signs.forEach((sign, i) => {
484
+ sign(x + w / 2, mid - 10 - (signs.length - 1 - i) * 34);
485
+ });
486
+ }
487
+ // Words.
488
+ function lookWords(mark) {
489
+ const turn = mark.turn ?? 0;
490
+ const turned = turn === 90
491
+ ? "turned a quarter turn clockwise"
492
+ : turn === 180
493
+ ? "turned upside down"
494
+ : turn === 270
495
+ ? "turned a quarter turn anticlockwise"
496
+ : "";
497
+ if (mark.mirror)
498
+ return turned ? `, mirrored, then ${turned}` : ", mirrored";
499
+ return turned ? `, ${turned}` : "";
500
+ }
501
+ const placeWords = (at) => (at === "centre" ? "in the centre" : `at the ${at}`);
502
+ const directions = ["up", "right", "down", "left"];
503
+ function markWords(mark) {
504
+ if ("dot" in mark)
505
+ return `a ${colors[mark.dot].word} dot ${placeWords(mark.at)}`;
506
+ if ("arrow" in mark)
507
+ return `a ${colors[mark.arrow].word} arrow ${placeWords(mark.at)} pointing ${directions[(mark.turn ?? 0) / 90]}`;
508
+ if ("letter" in mark)
509
+ return `the ${mark.color ? `${colors[mark.color].word} ` : ""}letter ${mark.letter} ${placeWords(mark.at)}${lookWords(mark)}`;
510
+ const word = wordOf(mark.thing);
511
+ return `${/^[aeiou]/.test(word) ? "an" : "a"} ${word} ${placeWords(mark.at)}${lookWords(mark)}`;
512
+ }
513
+ /** sayCard says a card in words. */
514
+ export function sayCard(card) {
515
+ const cut = card.cut ?? "none";
516
+ const shape = card.shape === "square" ? "square card" : "round card";
517
+ let words;
518
+ if (cut === "none")
519
+ words = `a ${colors[card.colors[0]].word} ${shape}`;
520
+ else {
521
+ const named = pieces[cut]
522
+ .map((piece, i) => `the ${piece.name} is ${colors[card.colors[i]].word}`)
523
+ .join(", ");
524
+ words = `a ${shape} ${cutWords[cut]}: ${named}`;
525
+ }
526
+ const marks = card.marks ?? [];
527
+ if (marks.length > 0)
528
+ words += `; on it ${marks.map(markWords).join(", ")}`;
529
+ return words;
530
+ }
531
+ function changeWords(how) {
532
+ const steps = [];
533
+ if (how.flip === "horizontal")
534
+ steps.push("flipped over from left to right, as in a mirror");
535
+ if (how.flip === "vertical")
536
+ steps.push("flipped over from top to bottom, as in a mirror");
537
+ if (how.turn === 90)
538
+ steps.push("turned a quarter turn clockwise");
539
+ if (how.turn === 180)
540
+ steps.push("turned a half turn");
541
+ if (how.turn === 270)
542
+ steps.push("turned a quarter turn anticlockwise");
543
+ for (const [a, b] of how.swap ?? [])
544
+ steps.push(`its ${colors[a].word} and ${colors[b].word} switched`);
545
+ return steps.join(", then ");
546
+ }
547
+ function checkCard(card, problems) {
548
+ if (!isObj(card)) {
549
+ problems.push("card: must be a card with a shape and colours");
550
+ return;
551
+ }
552
+ only(card, ["shape", "cut", "colors", "marks"], "card", problems);
553
+ const shape = oneOf(card.shape, cardShapes, "card.shape", problems);
554
+ const cut = card.cut === undefined ? "none" : oneOf(card.cut, cuts, "card.cut", problems);
555
+ if (cut === "grid" && shape === "circle")
556
+ problems.push("card.cut: a round card is not cut into a grid");
557
+ const want = pieces[cut].length;
558
+ const list_ = list(card.colors, "card.colors", problems, want, want);
559
+ if (list_.length !== want)
560
+ problems.push(`card.colors: a card cut "${cut}" has ${want} part${want === 1 ? "" : "s"}: ${pieces[cut].map((x) => x.name).join(", ")}`);
561
+ list_.forEach((c, i) => {
562
+ if (!isColor(c))
563
+ problems.push(`card.colors[${i}]: must be a colour: ${colorNames.join(", ")}`);
564
+ });
565
+ if (card.marks === undefined)
566
+ return;
567
+ const seen = new Set();
568
+ list(card.marks, "card.marks", problems, 1, 9).forEach((m, i) => {
569
+ const path = `card.marks[${i}]`;
570
+ if (!isObj(m)) {
571
+ problems.push(`${path}: must be a dot, a thing, an arrow or a letter, at a place`);
572
+ return;
573
+ }
574
+ const at = oneOf(m.at, positions, `${path}.at`, problems);
575
+ if (positions.includes(m.at)) {
576
+ if (seen.has(at))
577
+ problems.push(`${path}.at: another mark is ${placeWords(at)} already`);
578
+ seen.add(at);
579
+ }
580
+ const look_ = ["at", "turn", "mirror"];
581
+ if ("dot" in m) {
582
+ only(m, ["at", "dot"], path, problems);
583
+ if (!isColor(m.dot))
584
+ problems.push(`${path}.dot: must be a colour`);
585
+ }
586
+ else if ("arrow" in m) {
587
+ only(m, ["at", "arrow", "turn"], path, problems);
588
+ if (!isColor(m.arrow))
589
+ problems.push(`${path}.arrow: must be a colour`);
590
+ }
591
+ else if ("letter" in m) {
592
+ only(m, [...look_, "letter", "color"], path, problems);
593
+ text(m.letter, `${path}.letter`, problems, 2);
594
+ if (m.color !== undefined && !isColor(m.color))
595
+ problems.push(`${path}.color: must be a colour`);
596
+ }
597
+ else if ("thing" in m) {
598
+ only(m, [...look_, "thing"], path, problems);
599
+ checkThing(m.thing, `${path}.thing`, problems);
600
+ if (isObj(m.thing) && ("ask" in m.thing || "empty" in m.thing))
601
+ problems.push(`${path}.thing: a mark is something on the card, not ask or empty`);
602
+ }
603
+ else
604
+ problems.push(`${path}: must be a dot, a thing, an arrow or a letter`);
605
+ if (m.turn !== undefined && !turns.includes(m.turn))
606
+ problems.push(`${path}.turn: must be one of ${turns.join(", ")}`);
607
+ if (m.mirror !== undefined && typeof m.mirror !== "boolean")
608
+ problems.push(`${path}.mirror: must be true or false`);
609
+ });
610
+ }
611
+ export const figure = {
612
+ kind: "figure",
613
+ members: ["kind", "card", "turn", "flip", "swap", "show"],
614
+ check(scene, problems) {
615
+ only(scene, this.members, "scene", problems);
616
+ checkCard(scene.card, problems);
617
+ if (scene.turn !== undefined && !turns.includes(scene.turn))
618
+ problems.push(`turn: must be one of ${turns.join(", ")}, clockwise`);
619
+ if (scene.flip !== undefined)
620
+ oneOf(scene.flip, flips, "flip", problems);
621
+ if (scene.swap !== undefined) {
622
+ const used = new Set();
623
+ list(scene.swap, "swap", problems, 1, 3).forEach((pair, i) => {
624
+ const pairList = list(pair, `swap[${i}]`, problems, 2, 2);
625
+ pairList.forEach((c, j) => {
626
+ if (!isColor(c))
627
+ problems.push(`swap[${i}][${j}]: must be a colour`);
628
+ else if (used.has(c))
629
+ problems.push(`swap[${i}][${j}]: ${c} is switched already`);
630
+ used.add(c);
631
+ });
632
+ });
633
+ }
634
+ if (scene.show !== undefined) {
635
+ oneOf(scene.show, shows, "show", problems);
636
+ const moves = (scene.turn !== undefined && scene.turn !== 0) ||
637
+ (scene.flip !== undefined && scene.flip !== "none") ||
638
+ scene.swap !== undefined;
639
+ if (scene.show !== "result" && !moves)
640
+ problems.push(`show: "${String(scene.show)}" shows a move; give a turn, a flip or a swap`);
641
+ }
642
+ },
643
+ draw(scene, pen) {
644
+ const typed = scene;
645
+ const show = typed.show ?? "result";
646
+ if (show === "result") {
647
+ const h = 84;
648
+ drawCard(pen, change(typed.card, typed), { x: margin + h, y: margin + h }, h);
649
+ return { width: 2 * h + 2 * margin, height: 2 * h + 2 * margin };
650
+ }
651
+ const h = 72;
652
+ const signs = (typed.flip && typed.flip !== "none" ? 1 : 0) +
653
+ (typed.turn ? 1 : 0) +
654
+ (typed.swap?.length ?? 0);
655
+ const top = margin + Math.max(0, signs * 34 - 2 * h + 30);
656
+ const mid = top + h;
657
+ const w = 130;
658
+ drawCard(pen, typed.card, { x: margin + h, y: mid }, h);
659
+ drawMove(pen, typed, margin + 2 * h + 8, mid, w);
660
+ const right = { x: margin + 2 * h + 16 + w + h, y: mid };
661
+ if (show === "both")
662
+ drawCard(pen, change(typed.card, typed), right, h);
663
+ else {
664
+ pen.el("path", {
665
+ d: outlinePath(typed.card.shape, right, h),
666
+ fill: "#ffffff",
667
+ stroke: ink,
668
+ "stroke-width": outline,
669
+ "stroke-dasharray": "7 6",
670
+ });
671
+ pen.text(right.x, right.y, "?", { size: 64 });
672
+ }
673
+ return { width: right.x + h + margin, height: mid + h + margin };
674
+ },
675
+ describe(scene) {
676
+ const typed = scene;
677
+ const show = typed.show ?? "result";
678
+ const result = change(typed.card, typed);
679
+ const cap = (s) => `${s[0].toUpperCase()}${s.slice(1)}`;
680
+ if (show === "result")
681
+ return `${cap(sayCard(result))}.`;
682
+ const first = `On the left, ${sayCard(typed.card)}. An arrow shows that it is ${changeWords(typed)}.`;
683
+ if (show === "ask")
684
+ return `${first} On the right, a question mark: what does the card look like then?`;
685
+ return `${first} On the right, what it becomes: ${sayCard(result)}.`;
686
+ },
687
+ };