@altnovatrail/picture-kit 0.0.0-stage → 0.1.1
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.
- package/LICENSE +201 -0
- package/NOTICE +7 -0
- package/README.md +28 -2
- package/lib/emoji.d.ts +16 -0
- package/lib/emoji.js +64 -0
- package/lib/index.d.ts +9 -0
- package/lib/index.js +10 -0
- package/lib/kind.d.ts +22 -0
- package/lib/kind.js +2 -0
- package/lib/kinds/balance.d.ts +34 -0
- package/lib/kinds/balance.js +217 -0
- package/lib/kinds/branches.d.ts +33 -0
- package/lib/kinds/branches.js +473 -0
- package/lib/kinds/calendar.d.ts +55 -0
- package/lib/kinds/calendar.js +532 -0
- package/lib/kinds/chart.d.ts +48 -0
- package/lib/kinds/chart.js +645 -0
- package/lib/kinds/choices.d.ts +14 -0
- package/lib/kinds/choices.js +53 -0
- package/lib/kinds/clock.d.ts +36 -0
- package/lib/kinds/clock.js +219 -0
- package/lib/kinds/coins.d.ts +37 -0
- package/lib/kinds/coins.js +550 -0
- package/lib/kinds/column.d.ts +37 -0
- package/lib/kinds/column.js +410 -0
- package/lib/kinds/commands-parts.d.ts +12 -0
- package/lib/kinds/commands-parts.js +24 -0
- package/lib/kinds/commands.d.ts +66 -0
- package/lib/kinds/commands.js +789 -0
- package/lib/kinds/containers.d.ts +52 -0
- package/lib/kinds/containers.js +758 -0
- package/lib/kinds/countfig.d.ts +41 -0
- package/lib/kinds/countfig.js +372 -0
- package/lib/kinds/cubes.d.ts +70 -0
- package/lib/kinds/cubes.js +371 -0
- package/lib/kinds/die.d.ts +32 -0
- package/lib/kinds/die.js +301 -0
- package/lib/kinds/domino.d.ts +37 -0
- package/lib/kinds/domino.js +450 -0
- package/lib/kinds/dotgrid.d.ts +38 -0
- package/lib/kinds/dotgrid.js +411 -0
- package/lib/kinds/figure.d.ts +62 -0
- package/lib/kinds/figure.js +687 -0
- package/lib/kinds/fold-parts.d.ts +73 -0
- package/lib/kinds/fold-parts.js +315 -0
- package/lib/kinds/fold.d.ts +52 -0
- package/lib/kinds/fold.js +650 -0
- package/lib/kinds/fraction.d.ts +50 -0
- package/lib/kinds/fraction.js +568 -0
- package/lib/kinds/gauges.d.ts +53 -0
- package/lib/kinds/gauges.js +880 -0
- package/lib/kinds/gears.d.ts +40 -0
- package/lib/kinds/gears.js +787 -0
- package/lib/kinds/grid.d.ts +45 -0
- package/lib/kinds/grid.js +591 -0
- package/lib/kinds/groups.d.ts +23 -0
- package/lib/kinds/groups.js +335 -0
- package/lib/kinds/index.d.ts +3 -0
- package/lib/kinds/index.js +87 -0
- package/lib/kinds/lanes.d.ts +36 -0
- package/lib/kinds/lanes.js +220 -0
- package/lib/kinds/layers.d.ts +31 -0
- package/lib/kinds/layers.js +419 -0
- package/lib/kinds/line.d.ts +38 -0
- package/lib/kinds/line.js +739 -0
- package/lib/kinds/magic-parts.d.ts +72 -0
- package/lib/kinds/magic-parts.js +186 -0
- package/lib/kinds/magic.d.ts +66 -0
- package/lib/kinds/magic.js +664 -0
- package/lib/kinds/map.d.ts +32 -0
- package/lib/kinds/map.js +370 -0
- package/lib/kinds/matchsticks.d.ts +26 -0
- package/lib/kinds/matchsticks.js +434 -0
- package/lib/kinds/necklace.d.ts +27 -0
- package/lib/kinds/necklace.js +342 -0
- package/lib/kinds/net.d.ts +47 -0
- package/lib/kinds/net.js +241 -0
- package/lib/kinds/overlay.d.ts +50 -0
- package/lib/kinds/overlay.js +519 -0
- package/lib/kinds/pieces.d.ts +41 -0
- package/lib/kinds/pieces.js +420 -0
- package/lib/kinds/pipes.d.ts +46 -0
- package/lib/kinds/pipes.js +657 -0
- package/lib/kinds/plane-parts.d.ts +105 -0
- package/lib/kinds/plane-parts.js +372 -0
- package/lib/kinds/plane.d.ts +35 -0
- package/lib/kinds/plane.js +755 -0
- package/lib/kinds/pyramid.d.ts +26 -0
- package/lib/kinds/pyramid.js +228 -0
- package/lib/kinds/ropes.d.ts +30 -0
- package/lib/kinds/ropes.js +571 -0
- package/lib/kinds/shapes.d.ts +59 -0
- package/lib/kinds/shapes.js +490 -0
- package/lib/kinds/solids.d.ts +78 -0
- package/lib/kinds/solids.js +1058 -0
- package/lib/kinds/tangram.d.ts +52 -0
- package/lib/kinds/tangram.js +713 -0
- package/lib/kinds/target.d.ts +39 -0
- package/lib/kinds/target.js +761 -0
- package/lib/kinds/tiles.d.ts +55 -0
- package/lib/kinds/tiles.js +606 -0
- package/lib/kinds/venn.d.ts +38 -0
- package/lib/kinds/venn.js +710 -0
- package/lib/noto.d.ts +2 -0
- package/lib/noto.js +212 -0
- package/lib/objects.d.ts +7 -0
- package/lib/objects.js +226 -0
- package/lib/pen.d.ts +32 -0
- package/lib/pen.js +60 -0
- package/lib/read.d.ts +9 -0
- package/lib/read.js +49 -0
- package/lib/render.d.ts +24 -0
- package/lib/render.js +68 -0
- package/lib/solve/balance.d.ts +46 -0
- package/lib/solve/balance.js +118 -0
- package/lib/style.d.ts +70 -0
- package/lib/style.js +39 -0
- package/lib/svg.d.ts +22 -0
- package/lib/svg.js +48 -0
- package/lib/things.d.ts +40 -0
- package/lib/things.js +269 -0
- package/package.json +28 -4
|
@@ -0,0 +1,739 @@
|
|
|
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 { colors, faint, ink, isColor, outline, textOn } from "../style.js";
|
|
5
|
+
import { n, poly } from "../svg.js";
|
|
6
|
+
import { checkThing, drawThing, wordOf } from "../things.js";
|
|
7
|
+
/**
|
|
8
|
+
* A line: points along a line and the gaps between them, in one of six
|
|
9
|
+
* styles — a fence of posts joined by rails, things in a row, a log with cut
|
|
10
|
+
* lines, a number line with ticks, stairs with steps, a strip with holes. The
|
|
11
|
+
* picture draws exactly as many posts, things, cuts, ticks, steps or holes as
|
|
12
|
+
* the scene counts, each in a place of its own; labels go on the points, on
|
|
13
|
+
* the gaps between them and on the whole length, and "?" marks the asked one.
|
|
14
|
+
*/
|
|
15
|
+
export const lineStyles = ["fence", "row", "log", "numbers", "stairs", "belt"];
|
|
16
|
+
/** The members each style takes besides kind and style. */
|
|
17
|
+
const styleMembers = {
|
|
18
|
+
fence: ["count", "labels", "gaps", "lengths", "total", "color"],
|
|
19
|
+
row: ["things", "labels", "gaps", "lengths", "total", "jumps"],
|
|
20
|
+
log: ["count", "gaps", "lengths", "total", "color"],
|
|
21
|
+
numbers: ["count", "start", "by", "labels", "jumps", "marks"],
|
|
22
|
+
stairs: ["count", "labels", "marks", "color"],
|
|
23
|
+
belt: ["count", "labels", "gaps", "lengths", "total", "color"],
|
|
24
|
+
};
|
|
25
|
+
/** What count counts in each style, and its limits. */
|
|
26
|
+
const countOf = {
|
|
27
|
+
fence: { word: "posts", least: 2, most: 16 },
|
|
28
|
+
log: { word: "cut lines", least: 1, most: 12 },
|
|
29
|
+
numbers: { word: "ticks", least: 2, most: 21 },
|
|
30
|
+
stairs: { word: "steps", least: 2, most: 10 },
|
|
31
|
+
belt: { word: "holes", least: 2, most: 14 },
|
|
32
|
+
};
|
|
33
|
+
const rowMost = 12;
|
|
34
|
+
const defaultColor = {
|
|
35
|
+
fence: "brown",
|
|
36
|
+
row: "green",
|
|
37
|
+
log: "brown",
|
|
38
|
+
numbers: "blue",
|
|
39
|
+
stairs: "orange",
|
|
40
|
+
belt: "yellow",
|
|
41
|
+
};
|
|
42
|
+
/** Room for a gap, in pixels: what it is drawn at, at least, and the longest line. */
|
|
43
|
+
const room = {
|
|
44
|
+
fence: { prefer: 72, least: 28, most: 560 },
|
|
45
|
+
row: { prefer: 96, least: 50, most: 572 },
|
|
46
|
+
log: { prefer: 96, least: 40, most: 560 },
|
|
47
|
+
belt: { prefer: 70, least: 30, most: 540 },
|
|
48
|
+
};
|
|
49
|
+
const thingSize = 46;
|
|
50
|
+
const labelMost = 6;
|
|
51
|
+
/** points is how many points the line has: posts, things, cuts, ticks, steps or holes. */
|
|
52
|
+
function points(scene) {
|
|
53
|
+
return scene.style === "row" ? (scene.things?.length ?? 0) : (scene.count ?? 0);
|
|
54
|
+
}
|
|
55
|
+
/** gapCount is how many gaps can be labelled: a log's pieces are one more than its cuts. */
|
|
56
|
+
function gapCount(scene) {
|
|
57
|
+
if (scene.style === "stairs" || scene.style === "numbers")
|
|
58
|
+
return 0;
|
|
59
|
+
return scene.style === "log" ? points(scene) + 1 : points(scene) - 1;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* spread places the boundaries of the gaps along the line, from 0, each gap
|
|
63
|
+
* as long as its length says; undefined when the shortest gap would be too
|
|
64
|
+
* short to draw beside the longest.
|
|
65
|
+
*/
|
|
66
|
+
function spread(style, lengths, gaps = []) {
|
|
67
|
+
const r = room[style];
|
|
68
|
+
// A gap that carries a label is at least as wide as the widest label.
|
|
69
|
+
const label = Math.max(0, ...gaps.map((g) => (g === "" ? 0 : textWidth(g, 16) + 22)));
|
|
70
|
+
const need = Math.max(r.least, label);
|
|
71
|
+
const sum = lengths.reduce((a, b) => a + b, 0);
|
|
72
|
+
const least = Math.min(...lengths);
|
|
73
|
+
let span = Math.min(r.most, Math.max(r.prefer, label + 16) * lengths.length);
|
|
74
|
+
if ((span * least) / sum < need)
|
|
75
|
+
span = Math.min(r.most, (need * sum) / least);
|
|
76
|
+
if ((span * least) / sum < r.least - 0.01)
|
|
77
|
+
return undefined;
|
|
78
|
+
const xs = [0];
|
|
79
|
+
let at = 0;
|
|
80
|
+
for (const l of lengths) {
|
|
81
|
+
at += l;
|
|
82
|
+
xs.push((span * at) / sum);
|
|
83
|
+
}
|
|
84
|
+
return xs;
|
|
85
|
+
}
|
|
86
|
+
function lengthsOf(scene) {
|
|
87
|
+
return scene.lengths ?? Array.from({ length: gapCount(scene) }, () => 1);
|
|
88
|
+
}
|
|
89
|
+
/** valueAt is the number a tick of a number line stands for. */
|
|
90
|
+
function valueAt(scene, i) {
|
|
91
|
+
return (scene.start ?? 0) + i * (scene.by ?? 1);
|
|
92
|
+
}
|
|
93
|
+
/** tickOf is the place of a number on a number line, or -1 when no tick has it. */
|
|
94
|
+
function tickOf(scene, value) {
|
|
95
|
+
const by = scene.by ?? 1;
|
|
96
|
+
const d = value - (scene.start ?? 0);
|
|
97
|
+
if (d < 0 || d % by !== 0)
|
|
98
|
+
return -1;
|
|
99
|
+
const i = d / by;
|
|
100
|
+
return i < points(scene) ? i : -1;
|
|
101
|
+
}
|
|
102
|
+
function checkLabels(value, path, how, most, problems) {
|
|
103
|
+
const items = list(value, path, problems, how, how);
|
|
104
|
+
if (Array.isArray(value) && value.length !== how)
|
|
105
|
+
problems.push(`${path}: must hold exactly ${how}, one for each, "" for none`);
|
|
106
|
+
items.forEach((l, i) => {
|
|
107
|
+
if (l === "")
|
|
108
|
+
return;
|
|
109
|
+
text(l, `${path}[${i}]`, problems, most);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
// --- drawing helpers ---------------------------------------------------------
|
|
113
|
+
/** tag writes a label on a white ground; "?" stands in a dashed box. */
|
|
114
|
+
function tag(pen, x, y, label, size = 16) {
|
|
115
|
+
const ask = label === "?";
|
|
116
|
+
const w = Math.max(ask ? 26 : 0, textWidth(label, size) + 10);
|
|
117
|
+
pen.el("rect", {
|
|
118
|
+
x: x - w / 2,
|
|
119
|
+
y: y - 13,
|
|
120
|
+
width: w,
|
|
121
|
+
height: 26,
|
|
122
|
+
rx: 7,
|
|
123
|
+
fill: "#ffffff",
|
|
124
|
+
stroke: ask ? ink : "none",
|
|
125
|
+
"stroke-width": ask ? 2 : undefined,
|
|
126
|
+
"stroke-dasharray": ask ? "4 3" : undefined,
|
|
127
|
+
});
|
|
128
|
+
pen.text(x, y, label, { size });
|
|
129
|
+
}
|
|
130
|
+
/** dim draws a measure from x1 to x2 at y, with its label on the middle of it. */
|
|
131
|
+
function dim(pen, a, b, y, label) {
|
|
132
|
+
// Neighbouring measures keep apart, so that each shows its own two ends.
|
|
133
|
+
const x1 = a + 2;
|
|
134
|
+
const x2 = b - 2;
|
|
135
|
+
pen.el("path", {
|
|
136
|
+
d: `M${n(x1)} ${n(y - 7)}V${n(y + 7)}M${n(x2)} ${n(y - 7)}V${n(y + 7)}M${n(x1)} ${n(y)}H${n(x2)}`,
|
|
137
|
+
fill: "none",
|
|
138
|
+
stroke: ink,
|
|
139
|
+
"stroke-width": 1.8,
|
|
140
|
+
class: "measure",
|
|
141
|
+
});
|
|
142
|
+
if (label)
|
|
143
|
+
tag(pen, (x1 + x2) / 2, y, label);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* dims draws the gaps' measures below a line, on two rows when a label is
|
|
147
|
+
* wider than its gap, and the whole length under them; it returns the y
|
|
148
|
+
* below the last.
|
|
149
|
+
*/
|
|
150
|
+
function dims(pen, xs, gaps, total, y) {
|
|
151
|
+
let bottom = y;
|
|
152
|
+
if (gaps?.some((g) => g !== "")) {
|
|
153
|
+
const tight = gaps.some((g, i) => g !== "" && textWidth(g, 16) + 16 > xs[i + 1] - xs[i] - 4);
|
|
154
|
+
gaps.forEach((g, i) => {
|
|
155
|
+
if (g === "")
|
|
156
|
+
return;
|
|
157
|
+
dim(pen, xs[i], xs[i + 1], y + 14 + (tight && i % 2 === 1 ? 30 : 0), g);
|
|
158
|
+
});
|
|
159
|
+
bottom = y + 30 + (tight ? 30 : 0);
|
|
160
|
+
}
|
|
161
|
+
if (total) {
|
|
162
|
+
dim(pen, total.from, total.to, bottom + 16, total.label);
|
|
163
|
+
bottom += 34;
|
|
164
|
+
}
|
|
165
|
+
return bottom;
|
|
166
|
+
}
|
|
167
|
+
/** arrowHead is a filled head with its tip at x, y, pointing along dx, dy. */
|
|
168
|
+
function arrowHead(pen, x, y, dx, dy, size = 11) {
|
|
169
|
+
const l = Math.hypot(dx, dy) || 1;
|
|
170
|
+
const ux = dx / l;
|
|
171
|
+
const uy = dy / l;
|
|
172
|
+
pen.el("path", {
|
|
173
|
+
d: poly([
|
|
174
|
+
{ x, y },
|
|
175
|
+
{ x: x - ux * size - uy * size * 0.55, y: y - uy * size + ux * size * 0.55 },
|
|
176
|
+
{ x: x - ux * size + uy * size * 0.55, y: y - uy * size - ux * size * 0.55 },
|
|
177
|
+
]),
|
|
178
|
+
fill: ink,
|
|
179
|
+
stroke: ink,
|
|
180
|
+
"stroke-width": 1.5,
|
|
181
|
+
"stroke-linejoin": "round",
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
/** arc draws a jump from x1 to x2, rising h above y (or falling, h < 0), its head at x2. */
|
|
185
|
+
function arc(pen, x1, x2, y, h, label) {
|
|
186
|
+
const dx = x2 - x1;
|
|
187
|
+
const k = h * 1.33;
|
|
188
|
+
const c1 = { x: x1 + dx * 0.12, y: y - k };
|
|
189
|
+
const c2 = { x: x2 - dx * 0.12, y: y - k };
|
|
190
|
+
pen.el("path", {
|
|
191
|
+
d: `M${n(x1)} ${n(y)}C${n(c1.x)} ${n(c1.y)} ${n(c2.x)} ${n(c2.y)} ${n(x2)} ${n(y)}`,
|
|
192
|
+
fill: "none",
|
|
193
|
+
stroke: ink,
|
|
194
|
+
"stroke-width": 2.5,
|
|
195
|
+
"stroke-linecap": "round",
|
|
196
|
+
class: "jump",
|
|
197
|
+
});
|
|
198
|
+
arrowHead(pen, x2, y, x2 - c2.x, y - c2.y);
|
|
199
|
+
if (label)
|
|
200
|
+
tag(pen, (x1 + x2) / 2, y - h - (h > 0 ? 14 : -14), label);
|
|
201
|
+
}
|
|
202
|
+
function arcHeight(dx) {
|
|
203
|
+
return Math.min(70, 22 + Math.abs(dx) * 0.18);
|
|
204
|
+
}
|
|
205
|
+
function drawFence(scene, pen) {
|
|
206
|
+
const xs0 = spread("fence", lengthsOf(scene), scene.gaps);
|
|
207
|
+
const left = margin + 30;
|
|
208
|
+
const xs = xs0.map((x) => left + x);
|
|
209
|
+
const c = colors[scene.color ?? defaultColor.fence];
|
|
210
|
+
const top = margin + (scene.labels?.some((l) => l !== "") ? 32 : 4);
|
|
211
|
+
const postW = 14;
|
|
212
|
+
const postH = 82;
|
|
213
|
+
const ground = top + postH;
|
|
214
|
+
pen.el("path", {
|
|
215
|
+
d: `M${n(xs[0] - 26)} ${n(ground)}H${n(xs.at(-1) + 26)}`,
|
|
216
|
+
stroke: colors.green.fill,
|
|
217
|
+
"stroke-width": 5,
|
|
218
|
+
"stroke-linecap": "round",
|
|
219
|
+
});
|
|
220
|
+
for (let i = 0; i + 1 < xs.length; i++) {
|
|
221
|
+
for (const y of [top + 22, top + 52]) {
|
|
222
|
+
pen.el("rect", {
|
|
223
|
+
x: xs[i],
|
|
224
|
+
y,
|
|
225
|
+
width: xs[i + 1] - xs[i],
|
|
226
|
+
height: 10,
|
|
227
|
+
fill: c.fill,
|
|
228
|
+
stroke: ink,
|
|
229
|
+
"stroke-width": outline,
|
|
230
|
+
class: "rail",
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
xs.forEach((x, i) => {
|
|
235
|
+
pen.el("path", {
|
|
236
|
+
d: poly([
|
|
237
|
+
{ x: x - postW / 2, y: ground },
|
|
238
|
+
{ x: x - postW / 2, y: top + 8 },
|
|
239
|
+
{ x, y: top },
|
|
240
|
+
{ x: x + postW / 2, y: top + 8 },
|
|
241
|
+
{ x: x + postW / 2, y: ground },
|
|
242
|
+
]),
|
|
243
|
+
fill: c.dark,
|
|
244
|
+
stroke: ink,
|
|
245
|
+
"stroke-width": outline,
|
|
246
|
+
"stroke-linejoin": "round",
|
|
247
|
+
class: "post",
|
|
248
|
+
});
|
|
249
|
+
const label = scene.labels?.[i];
|
|
250
|
+
if (label)
|
|
251
|
+
tag(pen, x, top - 18, label);
|
|
252
|
+
});
|
|
253
|
+
const total = scene.total ? { from: xs[0], to: xs.at(-1), label: scene.total } : undefined;
|
|
254
|
+
const bottom = dims(pen, xs, scene.gaps, total, ground + 6);
|
|
255
|
+
return { width: xs.at(-1) + 30 + margin, height: bottom + margin };
|
|
256
|
+
}
|
|
257
|
+
function drawRow(scene, pen) {
|
|
258
|
+
const xs0 = spread("row", lengthsOf(scene), scene.gaps);
|
|
259
|
+
const left = margin + 30;
|
|
260
|
+
const xs = xs0.map((x) => left + x);
|
|
261
|
+
const jumps = scene.jumps ?? [];
|
|
262
|
+
const lift = Math.max(0, ...jumps.map((j) => arcHeight(xs[j.to - 1] - xs[j.from - 1])));
|
|
263
|
+
const top = margin + (jumps.length > 0 ? lift + 30 : 0);
|
|
264
|
+
const cy = top + thingSize / 2;
|
|
265
|
+
const ground = top + thingSize + 2;
|
|
266
|
+
pen.el("path", {
|
|
267
|
+
d: `M${n(xs[0] - 30)} ${n(ground)}H${n(xs.at(-1) + 30)}`,
|
|
268
|
+
stroke: faint,
|
|
269
|
+
"stroke-width": 3,
|
|
270
|
+
"stroke-linecap": "round",
|
|
271
|
+
});
|
|
272
|
+
scene.things.forEach((t, i) => {
|
|
273
|
+
drawThing(pen, t, xs[i], cy, thingSize);
|
|
274
|
+
});
|
|
275
|
+
for (const j of jumps) {
|
|
276
|
+
const x1 = xs[j.from - 1];
|
|
277
|
+
const x2 = xs[j.to - 1];
|
|
278
|
+
arc(pen, x1 + (x2 > x1 ? 6 : -6), x2 + (x2 > x1 ? -6 : 6), top - 2, arcHeight(x2 - x1), j.label);
|
|
279
|
+
}
|
|
280
|
+
let y = ground;
|
|
281
|
+
if (scene.labels?.some((l) => l !== "")) {
|
|
282
|
+
scene.labels.forEach((l, i) => {
|
|
283
|
+
if (l)
|
|
284
|
+
tag(pen, xs[i], ground + 18, l);
|
|
285
|
+
});
|
|
286
|
+
y += 32;
|
|
287
|
+
}
|
|
288
|
+
const total = scene.total ? { from: xs[0], to: xs.at(-1), label: scene.total } : undefined;
|
|
289
|
+
const bottom = dims(pen, xs, scene.gaps, total, y + 4);
|
|
290
|
+
return { width: xs.at(-1) + 30 + margin, height: bottom + margin };
|
|
291
|
+
}
|
|
292
|
+
function drawLog(scene, pen) {
|
|
293
|
+
const xs0 = spread("log", lengthsOf(scene), scene.gaps);
|
|
294
|
+
const left = margin + 14;
|
|
295
|
+
const xs = xs0.map((x) => left + x);
|
|
296
|
+
const c = colors[scene.color ?? defaultColor.log];
|
|
297
|
+
const top = margin + 14;
|
|
298
|
+
const h = 56;
|
|
299
|
+
const x0 = xs[0];
|
|
300
|
+
const x1 = xs.at(-1);
|
|
301
|
+
pen.el("path", {
|
|
302
|
+
d: `M${n(x1)} ${n(top)}H${n(x0 + 12)}Q${n(x0)} ${n(top)} ${n(x0)} ${n(top + 12)}V${n(top + h - 12)}Q${n(x0)} ${n(top + h)} ${n(x0 + 12)} ${n(top + h)}H${n(x1)}`,
|
|
303
|
+
fill: c.fill,
|
|
304
|
+
stroke: ink,
|
|
305
|
+
"stroke-width": outline,
|
|
306
|
+
"stroke-linejoin": "round",
|
|
307
|
+
});
|
|
308
|
+
// Bark: a few short strokes in every piece, the same for the same scene.
|
|
309
|
+
for (let i = 0; i + 1 < xs.length; i++) {
|
|
310
|
+
const a = xs[i];
|
|
311
|
+
const b = xs[i + 1];
|
|
312
|
+
const w = b - a;
|
|
313
|
+
for (const [fx, fy, fl] of [
|
|
314
|
+
[0.2, 0.3, 0.35],
|
|
315
|
+
[0.45, 0.68, 0.4],
|
|
316
|
+
[0.15, 0.8, 0.2],
|
|
317
|
+
]) {
|
|
318
|
+
const sx = a + w * fx;
|
|
319
|
+
pen.el("path", {
|
|
320
|
+
d: `M${n(sx)} ${n(top + h * fy)}H${n(Math.min(b - 12, sx + Math.min(60, w * fl)))}`,
|
|
321
|
+
stroke: c.dark,
|
|
322
|
+
"stroke-width": 3,
|
|
323
|
+
"stroke-linecap": "round",
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
pen.el("ellipse", {
|
|
328
|
+
cx: x1,
|
|
329
|
+
cy: top + h / 2,
|
|
330
|
+
rx: 13,
|
|
331
|
+
ry: h / 2,
|
|
332
|
+
fill: colors.yellow.fill,
|
|
333
|
+
stroke: ink,
|
|
334
|
+
"stroke-width": outline,
|
|
335
|
+
});
|
|
336
|
+
pen.el("ellipse", {
|
|
337
|
+
cx: x1,
|
|
338
|
+
cy: top + h / 2,
|
|
339
|
+
rx: 6,
|
|
340
|
+
ry: h / 4,
|
|
341
|
+
fill: "none",
|
|
342
|
+
stroke: c.dark,
|
|
343
|
+
"stroke-width": 2,
|
|
344
|
+
});
|
|
345
|
+
for (let i = 1; i + 1 < xs.length; i++) {
|
|
346
|
+
pen.el("path", {
|
|
347
|
+
d: `M${n(xs[i])} ${n(top - 12)}V${n(top + h + 12)}`,
|
|
348
|
+
stroke: colors.red.fill,
|
|
349
|
+
"stroke-width": 4,
|
|
350
|
+
"stroke-dasharray": "7 5",
|
|
351
|
+
"stroke-linecap": "round",
|
|
352
|
+
class: "cut",
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
const total = scene.total ? { from: x0, to: x1 + 13, label: scene.total } : undefined;
|
|
356
|
+
const ends = [...xs];
|
|
357
|
+
ends[ends.length - 1] = x1 + 13;
|
|
358
|
+
const bottom = dims(pen, ends, scene.gaps, total, top + h + 14);
|
|
359
|
+
return { width: x1 + 13 + 14 + margin, height: bottom + margin };
|
|
360
|
+
}
|
|
361
|
+
function drawBelt(scene, pen) {
|
|
362
|
+
const xs0 = spread("belt", lengthsOf(scene), scene.gaps);
|
|
363
|
+
const left = margin + 40;
|
|
364
|
+
const xs = xs0.map((x) => left + x);
|
|
365
|
+
const c = colors[scene.color ?? defaultColor.belt];
|
|
366
|
+
const top = margin + (scene.labels?.some((l) => l !== "") ? 32 : 2);
|
|
367
|
+
const h = 46;
|
|
368
|
+
const a = xs[0] - 36;
|
|
369
|
+
const b = xs.at(-1) + 36;
|
|
370
|
+
pen.el("rect", {
|
|
371
|
+
x: a,
|
|
372
|
+
y: top,
|
|
373
|
+
width: b - a,
|
|
374
|
+
height: h,
|
|
375
|
+
rx: 10,
|
|
376
|
+
fill: c.fill,
|
|
377
|
+
stroke: ink,
|
|
378
|
+
"stroke-width": outline,
|
|
379
|
+
});
|
|
380
|
+
for (const y of [top + 7, top + h - 7])
|
|
381
|
+
pen.el("path", {
|
|
382
|
+
d: `M${n(a + 9)} ${n(y)}H${n(b - 9)}`,
|
|
383
|
+
stroke: c.dark,
|
|
384
|
+
"stroke-width": 2,
|
|
385
|
+
"stroke-dasharray": "6 5",
|
|
386
|
+
});
|
|
387
|
+
xs.forEach((x, i) => {
|
|
388
|
+
pen.el("circle", {
|
|
389
|
+
cx: x,
|
|
390
|
+
cy: top + h / 2,
|
|
391
|
+
r: 7.5,
|
|
392
|
+
fill: "#ffffff",
|
|
393
|
+
stroke: ink,
|
|
394
|
+
"stroke-width": outline,
|
|
395
|
+
class: "hole",
|
|
396
|
+
});
|
|
397
|
+
const label = scene.labels?.[i];
|
|
398
|
+
if (label)
|
|
399
|
+
tag(pen, x, top - 18, label);
|
|
400
|
+
});
|
|
401
|
+
const total = scene.total ? { from: xs[0], to: xs.at(-1), label: scene.total } : undefined;
|
|
402
|
+
const bottom = dims(pen, xs, scene.gaps, total, top + h + 8);
|
|
403
|
+
return { width: b + margin, height: bottom + margin };
|
|
404
|
+
}
|
|
405
|
+
function drawNumbers(scene, pen) {
|
|
406
|
+
const p = points(scene);
|
|
407
|
+
const step = Math.min(56, 560 / (p - 1));
|
|
408
|
+
const left = margin + 24;
|
|
409
|
+
const xs = Array.from({ length: p }, (_, i) => left + i * step);
|
|
410
|
+
const jumps = scene.jumps ?? [];
|
|
411
|
+
const marks = scene.marks ?? [];
|
|
412
|
+
const at = (v) => xs[tickOf(scene, v)];
|
|
413
|
+
const ahead = jumps.filter((j) => j.to > j.from);
|
|
414
|
+
const back = jumps.filter((j) => j.to < j.from);
|
|
415
|
+
const markRoom = marks.length > 0 ? 44 : 0;
|
|
416
|
+
// A jump rises over any thing that stands on the line.
|
|
417
|
+
const rise = (dx) => Math.max(arcHeight(dx), markRoom + 10);
|
|
418
|
+
const lift = Math.max(markRoom, ...ahead.map((j) => rise(at(j.to) - at(j.from))));
|
|
419
|
+
const y = margin + (ahead.length > 0 ? lift + 30 : markRoom) + 8;
|
|
420
|
+
const labels = Array.from({ length: p }, (_, i) => scene.labels?.[i] ?? String(valueAt(scene, i)));
|
|
421
|
+
const widest = Math.max(...labels.map((l) => [...l].length));
|
|
422
|
+
const size = Math.max(14, Math.min(18, (step - 6) / (widest * 0.6)));
|
|
423
|
+
pen.el("path", {
|
|
424
|
+
d: `M${n(xs[0] - 18)} ${n(y)}H${n(xs.at(-1) + 24)}`,
|
|
425
|
+
stroke: ink,
|
|
426
|
+
"stroke-width": 3,
|
|
427
|
+
"stroke-linecap": "round",
|
|
428
|
+
});
|
|
429
|
+
arrowHead(pen, xs.at(-1) + 34, y, 1, 0, 13);
|
|
430
|
+
xs.forEach((x, i) => {
|
|
431
|
+
pen.el("path", {
|
|
432
|
+
d: `M${n(x)} ${n(y - 9)}V${n(y + 9)}`,
|
|
433
|
+
stroke: ink,
|
|
434
|
+
"stroke-width": 2.5,
|
|
435
|
+
"stroke-linecap": "round",
|
|
436
|
+
class: "tick",
|
|
437
|
+
});
|
|
438
|
+
const l = labels[i];
|
|
439
|
+
if (l === "?")
|
|
440
|
+
tag(pen, x, y + 26, l, size);
|
|
441
|
+
else if (l)
|
|
442
|
+
pen.text(x, y + 26, l, { size });
|
|
443
|
+
});
|
|
444
|
+
for (const j of ahead) {
|
|
445
|
+
const x1 = at(j.from);
|
|
446
|
+
const x2 = at(j.to);
|
|
447
|
+
arc(pen, x1 + 3, x2 - 3, y - 6, rise(x2 - x1), j.label);
|
|
448
|
+
}
|
|
449
|
+
for (const m of marks)
|
|
450
|
+
drawThing(pen, m.thing, at(m.at), y - 8 - (thingSize - 8) / 2, thingSize - 8);
|
|
451
|
+
let bottom = y + 42;
|
|
452
|
+
if (back.length > 0) {
|
|
453
|
+
const drop = Math.max(...back.map((j) => arcHeight(at(j.from) - at(j.to))));
|
|
454
|
+
for (const j of back) {
|
|
455
|
+
const x1 = at(j.from);
|
|
456
|
+
const x2 = at(j.to);
|
|
457
|
+
arc(pen, x1 - 3, x2 + 3, bottom, -arcHeight(x1 - x2), j.label);
|
|
458
|
+
}
|
|
459
|
+
bottom += drop + 30;
|
|
460
|
+
}
|
|
461
|
+
return { width: xs.at(-1) + 36 + margin, height: bottom + margin };
|
|
462
|
+
}
|
|
463
|
+
function drawStairs(scene, pen) {
|
|
464
|
+
const p = points(scene);
|
|
465
|
+
const c = colors[scene.color ?? defaultColor.stairs];
|
|
466
|
+
const w = Math.min(60, 540 / (p + 1));
|
|
467
|
+
const h = Math.min(34, 250 / p);
|
|
468
|
+
const s = Math.min(thingSize, w - 4);
|
|
469
|
+
const marks = scene.marks ?? [];
|
|
470
|
+
const high = Math.max(p * h, ...marks.map((m) => m.at * h + s + 4));
|
|
471
|
+
const ground = margin + high + 4;
|
|
472
|
+
const left = margin + 8;
|
|
473
|
+
pen.el("path", {
|
|
474
|
+
d: `M${n(left - 4)} ${n(ground)}H${n(left + (p + 1) * w + 10)}`,
|
|
475
|
+
stroke: ink,
|
|
476
|
+
"stroke-width": 3,
|
|
477
|
+
"stroke-linecap": "round",
|
|
478
|
+
});
|
|
479
|
+
for (let i = 1; i <= p; i++) {
|
|
480
|
+
const x = left + i * w;
|
|
481
|
+
const top = ground - i * h;
|
|
482
|
+
pen.el("rect", {
|
|
483
|
+
x,
|
|
484
|
+
y: top,
|
|
485
|
+
width: w,
|
|
486
|
+
height: i * h,
|
|
487
|
+
fill: c.fill,
|
|
488
|
+
stroke: ink,
|
|
489
|
+
"stroke-width": outline,
|
|
490
|
+
"stroke-linejoin": "round",
|
|
491
|
+
class: "step",
|
|
492
|
+
});
|
|
493
|
+
pen.el("rect", {
|
|
494
|
+
x,
|
|
495
|
+
y: top,
|
|
496
|
+
width: w,
|
|
497
|
+
height: 7,
|
|
498
|
+
fill: c.dark,
|
|
499
|
+
stroke: ink,
|
|
500
|
+
"stroke-width": outline,
|
|
501
|
+
});
|
|
502
|
+
const label = scene.labels?.[i - 1];
|
|
503
|
+
if (label === "?")
|
|
504
|
+
tag(pen, x + w / 2, top + 7 + (h - 7) / 2 + 1, label, 15);
|
|
505
|
+
else if (label)
|
|
506
|
+
pen.text(x + w / 2, top + 7 + (h - 7) / 2, label, {
|
|
507
|
+
size: Math.min(18, h - 14),
|
|
508
|
+
fill: textOn(scene.color ?? defaultColor.stairs),
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
for (const m of marks) {
|
|
512
|
+
drawThing(pen, m.thing, left + m.at * w + w / 2, ground - m.at * h - s / 2 - 1, s);
|
|
513
|
+
}
|
|
514
|
+
return { width: left + (p + 1) * w + 10 + margin, height: ground + 2 + margin };
|
|
515
|
+
}
|
|
516
|
+
// --- words -------------------------------------------------------------------
|
|
517
|
+
function ordinal(i) {
|
|
518
|
+
const tail = i % 100 >= 11 && i % 100 <= 13 ? "th" : (["th", "st", "nd", "rd"][i % 10] ?? "th");
|
|
519
|
+
return `${i}${tail}`;
|
|
520
|
+
}
|
|
521
|
+
function a(thing) {
|
|
522
|
+
const word = wordOf(thing);
|
|
523
|
+
return /^[aeiou]/.test(word) ? `an ${word}` : `a ${word}`;
|
|
524
|
+
}
|
|
525
|
+
function sayGaps(gaps, what) {
|
|
526
|
+
if (!gaps?.some((g) => g !== ""))
|
|
527
|
+
return "";
|
|
528
|
+
return ` The ${what}, from left to right: ${gaps.map((g) => (g === "" ? "not marked" : g)).join(", ")}.`;
|
|
529
|
+
}
|
|
530
|
+
function sayLabels(labels, what) {
|
|
531
|
+
if (!labels?.some((l) => l !== ""))
|
|
532
|
+
return "";
|
|
533
|
+
return ` The ${what} are marked, from left to right: ${labels.map((l) => (l === "" ? "no mark" : l)).join(", ")}.`;
|
|
534
|
+
}
|
|
535
|
+
// --- the kind ----------------------------------------------------------------
|
|
536
|
+
export const line = {
|
|
537
|
+
kind: "line",
|
|
538
|
+
members: [
|
|
539
|
+
"kind",
|
|
540
|
+
"style",
|
|
541
|
+
"count",
|
|
542
|
+
"things",
|
|
543
|
+
"start",
|
|
544
|
+
"by",
|
|
545
|
+
"labels",
|
|
546
|
+
"gaps",
|
|
547
|
+
"lengths",
|
|
548
|
+
"total",
|
|
549
|
+
"jumps",
|
|
550
|
+
"marks",
|
|
551
|
+
"color",
|
|
552
|
+
],
|
|
553
|
+
check(scene, problems) {
|
|
554
|
+
only(scene, this.members, "scene", problems);
|
|
555
|
+
if (typeof scene.style !== "string" ||
|
|
556
|
+
!lineStyles.includes(scene.style)) {
|
|
557
|
+
oneOf(scene.style, lineStyles, "style", problems);
|
|
558
|
+
return;
|
|
559
|
+
}
|
|
560
|
+
const style = scene.style;
|
|
561
|
+
const takes = styleMembers[style];
|
|
562
|
+
for (const key of Object.keys(scene)) {
|
|
563
|
+
if (key === "kind" || key === "style" || !this.members.includes(key))
|
|
564
|
+
continue;
|
|
565
|
+
if (!takes.includes(key))
|
|
566
|
+
problems.push(`${key}: not a member of a line of style ${style}; it takes ${takes.join(", ")}`);
|
|
567
|
+
}
|
|
568
|
+
let p;
|
|
569
|
+
if (style === "row") {
|
|
570
|
+
const things = list(scene.things, "things", problems, 2, rowMost);
|
|
571
|
+
things.forEach((t, i) => {
|
|
572
|
+
checkThing(t, `things[${i}]`, problems);
|
|
573
|
+
});
|
|
574
|
+
p = things.length;
|
|
575
|
+
if (p < 2 || p > rowMost)
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
else {
|
|
579
|
+
const c = countOf[style];
|
|
580
|
+
p = int(scene.count, "count", problems, c.least, c.most);
|
|
581
|
+
if (p < c.least || p > c.most)
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
const typed = scene;
|
|
585
|
+
const g = style === "log" ? p + 1 : style === "stairs" || style === "numbers" ? 0 : p - 1;
|
|
586
|
+
if (scene.labels !== undefined && takes.includes("labels"))
|
|
587
|
+
checkLabels(scene.labels, "labels", p, style === "stairs" || style === "numbers" ? 3 : labelMost, problems);
|
|
588
|
+
if (scene.gaps !== undefined && takes.includes("gaps"))
|
|
589
|
+
checkLabels(scene.gaps, "gaps", g, labelMost, problems);
|
|
590
|
+
if (scene.total !== undefined && takes.includes("total"))
|
|
591
|
+
text(scene.total, "total", problems, 8);
|
|
592
|
+
if (scene.color !== undefined && takes.includes("color") && !isColor(scene.color))
|
|
593
|
+
problems.push(`color: must be a colour: ${Object.keys(colors).join(", ")}`);
|
|
594
|
+
if (scene.lengths !== undefined && takes.includes("lengths")) {
|
|
595
|
+
const before = problems.length;
|
|
596
|
+
const lengths = list(scene.lengths, "lengths", problems, g, g);
|
|
597
|
+
if (Array.isArray(scene.lengths) && scene.lengths.length !== g)
|
|
598
|
+
problems.push(`lengths: must hold exactly ${g}, one for each gap`);
|
|
599
|
+
lengths.forEach((l, i) => {
|
|
600
|
+
int(l, `lengths[${i}]`, problems, 1, 100);
|
|
601
|
+
});
|
|
602
|
+
if (problems.length === before &&
|
|
603
|
+
spread(style, lengths, Array.isArray(scene.gaps) ? scene.gaps.map(String) : []) === undefined)
|
|
604
|
+
problems.push(`lengths: too uneven to draw; the shortest gap would be under ${room[style].least} px beside the others, so make them closer or fewer`);
|
|
605
|
+
}
|
|
606
|
+
if (style === "numbers") {
|
|
607
|
+
const start = scene.start === undefined ? 0 : int(scene.start, "start", problems, 0, 999);
|
|
608
|
+
const by = scene.by === undefined ? 1 : int(scene.by, "by", problems, 1, 100);
|
|
609
|
+
if (start + (p - 1) * by > 999)
|
|
610
|
+
problems.push(`count: the last tick would be ${start + (p - 1) * by}; numbers go up to 999`);
|
|
611
|
+
}
|
|
612
|
+
const isTick = (v, path) => {
|
|
613
|
+
if (style === "numbers") {
|
|
614
|
+
if (typeof v !== "number" || !Number.isInteger(v)) {
|
|
615
|
+
problems.push(`${path}: must be a whole number on the line`);
|
|
616
|
+
return false;
|
|
617
|
+
}
|
|
618
|
+
if (tickOf({ ...typed, count: p }, v) < 0) {
|
|
619
|
+
problems.push(`${path}: ${v} is not on the line; its ticks are ${valueAt(typed, 0)} to ${valueAt(typed, p - 1)} by ${typed.by ?? 1}`);
|
|
620
|
+
return false;
|
|
621
|
+
}
|
|
622
|
+
return true;
|
|
623
|
+
}
|
|
624
|
+
const least = style === "stairs" ? 0 : 1;
|
|
625
|
+
const before = problems.length;
|
|
626
|
+
int(v, path, problems, least, p);
|
|
627
|
+
return problems.length === before;
|
|
628
|
+
};
|
|
629
|
+
if (scene.jumps !== undefined && takes.includes("jumps")) {
|
|
630
|
+
list(scene.jumps, "jumps", problems, 1, 8).forEach((j, i) => {
|
|
631
|
+
const path = `jumps[${i}]`;
|
|
632
|
+
if (!isObj(j)) {
|
|
633
|
+
problems.push(`${path}: must be a jump with from, to and a label`);
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
636
|
+
only(j, ["from", "to", "label"], path, problems);
|
|
637
|
+
const ok = isTick(j.from, `${path}.from`) && isTick(j.to, `${path}.to`);
|
|
638
|
+
if (ok && j.from === j.to)
|
|
639
|
+
problems.push(`${path}.to: must differ from from`);
|
|
640
|
+
if (j.label !== undefined)
|
|
641
|
+
text(j.label, `${path}.label`, problems, 4);
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
if (scene.marks !== undefined && takes.includes("marks")) {
|
|
645
|
+
const seen = new Set();
|
|
646
|
+
list(scene.marks, "marks", problems, 1, 4).forEach((m, i) => {
|
|
647
|
+
const path = `marks[${i}]`;
|
|
648
|
+
if (!isObj(m)) {
|
|
649
|
+
problems.push(`${path}: must be a mark with at and a thing`);
|
|
650
|
+
return;
|
|
651
|
+
}
|
|
652
|
+
only(m, ["at", "thing"], path, problems);
|
|
653
|
+
if (isTick(m.at, `${path}.at`)) {
|
|
654
|
+
if (seen.has(m.at))
|
|
655
|
+
problems.push(`${path}.at: another mark stands there`);
|
|
656
|
+
seen.add(m.at);
|
|
657
|
+
}
|
|
658
|
+
checkThing(m.thing, `${path}.thing`, problems);
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
},
|
|
662
|
+
draw(scene, pen) {
|
|
663
|
+
const typed = scene;
|
|
664
|
+
switch (typed.style) {
|
|
665
|
+
case "fence":
|
|
666
|
+
return drawFence(typed, pen);
|
|
667
|
+
case "row":
|
|
668
|
+
return drawRow(typed, pen);
|
|
669
|
+
case "log":
|
|
670
|
+
return drawLog(typed, pen);
|
|
671
|
+
case "belt":
|
|
672
|
+
return drawBelt(typed, pen);
|
|
673
|
+
case "numbers":
|
|
674
|
+
return drawNumbers(typed, pen);
|
|
675
|
+
case "stairs":
|
|
676
|
+
return drawStairs(typed, pen);
|
|
677
|
+
}
|
|
678
|
+
},
|
|
679
|
+
describe(scene) {
|
|
680
|
+
const s = scene;
|
|
681
|
+
const p = points(s);
|
|
682
|
+
const color = (fallback) => colors[s.color ?? fallback].word;
|
|
683
|
+
const total = s.total
|
|
684
|
+
? ` ${s.total === "?" ? "The whole length is asked: ?" : `The whole length: ${s.total}`}.`
|
|
685
|
+
: "";
|
|
686
|
+
switch (s.style) {
|
|
687
|
+
case "fence":
|
|
688
|
+
return `A ${color("brown")} fence of ${p} posts in a row, every two neighbouring posts joined by rails.${sayLabels(s.labels, "posts")}${sayGaps(s.gaps, "gaps between neighbouring posts")}${total.replace("whole length", "length from the first post to the last")}`;
|
|
689
|
+
case "row": {
|
|
690
|
+
const things = s.things.map((t, i) => `${a(t)}${s.labels?.[i] ? ` (${s.labels[i]})` : ""}`);
|
|
691
|
+
const jumps = (s.jumps ?? [])
|
|
692
|
+
.map((j) => ` An arrow goes from the ${ordinal(j.from)} to the ${ordinal(j.to)}${j.label ? `, marked ${j.label}` : ""}.`)
|
|
693
|
+
.join("");
|
|
694
|
+
return `${p} things in a row, from left to right: ${things.join(", ")}.${sayGaps(s.gaps, "distances between neighbours")}${total.replace("whole length", "distance from the first to the last")}${jumps}`;
|
|
695
|
+
}
|
|
696
|
+
case "log":
|
|
697
|
+
return `A ${color("brown")} log with ${p} cut line${p === 1 ? "" : "s"} across it, which make ${p + 1} pieces.${sayGaps(s.gaps, "pieces")}${total.replace("whole length", "whole log")}`;
|
|
698
|
+
case "belt":
|
|
699
|
+
return `A ${color("yellow")} strip with ${p} holes in a row.${sayLabels(s.labels, "holes")}${sayGaps(s.gaps, "distances between neighbouring holes")}${total.replace("whole length", "distance from the first hole to the last")}`;
|
|
700
|
+
case "numbers": {
|
|
701
|
+
const by = s.by ?? 1;
|
|
702
|
+
const changed = (s.labels ?? [])
|
|
703
|
+
.map((l, i) => ({ l, i }))
|
|
704
|
+
.filter(({ l, i }) => l !== String(valueAt(s, i)));
|
|
705
|
+
const hidden = changed.length
|
|
706
|
+
? ` ${changed
|
|
707
|
+
.map(({ l, i }) => l === ""
|
|
708
|
+
? `the number at the ${ordinal(i + 1)} tick is not written`
|
|
709
|
+
: `the ${ordinal(i + 1)} tick shows ${l} instead of its number`)
|
|
710
|
+
.join("; ")}.`
|
|
711
|
+
: "";
|
|
712
|
+
const shown = changed.length
|
|
713
|
+
? ` Written: ${Array.from({ length: p }, (_, i) => s.labels?.[i] ?? String(valueAt(s, i)))
|
|
714
|
+
.map((l) => l || "blank")
|
|
715
|
+
.join(", ")}.`
|
|
716
|
+
: "";
|
|
717
|
+
const jumps = (s.jumps ?? [])
|
|
718
|
+
.map((j) => ` A jump arrow from ${j.from} to ${j.to}${j.label ? `, marked ${j.label}` : ""}.`)
|
|
719
|
+
.join("");
|
|
720
|
+
const marks = (s.marks ?? [])
|
|
721
|
+
.map((m) => ` ${a(m.thing)} stands at ${m.at}.`.replace(/^ a/, " A").replace(/^ an/, " An"))
|
|
722
|
+
.join("");
|
|
723
|
+
return `A number line with ${p} ticks for the numbers ${valueAt(s, 0)} to ${valueAt(s, p - 1)}${by === 1 ? "" : `, counting by ${by}`}.${hidden}${shown}${marks}${jumps}`;
|
|
724
|
+
}
|
|
725
|
+
case "stairs": {
|
|
726
|
+
const marks = (s.marks ?? [])
|
|
727
|
+
.map((m) => {
|
|
728
|
+
const who = a(m.thing);
|
|
729
|
+
const big = who[0].toUpperCase() + who.slice(1);
|
|
730
|
+
return m.at === 0
|
|
731
|
+
? ` ${big} stands on the floor before the first step.`
|
|
732
|
+
: ` ${big} stands on the ${ordinal(m.at)} step from the bottom.`;
|
|
733
|
+
})
|
|
734
|
+
.join("");
|
|
735
|
+
return `${color("orange")[0].toUpperCase()}${color("orange").slice(1)} stairs of ${p} steps going up from left to right.${sayLabels(s.labels, "steps").replace("from left to right", "from the bottom up")}${marks}`;
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
},
|
|
739
|
+
};
|