@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,571 @@
1
+ import { margin } from "../kind.js";
2
+ import { textWidth } from "../pen.js";
3
+ import { isObj, list, only, text } from "../read.js";
4
+ import { colorNames, colors, ground, ink, isColor, outline } from "../style.js";
5
+ import { n } from "../svg.js";
6
+ import { checkThing, drawThing, wordOf } from "../things.js";
7
+ const members = ["kind", "ropes", "pegs"];
8
+ /** One step of the grid, in pixels. */
9
+ const unit = 44;
10
+ const across = 12;
11
+ const down = 8;
12
+ const thick = 12;
13
+ const core = thick - 2 * outline;
14
+ const halo = thick + 9;
15
+ /** How far along a strand, each way from a crossing, the strand on top is drawn again. */
16
+ const reach = 26;
17
+ const pegRadius = 10;
18
+ const thingSize = 40;
19
+ const defaults = ["red", "blue", "green", "orange", "purple", "yellow"];
20
+ const dist = (a, b) => Math.hypot(a.x - b.x, a.y - b.y);
21
+ const keyOf = (x, y) => `${x},${y}`;
22
+ const sayPoint = (x, y) => `(${x}, ${y})`;
23
+ function article(words) {
24
+ return /^[aeiou]/.test(words) ? `an ${words}` : `a ${words}`;
25
+ }
26
+ /** bezier is the smooth line through the points, a cubic piece between each two. */
27
+ function pieces(points, closed) {
28
+ const k = points.length;
29
+ const at = (i) => {
30
+ if (closed)
31
+ return points[((i % k) + k) % k];
32
+ return points[Math.max(0, Math.min(k - 1, i))];
33
+ };
34
+ const out = [];
35
+ const last = closed ? k : k - 1;
36
+ for (let i = 0; i < last; i++) {
37
+ const p0 = at(i - 1);
38
+ const p1 = at(i);
39
+ const p2 = at(i + 1);
40
+ const p3 = at(i + 2);
41
+ out.push([
42
+ p1,
43
+ { x: p1.x + (p2.x - p0.x) / 6, y: p1.y + (p2.y - p0.y) / 6 },
44
+ { x: p2.x - (p3.x - p1.x) / 6, y: p2.y - (p3.y - p1.y) / 6 },
45
+ p2,
46
+ ]);
47
+ }
48
+ return out;
49
+ }
50
+ function pointOn(c, t) {
51
+ const u = 1 - t;
52
+ const a = u * u * u;
53
+ const b = 3 * u * u * t;
54
+ const d = 3 * u * t * t;
55
+ const e = t * t * t;
56
+ return {
57
+ x: a * c[0].x + b * c[1].x + d * c[2].x + e * c[3].x,
58
+ y: a * c[0].y + b * c[1].y + d * c[2].y + e * c[3].y,
59
+ };
60
+ }
61
+ /** samples walks a rope every two pixels or so, with the length walked so far. */
62
+ function samplesOf(curves) {
63
+ const samples = [];
64
+ const starts = [];
65
+ let s = 0;
66
+ curves.forEach((c, i) => {
67
+ const rough = dist(c[0], c[1]) + dist(c[1], c[2]) + dist(c[2], c[3]);
68
+ const k = Math.max(4, Math.ceil(rough / 2));
69
+ starts.push(samples.length === 0 ? 0 : samples.length - 1);
70
+ for (let j = i === 0 ? 0 : 1; j <= k; j++) {
71
+ const p = pointOn(c, j / k);
72
+ if (samples.length > 0)
73
+ s += dist(samples.at(-1), p);
74
+ samples.push({ ...p, s });
75
+ }
76
+ });
77
+ return { samples, starts };
78
+ }
79
+ function segmentsMeet(a, b, c, d) {
80
+ const r = { x: b.x - a.x, y: b.y - a.y };
81
+ const q = { x: d.x - c.x, y: d.y - c.y };
82
+ const cross = r.x * q.y - r.y * q.x;
83
+ if (Math.abs(cross) < 1e-12)
84
+ return undefined;
85
+ const t = ((c.x - a.x) * q.y - (c.y - a.y) * q.x) / cross;
86
+ const u = ((c.x - a.x) * r.y - (c.y - a.y) * r.x) / cross;
87
+ if (t < 0 || t > 1 || u < 0 || u > 1)
88
+ return undefined;
89
+ return { x: a.x + t * r.x, y: a.y + t * r.y };
90
+ }
91
+ const toPx = (x, y) => ({ x: x * unit, y: y * unit });
92
+ function checkScene(scene, problems) {
93
+ only(scene, members, "scene", problems);
94
+ const coordinate = (v, path, most) => {
95
+ if (typeof v !== "number" || !Number.isInteger(v * 2) || v < 0 || v > most) {
96
+ problems.push(`${path}: must be a number from 0 to ${most}, in steps of 0.5`);
97
+ return false;
98
+ }
99
+ return true;
100
+ };
101
+ list(scene.ropes, "ropes", problems, 1, 6).forEach((r, i) => {
102
+ const path = `ropes[${i}]`;
103
+ if (!isObj(r)) {
104
+ problems.push(`${path}: must be a rope with a path`);
105
+ return;
106
+ }
107
+ only(r, ["path", "color", "closed", "start", "end", "label"], path, problems);
108
+ if (r.closed !== undefined && typeof r.closed !== "boolean")
109
+ problems.push(`${path}.closed: must be true or false`);
110
+ const closed = r.closed === true;
111
+ const points = list(r.path, `${path}.path`, problems, closed ? 3 : 2, 24);
112
+ points.forEach((p, j) => {
113
+ const ppath = `${path}.path[${j}]`;
114
+ if (!Array.isArray(p) || p.length < 2 || p.length > 3) {
115
+ problems.push(`${ppath}: must be [across, down] or [across, down, "under"]`);
116
+ return;
117
+ }
118
+ coordinate(p[0], `${ppath}[0]`, across);
119
+ coordinate(p[1], `${ppath}[1]`, down);
120
+ if (p.length === 3 && p[2] !== "under")
121
+ problems.push(`${ppath}[2]: may only be "under", where the rope goes under another strand`);
122
+ const prev = points[j - 1];
123
+ if (Array.isArray(prev) && prev[0] === p[0] && prev[1] === p[1])
124
+ problems.push(`${ppath}: is the same point as the one before; leave it out`);
125
+ });
126
+ if (closed && points.length > 2) {
127
+ const a = points[0];
128
+ const z = points.at(-1);
129
+ if (Array.isArray(a) && Array.isArray(z) && a[0] === z[0] && a[1] === z[1])
130
+ problems.push(`${path}.path: a closed rope comes back to its first point by itself; leave out the last point`);
131
+ }
132
+ if (r.color !== undefined && !isColor(r.color))
133
+ problems.push(`${path}.color: must be a colour: ${colorNames.join(", ")}`);
134
+ for (const end of ["start", "end"]) {
135
+ if (r[end] === undefined)
136
+ continue;
137
+ if (closed)
138
+ problems.push(`${path}.${end}: a closed rope has no ends`);
139
+ else
140
+ checkThing(r[end], `${path}.${end}`, problems);
141
+ }
142
+ if (r.label !== undefined)
143
+ text(r.label, `${path}.label`, problems, 3);
144
+ });
145
+ if (scene.pegs !== undefined)
146
+ list(scene.pegs, "pegs", problems, 1, 12).forEach((g, i) => {
147
+ const path = `pegs[${i}]`;
148
+ if (!isObj(g)) {
149
+ problems.push(`${path}: must be a peg with at`);
150
+ return;
151
+ }
152
+ only(g, ["at", "label"], path, problems);
153
+ const at = list(g.at, `${path}.at`, problems, 2, 2);
154
+ if (at.length === 2) {
155
+ coordinate(at[0], `${path}.at[0]`, across);
156
+ coordinate(at[1], `${path}.at[1]`, down);
157
+ }
158
+ if (g.label !== undefined)
159
+ text(g.label, `${path}.label`, problems, 2);
160
+ });
161
+ return problems.length > 0 ? undefined : scene;
162
+ }
163
+ /** lay finds the crossings, draws nothing, and records what cannot be drawn clearly. */
164
+ function lay(s, problems) {
165
+ // Crossings: the points two passes share.
166
+ const visits = new Map();
167
+ s.ropes.forEach((r, ri) => {
168
+ r.path.forEach((p, index) => {
169
+ const key = keyOf(p[0], p[1]);
170
+ const list_ = visits.get(key) ?? [];
171
+ list_.push({ rope: ri, index, under: p[2] === "under" });
172
+ visits.set(key, list_);
173
+ });
174
+ });
175
+ const crossings = [];
176
+ for (const [key, v] of visits) {
177
+ const [x, y] = key.split(",").map(Number);
178
+ const first = v[0];
179
+ const where = `ropes[${first.rope}].path[${first.index}]`;
180
+ if (v.length > 2) {
181
+ problems.push(`${where}: ${v.length} strands pass ${sayPoint(x, y)}; a crossing has two`);
182
+ continue;
183
+ }
184
+ if (v.length === 1) {
185
+ if (first.under)
186
+ problems.push(`${where}: is marked "under", but no other strand passes ${sayPoint(x, y)}`);
187
+ continue;
188
+ }
189
+ const second = v[1];
190
+ for (const one of v) {
191
+ const rope = s.ropes[one.rope];
192
+ if (!rope.closed && (one.index === 0 || one.index === rope.path.length - 1))
193
+ problems.push(`ropes[${one.rope}].path[${one.index}]: an end of a rope cannot be a crossing at ${sayPoint(x, y)}`);
194
+ }
195
+ if (first.under === second.under)
196
+ problems.push(`ropes[${second.rope}].path[${second.index}]: two strands cross at ${sayPoint(x, y)}; mark exactly one of the two passes "under"`);
197
+ crossings.push({ at: toPx(x, y), key, visits: [first, second] });
198
+ }
199
+ if (problems.length > 0)
200
+ return undefined;
201
+ // The lines, and how each pass meets the other at a crossing.
202
+ const ropes = s.ropes.map((rope, i) => {
203
+ const points = rope.path.map((p) => toPx(p[0], p[1]));
204
+ const curves = pieces(points, rope.closed === true);
205
+ const { samples, starts } = samplesOf(curves);
206
+ const d = `M${n(points[0].x)} ${n(points[0].y)}` +
207
+ curves
208
+ .map((c) => `C${n(c[1].x)} ${n(c[1].y)} ${n(c[2].x)} ${n(c[2].y)} ${n(c[3].x)} ${n(c[3].y)}`)
209
+ .join("") +
210
+ (rope.closed ? "Z" : "");
211
+ return {
212
+ rope,
213
+ color: rope.color ?? defaults[i % defaults.length],
214
+ d,
215
+ samples,
216
+ starts,
217
+ points,
218
+ };
219
+ });
220
+ const tangent = (v) => {
221
+ const pts = ropes[v.rope].points;
222
+ const k = pts.length;
223
+ const closed = s.ropes[v.rope].closed === true;
224
+ const prev = pts[closed ? (v.index - 1 + k) % k : v.index - 1];
225
+ const next = pts[closed ? (v.index + 1) % k : v.index + 1];
226
+ return { x: next.x - prev.x, y: next.y - prev.y };
227
+ };
228
+ for (const c of crossings) {
229
+ const a = tangent(c.visits[0]);
230
+ const b = tangent(c.visits[1]);
231
+ const cos = Math.abs(a.x * b.x + a.y * b.y) / (Math.hypot(a.x, a.y) * Math.hypot(b.x, b.y));
232
+ if (cos > Math.cos((35 * Math.PI) / 180)) {
233
+ const [x, y] = c.key.split(",");
234
+ problems.push(`ropes[${c.visits[1].rope}].path[${c.visits[1].index}]: the strands at (${x}, ${y}) run almost alongside; they must cross there at a clear angle`);
235
+ }
236
+ }
237
+ if (problems.length > 0)
238
+ return undefined;
239
+ // No other crossing, and no strand passing close by another.
240
+ const nearCrossing = (p, d) => crossings.some((c) => dist(c.at, p) < d);
241
+ const where = (p) => sayPoint(Math.round((p.x / unit) * 2) / 2, Math.round((p.y / unit) * 2) / 2);
242
+ const said = new Set();
243
+ const say = (words) => {
244
+ if (!said.has(words))
245
+ problems.push(words);
246
+ said.add(words);
247
+ };
248
+ for (let i = 0; i < ropes.length; i++)
249
+ for (let j = i; j < ropes.length; j++) {
250
+ const a = ropes[i].samples;
251
+ const b = ropes[j].samples;
252
+ const ropeName = j === i ? "itself" : `ropes[${j}]`;
253
+ for (let x = 0; x + 1 < a.length; x++)
254
+ for (let y = j === i ? x + 1 : 0; y + 1 < b.length; y++) {
255
+ const p = a[x];
256
+ const q = b[y];
257
+ if (j === i) {
258
+ const apart = Math.abs(p.s - q.s);
259
+ const loop = a.at(-1).s;
260
+ const round = s.ropes[i].closed ? Math.min(apart, loop - apart) : apart;
261
+ if (round < 40)
262
+ continue;
263
+ }
264
+ if (Math.abs(p.x - q.x) > 24 || Math.abs(p.y - q.y) > 24)
265
+ continue;
266
+ const at = segmentsMeet(p, a[x + 1], q, b[y + 1]);
267
+ if (at && !nearCrossing(at, 6)) {
268
+ say(`ropes[${i}]: crosses ${ropeName} near ${where(at)}, where no point of both paths is; put the crossing on a point that both paths pass`);
269
+ continue;
270
+ }
271
+ if (dist(p, q) < thick + 5 && !nearCrossing(p, reach + 4) && !nearCrossing(q, reach + 4))
272
+ say(`ropes[${i}]: comes too close to ${ropeName} near ${where(p)}; keep the strands apart or let them cross at a shared point`);
273
+ }
274
+ }
275
+ // Pegs stand clear of the ropes, ends and things clear of other strands.
276
+ for (const [k, g] of (s.pegs ?? []).entries()) {
277
+ const at = toPx(g.at[0], g.at[1]);
278
+ for (const [i, r] of ropes.entries())
279
+ if (r.samples.some((p) => dist(p, at) < pegRadius + thick / 2 - 1))
280
+ say(`pegs[${k}]: ropes[${i}] runs through the peg at ${sayPoint(g.at[0], g.at[1])}; let the rope pass beside it`);
281
+ }
282
+ for (const [i, r] of ropes.entries()) {
283
+ if (r.rope.closed)
284
+ continue;
285
+ for (const end of ["start", "end"]) {
286
+ const at = end === "start" ? r.points[0] : r.points.at(-1);
287
+ const room = r.rope[end] !== undefined ? thingSize / 2 + thick / 2 + 2 : 9 + thick / 2;
288
+ const own = end === "start" ? 0 : r.samples.at(-1).s;
289
+ for (const [j, o] of ropes.entries())
290
+ if (o.samples.some((p) => (j !== i || Math.abs(p.s - own) > room + 20) && dist(p, at) < room))
291
+ say(`ropes[${i}].${end}: the end at ${where(at)} touches ${j === i ? "its own rope" : `ropes[${j}]`}; move the end away`);
292
+ }
293
+ }
294
+ if (problems.length > 0)
295
+ return undefined;
296
+ // Labels stand beyond the first point, away from the rope.
297
+ const labels = [];
298
+ for (const [i, r] of ropes.entries()) {
299
+ if (!r.rope.label)
300
+ continue;
301
+ const a = r.points[0];
302
+ const b = r.points[1];
303
+ const l = dist(a, b);
304
+ const back = r.rope.closed ? 22 : r.rope.start !== undefined ? thingSize / 2 + 18 : 24;
305
+ // A loop's label stands outside it, away from the middle of its points.
306
+ const mid = {
307
+ x: r.points.reduce((t, p) => t + p.x, 0) / r.points.length,
308
+ y: r.points.reduce((t, p) => t + p.y, 0) / r.points.length,
309
+ };
310
+ const out = dist(a, mid);
311
+ const dir = r.rope.closed
312
+ ? out > 0
313
+ ? { x: (a.x - mid.x) / out, y: (a.y - mid.y) / out }
314
+ : { x: 0, y: -1 }
315
+ : { x: (a.x - b.x) / l, y: (a.y - b.y) / l };
316
+ const at = { x: a.x + dir.x * back, y: a.y + dir.y * back };
317
+ if (ropes.some((o) => o.samples.some((p) => dist(p, at) < 17)))
318
+ say(`ropes[${i}].label: there is no room for the label beside the rope's first point; leave a free step before it`);
319
+ labels.push({ at, text: r.rope.label });
320
+ }
321
+ if (problems.length > 0)
322
+ return undefined;
323
+ // The picture's box, from everything drawn.
324
+ let minX = Number.POSITIVE_INFINITY;
325
+ let minY = Number.POSITIVE_INFINITY;
326
+ let maxX = Number.NEGATIVE_INFINITY;
327
+ let maxY = Number.NEGATIVE_INFINITY;
328
+ const grow = (p, r) => {
329
+ minX = Math.min(minX, p.x - r);
330
+ maxX = Math.max(maxX, p.x + r);
331
+ minY = Math.min(minY, p.y - r);
332
+ maxY = Math.max(maxY, p.y + r);
333
+ };
334
+ for (const r of ropes) {
335
+ for (const p of r.samples)
336
+ grow(p, thick / 2 + 1);
337
+ if (r.rope.closed)
338
+ continue;
339
+ if (r.rope.start !== undefined)
340
+ grow(r.points[0], thingSize / 2);
341
+ if (r.rope.end !== undefined)
342
+ grow(r.points.at(-1), thingSize / 2);
343
+ }
344
+ for (const g of s.pegs ?? [])
345
+ grow(toPx(g.at[0], g.at[1]), pegRadius + (g.label ? 30 : 2));
346
+ for (const l of labels)
347
+ grow(l.at, textWidth(l.text, 17) / 2 + 9);
348
+ const dx = margin - minX;
349
+ const dy = margin - minY;
350
+ const move = (p) => ({ x: p.x + dx, y: p.y + dy });
351
+ return {
352
+ ropes: s.ropes.map((rope, i) => {
353
+ const points = rope.path.map((p) => move(toPx(p[0], p[1])));
354
+ const curves = pieces(points, rope.closed === true);
355
+ const d = `M${n(points[0].x)} ${n(points[0].y)}` +
356
+ curves
357
+ .map((c) => `C${n(c[1].x)} ${n(c[1].y)} ${n(c[2].x)} ${n(c[2].y)} ${n(c[3].x)} ${n(c[3].y)}`)
358
+ .join("") +
359
+ (rope.closed ? "Z" : "");
360
+ const samples = ropes[i].samples.map((p) => ({ ...move(p), s: p.s }));
361
+ return { rope, color: ropes[i].color, d, samples, starts: ropes[i].starts, points };
362
+ }),
363
+ crossings: crossings.map((c) => ({ ...c, at: move(c.at) })),
364
+ labels: labels.map((l) => ({ ...l, at: move(l.at) })),
365
+ shift: { x: dx, y: dy },
366
+ width: maxX - minX + 2 * margin,
367
+ height: maxY - minY + 2 * margin,
368
+ };
369
+ }
370
+ /** around is the piece of a rope that runs within reach of a sample, in order along the rope. */
371
+ function around(samples, centre, closed) {
372
+ // A closed rope's last sample is its first again.
373
+ const k = closed ? samples.length - 1 : samples.length;
374
+ const walk = (dir) => {
375
+ const out = [];
376
+ let walked = 0;
377
+ let i = centre;
378
+ for (let step = 0; step < k - 1; step++) {
379
+ const j = closed ? (i + dir + k) % k : i + dir;
380
+ if (j < 0 || j >= k)
381
+ break;
382
+ walked += dist(samples[i], samples[j]);
383
+ if (walked > reach)
384
+ break;
385
+ out.push(samples[j]);
386
+ i = j;
387
+ }
388
+ return out;
389
+ };
390
+ return [...walk(-1).reverse(), samples[centre], ...walk(1)];
391
+ }
392
+ /** trim cuts a length off both ends of a line of points. */
393
+ function trim(piece, by) {
394
+ const cut = (pts) => {
395
+ let left = by;
396
+ for (let i = 0; i + 1 < pts.length; i++) {
397
+ const a = pts[i];
398
+ const b = pts[i + 1];
399
+ const l = dist(a, b);
400
+ if (l >= left) {
401
+ const t = left / l;
402
+ return [{ x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t }, ...pts.slice(i + 1)];
403
+ }
404
+ left -= l;
405
+ }
406
+ return [...pts];
407
+ };
408
+ return cut(cut(piece).reverse()).reverse();
409
+ }
410
+ const line = (pts) => pts.map((p, i) => `${i === 0 ? "M" : "L"}${n(p.x)} ${n(p.y)}`).join("");
411
+ /** strand draws a rope: an outline of ink with the rope's colour inside. */
412
+ function strand(pen, d, color, inked = d, caps = "round") {
413
+ for (const [path, stroke, width] of [
414
+ [inked, ink, thick],
415
+ [d, colors[color].fill, core],
416
+ ])
417
+ pen.el("path", {
418
+ d: path,
419
+ fill: "none",
420
+ stroke,
421
+ "stroke-width": width,
422
+ "stroke-linecap": caps,
423
+ "stroke-linejoin": "round",
424
+ });
425
+ }
426
+ export const ropes = {
427
+ kind: "ropes",
428
+ members,
429
+ check(scene, problems) {
430
+ const typed = checkScene(scene, problems);
431
+ if (typed)
432
+ lay(typed, problems);
433
+ },
434
+ draw(scene, pen) {
435
+ const s = scene;
436
+ const laid = lay(s, []);
437
+ for (const g of s.pegs ?? []) {
438
+ const at = { x: g.at[0] * unit + laid.shift.x, y: g.at[1] * unit + laid.shift.y };
439
+ pen.el("circle", {
440
+ cx: at.x,
441
+ cy: at.y,
442
+ r: pegRadius,
443
+ fill: colors.brown.fill,
444
+ stroke: ink,
445
+ "stroke-width": outline,
446
+ class: "peg",
447
+ });
448
+ pen.el("circle", { cx: at.x, cy: at.y, r: 4, fill: colors.brown.dark });
449
+ if (g.label)
450
+ pen.text(at.x + pegRadius + 12, at.y - pegRadius - 6, g.label, { size: 16 });
451
+ }
452
+ for (const r of laid.ropes) {
453
+ pen.add('<g class="rope">');
454
+ strand(pen, r.d, r.color);
455
+ pen.add("</g>");
456
+ }
457
+ // At each crossing the pass on top is drawn again over a gap in the ground's colour.
458
+ // The gap stops short of the outline, and the outline of the colour, so that the
459
+ // piece joins its rope with no seam.
460
+ for (const c of laid.crossings) {
461
+ const over = c.visits.find((v) => !v.under);
462
+ const r = laid.ropes[over.rope];
463
+ const piece = around(r.samples, r.starts[over.index], r.rope.closed === true);
464
+ pen.add('<g class="over">');
465
+ pen.el("path", {
466
+ d: line(trim(piece, 3)),
467
+ fill: "none",
468
+ stroke: ground,
469
+ "stroke-width": halo,
470
+ });
471
+ strand(pen, line(piece), r.color, line(trim(piece, 1.5)), "butt");
472
+ pen.add("</g>");
473
+ }
474
+ for (const r of laid.ropes) {
475
+ if (r.rope.closed)
476
+ continue;
477
+ for (const end of ["start", "end"]) {
478
+ const at = end === "start" ? r.points[0] : r.points.at(-1);
479
+ const thing = r.rope[end];
480
+ if (thing !== undefined)
481
+ drawThing(pen, thing, at.x, at.y, thingSize);
482
+ else
483
+ pen.el("circle", {
484
+ cx: at.x,
485
+ cy: at.y,
486
+ r: 8,
487
+ fill: colors[r.color].dark,
488
+ stroke: ink,
489
+ "stroke-width": outline,
490
+ class: "end",
491
+ });
492
+ }
493
+ }
494
+ for (const l of laid.labels) {
495
+ const w = textWidth(l.text, 17) + 14;
496
+ pen.el("rect", {
497
+ x: l.at.x - w / 2,
498
+ y: l.at.y - 13,
499
+ width: w,
500
+ height: 26,
501
+ rx: 7,
502
+ fill: "#ffffff",
503
+ stroke: ink,
504
+ "stroke-width": 1.4,
505
+ });
506
+ pen.text(l.at.x, l.at.y, l.text, { size: 17 });
507
+ }
508
+ return { width: laid.width, height: laid.height };
509
+ },
510
+ describe(scene) {
511
+ const s = scene;
512
+ const k = s.ropes.length;
513
+ const pegs = s.pegs ?? [];
514
+ const name = (i) => {
515
+ const r = s.ropes[i];
516
+ return r.label ? `rope ${r.label}` : k === 1 ? "the rope" : `rope ${i + 1}`;
517
+ };
518
+ const capital = (w) => `${w[0].toUpperCase()}${w.slice(1)}`;
519
+ const out = [
520
+ `${k === 1 ? "A rope" : `${k} ropes`} on a table${pegs.length > 0 ? `, and ${pegs.length} peg${pegs.length === 1 ? "" : "s"}` : ""}. Places are given as (across, down), in steps of the grid from the top left corner.`,
521
+ ];
522
+ // Crossings, told along each rope from its start: over or under, and what.
523
+ const at = new Map();
524
+ s.ropes.forEach((r, ri) => {
525
+ r.path.forEach((p, index) => {
526
+ const key = keyOf(p[0], p[1]);
527
+ at.set(key, [...(at.get(key) ?? []), { rope: ri, index, under: p[2] === "under" }]);
528
+ });
529
+ });
530
+ s.ropes.forEach((r, i) => {
531
+ const color = colors[r.color ?? defaults[i % defaults.length]].word;
532
+ const pts = r.path.map((p) => sayPoint(p[0], p[1]));
533
+ const route = r.closed
534
+ ? `a closed loop through ${pts.join(", ")} and back to ${pts[0]}`
535
+ : `from ${pts[0]}${pts.length > 2 ? ` through ${pts.slice(1, -1).join(", ")}` : ""} to ${pts.at(-1)}`;
536
+ const meets = r.path
537
+ .map((p, index) => {
538
+ const v = at.get(keyOf(p[0], p[1]));
539
+ if (v.length < 2)
540
+ return "";
541
+ const other = v.find((o) => !(o.rope === i && o.index === index));
542
+ const what = other.rope === i ? "itself" : name(other.rope);
543
+ return `${p[2] === "under" ? "under" : "over"} ${what} at ${sayPoint(p[0], p[1])}`;
544
+ })
545
+ .filter(Boolean);
546
+ const crossing = meets.length > 0
547
+ ? ` Going along it${r.closed ? "" : " from its start"}, it passes ${meets.join(", then ")}.`
548
+ : " It crosses nothing.";
549
+ out.push(`${capital(name(i))}: ${color}, ${route}.${crossing}`);
550
+ });
551
+ // The things at the ends are told by place, top to bottom and left to right, and not
552
+ // rope by rope: which thing a rope joins to which is what a picture may ask.
553
+ const ends = [];
554
+ for (const r of s.ropes) {
555
+ if (r.closed)
556
+ continue;
557
+ const a = r.path[0];
558
+ const z = r.path.at(-1);
559
+ if (r.start !== undefined)
560
+ ends.push({ x: a[0], y: a[1], thing: r.start });
561
+ if (r.end !== undefined)
562
+ ends.push({ x: z[0], y: z[1], thing: r.end });
563
+ }
564
+ ends.sort((a, b) => a.y - b.y || a.x - b.x);
565
+ if (ends.length > 0)
566
+ out.push(`At the ends of the ropes: ${ends.map((e) => `${article(wordOf(e.thing))} at ${sayPoint(e.x, e.y)}`).join(", ")}.`);
567
+ if (pegs.length > 0)
568
+ out.push(`Pegs: ${pegs.map((g, i) => `${g.label ? `peg ${g.label}` : `peg ${i + 1}`} at ${sayPoint(g.at[0], g.at[1])}`).join(", ")}.`);
569
+ return out.join(" ");
570
+ },
571
+ };
@@ -0,0 +1,59 @@
1
+ import type { Kind } from "../kind.ts";
2
+ import { type ColorName } from "../style.ts";
3
+ import { type ShapeName } from "../things.ts";
4
+ /**
5
+ * Shapes: geometric shapes of the palette in rows — side by side, standing
6
+ * one on another in a tower, lying one over another in a pile with a clear
7
+ * order, or in equations with operators and totals ("▲ + ▲ = 10") — or in a
8
+ * table whose rows and columns carry totals. A scene may carry its facts, the
9
+ * value of each shape, which the picture does not show: then the check proves
10
+ * that every equation and every total holds.
11
+ */
12
+ export declare const sizes: readonly ["small", "medium", "large"];
13
+ export type Size = (typeof sizes)[number];
14
+ export declare const ops: readonly ["+", "-", "×", "=", "<", ">"];
15
+ export type Op = (typeof ops)[number];
16
+ export type Piece = {
17
+ shape: ShapeName;
18
+ color: ColorName;
19
+ label?: string;
20
+ size?: Size;
21
+ };
22
+ export type PilePiece = {
23
+ shape: ShapeName;
24
+ color: ColorName;
25
+ at?: [number, number];
26
+ };
27
+ export type Item = Piece | {
28
+ op: Op;
29
+ } | {
30
+ number: string;
31
+ } | {
32
+ ask: true;
33
+ } | {
34
+ tower: Piece[];
35
+ } | {
36
+ pile: PilePiece[];
37
+ };
38
+ export type Row = {
39
+ items: Item[];
40
+ label?: string;
41
+ };
42
+ export type Table = {
43
+ cells: Piece[][];
44
+ right?: string[];
45
+ below?: string[];
46
+ };
47
+ export type ShapesScene = {
48
+ kind: "shapes";
49
+ rows?: Row[];
50
+ table?: Table;
51
+ facts?: {
52
+ values: {
53
+ shape: ShapeName;
54
+ color: ColorName;
55
+ value: number;
56
+ }[];
57
+ };
58
+ };
59
+ export declare const shapes: Kind;