@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,450 @@
1
+ import { margin } from "../kind.js";
2
+ import { int, isObj, list, oneOf, only } from "../read.js";
3
+ import { colorNames, colors, ink, isColor, outline, soft, textOn, } from "../style.js";
4
+ import { n } from "../svg.js";
5
+ import { plural } from "./magic-parts.js";
6
+ /**
7
+ * Domino: tiles of two halves, each half 0 to 9 pips, "?" where the question
8
+ * asks, or "cloud" for a half hidden under a cloud. layout lays them out:
9
+ *
10
+ * - row: apart, from left to right, standing upright with upright, else
11
+ * lying flat; a tile is [first, second] — top and bottom, or left and right.
12
+ * - chain: lying flat end to end, from left to right.
13
+ * - ring: end to end in a closed ring round the edge of a rectangle columns
14
+ * halves across (3 or more; as square as it can be unless given), clockwise
15
+ * from the top-left corner; a tile's halves go in that order, so that each
16
+ * tile's second half touches the next tile's first.
17
+ * - grid: each tile an object with its halves, at — [row, column] of its
18
+ * first half, its top or left one, counted from 1 at the top left of a board
19
+ * of halves — and upright; no two tiles share a place.
20
+ *
21
+ * color is the tiles' face, white unless given.
22
+ */
23
+ export const dominoLayouts = ["row", "chain", "ring", "grid"];
24
+ const room = 640 - 2 * margin;
25
+ const biggest = 54;
26
+ const gap = 18;
27
+ const board = 10;
28
+ /** pips are the places of the pips of each value in a 3 × 3 lattice, column and row, on an upright tile. */
29
+ const pips = [
30
+ [],
31
+ [[1, 1]],
32
+ [
33
+ [2, 0],
34
+ [0, 2],
35
+ ],
36
+ [
37
+ [2, 0],
38
+ [1, 1],
39
+ [0, 2],
40
+ ],
41
+ [
42
+ [0, 0],
43
+ [2, 0],
44
+ [0, 2],
45
+ [2, 2],
46
+ ],
47
+ [
48
+ [0, 0],
49
+ [2, 0],
50
+ [1, 1],
51
+ [0, 2],
52
+ [2, 2],
53
+ ],
54
+ [
55
+ [0, 0],
56
+ [0, 1],
57
+ [0, 2],
58
+ [2, 0],
59
+ [2, 1],
60
+ [2, 2],
61
+ ],
62
+ [
63
+ [0, 0],
64
+ [0, 1],
65
+ [0, 2],
66
+ [1, 1],
67
+ [2, 0],
68
+ [2, 1],
69
+ [2, 2],
70
+ ],
71
+ [
72
+ [0, 0],
73
+ [1, 0],
74
+ [2, 0],
75
+ [0, 1],
76
+ [2, 1],
77
+ [0, 2],
78
+ [1, 2],
79
+ [2, 2],
80
+ ],
81
+ [
82
+ [0, 0],
83
+ [1, 0],
84
+ [2, 0],
85
+ [0, 1],
86
+ [1, 1],
87
+ [2, 1],
88
+ [0, 2],
89
+ [1, 2],
90
+ [2, 2],
91
+ ],
92
+ ];
93
+ function checkHalf(value, path, problems) {
94
+ if (value === "?" || value === "cloud")
95
+ return;
96
+ if (typeof value !== "number") {
97
+ problems.push(`${path}: must be a number of pips from 0 to 9, "?" or "cloud"`);
98
+ return;
99
+ }
100
+ int(value, path, problems, 0, 9);
101
+ }
102
+ function checkPair(value, path, problems) {
103
+ if (!Array.isArray(value) || value.length !== 2) {
104
+ problems.push(`${path}: must be the two halves of a tile, such as [3, 5]`);
105
+ return;
106
+ }
107
+ value.forEach((h, i) => {
108
+ checkHalf(h, `${path}[${i}]`, problems);
109
+ });
110
+ }
111
+ /** ringSize is the rectangle a ring of k tiles goes round: columns and rows of halves. */
112
+ function ringSize(k, columns) {
113
+ const c = columns ?? Math.ceil((k + 2) / 2);
114
+ return { columns: c, rows: k + 2 - c };
115
+ }
116
+ /** perimeter lists the places round the edge of a rectangle, clockwise from the top left. */
117
+ function perimeter(columns, rows) {
118
+ const out = [];
119
+ for (let c = 0; c < columns; c++)
120
+ out.push([0, c]);
121
+ for (let r = 1; r < rows; r++)
122
+ out.push([r, columns - 1]);
123
+ for (let c = columns - 2; c >= 0; c--)
124
+ out.push([rows - 1, c]);
125
+ for (let r = rows - 2; r >= 1; r--)
126
+ out.push([r, 0]);
127
+ return out;
128
+ }
129
+ /** layOut places every tile of a scene on a board of halves; a row keeps its tiles apart. */
130
+ function layOut(s) {
131
+ if (s.layout === "grid")
132
+ return s.tiles.map((t) => {
133
+ const [r, c] = [t.at[0] - 1, t.at[1] - 1];
134
+ const upright = t.upright === true;
135
+ return { halves: t.halves, places: [[r, c], upright ? [r + 1, c] : [r, c + 1]], upright };
136
+ });
137
+ const tiles = s.tiles;
138
+ if (s.layout === "ring") {
139
+ const { columns, rows } = ringSize(tiles.length, s.columns);
140
+ const edge = perimeter(columns, rows);
141
+ return tiles.map((halves, i) => {
142
+ const [a, b] = [edge[2 * i], edge[2 * i + 1]];
143
+ return { halves, places: [a, b], upright: a[1] === b[1] };
144
+ });
145
+ }
146
+ const upright = s.layout === "row" && s.upright === true;
147
+ return tiles.map((halves, i) => ({
148
+ halves,
149
+ places: upright
150
+ ? [
151
+ [0, i],
152
+ [1, i],
153
+ ]
154
+ : [
155
+ [0, 2 * i],
156
+ [0, 2 * i + 1],
157
+ ],
158
+ upright,
159
+ }));
160
+ }
161
+ /** extent is how many columns and rows of halves the laid tiles take. */
162
+ function extent(laid) {
163
+ const all = laid.flatMap((t) => t.places);
164
+ return {
165
+ columns: Math.max(...all.map((p) => p[1])) + 1,
166
+ rows: Math.max(...all.map((p) => p[0])) + 1,
167
+ };
168
+ }
169
+ /** halfSize is the side of a half that lets the picture fit. */
170
+ function halfSize(s, laid) {
171
+ if (s.layout === "row") {
172
+ const across = laid.reduce((sum, t) => sum + (t.upright ? 1 : 2), 0);
173
+ return Math.min(biggest, (room - gap * (laid.length - 1)) / across);
174
+ }
175
+ return Math.min(biggest, room / extent(laid).columns);
176
+ }
177
+ function drawPips(pen, value, x, y, h, upright, color) {
178
+ for (const [c, r] of pips[value]) {
179
+ const [u, v] = upright ? [c, r] : [2 - r, c];
180
+ pen.el("circle", {
181
+ class: "pip",
182
+ cx: x + h * (0.26 + 0.24 * u),
183
+ cy: y + h * (0.26 + 0.24 * v),
184
+ r: h * 0.085,
185
+ fill: color,
186
+ });
187
+ }
188
+ }
189
+ // A cloud: puffs over a half, a unit square centred at 0, 0, each a circle
190
+ // x, y, r. They cover every place a pip could take and reach no more than a
191
+ // few pixels past the half, well short of the pips of the half beside it.
192
+ const puffs = [
193
+ [-0.26, 0.16, 0.3],
194
+ [0.26, 0.16, 0.3],
195
+ [0, 0.2, 0.3],
196
+ [-0.16, -0.14, 0.32],
197
+ [0.2, -0.12, 0.28],
198
+ ];
199
+ /**
200
+ * drawClouds draws a cloud over each half h pixels square centred at a
201
+ * point: every puff outlined first, then every puff filled over the
202
+ * outlines, so that clouds side by side make one cloud with one outline.
203
+ */
204
+ function drawClouds(pen, centres, h) {
205
+ for (const pass of [0, 1])
206
+ for (const c of centres)
207
+ puffs.forEach(([x, y, r], i) => {
208
+ pen.el("circle", {
209
+ class: pass === 0 && i === 0 ? "cloud" : undefined,
210
+ cx: c.x + x * h,
211
+ cy: c.y + y * h,
212
+ r: r * h,
213
+ fill: soft,
214
+ stroke: pass === 0 ? ink : undefined,
215
+ "stroke-width": pass === 0 ? outline * 2 : undefined,
216
+ });
217
+ });
218
+ }
219
+ /** drawTile draws a tile with its top left at x, y, its half h pixels square. */
220
+ function drawTile(pen, halves, x, y, h, upright, face, inset) {
221
+ const w = upright ? h : 2 * h;
222
+ const t = upright ? 2 * h : h;
223
+ pen.el("rect", {
224
+ class: "tile",
225
+ x: x + inset,
226
+ y: y + inset,
227
+ width: w - 2 * inset,
228
+ height: t - 2 * inset,
229
+ rx: h * 0.16,
230
+ fill: colors[face].fill,
231
+ stroke: ink,
232
+ "stroke-width": outline,
233
+ });
234
+ const line = upright
235
+ ? `M${n(x + h * 0.2)} ${n(y + h)}H${n(x + h * 0.8)}`
236
+ : `M${n(x + h)} ${n(y + h * 0.2)}V${n(y + h * 0.8)}`;
237
+ pen.el("path", { d: line, stroke: ink, "stroke-width": 2, "stroke-linecap": "round" });
238
+ const ink_ = face === "white" ? ink : textOn(face);
239
+ halves.forEach((half, i) => {
240
+ const hx = upright ? x : x + i * h;
241
+ const hy = upright ? y + i * h : y;
242
+ if (half === "?")
243
+ pen.text(hx + h / 2, hy + h / 2, "?", { size: h * 0.58, fill: ink_ });
244
+ else if (typeof half === "number")
245
+ drawPips(pen, half, hx, hy, h, upright, ink_);
246
+ });
247
+ }
248
+ const sayPips = (v) => v === "?" ? "a question mark" : v === "cloud" ? "a cloud" : v === 0 ? "blank" : plural(v, "pip");
249
+ /** sayTile says a tile's halves: "3 pips on the top half and the bottom half hidden under a cloud". */
250
+ function sayTile(halves, upright) {
251
+ const sides = upright ? ["top", "bottom"] : ["left", "right"];
252
+ return halves
253
+ .map((v, i) => v === "cloud"
254
+ ? `the ${sides[i]} half hidden under a cloud`
255
+ : v === 0
256
+ ? `the ${sides[i]} half blank`
257
+ : `${sayPips(v)} on the ${sides[i]} half`)
258
+ .join(" and ");
259
+ }
260
+ /** noteOf says how to read the values of a ring or a grid that holds a cloud or a 0, if it does. */
261
+ function noteOf(tiles) {
262
+ const halves = tiles.flat();
263
+ const notes = [
264
+ ...(halves.includes("cloud") ? ['a "cloud" half is hidden under a cloud'] : []),
265
+ ...(halves.includes(0) ? ["a 0 half is blank"] : []),
266
+ ];
267
+ if (notes.length === 0)
268
+ return "";
269
+ const text = notes.join("; ");
270
+ return ` ${text[0].toUpperCase()}${text.slice(1)}.`;
271
+ }
272
+ const sayValue = (v) => (v === "?" ? "?" : v === "cloud" ? "cloud" : String(v));
273
+ export const domino = {
274
+ kind: "domino",
275
+ members: ["kind", "layout", "tiles", "upright", "columns", "color"],
276
+ check(scene, problems) {
277
+ only(scene, this.members, "scene", problems);
278
+ const before = problems.length;
279
+ const layout = oneOf(scene.layout, dominoLayouts, "layout", problems);
280
+ if (problems.length > before)
281
+ return;
282
+ if (scene.upright !== undefined) {
283
+ if (typeof scene.upright !== "boolean")
284
+ problems.push("upright: must be true or false");
285
+ else if (layout !== "row")
286
+ problems.push("upright: is for a row; a chain lies flat, a ring turns its tiles as it goes, and a tile of a grid says its own");
287
+ }
288
+ if (scene.color !== undefined && !isColor(scene.color))
289
+ problems.push(`color: must be a colour: ${colorNames.join(", ")}`);
290
+ const most = layout === "row"
291
+ ? scene.upright === true
292
+ ? 8
293
+ : 5
294
+ : layout === "chain"
295
+ ? 6
296
+ : layout === "ring"
297
+ ? 12
298
+ : 20;
299
+ const least = layout === "ring" ? 4 : 1;
300
+ const tiles = list(scene.tiles, "tiles", problems, least, most);
301
+ if (layout !== "grid") {
302
+ tiles.forEach((t, i) => {
303
+ checkPair(t, `tiles[${i}]`, problems);
304
+ });
305
+ if (scene.columns !== undefined) {
306
+ if (layout !== "ring")
307
+ problems.push("columns: is for a ring");
308
+ else if (tiles.length >= least)
309
+ int(scene.columns, "columns", problems, 3, tiles.length - 1);
310
+ }
311
+ return;
312
+ }
313
+ if (scene.columns !== undefined)
314
+ problems.push("columns: is for a ring");
315
+ const taken = new Map();
316
+ tiles.forEach((t, i) => {
317
+ const path = `tiles[${i}]`;
318
+ if (!isObj(t)) {
319
+ problems.push(`${path}: must be a tile with halves, at and upright`);
320
+ return;
321
+ }
322
+ only(t, ["halves", "at", "upright"], path, problems);
323
+ checkPair(t.halves, `${path}.halves`, problems);
324
+ if (t.upright !== undefined && typeof t.upright !== "boolean")
325
+ problems.push(`${path}.upright: must be true or false`);
326
+ if (!Array.isArray(t.at) || t.at.length !== 2) {
327
+ problems.push(`${path}.at: must be [row, column] of its top or left half, counted from 1`);
328
+ return;
329
+ }
330
+ const mark = problems.length;
331
+ const upright = t.upright === true;
332
+ const r = int(t.at[0], `${path}.at[0]`, problems, 1, upright ? board - 1 : board);
333
+ const c = int(t.at[1], `${path}.at[1]`, problems, 1, upright ? board : board - 1);
334
+ if (problems.length > mark)
335
+ return;
336
+ const cells = [[r, c], upright ? [r + 1, c] : [r, c + 1]];
337
+ for (const [cr, cc] of cells) {
338
+ const key = `${cr},${cc}`;
339
+ const other = taken.get(key);
340
+ if (other !== undefined)
341
+ problems.push(`${path}: covers row ${cr}, column ${cc}, as tiles[${other}] does`);
342
+ else
343
+ taken.set(key, i);
344
+ }
345
+ });
346
+ },
347
+ draw(scene, pen) {
348
+ const s = scene;
349
+ const laid = layOut(s);
350
+ const face = s.color ?? "white";
351
+ const h = halfSize(s, laid);
352
+ const clouds = [];
353
+ let width;
354
+ let height;
355
+ if (s.layout === "row") {
356
+ let x = margin;
357
+ const tall = laid.some((t) => t.upright) ? 2 * h : h;
358
+ for (const t of laid) {
359
+ const y = margin + (tall - (t.upright ? 2 * h : h)) / 2;
360
+ drawTile(pen, t.halves, x, y, h, t.upright, face, 0);
361
+ t.halves.forEach((v, i) => {
362
+ if (v === "cloud")
363
+ clouds.push({
364
+ x: x + (t.upright ? 0 : i * h) + h / 2,
365
+ y: y + (t.upright ? i * h : 0) + h / 2,
366
+ });
367
+ });
368
+ x += (t.upright ? h : 2 * h) + gap;
369
+ }
370
+ width = x - gap + margin;
371
+ height = tall + 2 * margin;
372
+ }
373
+ else {
374
+ const { columns, rows } = extent(laid);
375
+ for (const t of laid) {
376
+ const [first, second] = t.places;
377
+ // a tile is drawn from its top-left place; in a ring it may run left or up
378
+ const top = Math.min(first[0], second[0]);
379
+ const left = Math.min(first[1], second[1]);
380
+ const flipped = first[0] > second[0] || first[1] > second[1];
381
+ const halves = flipped ? [t.halves[1], t.halves[0]] : t.halves;
382
+ drawTile(pen, halves, margin + left * h, margin + top * h, h, t.upright, face, 2);
383
+ [first, second].forEach(([r, c], i) => {
384
+ if (t.halves[i] === "cloud")
385
+ clouds.push({ x: margin + (c + 0.5) * h, y: margin + (r + 0.5) * h });
386
+ });
387
+ }
388
+ width = columns * h + 2 * margin;
389
+ height = rows * h + 2 * margin;
390
+ }
391
+ drawClouds(pen, clouds, h);
392
+ return { width, height };
393
+ },
394
+ describe(scene) {
395
+ const s = scene;
396
+ const k = s.tiles.length;
397
+ const tiles = plural(k, "domino tile");
398
+ const color = s.color && s.color !== "white" ? ` The tiles are ${colors[s.color].word}.` : "";
399
+ const laid = layOut(s);
400
+ const each = (upright) => laid.map((t, i) => `Tile ${i + 1}: ${sayTile(t.halves, upright(i))}.`).join(" ");
401
+ if (s.layout === "row")
402
+ return `${tiles} in a row, apart, ${s.upright ? "standing upright" : "lying flat"}, from left to right.${color} ${each(() => s.upright === true)}`;
403
+ if (s.layout === "chain")
404
+ return `${tiles} lying flat end to end in a chain, from left to right.${color} ${each(() => false)}`;
405
+ if (s.layout === "ring") {
406
+ const { columns, rows } = ringSize(k, s.columns);
407
+ const edge = perimeter(columns, rows);
408
+ const value = new Map();
409
+ s.tiles.forEach((t, i) => {
410
+ value.set(`${edge[2 * i][0]},${edge[2 * i][1]}`, t[0]);
411
+ value.set(`${edge[2 * i + 1][0]},${edge[2 * i + 1][1]}`, t[1]);
412
+ });
413
+ const at = (r, c) => sayValue(value.get(`${r},${c}`));
414
+ const range = (a, b) => Array.from({ length: Math.abs(b - a) + 1 }, (_, i) => (a <= b ? a + i : a - i));
415
+ const sides = [
416
+ `the top side, from left to right: ${range(0, columns - 1)
417
+ .map((c) => at(0, c))
418
+ .join(", ")}`,
419
+ `the right side, from top to bottom: ${range(0, rows - 1)
420
+ .map((r) => at(r, columns - 1))
421
+ .join(", ")}`,
422
+ `the bottom side, from right to left: ${range(columns - 1, 0)
423
+ .map((c) => at(rows - 1, c))
424
+ .join(", ")}`,
425
+ `the left side, from bottom to top: ${range(rows - 1, 0)
426
+ .map((r) => at(r, 0))
427
+ .join(", ")}`,
428
+ ];
429
+ return `${tiles} laid end to end in a closed ring round the edge of a rectangle ${columns} halves across and ${rows} halves down, the middle empty; each tile's second half touches the next tile's first.${color} Clockwise from the top-left corner, the tiles are: ${s.tiles
430
+ .map((t) => `${sayValue(t[0])} and ${sayValue(t[1])}`)
431
+ .join("; ")}. The halves along each side, the corners shared: ${sides.join("; ")}.${noteOf(s.tiles)}`;
432
+ }
433
+ const { columns, rows } = extent(laid);
434
+ const value = new Map();
435
+ for (const t of laid)
436
+ t.places.forEach(([r, c], i) => {
437
+ value.set(`${r},${c}`, t.halves[i]);
438
+ });
439
+ const rowWords = Array.from({ length: rows }, (_, r) => `row ${r + 1}: ${Array.from({ length: columns }, (_, c) => {
440
+ const v = value.get(`${r},${c}`);
441
+ return v === undefined ? "empty" : sayValue(v);
442
+ }).join(", ")}`);
443
+ const where = ({ places: [[r, c]], upright }) => upright
444
+ ? `upright in column ${c + 1}, rows ${r + 1} and ${r + 2}`
445
+ : `flat in row ${r + 1}, columns ${c + 1} and ${c + 2}`;
446
+ return `${tiles} on a board ${columns} halves across and ${rows} halves down.${color} ${laid
447
+ .map((t, i) => `Tile ${i + 1}, ${where(t)}: ${sayTile(t.halves, t.upright)}.`)
448
+ .join(" ")} Row by row from the top, the halves read: ${rowWords.join("; ")}.${noteOf(laid.map((t) => t.halves))}`;
449
+ },
450
+ };
@@ -0,0 +1,38 @@
1
+ import type { Kind } from "../kind.ts";
2
+ import { type ColorName } from "../style.ts";
3
+ /**
4
+ * Dotgrid: dot paper, squared paper or a geoboard — points in a square or a
5
+ * triangular lattice — with shapes whose corners are points of it: shaded
6
+ * shapes with an ink outline, or rubber bands, outlines in a colour. A point
7
+ * is [x, y]: the x-th point of row y, both counted from 0 at the top left;
8
+ * in a triangular lattice each odd row is shifted half a step to the right,
9
+ * so that every point has six nearest points. A shape's label stands inside
10
+ * it. The picture never says an area: the scene may carry the areas as facts,
11
+ * in unit squares or in small triangles, and the check works them out itself.
12
+ */
13
+ export declare const lattices: readonly ["square", "triangle"];
14
+ export type Lattice = (typeof lattices)[number];
15
+ export declare const looks: readonly ["dots", "lines", "board"];
16
+ export type Look = (typeof looks)[number];
17
+ export declare const styles: readonly ["fill", "band"];
18
+ export type Style = (typeof styles)[number];
19
+ export type DotShape = {
20
+ corners: [number, number][];
21
+ color: ColorName;
22
+ style?: Style;
23
+ label?: string;
24
+ };
25
+ export type DotgridScene = {
26
+ kind: "dotgrid";
27
+ lattice: Lattice;
28
+ columns: number;
29
+ rows: number;
30
+ look?: Look;
31
+ shapes?: DotShape[];
32
+ facts?: {
33
+ areas: number[];
34
+ };
35
+ };
36
+ /** areaOf is the area of a shape: unit squares on a square lattice, small triangles on a triangular one. */
37
+ export declare function areaOf(d: DotgridScene, s: DotShape): number;
38
+ export declare const dotgrid: Kind;