@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,31 @@
1
+ import type { Kind } from "../kind.ts";
2
+ import { type ColorName } from "../style.ts";
3
+ /**
4
+ * Layers: two to eight flat sheets of paper — rectangles, circles, triangles,
5
+ * diamonds, hexagons — put down on a table one after another, each opaque and
6
+ * covering what lies under it. The scene lists the sheets in the order they
7
+ * were put down, the first at the bottom; the picture draws them so, and the
8
+ * order is what a question asks to find out from it. Each sheet has a place
9
+ * and a size on a grid of units of 20 pixels — at is its top-left corner, size
10
+ * its width and height — a colour, and maybe a label, which the program
11
+ * writes where most of the sheet shows. The check proves the picture can be
12
+ * read: each sheet shows enough to be seen and to hold its label, and
13
+ * wherever two sheets overlap, the edge of the upper one is seen lying across
14
+ * the lower one, so which lies on which is never a guess.
15
+ */
16
+ export declare const layerShapes: readonly ["rectangle", "circle", "triangle", "diamond", "hexagon"];
17
+ export type LayerShape = (typeof layerShapes)[number];
18
+ export declare const turns: readonly [0, 90, 180, 270];
19
+ export type Layer = {
20
+ shape: LayerShape;
21
+ color: ColorName;
22
+ at: [number, number];
23
+ size: [number, number];
24
+ turn?: number;
25
+ label?: string;
26
+ };
27
+ export type LayersScene = {
28
+ kind: "layers";
29
+ sheets: Layer[];
30
+ };
31
+ export declare const layers: Kind;
@@ -0,0 +1,419 @@
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, ink, isColor, outline, textOn } from "../style.js";
5
+ import { poly } from "../svg.js";
6
+ /**
7
+ * Layers: two to eight flat sheets of paper — rectangles, circles, triangles,
8
+ * diamonds, hexagons — put down on a table one after another, each opaque and
9
+ * covering what lies under it. The scene lists the sheets in the order they
10
+ * were put down, the first at the bottom; the picture draws them so, and the
11
+ * order is what a question asks to find out from it. Each sheet has a place
12
+ * and a size on a grid of units of 20 pixels — at is its top-left corner, size
13
+ * its width and height — a colour, and maybe a label, which the program
14
+ * writes where most of the sheet shows. The check proves the picture can be
15
+ * read: each sheet shows enough to be seen and to hold its label, and
16
+ * wherever two sheets overlap, the edge of the upper one is seen lying across
17
+ * the lower one, so which lies on which is never a guess.
18
+ */
19
+ export const layerShapes = ["rectangle", "circle", "triangle", "diamond", "hexagon"];
20
+ export const turns = [0, 90, 180, 270];
21
+ const unit = 20;
22
+ const most = { sheets: 8, wide: 30, high: 18, side: 18 };
23
+ /** Samples per unit: the check looks at the picture every 2 pixels. */
24
+ const fine = 10;
25
+ const step = unit / fine;
26
+ const labelSize = 20;
27
+ /** How long, in samples, an upper edge must be seen on a lower sheet, and how thick an overlap must be. */
28
+ const seenEdge = 8;
29
+ const thick = 4;
30
+ /** shapeOf gives a sheet's outline in units, from its box. */
31
+ function shapeOf(sheet) {
32
+ const [x0, y0] = sheet.at;
33
+ const [w, h] = sheet.size;
34
+ const x1 = x0 + w;
35
+ const y1 = y0 + h;
36
+ const cx = x0 + w / 2;
37
+ const cy = y0 + h / 2;
38
+ if (sheet.shape === "circle") {
39
+ const r = w / 2;
40
+ return {
41
+ contains: (x, y) => (x - cx) ** 2 + (y - cy) ** 2 < r * r,
42
+ circle: { c: { x: cx, y: cy }, r },
43
+ };
44
+ }
45
+ let points;
46
+ if (sheet.shape === "rectangle")
47
+ points = [
48
+ { x: x0, y: y0 },
49
+ { x: x1, y: y0 },
50
+ { x: x1, y: y1 },
51
+ { x: x0, y: y1 },
52
+ ];
53
+ else if (sheet.shape === "diamond")
54
+ points = [
55
+ { x: cx, y: y0 },
56
+ { x: x1, y: cy },
57
+ { x: cx, y: y1 },
58
+ { x: x0, y: cy },
59
+ ];
60
+ else if (sheet.shape === "hexagon")
61
+ points = [
62
+ { x: x0, y: cy },
63
+ { x: x0 + w / 4, y: y0 },
64
+ { x: x1 - w / 4, y: y0 },
65
+ { x: x1, y: cy },
66
+ { x: x1 - w / 4, y: y1 },
67
+ { x: x0 + w / 4, y: y1 },
68
+ ];
69
+ else {
70
+ const turn = sheet.turn ?? 0;
71
+ points =
72
+ turn === 0
73
+ ? [
74
+ { x: cx, y: y0 },
75
+ { x: x1, y: y1 },
76
+ { x: x0, y: y1 },
77
+ ]
78
+ : turn === 90
79
+ ? [
80
+ { x: x1, y: cy },
81
+ { x: x0, y: y1 },
82
+ { x: x0, y: y0 },
83
+ ]
84
+ : turn === 180
85
+ ? [
86
+ { x: cx, y: y1 },
87
+ { x: x0, y: y0 },
88
+ { x: x1, y: y0 },
89
+ ]
90
+ : [
91
+ { x: x0, y: cy },
92
+ { x: x1, y: y0 },
93
+ { x: x1, y: y1 },
94
+ ];
95
+ }
96
+ return { contains: (x, y) => inside(points, x, y), points };
97
+ }
98
+ /** inside tells whether a point lies inside a convex polygon whose corners go clockwise. */
99
+ function inside(points, x, y) {
100
+ for (let i = 0; i < points.length; i++) {
101
+ const a = points[i];
102
+ const b = points[(i + 1) % points.length];
103
+ if ((b.x - a.x) * (y - a.y) - (b.y - a.y) * (x - a.x) <= 0)
104
+ return false;
105
+ }
106
+ return true;
107
+ }
108
+ function boxOf(sheets) {
109
+ return {
110
+ x0: Math.min(...sheets.map((s) => s.at[0])),
111
+ y0: Math.min(...sheets.map((s) => s.at[1])),
112
+ x1: Math.max(...sheets.map((s) => s.at[0] + s.size[0])),
113
+ y1: Math.max(...sheets.map((s) => s.at[1] + s.size[1])),
114
+ };
115
+ }
116
+ function look(sheets) {
117
+ const box = boxOf(sheets);
118
+ const cols = (box.x1 - box.x0) * fine;
119
+ const rows = (box.y1 - box.y0) * fine;
120
+ const shapes = sheets.map(shapeOf);
121
+ const top = new Int8Array(cols * rows).fill(-1);
122
+ const under = new Int32Array(cols * rows);
123
+ for (let r = 0; r < rows; r++)
124
+ for (let c = 0; c < cols; c++) {
125
+ const x = box.x0 + (c + 0.5) / fine;
126
+ const y = box.y0 + (r + 0.5) / fine;
127
+ shapes.forEach((shape, i) => {
128
+ if (!shape.contains(x, y))
129
+ return;
130
+ top[r * cols + c] = i;
131
+ under[r * cols + c] = under[r * cols + c] | (1 << i);
132
+ });
133
+ }
134
+ const k = sheets.length;
135
+ const spots = sheets.map((_, i) => {
136
+ const dist = distances(cols, rows, (j) => top[j] === i);
137
+ let best = -1;
138
+ let bestD = 0;
139
+ let sx = 0;
140
+ let sy = 0;
141
+ let count = 0;
142
+ for (let j = 0; j < cols * rows; j++)
143
+ if (top[j] === i) {
144
+ sx += j % cols;
145
+ sy += Math.floor(j / cols);
146
+ count++;
147
+ }
148
+ const mx = count > 0 ? sx / count : 0;
149
+ const my = count > 0 ? sy / count : 0;
150
+ let bestNear = Infinity;
151
+ for (let j = 0; j < cols * rows; j++) {
152
+ const d = dist[j];
153
+ if (d === 0 || d < bestD)
154
+ continue;
155
+ const near = ((j % cols) - mx) ** 2 + (Math.floor(j / cols) - my) ** 2;
156
+ if (d > bestD || near < bestNear) {
157
+ best = j;
158
+ bestD = d;
159
+ bestNear = near;
160
+ }
161
+ }
162
+ return {
163
+ at: {
164
+ x: box.x0 + ((best % cols) + 0.5) / fine,
165
+ y: box.y0 + (Math.floor(best / cols) + 0.5) / fine,
166
+ },
167
+ // A chamfer distance counts 3 per sample; the outline eats into the room.
168
+ room: best < 0 ? 0 : (bestD / 3) * step - outline / 2,
169
+ };
170
+ });
171
+ const seen = Array.from({ length: k }, () => new Array(k).fill(0));
172
+ for (let r = 0; r < rows; r++)
173
+ for (let c = 0; c < cols; c++) {
174
+ const p = r * cols + c;
175
+ for (const q of [c + 1 < cols ? p + 1 : -1, r + 1 < rows ? p + cols : -1]) {
176
+ if (q < 0)
177
+ continue;
178
+ const a = top[p];
179
+ const b = top[q];
180
+ if (a < 0 || b < 0 || a === b)
181
+ continue;
182
+ // The upper sheet's edge is seen on the lower one when the lower one
183
+ // goes on under the upper one across that edge.
184
+ if (a > b && (under[p] & (1 << b)) !== 0)
185
+ seen[a][b]++;
186
+ if (b > a && (under[q] & (1 << a)) !== 0)
187
+ seen[b][a]++;
188
+ }
189
+ }
190
+ const thickness = Array.from({ length: k }, () => new Array(k).fill(0));
191
+ for (let i = 0; i < k; i++)
192
+ for (let j = 0; j < i; j++) {
193
+ const both = (1 << i) | (1 << j);
194
+ const dist = distances(cols, rows, (p) => (under[p] & both) === both);
195
+ let t = 0;
196
+ for (const d of dist)
197
+ t = Math.max(t, Math.ceil(d / 3));
198
+ thickness[i][j] = t;
199
+ thickness[j][i] = t;
200
+ }
201
+ return { cols, rows, top, under, spots, seen, thickness };
202
+ }
203
+ /** distances is the chamfer distance, 3 per sample across and 4 aslant, from each sample in a region to the nearest outside it. */
204
+ function distances(cols, rows, inRegion) {
205
+ const big = 1 << 20;
206
+ const d = new Int32Array(cols * rows);
207
+ for (let p = 0; p < cols * rows; p++)
208
+ d[p] = inRegion(p) ? big : 0;
209
+ const at = (r, c) => r < 0 || c < 0 || r >= rows || c >= cols ? 0 : d[r * cols + c];
210
+ for (let r = 0; r < rows; r++)
211
+ for (let c = 0; c < cols; c++) {
212
+ const p = r * cols + c;
213
+ if (d[p] === 0)
214
+ continue;
215
+ d[p] = Math.min(d[p], at(r, c - 1) + 3, at(r - 1, c) + 3, at(r - 1, c - 1) + 4, at(r - 1, c + 1) + 4);
216
+ }
217
+ for (let r = rows - 1; r >= 0; r--)
218
+ for (let c = cols - 1; c >= 0; c--) {
219
+ const p = r * cols + c;
220
+ if (d[p] === 0)
221
+ continue;
222
+ d[p] = Math.min(d[p], at(r, c + 1) + 3, at(r + 1, c) + 3, at(r + 1, c + 1) + 4, at(r + 1, c - 1) + 4);
223
+ }
224
+ return d;
225
+ }
226
+ /** labelRoom is how far from its middle a label needs clear paper, in pixels. */
227
+ function labelRoom(label) {
228
+ if (label === undefined)
229
+ return 9;
230
+ return Math.max(textWidth(label, labelSize) / 2, labelSize / 2) + 3;
231
+ }
232
+ // Words.
233
+ function shapeWord(sheet) {
234
+ if (sheet.shape === "rectangle")
235
+ return sheet.size[0] === sheet.size[1] ? "square" : "rectangle";
236
+ if (sheet.shape === "triangle") {
237
+ const way = ["up", "right", "down", "left"][(sheet.turn ?? 0) / 90];
238
+ return `triangle pointing ${way}`;
239
+ }
240
+ return sheet.shape;
241
+ }
242
+ function nameOf(sheet) {
243
+ return sheet.label === undefined
244
+ ? `the ${colors[sheet.color].word} ${shapeWord(sheet).replace(/ pointing \w+/, "")}`
245
+ : `sheet ${sheet.label}`;
246
+ }
247
+ /** order lists the sheets as the words name them: by label, else from left to right. */
248
+ function order(sheets) {
249
+ const idx = sheets.map((_, i) => i);
250
+ if (sheets.every((s) => s.label !== undefined))
251
+ return idx.sort((a, b) => sheets[a].label.localeCompare(sheets[b].label, "en", { numeric: true }));
252
+ const mid = (s) => ({ x: s.at[0] + s.size[0] / 2, y: s.at[1] + s.size[1] / 2 });
253
+ return idx.sort((a, b) => mid(sheets[a]).x - mid(sheets[b]).x || mid(sheets[a]).y - mid(sheets[b]).y);
254
+ }
255
+ function placeWords(sheet, box) {
256
+ const x = (sheet.at[0] + sheet.size[0] / 2 - box.x0) / (box.x1 - box.x0);
257
+ const y = (sheet.at[1] + sheet.size[1] / 2 - box.y0) / (box.y1 - box.y0);
258
+ const across = x < 0.36 ? "left" : x > 0.64 ? "right" : "";
259
+ const down = y < 0.36 ? "top" : y > 0.64 ? "bottom" : "";
260
+ if (!across && !down)
261
+ return "in the middle";
262
+ if (!across)
263
+ return `at the ${down}`;
264
+ if (!down)
265
+ return `on the ${across}`;
266
+ return `at the ${down} ${across}`;
267
+ }
268
+ function joinNames(names) {
269
+ if (names.length === 1)
270
+ return names[0];
271
+ return `${names.slice(0, -1).join(", ")} and ${names[names.length - 1]}`;
272
+ }
273
+ export const layers = {
274
+ kind: "layers",
275
+ members: ["kind", "sheets"],
276
+ check(scene, problems) {
277
+ only(scene, this.members, "scene", problems);
278
+ const sheets = list(scene.sheets, "sheets", problems, 2, most.sheets);
279
+ sheets.forEach((sheet, i) => {
280
+ const path = `sheets[${i}]`;
281
+ if (!isObj(sheet)) {
282
+ problems.push(`${path}: must be a sheet: shape, color, at and size`);
283
+ return;
284
+ }
285
+ only(sheet, ["shape", "color", "at", "size", "turn", "label"], path, problems);
286
+ const shape = oneOf(sheet.shape, layerShapes, `${path}.shape`, problems);
287
+ if (!isColor(sheet.color))
288
+ problems.push(`${path}.color: must be a colour: ${colorNames.join(", ")}`);
289
+ const before = problems.length;
290
+ if (!Array.isArray(sheet.size) || sheet.size.length !== 2)
291
+ problems.push(`${path}.size: must be [width, height] in units of 20 pixels`);
292
+ else {
293
+ int(sheet.size[0], `${path}.size[0]`, problems, 2, most.side);
294
+ int(sheet.size[1], `${path}.size[1]`, problems, 2, most.side);
295
+ }
296
+ if (!Array.isArray(sheet.at) || sheet.at.length !== 2)
297
+ problems.push(`${path}.at: must be [x, y] of the top-left corner, in units of 20 pixels from the top left`);
298
+ else {
299
+ int(sheet.at[0], `${path}.at[0]`, problems, 0, most.wide - 2);
300
+ int(sheet.at[1], `${path}.at[1]`, problems, 0, most.high - 2);
301
+ }
302
+ if (problems.length === before) {
303
+ const at = sheet.at;
304
+ const size = sheet.size;
305
+ if (at[0] + size[0] > most.wide || at[1] + size[1] > most.high)
306
+ problems.push(`${path}: reaches past the table, ${most.wide} units wide and ${most.high} high`);
307
+ if (shape === "circle" && size[0] !== size[1])
308
+ problems.push(`${path}.size: a circle is as wide as it is high`);
309
+ }
310
+ if (sheet.turn !== undefined) {
311
+ if (shape !== "triangle")
312
+ problems.push(`${path}.turn: only a triangle turns`);
313
+ else if (!turns.includes(sheet.turn))
314
+ problems.push(`${path}.turn: must be one of ${turns.join(", ")}, clockwise`);
315
+ }
316
+ if (sheet.label !== undefined)
317
+ text(sheet.label, `${path}.label`, problems, 2);
318
+ });
319
+ if (problems.length > 0)
320
+ return;
321
+ const typed = scene;
322
+ const labelled = typed.sheets.filter((s) => s.label !== undefined).length;
323
+ if (labelled !== 0 && labelled !== typed.sheets.length)
324
+ problems.push("sheets: label every sheet, or none");
325
+ const names = new Map();
326
+ typed.sheets.forEach((sheet, i) => {
327
+ const name = labelled > 0 ? `label "${sheet.label}"` : `${colors[sheet.color].word} ${shapeWord(sheet)}`;
328
+ const other = names.get(name);
329
+ if (other !== undefined)
330
+ problems.push(`sheets[${i}]: sheets[${other}] is ${labelled > 0 ? `labelled "${sheet.label}"` : article(name)} too; ${labelled > 0 ? "give each sheet a label of its own" : "give each sheet a colour or a shape of its own, or label them"}`);
331
+ names.set(name, i);
332
+ });
333
+ if (problems.length > 0)
334
+ return;
335
+ const seenBy = look(typed.sheets);
336
+ typed.sheets.forEach((sheet, i) => {
337
+ if (seenBy.spots[i].room < labelRoom(sheet.label))
338
+ problems.push(`sheets[${i}]: too little of it shows${sheet.label !== undefined ? " to hold its label" : ""}; move it, or the sheets put down after it`);
339
+ });
340
+ for (let i = 0; i < typed.sheets.length; i++)
341
+ for (let j = 0; j < i; j++) {
342
+ if (seenBy.thickness[i][j] === 0)
343
+ continue;
344
+ if (seenBy.thickness[i][j] < thick || seenBy.seen[i][j] < seenEdge)
345
+ problems.push(`sheets[${i}]: it overlaps sheets[${j}], but the picture cannot show which lies on top; let them overlap more, where no other sheet covers the edge between them, or keep them apart`);
346
+ }
347
+ },
348
+ draw(scene, pen) {
349
+ const typed = scene;
350
+ const box = boxOf(typed.sheets);
351
+ const ox = margin + outline;
352
+ const oy = margin + outline;
353
+ const at = (p) => ({ x: ox + (p.x - box.x0) * unit, y: oy + (p.y - box.y0) * unit });
354
+ typed.sheets.forEach((sheet) => {
355
+ const shape = shapeOf(sheet);
356
+ const style = {
357
+ class: "sheet",
358
+ fill: colors[sheet.color].fill,
359
+ stroke: ink,
360
+ "stroke-width": outline,
361
+ "stroke-linejoin": "round",
362
+ };
363
+ if (shape.circle) {
364
+ const c = at(shape.circle.c);
365
+ pen.el("circle", { cx: c.x, cy: c.y, r: shape.circle.r * unit, ...style });
366
+ }
367
+ else
368
+ pen.el("path", { d: poly(shape.points.map(at)), ...style });
369
+ });
370
+ const spots = look(typed.sheets).spots;
371
+ typed.sheets.forEach((sheet, i) => {
372
+ if (sheet.label === undefined)
373
+ return;
374
+ const p = at(spots[i].at);
375
+ pen.text(p.x, p.y, sheet.label, { size: labelSize, fill: textOn(sheet.color) });
376
+ });
377
+ return {
378
+ width: (box.x1 - box.x0) * unit + 2 * ox,
379
+ height: (box.y1 - box.y0) * unit + 2 * oy,
380
+ };
381
+ },
382
+ describe(scene) {
383
+ const typed = scene;
384
+ const box = boxOf(typed.sheets);
385
+ const k = typed.sheets.length;
386
+ const seenBy = look(typed.sheets);
387
+ const out = [
388
+ `${k} sheets of paper lie on a table, put down one after another; each covers what lies under it.`,
389
+ ];
390
+ const listed = order(typed.sheets);
391
+ for (const i of listed) {
392
+ const sheet = typed.sheets[i];
393
+ const what = `${article(`${colors[sheet.color].word} ${shapeWord(sheet)}`)} ${placeWords(sheet, box)}`;
394
+ out.push(sheet.label === undefined ? `${cap(what)}.` : `Sheet ${sheet.label}: ${what}.`);
395
+ }
396
+ for (const i of listed) {
397
+ const on = listed
398
+ .filter((j) => j < i && seenBy.thickness[i][j] > 0)
399
+ .map((j) => nameOf(typed.sheets[j]));
400
+ const under = listed
401
+ .filter((j) => j > i && seenBy.thickness[i][j] > 0)
402
+ .map((j) => nameOf(typed.sheets[j]));
403
+ const parts = [
404
+ on.length > 0 ? `lies on ${joinNames(on)}` : "",
405
+ under.length > 0 ? `${on.length > 0 ? "and " : ""}lies under ${joinNames(under)}` : "",
406
+ ].filter((p) => p !== "");
407
+ out.push(parts.length === 0
408
+ ? `${cap(nameOf(typed.sheets[i]))} touches no other sheet.`
409
+ : `${cap(nameOf(typed.sheets[i]))} ${parts.join(" ")}.`);
410
+ }
411
+ return out.join(" ");
412
+ },
413
+ };
414
+ function article(words) {
415
+ return `${/^[aeiou]/.test(words) ? "an" : "a"} ${words}`;
416
+ }
417
+ function cap(words) {
418
+ return `${words[0].toUpperCase()}${words.slice(1)}`;
419
+ }
@@ -0,0 +1,38 @@
1
+ import type { Kind } from "../kind.ts";
2
+ import { type ColorName } from "../style.ts";
3
+ import { type Thing } from "../things.ts";
4
+ /**
5
+ * A line: points along a line and the gaps between them, in one of six
6
+ * styles — a fence of posts joined by rails, things in a row, a log with cut
7
+ * lines, a number line with ticks, stairs with steps, a strip with holes. The
8
+ * picture draws exactly as many posts, things, cuts, ticks, steps or holes as
9
+ * the scene counts, each in a place of its own; labels go on the points, on
10
+ * the gaps between them and on the whole length, and "?" marks the asked one.
11
+ */
12
+ export declare const lineStyles: readonly ["fence", "row", "log", "numbers", "stairs", "belt"];
13
+ export type LineStyle = (typeof lineStyles)[number];
14
+ export type Jump = {
15
+ from: number;
16
+ to: number;
17
+ label?: string;
18
+ };
19
+ export type Mark = {
20
+ at: number;
21
+ thing: Thing;
22
+ };
23
+ export type LineScene = {
24
+ kind: "line";
25
+ style: LineStyle;
26
+ count?: number;
27
+ things?: Thing[];
28
+ start?: number;
29
+ by?: number;
30
+ labels?: string[];
31
+ gaps?: string[];
32
+ lengths?: number[];
33
+ total?: string;
34
+ jumps?: Jump[];
35
+ marks?: Mark[];
36
+ color?: ColorName;
37
+ };
38
+ export declare const line: Kind;