@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,50 @@
1
+ import type { Kind } from "../kind.ts";
2
+ import { type ColorName } from "../style.ts";
3
+ /**
4
+ * Fraction: shapes cut into parts, some of them coloured — a circle cut from
5
+ * its centre into sectors, a bar cut across into pieces, a rectangle or a
6
+ * square cut into columns, rows, a grid or along its diagonals — and any
7
+ * part of a rectangle cut again, as deep as four cuts. The parts of a cut
8
+ * are equal unless the scene gives their sizes. One to six such pictures
9
+ * stand in a row, or one under another, each with its label. The words say
10
+ * every cut and the colour of every part, never the fraction a question asks
11
+ * for; the scene may carry the fractions as facts, and the check works them
12
+ * out itself.
13
+ */
14
+ export declare const figureShapes: readonly ["circle", "bar", "rectangle", "square"];
15
+ export type FigureShape = (typeof figureShapes)[number];
16
+ export declare const cuts: readonly ["columns", "rows", "diagonal", "anti-diagonal", "diagonals"];
17
+ export type Cut = (typeof cuts)[number];
18
+ export type Part = ColorName | {
19
+ cut: Cut;
20
+ sizes?: number[];
21
+ parts: Part[];
22
+ } | {
23
+ grid: Part[][];
24
+ };
25
+ export type Figure = {
26
+ shape: FigureShape;
27
+ color?: ColorName;
28
+ cut?: Cut;
29
+ sizes?: number[];
30
+ parts?: Part[];
31
+ grid?: Part[][];
32
+ label?: string;
33
+ };
34
+ export type Share = {
35
+ figure: number;
36
+ color: ColorName;
37
+ share: string;
38
+ };
39
+ export type FractionScene = {
40
+ kind: "fraction";
41
+ figures: Figure[];
42
+ layout?: "row" | "column";
43
+ facts?: {
44
+ shares: Share[];
45
+ };
46
+ };
47
+ export declare const layouts: readonly ["row", "column"];
48
+ /** shareOf is the part of a figure's area that a colour takes, as a fraction. */
49
+ export declare function shareOf(f: Figure, color: ColorName): string;
50
+ export declare const fraction: Kind;
@@ -0,0 +1,568 @@
1
+ import { margin } from "../kind.js";
2
+ import { int, isObj, list, oneOf, only, text } from "../read.js";
3
+ import { colorNames, colors, ink, isColor, outline } from "../style.js";
4
+ import { n, poly } from "../svg.js";
5
+ import { capital, runs } from "./plane-parts.js";
6
+ /**
7
+ * Fraction: shapes cut into parts, some of them coloured — a circle cut from
8
+ * its centre into sectors, a bar cut across into pieces, a rectangle or a
9
+ * square cut into columns, rows, a grid or along its diagonals — and any
10
+ * part of a rectangle cut again, as deep as four cuts. The parts of a cut
11
+ * are equal unless the scene gives their sizes. One to six such pictures
12
+ * stand in a row, or one under another, each with its label. The words say
13
+ * every cut and the colour of every part, never the fraction a question asks
14
+ * for; the scene may carry the fractions as facts, and the check works them
15
+ * out itself.
16
+ */
17
+ export const figureShapes = ["circle", "bar", "rectangle", "square"];
18
+ export const cuts = ["columns", "rows", "diagonal", "anti-diagonal", "diagonals"];
19
+ export const layouts = ["row", "column"];
20
+ const deepest = 4;
21
+ const mostParts = 100;
22
+ const room = 640 - 2 * margin;
23
+ const labelSize = 18;
24
+ const triangles = {
25
+ diagonal: 2,
26
+ "anti-diagonal": 2,
27
+ diagonals: 4,
28
+ };
29
+ const gcd = (a, b) => (b === 0 ? a : gcd(b, a % b));
30
+ const q = (num, den) => {
31
+ const g = gcd(num, den) || 1;
32
+ return { n: num / g, d: den / g };
33
+ };
34
+ const times = (a, b) => q(a.n * b.n, a.d * b.d);
35
+ const plus = (a, b) => q(a.n * b.d + b.n * a.d, a.d * b.d);
36
+ const sayQ = (a) => (a.d === 1 ? String(a.n) : `${a.n}/${a.d}`);
37
+ function isGrid(p) {
38
+ return typeof p === "object" && "grid" in p;
39
+ }
40
+ /** The whole of a figure as a part: its cut, its grid or its one colour. */
41
+ function rootOf(f) {
42
+ if (f.grid)
43
+ return { grid: f.grid };
44
+ if (f.parts) {
45
+ const out = {
46
+ cut: f.shape === "bar" || f.shape === "circle" ? "columns" : (f.cut ?? "columns"),
47
+ parts: f.parts,
48
+ };
49
+ if (f.sizes)
50
+ out.sizes = f.sizes;
51
+ return out;
52
+ }
53
+ return f.color ?? "white";
54
+ }
55
+ /** leaves lists the coloured parts of a part with the share of the whole each takes. */
56
+ function leaves(p, share = q(1, 1)) {
57
+ if (typeof p === "string")
58
+ return [{ color: p, share }];
59
+ if (isGrid(p)) {
60
+ const k = p.grid.length * p.grid[0].length;
61
+ return p.grid.flat().flatMap((c) => leaves(c, times(share, q(1, k))));
62
+ }
63
+ const sizes = p.sizes ?? p.parts.map(() => 1);
64
+ const total = sizes.reduce((s, v) => s + v, 0);
65
+ return p.parts.flatMap((c, i) => leaves(c, times(share, q(sizes[i], total))));
66
+ }
67
+ /** shareOf is the part of a figure's area that a colour takes, as a fraction. */
68
+ export function shareOf(f, color) {
69
+ return sayQ(leaves(rootOf(f))
70
+ .filter((l) => l.color === color)
71
+ .reduce((s, l) => plus(s, l.share), q(0, 1)));
72
+ }
73
+ /** The natural size of a figure, before a row scales it to fit. */
74
+ function sizeOf(f, alone) {
75
+ const big = alone ? 1.25 : 1;
76
+ switch (f.shape) {
77
+ case "circle":
78
+ return { w: 136 * big, h: 136 * big };
79
+ case "square":
80
+ return { w: 150 * big, h: 150 * big };
81
+ case "bar":
82
+ return { w: 330 * big, h: 46 * big };
83
+ case "rectangle": {
84
+ if (f.grid) {
85
+ const r = f.grid.length;
86
+ const c = f.grid[0].length;
87
+ const cell = Math.min(46 * big, (330 * big) / c, (230 * big) / r);
88
+ return { w: c * cell, h: r * cell };
89
+ }
90
+ return { w: 216 * big, h: 144 * big };
91
+ }
92
+ }
93
+ }
94
+ /** place lays the figures out: in a row, scaled down together if they are too wide, or one under another. */
95
+ function place(s) {
96
+ const alone = s.figures.length === 1;
97
+ const sizes = s.figures.map((f) => sizeOf(f, alone));
98
+ const labelled = s.figures.some((f) => f.label);
99
+ if ((s.layout ?? "row") === "column") {
100
+ const gutter = labelled ? 34 : 0;
101
+ const wide = Math.max(...sizes.map((z) => z.w));
102
+ const scale = Math.min(1, (room - gutter) / wide);
103
+ let y = margin;
104
+ const laid = sizes.map((z) => {
105
+ const one = {
106
+ x: margin + gutter + ((wide - z.w) * scale) / 2,
107
+ y,
108
+ w: z.w * scale,
109
+ h: z.h * scale,
110
+ scale,
111
+ };
112
+ y += z.h * scale + 18;
113
+ return one;
114
+ });
115
+ return { laid, width: wide * scale + gutter + 2 * margin, height: y - 18 + margin };
116
+ }
117
+ const gutter = 26;
118
+ const total = sizes.reduce((sum, z) => sum + z.w, 0);
119
+ const scale = Math.min(1, (room - gutter * (sizes.length - 1)) / total);
120
+ const tallest = Math.max(...sizes.map((z) => z.h)) * scale;
121
+ let x = margin;
122
+ const laid = sizes.map((z) => {
123
+ const one = {
124
+ x,
125
+ y: margin + (tallest - z.h * scale) / 2,
126
+ w: z.w * scale,
127
+ h: z.h * scale,
128
+ scale,
129
+ };
130
+ x += z.w * scale + gutter;
131
+ return one;
132
+ });
133
+ return {
134
+ laid,
135
+ width: x - gutter + margin,
136
+ height: margin + tallest + (labelled ? 34 : 0) + margin,
137
+ };
138
+ }
139
+ /** regions draws (or, with no pen, measures) a part cut into a rectangle of the picture. */
140
+ function regions(p, x, y, w, h, each) {
141
+ if (typeof p === "string") {
142
+ each([
143
+ { x, y },
144
+ { x: x + w, y },
145
+ { x: x + w, y: y + h },
146
+ { x, y: y + h },
147
+ ], p);
148
+ return;
149
+ }
150
+ if (isGrid(p)) {
151
+ const r = p.grid.length;
152
+ const c = p.grid[0].length;
153
+ p.grid.forEach((row, i) => {
154
+ row.forEach((cell, j) => {
155
+ regions(cell, x + (j * w) / c, y + (i * h) / r, w / c, h / r, each);
156
+ });
157
+ });
158
+ return;
159
+ }
160
+ const tl = { x, y };
161
+ const tr = { x: x + w, y };
162
+ const br = { x: x + w, y: y + h };
163
+ const bl = { x, y: y + h };
164
+ const mid = { x: x + w / 2, y: y + h / 2 };
165
+ const colorOf = (k) => p.parts[k];
166
+ if (p.cut === "diagonal") {
167
+ each([tl, tr, br], colorOf(0));
168
+ each([tl, br, bl], colorOf(1));
169
+ return;
170
+ }
171
+ if (p.cut === "anti-diagonal") {
172
+ each([tl, tr, bl], colorOf(0));
173
+ each([tr, br, bl], colorOf(1));
174
+ return;
175
+ }
176
+ if (p.cut === "diagonals") {
177
+ each([tl, tr, mid], colorOf(0));
178
+ each([tr, br, mid], colorOf(1));
179
+ each([br, bl, mid], colorOf(2));
180
+ each([bl, tl, mid], colorOf(3));
181
+ return;
182
+ }
183
+ const sizes = p.sizes ?? p.parts.map(() => 1);
184
+ const total = sizes.reduce((s, v) => s + v, 0);
185
+ let at = 0;
186
+ p.parts.forEach((c, i) => {
187
+ const t0 = at / total;
188
+ at += sizes[i];
189
+ const t1 = at / total;
190
+ if (p.cut === "rows")
191
+ regions(c, x, y + t0 * h, w, (t1 - t0) * h, each);
192
+ else
193
+ regions(c, x + t0 * w, y, (t1 - t0) * w, h, each);
194
+ });
195
+ }
196
+ /**
197
+ * sectorPath is a sector of a circle from angle a0 to a1, clockwise from the
198
+ * top. Its arc goes in pieces of at most a quarter turn: the ends of an arc
199
+ * are rounded, and a rounded end moves the centre of an arc near a half turn
200
+ * by pixels, so the sector would stand off its circle.
201
+ */
202
+ function sectorPath(c, r, a0, a1) {
203
+ const at = (a) => ({ x: c.x + r * Math.sin(a), y: c.y - r * Math.cos(a) });
204
+ const p0 = at(a0);
205
+ const k = Math.max(1, Math.ceil((a1 - a0) / (Math.PI / 2) - 1e-9));
206
+ let d = `M${n(c.x)} ${n(c.y)}L${n(p0.x)} ${n(p0.y)}`;
207
+ for (let j = 1; j <= k; j++) {
208
+ const p = at(a0 + ((a1 - a0) * j) / k);
209
+ d += `A${n(r)} ${n(r)} 0 0 1 ${n(p.x)} ${n(p.y)}`;
210
+ }
211
+ return `${d}Z`;
212
+ }
213
+ /** circlePath is a whole circle, in four quarter arcs for the same reason. */
214
+ function circlePath(c, r) {
215
+ const at = (a) => ({ x: c.x + r * Math.sin(a), y: c.y - r * Math.cos(a) });
216
+ const start = at(0);
217
+ let d = `M${n(start.x)} ${n(start.y)}`;
218
+ for (let j = 1; j <= 4; j++) {
219
+ const p = at((j * Math.PI) / 2);
220
+ d += `A${n(r)} ${n(r)} 0 0 1 ${n(p.x)} ${n(p.y)}`;
221
+ }
222
+ return `${d}Z`;
223
+ }
224
+ function checkPart(p, path, problems, depth, inTriangle = false) {
225
+ if (typeof p === "string") {
226
+ if (!isColor(p))
227
+ problems.push(`${path}: must be a colour: ${colorNames.join(", ")}, or a part cut again`);
228
+ return;
229
+ }
230
+ if (inTriangle) {
231
+ problems.push(`${path}: a triangle of a diagonal cut is not cut again; give its colour`);
232
+ return;
233
+ }
234
+ if (!isObj(p)) {
235
+ problems.push(`${path}: must be a colour or a part cut again, as {"cut": "rows", "parts": ["red", "white"]}`);
236
+ return;
237
+ }
238
+ if (depth >= deepest) {
239
+ problems.push(`${path}: cut too deep; at most ${deepest} cuts one inside another`);
240
+ return;
241
+ }
242
+ if ("grid" in p) {
243
+ only(p, ["grid"], path, problems);
244
+ checkGrid(p.grid, `${path}.grid`, problems, depth + 1);
245
+ return;
246
+ }
247
+ only(p, ["cut", "sizes", "parts"], path, problems);
248
+ const cut = oneOf(p.cut, cuts, `${path}.cut`, problems);
249
+ checkCut(cut, p.sizes, p.parts, path, problems, depth + 1);
250
+ }
251
+ function checkGrid(g, path, problems, depth) {
252
+ const rows = list(g, path, problems, 1, 10);
253
+ let cols = 0;
254
+ rows.forEach((row, i) => {
255
+ const cells = list(row, `${path}[${i}]`, problems, 1, 10);
256
+ if (i === 0)
257
+ cols = cells.length;
258
+ else if (cells.length !== cols)
259
+ problems.push(`${path}[${i}]: must hold ${cols} parts, as ${path}[0] does`);
260
+ cells.forEach((c, j) => {
261
+ checkPart(c, `${path}[${i}][${j}]`, problems, depth);
262
+ });
263
+ });
264
+ if (rows.length * cols === 1)
265
+ problems.push(`${path}: a grid of one part is no cut; give its colour`);
266
+ }
267
+ function checkCut(cut, sizes, parts, path, problems, depth) {
268
+ if (cut === "columns" || cut === "rows") {
269
+ const items = list(parts, `${path}.parts`, problems, 2, 16);
270
+ items.forEach((c, i) => {
271
+ checkPart(c, `${path}.parts[${i}]`, problems, depth);
272
+ });
273
+ if (sizes !== undefined) {
274
+ const values = list(sizes, `${path}.sizes`, problems, 0);
275
+ if (Array.isArray(sizes) && values.length !== items.length)
276
+ problems.push(`${path}.sizes: must hold one size for each part, ${items.length}`);
277
+ values.forEach((v, i) => {
278
+ int(v, `${path}.sizes[${i}]`, problems, 1, 12);
279
+ });
280
+ }
281
+ return;
282
+ }
283
+ const k = triangles[cut];
284
+ const items = list(parts, `${path}.parts`, problems, 0);
285
+ if (Array.isArray(parts) && items.length !== k)
286
+ problems.push(`${path}.parts: a ${cut} cut makes ${k} triangles; give a colour for each`);
287
+ items.forEach((c, i) => {
288
+ checkPart(c, `${path}.parts[${i}]`, problems, depth, true);
289
+ });
290
+ if (sizes !== undefined)
291
+ problems.push(`${path}.sizes: a ${cut} cut has no sizes; its triangles are what they are`);
292
+ }
293
+ const cutWords = {
294
+ diagonal: {
295
+ how: "along its diagonal from the top-left corner to the bottom-right corner into 2 triangles",
296
+ names: ["the top-right one", "the bottom-left one"],
297
+ },
298
+ "anti-diagonal": {
299
+ how: "along its diagonal from the top-right corner to the bottom-left corner into 2 triangles",
300
+ names: ["the top-left one", "the bottom-right one"],
301
+ },
302
+ diagonals: {
303
+ how: "along both diagonals into 4 triangles",
304
+ names: ["the top one", "the right one", "the bottom one", "the left one"],
305
+ },
306
+ };
307
+ const word = (c) => colors[c].word;
308
+ /** item says a part in a list: its colour, or in brackets how it is cut again. */
309
+ const item = (p) => (typeof p === "string" ? word(p) : `(a part ${sayPart(p)})`);
310
+ /** sayPart says a part: its colour, or how it is cut and what each of its parts is. */
311
+ function sayPart(p, pieces = "columns") {
312
+ if (typeof p === "string")
313
+ return word(p);
314
+ if (isGrid(p)) {
315
+ const r = p.grid.length;
316
+ const c = p.grid[0].length;
317
+ const rows = p.grid.map((row, i) => `row ${i + 1}: ${row.map(item).join(", ")}`);
318
+ return `cut into a grid of ${r} row${r === 1 ? "" : "s"} of ${c} equal parts; the rows from the top, each from the left: ${rows.join("; ")}`;
319
+ }
320
+ if (p.cut in triangles) {
321
+ const words = cutWords[p.cut];
322
+ return `cut ${words.how}: ${p.parts.map((c, i) => `${words.names[i]} ${sayPart(c)}`).join(", ")}`;
323
+ }
324
+ const k = p.parts.length;
325
+ const flat = p.parts.every((c) => typeof c === "string");
326
+ const how = p.cut === "rows"
327
+ ? `cut by lines across into ${k} rows`
328
+ : `cut by upright lines into ${k} ${pieces}`;
329
+ const size = p.sizes
330
+ ? p.sizes.every((v) => v === p.sizes[0])
331
+ ? " of equal size"
332
+ : ` whose ${p.cut === "rows" ? "heights" : "widths"} are in the ratio ${p.sizes.join(" : ")}`
333
+ : ` of equal ${p.cut === "rows" ? "height" : "width"}`;
334
+ const from = p.cut === "rows" ? "from the top" : "from the left";
335
+ const items = flat
336
+ ? runs(p.parts.map((c) => word(c)))
337
+ : p.parts.map(item).join(", then ");
338
+ return `${how}${size}; ${from}: ${items}`;
339
+ }
340
+ function sayFigure(f) {
341
+ if (f.shape === "circle") {
342
+ if (!f.parts)
343
+ return `a circle, all ${word(f.color ?? "white")}`;
344
+ const k = f.parts.length;
345
+ const sizes = f.sizes && !f.sizes.every((v) => v === f.sizes[0]);
346
+ return `a circle cut from its centre into ${k} ${sizes ? `sectors whose sizes are in the ratio ${f.sizes.join(" : ")}` : "equal sectors"}, the first starting at the top; going clockwise: ${runs(f.parts.map((c) => word(c)))}`;
347
+ }
348
+ const name = f.shape === "bar"
349
+ ? "a bar"
350
+ : f.shape === "square"
351
+ ? "a square"
352
+ : "a rectangle, wider than it is tall";
353
+ const root = rootOf(f);
354
+ if (typeof root === "string")
355
+ return `${name}, all ${word(root)}`;
356
+ return `${name}, ${sayPart(root, f.shape === "bar" ? "pieces" : "columns")}`;
357
+ }
358
+ /** The smallest part of a figure at its size: the narrowest side of a piece, or the narrowest sector, in pixels. */
359
+ function smallest(f, w, h) {
360
+ if (f.shape === "circle") {
361
+ if (!f.parts)
362
+ return w;
363
+ const sizes = f.sizes ?? f.parts.map(() => 1);
364
+ const total = sizes.reduce((s, v) => s + v, 0);
365
+ return (Math.min(...sizes) / total) * Math.PI * w;
366
+ }
367
+ let least = Number.POSITIVE_INFINITY;
368
+ regions(rootOf(f), 0, 0, w, h, (points) => {
369
+ const xs = points.map((p) => p.x);
370
+ const ys = points.map((p) => p.y);
371
+ const side = Math.min(Math.max(...xs) - Math.min(...xs), Math.max(...ys) - Math.min(...ys));
372
+ least = Math.min(least, points.length === 3 ? side / 2 : side);
373
+ });
374
+ return least;
375
+ }
376
+ export const fraction = {
377
+ kind: "fraction",
378
+ members: ["kind", "figures", "layout", "facts"],
379
+ check(scene, problems) {
380
+ only(scene, this.members, "scene", problems);
381
+ const before = problems.length;
382
+ const figures = list(scene.figures, "figures", problems, 1, 6);
383
+ figures.forEach((f, i) => {
384
+ const path = `figures[${i}]`;
385
+ if (!isObj(f)) {
386
+ problems.push(`${path}: must be a figure, as {"shape": "circle", "parts": ["red", "white"]}`);
387
+ return;
388
+ }
389
+ const shape = oneOf(f.shape, figureShapes, `${path}.shape`, problems);
390
+ const allowed = shape === "circle" || shape === "bar"
391
+ ? ["shape", "color", "parts", "sizes", "label"]
392
+ : ["shape", "color", "cut", "parts", "sizes", "grid", "label"];
393
+ only(f, allowed, path, problems);
394
+ if (f.label !== undefined)
395
+ text(f.label, `${path}.label`, problems, 3);
396
+ const given = ["color", "parts", "grid"].filter((m) => f[m] !== undefined);
397
+ if (given.length !== 1) {
398
+ problems.push(`${path}: give one of ${shape === "circle" || shape === "bar" ? "color and parts" : "color, parts and grid"}: a whole colour, the parts of a cut${shape === "circle" || shape === "bar" ? "" : " or a grid"}`);
399
+ return;
400
+ }
401
+ if (f.color !== undefined) {
402
+ if (!isColor(f.color))
403
+ problems.push(`${path}.color: must be a colour: ${colorNames.join(", ")}`);
404
+ for (const m of ["cut", "sizes"])
405
+ if (f[m] !== undefined)
406
+ problems.push(`${path}.${m}: a figure all of one colour has no ${m}`);
407
+ return;
408
+ }
409
+ if (f.grid !== undefined) {
410
+ for (const m of ["cut", "sizes"])
411
+ if (f[m] !== undefined)
412
+ problems.push(`${path}.${m}: a grid has no ${m}`);
413
+ checkGrid(f.grid, `${path}.grid`, problems, 1);
414
+ return;
415
+ }
416
+ if (shape === "circle") {
417
+ const parts = list(f.parts, `${path}.parts`, problems, 2, 16);
418
+ parts.forEach((c, j) => {
419
+ if (!isColor(c))
420
+ problems.push(`${path}.parts[${j}]: must be a colour: a sector is not cut again`);
421
+ });
422
+ if (f.sizes !== undefined) {
423
+ const values = list(f.sizes, `${path}.sizes`, problems, 0);
424
+ if (Array.isArray(f.sizes) && values.length !== parts.length)
425
+ problems.push(`${path}.sizes: must hold one size for each sector, ${parts.length}`);
426
+ values.forEach((v, j) => {
427
+ int(v, `${path}.sizes[${j}]`, problems, 1, 12);
428
+ });
429
+ }
430
+ return;
431
+ }
432
+ const cut = shape === "bar"
433
+ ? "columns"
434
+ : f.cut === undefined
435
+ ? "columns"
436
+ : oneOf(f.cut, cuts, `${path}.cut`, problems);
437
+ checkCut(cut, f.sizes, f.parts, path, problems, 1);
438
+ });
439
+ if (scene.layout !== undefined)
440
+ oneOf(scene.layout, layouts, "layout", problems);
441
+ const facts = scene.facts;
442
+ if (facts !== undefined) {
443
+ if (!isObj(facts))
444
+ problems.push('facts: must hold shares, as {"shares": [{"figure": 0, "color": "gray", "share": "3/8"}]}');
445
+ else {
446
+ only(facts, ["shares"], "facts", problems);
447
+ list(facts.shares, "facts.shares", problems, 1, 24).forEach((sh, k) => {
448
+ const at = `facts.shares[${k}]`;
449
+ if (!isObj(sh)) {
450
+ problems.push(`${at}: must be a figure, a colour and a share`);
451
+ return;
452
+ }
453
+ only(sh, ["figure", "color", "share"], at, problems);
454
+ int(sh.figure, `${at}.figure`, problems, 0, Math.max(0, figures.length - 1));
455
+ if (!isColor(sh.color))
456
+ problems.push(`${at}.color: must be a colour`);
457
+ if (typeof sh.share !== "string" || !/^\d+(\/[1-9]\d*)?$/.test(sh.share))
458
+ problems.push(`${at}.share: must be a fraction, as "3/8", or a whole number, as "1" or "0"`);
459
+ });
460
+ }
461
+ }
462
+ if (problems.length > before)
463
+ return;
464
+ const s = scene;
465
+ s.figures.forEach((f, i) => {
466
+ const k = f.shape === "circle" ? (f.parts?.length ?? 1) : leaves(rootOf(f)).length;
467
+ if (k > mostParts)
468
+ problems.push(`figures[${i}]: ${k} parts; at most ${mostParts}`);
469
+ });
470
+ if (problems.length > before)
471
+ return;
472
+ // A row is scaled down to fit the width; what that leaves too small is refused below.
473
+ const { laid } = place(s);
474
+ s.figures.forEach((f, i) => {
475
+ const l = laid[i];
476
+ const least = smallest(f, f.shape === "circle" ? l.w / 2 : l.w, l.h);
477
+ if (f.shape === "circle" ? least < 14 : least < 9)
478
+ problems.push(`figures[${i}]: a part is too small to see; cut it into fewer or larger parts`);
479
+ });
480
+ if (problems.length > before || !s.facts)
481
+ return;
482
+ s.facts.shares.forEach((sh, k) => {
483
+ const got = shareOf(s.figures[sh.figure], sh.color);
484
+ const [a, b = "1"] = sh.share.split("/");
485
+ const want = sayQ(q(Number(a), Number(b)));
486
+ if (got !== want)
487
+ problems.push(`facts.shares[${k}].share: ${word(sh.color)} takes ${got} of figures[${sh.figure}], not ${sh.share}`);
488
+ });
489
+ },
490
+ draw(scene, pen) {
491
+ const s = scene;
492
+ const { laid, width, height } = place(s);
493
+ const column = (s.layout ?? "row") === "column";
494
+ s.figures.forEach((f, i) => {
495
+ const l = laid[i];
496
+ if (f.shape === "circle") {
497
+ const r = l.w / 2;
498
+ const c = { x: l.x + r, y: l.y + r };
499
+ if (!f.parts) {
500
+ pen.el("path", {
501
+ class: "part",
502
+ d: circlePath(c, r),
503
+ fill: colors[f.color ?? "white"].fill,
504
+ });
505
+ }
506
+ else {
507
+ const sizes = f.sizes ?? f.parts.map(() => 1);
508
+ const total = sizes.reduce((sum, v) => sum + v, 0);
509
+ let at = 0;
510
+ f.parts.forEach((color, j) => {
511
+ const a0 = (at / total) * 2 * Math.PI;
512
+ at += sizes[j];
513
+ const a1 = (at / total) * 2 * Math.PI;
514
+ pen.el("path", {
515
+ class: "part",
516
+ d: sectorPath(c, r, a0, a1),
517
+ fill: colors[color].fill,
518
+ stroke: ink,
519
+ "stroke-width": 2,
520
+ "stroke-linejoin": "round",
521
+ });
522
+ });
523
+ }
524
+ pen.el("path", { d: circlePath(c, r), fill: "none", stroke: ink, "stroke-width": outline });
525
+ }
526
+ else {
527
+ regions(rootOf(f), l.x, l.y, l.w, l.h, (points, color) => {
528
+ pen.el("path", {
529
+ class: "part",
530
+ d: poly(points),
531
+ fill: colors[color].fill,
532
+ stroke: ink,
533
+ "stroke-width": 2,
534
+ "stroke-linejoin": "round",
535
+ });
536
+ });
537
+ pen.el("rect", {
538
+ x: l.x,
539
+ y: l.y,
540
+ width: l.w,
541
+ height: l.h,
542
+ fill: "none",
543
+ stroke: ink,
544
+ "stroke-width": outline,
545
+ "stroke-linejoin": "round",
546
+ });
547
+ }
548
+ if (!f.label)
549
+ return;
550
+ if (column)
551
+ pen.text(margin + 10, l.y + l.h / 2, f.label, { size: labelSize });
552
+ else
553
+ pen.text(l.x + l.w / 2, height - margin - 14, f.label, { size: labelSize });
554
+ });
555
+ return { width, height };
556
+ },
557
+ describe(scene) {
558
+ const s = scene;
559
+ if (s.figures.length === 1) {
560
+ const f = s.figures[0];
561
+ return `${capital(sayFigure(f))}${f.label ? `, labelled ${f.label}` : ""}.`;
562
+ }
563
+ const column = (s.layout ?? "row") === "column";
564
+ const intro = `${s.figures.length} pictures, ${column ? "one under another, from the top" : "in a row, from the left"}.`;
565
+ const each = s.figures.map((f, i) => `${f.label ? `Picture ${f.label}` : `Picture ${i + 1}`}: ${sayFigure(f)}.`);
566
+ return [intro, ...each].join(" ");
567
+ },
568
+ };
@@ -0,0 +1,53 @@
1
+ import type { Kind } from "../kind.ts";
2
+ import { type ColorName } from "../style.ts";
3
+ import { type Thing } from "../things.ts";
4
+ /**
5
+ * Gauges: measuring instruments, one to five of one sort side by side — a
6
+ * ruler, whole or a broken piece of one, with things laid along it; a
7
+ * thermometer; a kitchen scale with a dial and things on its pan; a
8
+ * measuring jug. An instrument's scale runs from one number to another: a
9
+ * number every step and a mark every tick. The level, the pointer or the end
10
+ * of a thing stands exactly on a mark, so the picture can be read as exactly
11
+ * as the scene says; the reading itself is never written.
12
+ */
13
+ export declare const instruments: readonly ["ruler", "thermometer", "scale", "jug"];
14
+ export type Instrument = (typeof instruments)[number];
15
+ export declare const objectNames: readonly ["pencil", "crayon", "ribbon", "stick", "nail", "caterpillar"];
16
+ export type ObjectName = (typeof objectNames)[number];
17
+ export declare const breaks: readonly ["left", "right", "both"];
18
+ export type Laid = {
19
+ object: ObjectName;
20
+ from: number;
21
+ to: number;
22
+ color?: ColorName;
23
+ label?: string;
24
+ };
25
+ export type Gauge = {
26
+ from: number;
27
+ to: number;
28
+ step?: number;
29
+ tick?: number;
30
+ unit?: string;
31
+ value?: number;
32
+ label?: string;
33
+ color?: ColorName;
34
+ things?: Thing[];
35
+ objects?: Laid[];
36
+ broken?: (typeof breaks)[number];
37
+ };
38
+ export type GaugesScene = {
39
+ kind: "gauges";
40
+ instrument: Instrument;
41
+ gauges: Gauge[];
42
+ };
43
+ /** fmt writes a number as the picture shows it, a true minus sign before a negative one. */
44
+ export declare function fmt(v: number): string;
45
+ /** marks lists the marks of a scale from its first number up: big where a number stands. */
46
+ export declare function marks(g: Gauge): {
47
+ v: number;
48
+ big: boolean;
49
+ mid: boolean;
50
+ }[];
51
+ /** turn is the angle of a value on the dial, in degrees clockwise from the top. */
52
+ export declare function turn(g: Gauge, v: number): number;
53
+ export declare const gauges: Kind;