@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,411 @@
1
+ import { margin } from "../kind.js";
2
+ import { textWidth } from "../pen.js";
3
+ import { int, isObj, list, oneOf, only, text } from "../read.js";
4
+ import { colorNames, colors, faint, ink, isColor, outline, soft, textOn, } from "../style.js";
5
+ import { poly } from "../svg.js";
6
+ import { area, boxAt, boxToSegment, capital, centroid, crossing, decimal, distance, edgesOf, inside, listOf, meet, ordinal, overlap, pointOf, say, toBox, toLine, toSegment, } from "./plane-parts.js";
7
+ /**
8
+ * Dotgrid: dot paper, squared paper or a geoboard — points in a square or a
9
+ * triangular lattice — with shapes whose corners are points of it: shaded
10
+ * shapes with an ink outline, or rubber bands, outlines in a colour. A point
11
+ * is [x, y]: the x-th point of row y, both counted from 0 at the top left;
12
+ * in a triangular lattice each odd row is shifted half a step to the right,
13
+ * so that every point has six nearest points. A shape's label stands inside
14
+ * it. The picture never says an area: the scene may carry the areas as facts,
15
+ * in unit squares or in small triangles, and the check works them out itself.
16
+ */
17
+ export const lattices = ["square", "triangle"];
18
+ export const looks = ["dots", "lines", "board"];
19
+ export const styles = ["fill", "band"];
20
+ const tall = Math.sqrt(3) / 2;
21
+ const most = {
22
+ square: { columns: 13, rows: 11 },
23
+ triangle: { columns: 12, rows: 10 },
24
+ };
25
+ const bandW = 5;
26
+ /** stepOf is the distance between two neighbouring points, in pixels. */
27
+ function stepOf(d) {
28
+ const across = d.lattice === "square" ? d.columns - 1 : d.columns - 0.5;
29
+ const down = (d.rows - 1) * (d.lattice === "square" ? 1 : tall);
30
+ return Math.min(52, 560 / across, 380 / down);
31
+ }
32
+ /** spot is where the point [x, y] of the lattice lies, in steps. */
33
+ function spot(d, x, y) {
34
+ if (d.lattice === "square")
35
+ return { x, y };
36
+ return { x: x + (y % 2 === 1 ? 0.5 : 0), y: y * tall };
37
+ }
38
+ const cornersOf = (d, s) => s.corners.map(([x, y]) => spot(d, x, y));
39
+ /** areaOf is the area of a shape: unit squares on a square lattice, small triangles on a triangular one. */
40
+ export function areaOf(d, s) {
41
+ const a = area(cornersOf(d, s));
42
+ const units = d.lattice === "square" ? a : a / (Math.sqrt(3) / 4);
43
+ return Math.round(units * 1e6) / 1e6;
44
+ }
45
+ /** lattice lines: the lines of squared or triangular paper, each from end to end, in steps. */
46
+ function paperLines(d) {
47
+ const out = [];
48
+ const { columns: c, rows: r } = d;
49
+ if (d.lattice === "square") {
50
+ for (let y = 0; y < r; y++)
51
+ out.push([spot(d, 0, y), spot(d, c - 1, y)]);
52
+ for (let x = 0; x < c; x++)
53
+ out.push([spot(d, x, 0), spot(d, x, r - 1)]);
54
+ return out;
55
+ }
56
+ for (let y = 0; y < r; y++)
57
+ out.push([spot(d, 0, y), spot(d, c - 1, y)]);
58
+ // The neighbours below a point: to the left and to the right.
59
+ const below = (x, y, right) => y % 2 === 0 ? [right ? x : x - 1, y + 1] : [right ? x + 1 : x, y + 1];
60
+ const has = ([x, y]) => x >= 0 && x < c && y >= 0 && y < r;
61
+ for (const right of [false, true]) {
62
+ for (let y = 0; y < r; y++)
63
+ for (let x = 0; x < c; x++) {
64
+ // Start only where no point lies above, along the same line.
65
+ const above = y % 2 === 0 ? [right ? x - 1 : x, y - 1] : [right ? x : x + 1, y - 1];
66
+ if (has(above))
67
+ continue;
68
+ let end = [x, y];
69
+ for (let next = below(x, y, right); has(next); next = below(next[0], next[1], right))
70
+ end = next;
71
+ if (end[1] !== y)
72
+ out.push([spot(d, x, y), spot(d, end[0], end[1])]);
73
+ }
74
+ }
75
+ return out;
76
+ }
77
+ /** labelSpots are the places a label may stand: the middle of each cell or each small triangle. */
78
+ function labelSpots(d) {
79
+ const out = [];
80
+ const { columns: c, rows: r } = d;
81
+ if (d.lattice === "square") {
82
+ for (let y = 0; y + 1 < r; y++)
83
+ for (let x = 0; x + 1 < c; x++)
84
+ out.push({ x: x + 0.5, y: y + 0.5 });
85
+ return out;
86
+ }
87
+ for (let y = 0; y + 1 < r; y++) {
88
+ const shift = y % 2 === 1 ? 0.5 : 0;
89
+ const next = shift === 0 ? 0.5 : 0;
90
+ for (let x = 0; x + 1 < c; x++) {
91
+ // A triangle pointing down: two points of row y, one of row y + 1.
92
+ const mid = x + 0.5 + shift;
93
+ const low = mid - next;
94
+ if (low >= 0 && low <= c - 1)
95
+ out.push({ x: mid, y: (y + 1 / 3) * tall });
96
+ }
97
+ for (let x = 0; x + 1 < c; x++) {
98
+ // A triangle pointing up: one point of row y, two of row y + 1.
99
+ const mid = x + 0.5 + next;
100
+ const high = mid - shift;
101
+ if (high >= 0 && high <= c - 1)
102
+ out.push({ x: mid, y: (y + 2 / 3) * tall });
103
+ }
104
+ }
105
+ return out;
106
+ }
107
+ function sizes(step) {
108
+ return {
109
+ dot: step >= 40 ? 3 : 2.6,
110
+ peg: Math.min(6, Math.max(4, step * 0.12)),
111
+ font: step >= 40 ? 18 : step >= 32 ? 16 : 14,
112
+ };
113
+ }
114
+ /**
115
+ * labelPlaces finds where each shape's label stands, in steps: the middle
116
+ * of a cell or a small triangle inside the shape and inside no other, as
117
+ * far from every line, dot and outline as can be; undefined where none has
118
+ * room.
119
+ */
120
+ function labelPlaces(d) {
121
+ const shapes = d.shapes ?? [];
122
+ const step = stepOf(d);
123
+ const { dot: dotR, peg, font } = sizes(step);
124
+ const look = d.look ?? "dots";
125
+ const px = (p) => ({ x: p.x * step, y: p.y * step });
126
+ const polys = shapes.map((s) => cornersOf(d, s).map(px));
127
+ const lines = look === "lines" ? paperLines(d).map(([a, b]) => [px(a), px(b)]) : [];
128
+ const dots = [];
129
+ if (look !== "lines")
130
+ for (let y = 0; y < d.rows; y++)
131
+ for (let x = 0; x < d.columns; x++)
132
+ dots.push(px(spot(d, x, y)));
133
+ const r = look === "board" ? peg + 1 : dotR + 1;
134
+ const candidates = labelSpots(d).map(px);
135
+ return shapes.map((s, i) => {
136
+ if (!s.label)
137
+ return undefined;
138
+ const w = textWidth(s.label, font) + 2;
139
+ const h = font * 0.78;
140
+ const middle = centroid(polys[i]);
141
+ let best;
142
+ for (const c of candidates) {
143
+ if (!inside(c, polys[i]) || polys.some((q, j) => j !== i && inside(c, q)))
144
+ continue;
145
+ const box = boxAt(c, w, h);
146
+ const clear = Math.min(...polys.flatMap((q, j) => {
147
+ const half = (shapes[j].style ?? "fill") === "band" ? bandW / 2 + 1.5 : outline / 2;
148
+ return edgesOf(q).map(([a, b]) => boxToSegment(box, a, b) - half);
149
+ }), ...lines.map(([a, b]) => boxToSegment(box, a, b) - 1), ...dots.map((q) => toBox(q, box) - r));
150
+ const far = Math.round(distance(c, middle) * 100) / 100;
151
+ const rounded = Math.round(clear * 100) / 100;
152
+ if (!best || rounded > best.clear || (rounded === best.clear && far < best.far))
153
+ best = { at: c, clear: rounded, far };
154
+ }
155
+ return best && best.clear >= 1.5 ? { x: best.at.x / step, y: best.at.y / step } : undefined;
156
+ });
157
+ }
158
+ const sayPoint = ([x, y]) => `(${x}, ${y})`;
159
+ export const dotgrid = {
160
+ kind: "dotgrid",
161
+ members: ["kind", "lattice", "columns", "rows", "look", "shapes", "facts"],
162
+ check(scene, problems) {
163
+ only(scene, this.members, "scene", problems);
164
+ const before = problems.length;
165
+ const lattice = oneOf(scene.lattice, lattices, "lattice", problems);
166
+ const columns = int(scene.columns, "columns", problems, 2, most[lattice].columns);
167
+ const rows = int(scene.rows, "rows", problems, 2, most[lattice].rows);
168
+ if (scene.look !== undefined)
169
+ oneOf(scene.look, looks, "look", problems);
170
+ const shapes = scene.shapes === undefined ? [] : list(scene.shapes, "shapes", problems, 0, 8);
171
+ shapes.forEach((s, i) => {
172
+ const path = `shapes[${i}]`;
173
+ if (!isObj(s)) {
174
+ problems.push(`${path}: must be a shape, as {"corners": [[0, 0], [2, 0], [0, 2]], "color": "red"}`);
175
+ return;
176
+ }
177
+ only(s, ["corners", "color", "style", "label"], path, problems);
178
+ list(s.corners, `${path}.corners`, problems, 3, 12).forEach((c, j) => {
179
+ pointOf(c, `${path}.corners[${j}]`, problems, [columns - 1, rows - 1], 0);
180
+ });
181
+ if (!isColor(s.color))
182
+ problems.push(`${path}.color: must be a colour: ${colorNames.join(", ")}`);
183
+ if (s.style !== undefined)
184
+ oneOf(s.style, styles, `${path}.style`, problems);
185
+ if (s.label !== undefined)
186
+ text(s.label, `${path}.label`, problems, 2);
187
+ });
188
+ const facts = scene.facts;
189
+ if (facts !== undefined) {
190
+ if (!isObj(facts))
191
+ problems.push('facts: must hold the areas, as {"areas": [6, 4.5]}');
192
+ else {
193
+ only(facts, ["areas"], "facts", problems);
194
+ const areas = list(facts.areas, "facts.areas", problems, shapes.length, shapes.length);
195
+ if (Array.isArray(facts.areas) && areas.length !== shapes.length)
196
+ problems.push(`facts.areas: must hold one area for each shape, ${shapes.length}`);
197
+ areas.forEach((a, i) => {
198
+ decimal(a, `facts.areas[${i}]`, problems, 0, 1000, 1);
199
+ });
200
+ }
201
+ }
202
+ if (problems.length > before)
203
+ return;
204
+ const d = scene;
205
+ const list_ = d.shapes ?? [];
206
+ const polys = list_.map((s) => cornersOf(d, s));
207
+ list_.forEach((s, i) => {
208
+ const path = `shapes[${i}].corners`;
209
+ const seen = new Map();
210
+ s.corners.forEach((c, j) => {
211
+ const key = c.join(",");
212
+ if (seen.has(key))
213
+ problems.push(`${path}[${j}]: the point ${sayPoint(c)} is corners[${seen.get(key)}] already`);
214
+ else
215
+ seen.set(key, j);
216
+ });
217
+ if (seen.size !== s.corners.length)
218
+ return;
219
+ const corners = polys[i];
220
+ const k = corners.length;
221
+ corners.forEach((c, j) => {
222
+ const prev = corners[(j + k - 1) % k];
223
+ const next = corners[(j + 1) % k];
224
+ if (toLine(c, prev, next) < 1e-9)
225
+ problems.push(`${path}[${j}]: lies on the straight side from ${sayPoint(s.corners[(j + k - 1) % k])} to ${sayPoint(s.corners[(j + 1) % k])}; leave it out`);
226
+ });
227
+ const cross = crossing(corners, 1e-9);
228
+ if (cross)
229
+ problems.push(`${path}: the side from ${sayPoint(s.corners[cross[0]])} and the side from ${sayPoint(s.corners[cross[1]])} cross or touch; list the corners in order round the shape`);
230
+ });
231
+ if (problems.length > before)
232
+ return;
233
+ list_.forEach((s, i) => {
234
+ for (let j = 0; j < i; j++) {
235
+ const other = list_[j];
236
+ const fills = (s.style ?? "fill") === "fill" && (other.style ?? "fill") === "fill";
237
+ if (fills && overlap(polys[i], polys[j])) {
238
+ problems.push(`shapes[${i}]: overlaps shapes[${j}]; a shaded shape would hide part of another`);
239
+ continue;
240
+ }
241
+ if (fills)
242
+ continue;
243
+ for (const [a, b] of edgesOf(polys[i]))
244
+ for (const [c, e] of edgesOf(polys[j])) {
245
+ const m = meet(a, b, c, e, 1e-9);
246
+ if (m.how === "along") {
247
+ problems.push(`shapes[${i}]: runs along an outline of shapes[${j}]; one would hide the other where they meet`);
248
+ return;
249
+ }
250
+ }
251
+ }
252
+ });
253
+ if (problems.length > before)
254
+ return;
255
+ labelPlaces(d).forEach((at, i) => {
256
+ const s = list_[i];
257
+ if (s.label && !at)
258
+ problems.push(`shapes[${i}].label: no room for "${s.label}" inside the shape, clear of its lines and dots; leave the label out or make the shape larger`);
259
+ });
260
+ if (problems.length > before || !d.facts)
261
+ return;
262
+ d.facts.areas.forEach((given, i) => {
263
+ const real = areaOf(d, list_[i]);
264
+ if (Math.abs(real - given) > 1e-6)
265
+ problems.push(`facts.areas[${i}]: the shape covers ${say(real)} ${d.lattice === "square" ? "unit squares" : "small triangles"}, not ${say(given)}`);
266
+ });
267
+ },
268
+ draw(scene, pen) {
269
+ const d = scene;
270
+ const step = stepOf(d);
271
+ const look = d.look ?? "dots";
272
+ const { dot: dotR, peg, font } = sizes(step);
273
+ const shapes = d.shapes ?? [];
274
+ const right = Math.max(...Array.from({ length: d.rows }, (_, y) => spot(d, d.columns - 1, y).x));
275
+ const bottom = spot(d, 0, d.rows - 1).y;
276
+ const pad = look === "board" ? Math.max(18, step * 0.45) : bandW;
277
+ const x0 = margin + pad;
278
+ const y0 = margin + pad;
279
+ const px = (p) => ({ x: x0 + p.x * step, y: y0 + p.y * step });
280
+ if (look === "board")
281
+ pen.el("rect", {
282
+ x: margin,
283
+ y: margin,
284
+ width: right * step + 2 * pad,
285
+ height: bottom * step + 2 * pad,
286
+ rx: 14,
287
+ fill: soft,
288
+ stroke: ink,
289
+ "stroke-width": outline,
290
+ });
291
+ const polys = shapes.map((s) => cornersOf(d, s).map(px));
292
+ const fill = (s) => (s.style ?? "fill") === "fill";
293
+ shapes.forEach((s, i) => {
294
+ if (fill(s))
295
+ pen.el("path", { class: "shape", d: poly(polys[i]), fill: colors[s.color].fill });
296
+ });
297
+ if (look === "lines")
298
+ for (const [a, b] of paperLines(d))
299
+ pen.el("path", {
300
+ class: "line",
301
+ d: poly([px(a), px(b)], false),
302
+ fill: "none",
303
+ stroke: faint,
304
+ "stroke-width": 1.5,
305
+ });
306
+ shapes.forEach((s, i) => {
307
+ if (fill(s))
308
+ pen.el("path", {
309
+ d: poly(polys[i]),
310
+ fill: "none",
311
+ stroke: ink,
312
+ "stroke-width": outline,
313
+ "stroke-linejoin": "round",
314
+ });
315
+ });
316
+ shapes.forEach((s, i) => {
317
+ if (fill(s))
318
+ return;
319
+ const path = poly(polys[i]);
320
+ pen.el("path", {
321
+ class: "band",
322
+ d: path,
323
+ fill: "none",
324
+ stroke: ink,
325
+ "stroke-width": bandW + 3,
326
+ "stroke-linejoin": "round",
327
+ });
328
+ pen.el("path", {
329
+ d: path,
330
+ fill: "none",
331
+ stroke: colors[s.color].fill,
332
+ "stroke-width": bandW,
333
+ "stroke-linejoin": "round",
334
+ });
335
+ });
336
+ if (look !== "lines")
337
+ for (let y = 0; y < d.rows; y++)
338
+ for (let x = 0; x < d.columns; x++) {
339
+ const p = px(spot(d, x, y));
340
+ if (look === "board") {
341
+ pen.el("circle", {
342
+ class: "peg",
343
+ cx: p.x,
344
+ cy: p.y,
345
+ r: peg,
346
+ fill: "#ffffff",
347
+ stroke: ink,
348
+ "stroke-width": 2,
349
+ });
350
+ continue;
351
+ }
352
+ // A dot inside a shaded shape takes the colour that shows on it.
353
+ const under = shapes.findIndex((s, i) => fill(s) && inside(p, polys[i]) && !onEdge(p, polys[i]));
354
+ const color = under >= 0 ? textOn(shapes[under].color) : ink;
355
+ pen.el("circle", { class: "dot", cx: p.x, cy: p.y, r: dotR, fill: color });
356
+ }
357
+ labelPlaces(d).forEach((at, i) => {
358
+ if (!at)
359
+ return;
360
+ const s = shapes[i];
361
+ const p = px(at);
362
+ pen.text(p.x, p.y, s.label, { size: font, fill: fill(s) ? textOn(s.color) : ink });
363
+ });
364
+ return {
365
+ width: right * step + 2 * pad + 2 * margin,
366
+ height: bottom * step + 2 * pad + 2 * margin,
367
+ };
368
+ },
369
+ describe(scene) {
370
+ const d = scene;
371
+ const look = d.look ?? "dots";
372
+ const shapes = d.shapes ?? [];
373
+ const out = [];
374
+ const point = look === "board" ? "peg" : look === "lines" ? "crossing" : "dot";
375
+ if (d.lattice === "square") {
376
+ if (look === "lines")
377
+ out.push(`Squared paper of ${d.columns - 1} by ${d.rows - 1} squares, each 1 unit a side; a crossing of its lines is (x, y), x counted from 0 at the left and y from 0 at the top.`);
378
+ else
379
+ out.push(`${look === "board" ? "A geoboard: pegs" : "Dot paper: dots"} in a square lattice, ${d.columns} across and ${d.rows} down, each 1 unit from the next; a ${point} is (x, y), x counted from 0 at the left and y from 0 at the top.`);
380
+ }
381
+ else {
382
+ const what = look === "lines"
383
+ ? "Triangular paper: lines in three directions cut it into small equilateral triangles of side 1, whose corners lie"
384
+ : look === "board"
385
+ ? "A geoboard: pegs"
386
+ : "Triangular dot paper: dots";
387
+ out.push(`${what} in ${d.rows} rows of ${d.columns}; each ${point} is 1 unit from the ${point}s next to it in its row and in the rows above and below, and every odd row (counting the top row as row 0) is shifted half a unit to the right. A ${point} is (x, y): the x-th of row y, both counted from 0, x from the left and y from the top.`);
388
+ }
389
+ if (shapes.length === 0)
390
+ out.push(`Nothing is drawn on the ${point}s.`);
391
+ shapes.forEach((s, i) => {
392
+ const name = s.label
393
+ ? `Shape ${s.label}`
394
+ : shapes.length === 1
395
+ ? "A shape"
396
+ : `The ${ordinal(i + 1)} shape`;
397
+ const band = (s.style ?? "fill") === "band";
398
+ const how = band
399
+ ? look === "board"
400
+ ? `a ${colors[s.color].word} rubber band stretched round`
401
+ : `a ${colors[s.color].word} outline, not shaded, with corners at`
402
+ : `shaded ${colors[s.color].word}, with corners at`;
403
+ out.push(`${name}${s.label ? " (the letter stands inside it)" : ""}: ${how} ${s.corners.length} ${point}s, in order round it: ${listOf(s.corners.map(sayPoint))}.`);
404
+ });
405
+ return out.map(capital).join(" ");
406
+ },
407
+ };
408
+ /** onEdge tells whether p lies on a side of a polygon. */
409
+ function onEdge(p, polygon) {
410
+ return edgesOf(polygon).some(([a, b]) => toSegment(p, a, b) < 0.01);
411
+ }
@@ -0,0 +1,62 @@
1
+ import type { Kind } from "../kind.ts";
2
+ import type { Pen } from "../pen.ts";
3
+ import { type ColorName } from "../style.ts";
4
+ import { type P } from "../svg.ts";
5
+ import { type Thing } from "../things.ts";
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 declare const cardShapes: readonly ["square", "circle"];
15
+ export type CardShape = (typeof cardShapes)[number];
16
+ export declare const cuts: readonly ["none", "cross", "horizontal", "vertical", "diagonals", "diagonal", "anti-diagonal", "stripes", "bands", "grid"];
17
+ export type Cut = (typeof cuts)[number];
18
+ export declare const positions: readonly ["top-left", "top", "top-right", "left", "centre", "right", "bottom-left", "bottom", "bottom-right"];
19
+ export type Position = (typeof positions)[number];
20
+ export declare const turns: readonly [0, 90, 180, 270];
21
+ export declare const flips: readonly ["none", "horizontal", "vertical"];
22
+ export type Flip = (typeof flips)[number];
23
+ export declare const shows: readonly ["result", "both", "ask"];
24
+ type Look = {
25
+ at: Position;
26
+ turn?: number;
27
+ mirror?: boolean;
28
+ };
29
+ export type Mark = (Look & {
30
+ dot: ColorName;
31
+ }) | (Look & {
32
+ thing: Thing;
33
+ }) | (Look & {
34
+ arrow: ColorName;
35
+ }) | (Look & {
36
+ letter: string;
37
+ color?: ColorName;
38
+ });
39
+ export type Card = {
40
+ shape: CardShape;
41
+ cut?: Cut;
42
+ colors: ColorName[];
43
+ marks?: Mark[];
44
+ };
45
+ export type Change = {
46
+ turn?: number;
47
+ flip?: Flip;
48
+ swap?: [ColorName, ColorName][];
49
+ };
50
+ export type FigureScene = {
51
+ kind: "figure";
52
+ card: Card;
53
+ show?: (typeof shows)[number];
54
+ } & Change;
55
+ /** change works out what a card becomes: flipped first, then turned, then its colours switched. */
56
+ export declare function change(card: Card, how: Change): Card;
57
+ /** drawCard draws a card centred at c, h its half width. */
58
+ export declare function drawCard(pen: Pen, card: Card, c: P, h: number): void;
59
+ /** sayCard says a card in words. */
60
+ export declare function sayCard(card: Card): string;
61
+ export declare const figure: Kind;
62
+ export {};