@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.
- package/LICENSE +201 -0
- package/NOTICE +7 -0
- package/README.md +27 -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,335 @@
|
|
|
1
|
+
import { margin } from "../kind.js";
|
|
2
|
+
import { int, isObj, list, oneOf, only, text } from "../read.js";
|
|
3
|
+
import { colors, ink, outline } from "../style.js";
|
|
4
|
+
import { poly } from "../svg.js";
|
|
5
|
+
import { checkThing, count, drawThing } from "../things.js";
|
|
6
|
+
/**
|
|
7
|
+
* Groups: things in groups, each group in a holder the picture draws itself —
|
|
8
|
+
* a tree, a bush, a plate, a basket, a box, a pond, a jar — or in none. The
|
|
9
|
+
* holder has places, and the picture puts each thing in a place of its own,
|
|
10
|
+
* so a group shows exactly as many things as the scene says.
|
|
11
|
+
*/
|
|
12
|
+
export const holders = ["none", "tree", "bush", "plate", "basket", "box", "pond", "jar"];
|
|
13
|
+
const most = 24;
|
|
14
|
+
/** rowsIn lays k places out in rows that fit a shape, given each row's width. */
|
|
15
|
+
function rowsIn(k, height, widthAt, size) {
|
|
16
|
+
for (let s = size; s >= 14; s -= 2) {
|
|
17
|
+
const rows = Math.floor(height / s);
|
|
18
|
+
const capacity = [];
|
|
19
|
+
for (let r = 0; r < rows; r++) {
|
|
20
|
+
const y = (r + 0.5) * s + (height - rows * s) / 2;
|
|
21
|
+
capacity.push({ y, fits: Math.floor(widthAt(y) / s) });
|
|
22
|
+
}
|
|
23
|
+
const total = capacity.reduce((sum, c) => sum + c.fits, 0);
|
|
24
|
+
if (total < k)
|
|
25
|
+
continue;
|
|
26
|
+
// Use the fewest rows, taken from the middle out, that hold k.
|
|
27
|
+
const order = capacity
|
|
28
|
+
.map((_, i) => i)
|
|
29
|
+
.sort((a, b) => Math.abs(a - (rows - 1) / 2) - Math.abs(b - (rows - 1) / 2));
|
|
30
|
+
const used = new Set();
|
|
31
|
+
let room = 0;
|
|
32
|
+
for (const i of order) {
|
|
33
|
+
if (room >= k)
|
|
34
|
+
break;
|
|
35
|
+
used.add(i);
|
|
36
|
+
room += capacity[i].fits;
|
|
37
|
+
}
|
|
38
|
+
const chosen = [...used].sort((a, b) => a - b);
|
|
39
|
+
const per = chosen.map((i) => capacity[i].fits);
|
|
40
|
+
// Spread k over the chosen rows as evenly as their room allows.
|
|
41
|
+
const counts = per.map(() => 0);
|
|
42
|
+
for (let left = k, i = 0; left > 0; i = (i + 1) % counts.length) {
|
|
43
|
+
if (counts[i] < per[i]) {
|
|
44
|
+
counts[i]++;
|
|
45
|
+
left--;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const out = [];
|
|
49
|
+
chosen.forEach((rowIndex, j) => {
|
|
50
|
+
const y = capacity[rowIndex].y;
|
|
51
|
+
const c = counts[j];
|
|
52
|
+
for (let i = 0; i < c; i++)
|
|
53
|
+
out.push({ x: (i - (c - 1) / 2) * s, y });
|
|
54
|
+
});
|
|
55
|
+
return out.map((p) => ({ ...p, s }));
|
|
56
|
+
}
|
|
57
|
+
throw new Error(`no room for ${k} things`);
|
|
58
|
+
}
|
|
59
|
+
function expand(group) {
|
|
60
|
+
return group.items.flatMap((item) => Array.from({ length: item.count }, () => item.thing));
|
|
61
|
+
}
|
|
62
|
+
/** drawGroup draws one holder with its things, its top left at 0, 0. */
|
|
63
|
+
function drawGroup(pen, group, fit) {
|
|
64
|
+
const things = expand(group);
|
|
65
|
+
const k = things.length;
|
|
66
|
+
const room = Math.max(k, fit);
|
|
67
|
+
const holder = group.holder ?? "none";
|
|
68
|
+
const put = (places, dx, dy) => {
|
|
69
|
+
const s = places[0]?.s ?? 30;
|
|
70
|
+
places.forEach((p, i) => {
|
|
71
|
+
drawThing(pen, things[i], dx + p.x, dy + p.y, s - 3);
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
switch (holder) {
|
|
75
|
+
case "tree":
|
|
76
|
+
case "bush": {
|
|
77
|
+
const R = Math.max(62, Math.sqrt(room) * 24);
|
|
78
|
+
const cx = R + 14;
|
|
79
|
+
const cy = R + 6;
|
|
80
|
+
const leaf = colors.green;
|
|
81
|
+
const lobes = [
|
|
82
|
+
{ x: cx - R * 0.55, y: cy + R * 0.25, r: R * 0.62 },
|
|
83
|
+
{ x: cx + R * 0.55, y: cy + R * 0.25, r: R * 0.62 },
|
|
84
|
+
{ x: cx, y: cy - R * 0.35, r: R * 0.7 },
|
|
85
|
+
];
|
|
86
|
+
if (holder === "tree") {
|
|
87
|
+
pen.el("path", {
|
|
88
|
+
d: poly([
|
|
89
|
+
{ x: cx - 13, y: cy + R * 0.6 },
|
|
90
|
+
{ x: cx + 13, y: cy + R * 0.6 },
|
|
91
|
+
{ x: cx + 17, y: cy + R + 46 },
|
|
92
|
+
{ x: cx - 17, y: cy + R + 46 },
|
|
93
|
+
]),
|
|
94
|
+
fill: colors.brown.fill,
|
|
95
|
+
stroke: ink,
|
|
96
|
+
"stroke-width": outline,
|
|
97
|
+
"stroke-linejoin": "round",
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
for (const l of lobes)
|
|
101
|
+
pen.el("circle", {
|
|
102
|
+
cx: l.x,
|
|
103
|
+
cy: l.y,
|
|
104
|
+
r: l.r,
|
|
105
|
+
fill: leaf.fill,
|
|
106
|
+
stroke: ink,
|
|
107
|
+
"stroke-width": outline,
|
|
108
|
+
});
|
|
109
|
+
pen.el("circle", { cx, cy, r: R * 0.98, fill: leaf.fill });
|
|
110
|
+
for (const l of lobes)
|
|
111
|
+
pen.el("circle", { cx: l.x, cy: l.y, r: l.r - outline / 2, fill: leaf.fill });
|
|
112
|
+
const places = rowsIn(k, R * 1.7, (y) => 2 * Math.sqrt(Math.max(0, (R * 0.92) ** 2 - (y - R * 0.85) ** 2)), 40);
|
|
113
|
+
put(places, cx, cy - R * 0.85);
|
|
114
|
+
const height = holder === "tree" ? cy + R + 48 : cy + R * 0.95;
|
|
115
|
+
return { width: 2 * R + 28, height };
|
|
116
|
+
}
|
|
117
|
+
case "plate":
|
|
118
|
+
case "pond": {
|
|
119
|
+
const w = Math.max(150, Math.ceil(Math.sqrt(room * 2.2)) * 38 + 40);
|
|
120
|
+
const h = w * 0.55;
|
|
121
|
+
const cx = w / 2 + 6;
|
|
122
|
+
const cy = h / 2 + 6;
|
|
123
|
+
if (holder === "plate") {
|
|
124
|
+
pen.el("ellipse", {
|
|
125
|
+
cx,
|
|
126
|
+
cy,
|
|
127
|
+
rx: w / 2,
|
|
128
|
+
ry: h / 2,
|
|
129
|
+
fill: "#ffffff",
|
|
130
|
+
stroke: ink,
|
|
131
|
+
"stroke-width": outline,
|
|
132
|
+
});
|
|
133
|
+
pen.el("ellipse", {
|
|
134
|
+
cx,
|
|
135
|
+
cy,
|
|
136
|
+
rx: w / 2 - 12,
|
|
137
|
+
ry: h / 2 - 9,
|
|
138
|
+
fill: "none",
|
|
139
|
+
stroke: colors.blue.fill,
|
|
140
|
+
"stroke-width": 3,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
pen.el("ellipse", {
|
|
145
|
+
cx,
|
|
146
|
+
cy,
|
|
147
|
+
rx: w / 2,
|
|
148
|
+
ry: h / 2,
|
|
149
|
+
fill: "#9fd3f4",
|
|
150
|
+
stroke: ink,
|
|
151
|
+
"stroke-width": outline,
|
|
152
|
+
});
|
|
153
|
+
pen.el("path", {
|
|
154
|
+
d: `M${cx - w * 0.3} ${cy - h * 0.1}q14 -8 28 0q14 8 28 0`,
|
|
155
|
+
fill: "none",
|
|
156
|
+
stroke: "#ffffff",
|
|
157
|
+
"stroke-width": 2.5,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
const places = rowsIn(k, h * 0.8, (y) => 2 * (w / 2 - 16) * Math.sqrt(Math.max(0, 1 - ((y - h * 0.4) / (h * 0.45)) ** 2)), 38);
|
|
161
|
+
put(places, cx, cy - h * 0.4);
|
|
162
|
+
return { width: w + 12, height: h + 12 };
|
|
163
|
+
}
|
|
164
|
+
case "basket":
|
|
165
|
+
case "box":
|
|
166
|
+
case "jar": {
|
|
167
|
+
const s = 36;
|
|
168
|
+
const cols = Math.min(5, Math.max(2, Math.ceil(Math.sqrt(room * 1.4))));
|
|
169
|
+
const rows = Math.ceil(k / cols);
|
|
170
|
+
const fitRows = Math.ceil(room / cols);
|
|
171
|
+
const w = cols * s + 30;
|
|
172
|
+
const cx = w / 2 + 17;
|
|
173
|
+
const place = (i, base) => {
|
|
174
|
+
const row = Math.floor(i / cols);
|
|
175
|
+
const inRow = Math.min(cols, k - row * cols);
|
|
176
|
+
return { x: cx + ((i % cols) - (inRow - 1) / 2) * s, y: base - s / 2 - row * (s - 2) };
|
|
177
|
+
};
|
|
178
|
+
if (holder === "jar") {
|
|
179
|
+
const top = 20;
|
|
180
|
+
const bottom = top + fitRows * (s - 2) + 22;
|
|
181
|
+
pen.el("rect", {
|
|
182
|
+
x: cx - w / 2,
|
|
183
|
+
y: top,
|
|
184
|
+
width: w,
|
|
185
|
+
height: bottom - top,
|
|
186
|
+
rx: 18,
|
|
187
|
+
fill: "#e7f4fd",
|
|
188
|
+
stroke: ink,
|
|
189
|
+
"stroke-width": outline,
|
|
190
|
+
});
|
|
191
|
+
things.forEach((t, i) => {
|
|
192
|
+
const p = place(i, bottom - 8);
|
|
193
|
+
drawThing(pen, t, p.x, p.y, s - 4);
|
|
194
|
+
});
|
|
195
|
+
pen.el("rect", {
|
|
196
|
+
x: cx - w / 2 + 8,
|
|
197
|
+
y: top - 12,
|
|
198
|
+
width: w - 16,
|
|
199
|
+
height: 16,
|
|
200
|
+
rx: 5,
|
|
201
|
+
fill: colors.red.fill,
|
|
202
|
+
stroke: ink,
|
|
203
|
+
"stroke-width": outline,
|
|
204
|
+
});
|
|
205
|
+
return { width: w + 34, height: bottom + 4 };
|
|
206
|
+
}
|
|
207
|
+
// The things stand in rows on the rim, each whole in sight; the front
|
|
208
|
+
// of the holder covers no more than the lowest few pixels of the first row.
|
|
209
|
+
const heap = fitRows * (s - 2);
|
|
210
|
+
const rim = 22 + heap + (holder === "basket" ? 24 : 6);
|
|
211
|
+
const bottom = rim + 52;
|
|
212
|
+
const color = holder === "basket" ? colors.orange : colors.brown;
|
|
213
|
+
if (holder === "basket") {
|
|
214
|
+
const arc = `M${cx - w * 0.4} ${rim}C${cx - w * 0.4} ${rim - heap - 70} ${cx + w * 0.4} ${rim - heap - 70} ${cx + w * 0.4} ${rim}`;
|
|
215
|
+
pen.el("path", { d: arc, fill: "none", stroke: ink, "stroke-width": 7 });
|
|
216
|
+
pen.el("path", { d: arc, fill: "none", stroke: color.fill, "stroke-width": 3.5 });
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
pen.el("path", {
|
|
220
|
+
d: poly([
|
|
221
|
+
{ x: cx - w / 2, y: rim },
|
|
222
|
+
{ x: cx - w / 2 - 14, y: rim - 18 },
|
|
223
|
+
{ x: cx + w / 2 + 14, y: rim - 18 },
|
|
224
|
+
{ x: cx + w / 2, y: rim },
|
|
225
|
+
]),
|
|
226
|
+
fill: color.dark,
|
|
227
|
+
stroke: ink,
|
|
228
|
+
"stroke-width": outline,
|
|
229
|
+
"stroke-linejoin": "round",
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
things.forEach((t, i) => {
|
|
233
|
+
const p = place(i, rim + 6);
|
|
234
|
+
drawThing(pen, t, p.x, p.y, s - 4);
|
|
235
|
+
});
|
|
236
|
+
pen.el("path", {
|
|
237
|
+
d: poly([
|
|
238
|
+
{ x: cx - w / 2, y: rim },
|
|
239
|
+
{ x: cx + w / 2, y: rim },
|
|
240
|
+
{ x: cx + w / 2 - (holder === "basket" ? 12 : 0), y: bottom },
|
|
241
|
+
{ x: cx - w / 2 + (holder === "basket" ? 12 : 0), y: bottom },
|
|
242
|
+
]),
|
|
243
|
+
fill: color.fill,
|
|
244
|
+
stroke: ink,
|
|
245
|
+
"stroke-width": outline,
|
|
246
|
+
"stroke-linejoin": "round",
|
|
247
|
+
});
|
|
248
|
+
if (holder === "basket") {
|
|
249
|
+
for (let y = rim + 12; y < bottom - 4; y += 12)
|
|
250
|
+
pen.el("path", {
|
|
251
|
+
d: `M${cx - w / 2 + 8} ${y}H${cx + w / 2 - 8}`,
|
|
252
|
+
stroke: color.dark,
|
|
253
|
+
"stroke-width": 2,
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
void rows;
|
|
257
|
+
return { width: w + 34, height: bottom + 4 };
|
|
258
|
+
}
|
|
259
|
+
case "none": {
|
|
260
|
+
const s = 44;
|
|
261
|
+
const cols = Math.min(6, Math.max(1, Math.ceil(Math.sqrt(k * 1.6))));
|
|
262
|
+
const rows = Math.ceil(k / cols);
|
|
263
|
+
things.forEach((t, i) => {
|
|
264
|
+
const row = Math.floor(i / cols);
|
|
265
|
+
const inRow = Math.min(cols, k - row * cols);
|
|
266
|
+
drawThing(pen, t, (cols * s) / 2 + 4 + ((i % cols) - (inRow - 1) / 2) * s, row * s + s / 2 + 4, s - 4);
|
|
267
|
+
});
|
|
268
|
+
return { width: cols * s + 8, height: rows * s + 8 };
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
export const groups = {
|
|
273
|
+
kind: "groups",
|
|
274
|
+
members: ["kind", "groups"],
|
|
275
|
+
check(scene, problems) {
|
|
276
|
+
only(scene, this.members, "scene", problems);
|
|
277
|
+
list(scene.groups, "groups", problems, 1, 6).forEach((g, i) => {
|
|
278
|
+
const path = `groups[${i}]`;
|
|
279
|
+
if (!isObj(g)) {
|
|
280
|
+
problems.push(`${path}: must be a group`);
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
only(g, ["holder", "items", "label"], path, problems);
|
|
284
|
+
if (g.holder !== undefined)
|
|
285
|
+
oneOf(g.holder, holders, `${path}.holder`, problems);
|
|
286
|
+
let total = 0;
|
|
287
|
+
list(g.items, `${path}.items`, problems, 1, 4).forEach((item, j) => {
|
|
288
|
+
if (!isObj(item)) {
|
|
289
|
+
problems.push(`${path}.items[${j}]: must be a thing and a count`);
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
only(item, ["thing", "count"], `${path}.items[${j}]`, problems);
|
|
293
|
+
checkThing(item.thing, `${path}.items[${j}].thing`, problems);
|
|
294
|
+
total += int(item.count, `${path}.items[${j}].count`, problems, 1, most);
|
|
295
|
+
});
|
|
296
|
+
if (total > most)
|
|
297
|
+
problems.push(`${path}.items: at most ${most} things in a group`);
|
|
298
|
+
if (g.label !== undefined)
|
|
299
|
+
text(g.label, `${path}.label`, problems, 10);
|
|
300
|
+
});
|
|
301
|
+
},
|
|
302
|
+
draw(scene, pen) {
|
|
303
|
+
const list_ = scene.groups;
|
|
304
|
+
const fitOf = (h) => Math.max(...list_.filter((g) => (g.holder ?? "none") === h).map((g) => expand(g).length));
|
|
305
|
+
const drawn = list_.map((g) => {
|
|
306
|
+
const sub = pen.sub();
|
|
307
|
+
return { sub, label: g.label, ...drawGroup(sub, g, fitOf(g.holder ?? "none")) };
|
|
308
|
+
});
|
|
309
|
+
const labelled = drawn.some((d) => d.label);
|
|
310
|
+
const height = Math.max(...drawn.map((d) => d.height));
|
|
311
|
+
let x = margin;
|
|
312
|
+
for (const d of drawn) {
|
|
313
|
+
pen.place(d.sub, x, margin + height - d.height);
|
|
314
|
+
if (d.label)
|
|
315
|
+
pen.text(x + d.width / 2, margin + height + 18, d.label, { size: 18 });
|
|
316
|
+
x += d.width + 18;
|
|
317
|
+
}
|
|
318
|
+
return { width: x - 18 + margin, height: margin + height + (labelled ? 34 : 0) + margin };
|
|
319
|
+
},
|
|
320
|
+
describe(scene) {
|
|
321
|
+
const list_ = scene.groups;
|
|
322
|
+
return list_
|
|
323
|
+
.map((g, i) => {
|
|
324
|
+
const what = g.items.map((item) => count(item.thing, item.count)).join(" and ");
|
|
325
|
+
const holder = g.holder && g.holder !== "none" ? g.holder : "group";
|
|
326
|
+
const name = g.label
|
|
327
|
+
? `${holder[0].toUpperCase()}${holder.slice(1)} ${g.label}`
|
|
328
|
+
: list_.length > 1
|
|
329
|
+
? `${holder[0].toUpperCase()}${holder.slice(1)} ${i + 1}`
|
|
330
|
+
: `A ${holder}`;
|
|
331
|
+
return `${name}: ${what}.`;
|
|
332
|
+
})
|
|
333
|
+
.join(" ");
|
|
334
|
+
},
|
|
335
|
+
};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { balance } from "./balance.js";
|
|
2
|
+
import { branches } from "./branches.js";
|
|
3
|
+
import { calendar } from "./calendar.js";
|
|
4
|
+
import { chart } from "./chart.js";
|
|
5
|
+
import { choices } from "./choices.js";
|
|
6
|
+
import { clock } from "./clock.js";
|
|
7
|
+
import { coins } from "./coins.js";
|
|
8
|
+
import { column } from "./column.js";
|
|
9
|
+
import { commands } from "./commands.js";
|
|
10
|
+
import { containers } from "./containers.js";
|
|
11
|
+
import { countfig } from "./countfig.js";
|
|
12
|
+
import { cubes } from "./cubes.js";
|
|
13
|
+
import { die } from "./die.js";
|
|
14
|
+
import { domino } from "./domino.js";
|
|
15
|
+
import { dotgrid } from "./dotgrid.js";
|
|
16
|
+
import { figure } from "./figure.js";
|
|
17
|
+
import { fold } from "./fold.js";
|
|
18
|
+
import { fraction } from "./fraction.js";
|
|
19
|
+
import { gauges } from "./gauges.js";
|
|
20
|
+
import { gears } from "./gears.js";
|
|
21
|
+
import { grid } from "./grid.js";
|
|
22
|
+
import { groups } from "./groups.js";
|
|
23
|
+
import { lanes } from "./lanes.js";
|
|
24
|
+
import { layers } from "./layers.js";
|
|
25
|
+
import { line } from "./line.js";
|
|
26
|
+
import { magic } from "./magic.js";
|
|
27
|
+
import { map } from "./map.js";
|
|
28
|
+
import { matchsticks } from "./matchsticks.js";
|
|
29
|
+
import { necklace } from "./necklace.js";
|
|
30
|
+
import { net } from "./net.js";
|
|
31
|
+
import { overlay } from "./overlay.js";
|
|
32
|
+
import { pieces } from "./pieces.js";
|
|
33
|
+
import { pipes } from "./pipes.js";
|
|
34
|
+
import { plane } from "./plane.js";
|
|
35
|
+
import { pyramid } from "./pyramid.js";
|
|
36
|
+
import { ropes } from "./ropes.js";
|
|
37
|
+
import { shapes } from "./shapes.js";
|
|
38
|
+
import { solids } from "./solids.js";
|
|
39
|
+
import { tangram } from "./tangram.js";
|
|
40
|
+
import { target } from "./target.js";
|
|
41
|
+
import { tiles } from "./tiles.js";
|
|
42
|
+
import { venn } from "./venn.js";
|
|
43
|
+
/** kinds is the closed list of the kinds of scenes, in the order they came. */
|
|
44
|
+
export const kinds = [
|
|
45
|
+
balance,
|
|
46
|
+
groups,
|
|
47
|
+
grid,
|
|
48
|
+
pieces,
|
|
49
|
+
cubes,
|
|
50
|
+
die,
|
|
51
|
+
net,
|
|
52
|
+
tiles,
|
|
53
|
+
shapes,
|
|
54
|
+
figure,
|
|
55
|
+
line,
|
|
56
|
+
map,
|
|
57
|
+
clock,
|
|
58
|
+
chart,
|
|
59
|
+
venn,
|
|
60
|
+
calendar,
|
|
61
|
+
target,
|
|
62
|
+
commands,
|
|
63
|
+
branches,
|
|
64
|
+
lanes,
|
|
65
|
+
necklace,
|
|
66
|
+
magic,
|
|
67
|
+
pyramid,
|
|
68
|
+
column,
|
|
69
|
+
domino,
|
|
70
|
+
countfig,
|
|
71
|
+
dotgrid,
|
|
72
|
+
plane,
|
|
73
|
+
fraction,
|
|
74
|
+
fold,
|
|
75
|
+
overlay,
|
|
76
|
+
layers,
|
|
77
|
+
tangram,
|
|
78
|
+
coins,
|
|
79
|
+
gauges,
|
|
80
|
+
containers,
|
|
81
|
+
solids,
|
|
82
|
+
gears,
|
|
83
|
+
pipes,
|
|
84
|
+
ropes,
|
|
85
|
+
matchsticks,
|
|
86
|
+
choices,
|
|
87
|
+
];
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { Kind } from "../kind.ts";
|
|
2
|
+
import { type ColorName } from "../style.ts";
|
|
3
|
+
import { type Thing } from "../things.ts";
|
|
4
|
+
/**
|
|
5
|
+
* Lanes: vertical lanes side by side, a thing at the top of each and,
|
|
6
|
+
* if the scene says, at the bottom, "?" where the question asks. In a
|
|
7
|
+
* ladder (the ghost leg, amida) rungs join two lanes side by side, each at a
|
|
8
|
+
* level counted from the top: whoever goes down a lane crosses every rung it
|
|
9
|
+
* meets. In a sorting network bars join any two lanes, a dot at each end,
|
|
10
|
+
* and the two things on them compare there. Lanes are counted from 1 at the
|
|
11
|
+
* left, levels from 1 at the top. A ladder may trace one way down in a
|
|
12
|
+
* colour, the example a task gives.
|
|
13
|
+
*/
|
|
14
|
+
export declare const laneStyles: readonly ["ladder", "network"];
|
|
15
|
+
export type LaneStyle = (typeof laneStyles)[number];
|
|
16
|
+
export type Rung = {
|
|
17
|
+
between: [number, number];
|
|
18
|
+
at: number;
|
|
19
|
+
};
|
|
20
|
+
export type LanesScene = {
|
|
21
|
+
kind: "lanes";
|
|
22
|
+
style?: LaneStyle;
|
|
23
|
+
tops: Thing[];
|
|
24
|
+
bottoms?: Thing[];
|
|
25
|
+
rungs: Rung[];
|
|
26
|
+
trace?: {
|
|
27
|
+
from: number;
|
|
28
|
+
color?: ColorName;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
/** traceOf is the way down a ladder from the top of a lane: the corners it turns at, and the lane it ends in. */
|
|
32
|
+
export declare function traceOf(s: LanesScene, from: number): {
|
|
33
|
+
corners: [number, number][];
|
|
34
|
+
end: number;
|
|
35
|
+
};
|
|
36
|
+
export declare const lanes: Kind;
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { margin } from "../kind.js";
|
|
2
|
+
import { int, isObj, list, oneOf, only } from "../read.js";
|
|
3
|
+
import { colorNames, colors, ground, ink, isColor } from "../style.js";
|
|
4
|
+
import { n } from "../svg.js";
|
|
5
|
+
import { checkThing, drawThing, wordOf } from "../things.js";
|
|
6
|
+
import { smallText } from "./commands-parts.js";
|
|
7
|
+
/**
|
|
8
|
+
* Lanes: vertical lanes side by side, a thing at the top of each and,
|
|
9
|
+
* if the scene says, at the bottom, "?" where the question asks. In a
|
|
10
|
+
* ladder (the ghost leg, amida) rungs join two lanes side by side, each at a
|
|
11
|
+
* level counted from the top: whoever goes down a lane crosses every rung it
|
|
12
|
+
* meets. In a sorting network bars join any two lanes, a dot at each end,
|
|
13
|
+
* and the two things on them compare there. Lanes are counted from 1 at the
|
|
14
|
+
* left, levels from 1 at the top. A ladder may trace one way down in a
|
|
15
|
+
* colour, the example a task gives.
|
|
16
|
+
*/
|
|
17
|
+
export const laneStyles = ["ladder", "network"];
|
|
18
|
+
const mostLanes = 8;
|
|
19
|
+
const mostLevels = 12;
|
|
20
|
+
const mostRungs = 40;
|
|
21
|
+
const thing = 44;
|
|
22
|
+
const step = 30;
|
|
23
|
+
function spacing(lanes) {
|
|
24
|
+
return lanes <= 6 ? 76 : 68;
|
|
25
|
+
}
|
|
26
|
+
/** traceOf is the way down a ladder from the top of a lane: the corners it turns at, and the lane it ends in. */
|
|
27
|
+
export function traceOf(s, from) {
|
|
28
|
+
let lane = from;
|
|
29
|
+
const corners = [[lane, 0]];
|
|
30
|
+
const levels = [...new Set(s.rungs.map((r) => r.at))].sort((a, b) => a - b);
|
|
31
|
+
for (const at of levels) {
|
|
32
|
+
const rung = s.rungs.find((r) => r.at === at && (r.between[0] === lane || r.between[1] === lane));
|
|
33
|
+
if (!rung)
|
|
34
|
+
continue;
|
|
35
|
+
const other = rung.between[0] === lane ? rung.between[1] : rung.between[0];
|
|
36
|
+
corners.push([lane, at], [other, at]);
|
|
37
|
+
lane = other;
|
|
38
|
+
}
|
|
39
|
+
return { corners, end: lane };
|
|
40
|
+
}
|
|
41
|
+
function geometry(s) {
|
|
42
|
+
const lanes = s.tops.length;
|
|
43
|
+
const gap = spacing(lanes);
|
|
44
|
+
const levels = Math.max(...s.rungs.map((r) => r.at));
|
|
45
|
+
const top = margin + thing + 8;
|
|
46
|
+
const bottom = top + (levels + 1) * step;
|
|
47
|
+
const x = (lane) => margin + thing / 2 + 4 + (lane - 1) * gap;
|
|
48
|
+
const y = (at) => top + at * step;
|
|
49
|
+
const height = bottom + 8 + (s.bottoms ? thing : 0) + margin;
|
|
50
|
+
const width = x(lanes) + thing / 2 + 4 + margin;
|
|
51
|
+
return { lanes, top, bottom, x, y, width, height };
|
|
52
|
+
}
|
|
53
|
+
function checkEnds(value, path, problems, least, most) {
|
|
54
|
+
return list(value, path, problems, least, most).map((t, i) => {
|
|
55
|
+
const mark = problems.length;
|
|
56
|
+
checkThing(t, `${path}[${i}]`, problems);
|
|
57
|
+
const small = problems.length > mark ? undefined : smallText(t, thing, `${path}[${i}]`);
|
|
58
|
+
if (small)
|
|
59
|
+
problems.push(`${small}; write it on a card instead`);
|
|
60
|
+
return t;
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
function say(things) {
|
|
64
|
+
return things.map((t) => wordOf(t)).join(", ");
|
|
65
|
+
}
|
|
66
|
+
export const lanes = {
|
|
67
|
+
kind: "lanes",
|
|
68
|
+
members: ["kind", "style", "tops", "bottoms", "rungs", "trace"],
|
|
69
|
+
check(scene, problems) {
|
|
70
|
+
only(scene, this.members, "scene", problems);
|
|
71
|
+
const style = scene.style === undefined ? "ladder" : oneOf(scene.style, laneStyles, "style", problems);
|
|
72
|
+
const tops = checkEnds(scene.tops, "tops", problems, 2, mostLanes);
|
|
73
|
+
const count = tops.length;
|
|
74
|
+
if (scene.bottoms !== undefined) {
|
|
75
|
+
const bottoms = checkEnds(scene.bottoms, "bottoms", problems, 2, mostLanes);
|
|
76
|
+
if (count >= 2 && bottoms.length !== count)
|
|
77
|
+
problems.push(`bottoms: must hold one thing for each lane, ${count}, as tops does`);
|
|
78
|
+
}
|
|
79
|
+
const before = problems.length;
|
|
80
|
+
const rungs = list(scene.rungs, "rungs", problems, 1, mostRungs);
|
|
81
|
+
if (problems.length > before || count < 2 || count > mostLanes)
|
|
82
|
+
return;
|
|
83
|
+
const placed = [];
|
|
84
|
+
const word = style === "ladder" ? "rung" : "bar";
|
|
85
|
+
rungs.forEach((r, i) => {
|
|
86
|
+
const path = `rungs[${i}]`;
|
|
87
|
+
if (!isObj(r)) {
|
|
88
|
+
problems.push(`${path}: must be an object with between and at`);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
only(r, ["between", "at"], path, problems);
|
|
92
|
+
const mark = problems.length;
|
|
93
|
+
const at = int(r.at, `${path}.at`, problems, 1, mostLevels);
|
|
94
|
+
if (!Array.isArray(r.between) || r.between.length !== 2) {
|
|
95
|
+
problems.push(`${path}.between: must be two lanes, [left, right], counted from 1 at the left`);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
const a = int(r.between[0], `${path}.between[0]`, problems, 1, count);
|
|
99
|
+
const b = int(r.between[1], `${path}.between[1]`, problems, 1, count);
|
|
100
|
+
if (problems.length > mark)
|
|
101
|
+
return;
|
|
102
|
+
if (a >= b) {
|
|
103
|
+
problems.push(`${path}.between: the left lane comes first, and the two differ: [${Math.min(a, b)}, ${Math.max(a, b)}]`);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
if (style === "ladder" && b !== a + 1) {
|
|
107
|
+
problems.push(`${path}.between: a rung joins two lanes side by side, such as [${a}, ${a + 1}]`);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const clash = placed.find((o) => o.at === at && Math.max(o.a, a) <= Math.min(o.b, b));
|
|
111
|
+
if (clash)
|
|
112
|
+
problems.push(`${path}: meets rungs[${clash.i}] at level ${at}; two ${word}s at one level share no lane, so move one to another level`);
|
|
113
|
+
placed.push({ a, b, at, i });
|
|
114
|
+
});
|
|
115
|
+
if (scene.trace !== undefined) {
|
|
116
|
+
if (style !== "ladder")
|
|
117
|
+
problems.push("trace: only a ladder traces a way down");
|
|
118
|
+
else if (!isObj(scene.trace))
|
|
119
|
+
problems.push("trace: must be an object with from");
|
|
120
|
+
else {
|
|
121
|
+
only(scene.trace, ["from", "color"], "trace", problems);
|
|
122
|
+
int(scene.trace.from, "trace.from", problems, 1, count);
|
|
123
|
+
if (scene.trace.color !== undefined && !isColor(scene.trace.color))
|
|
124
|
+
problems.push(`trace.color: must be a colour: ${colorNames.join(", ")}`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
draw(scene, pen) {
|
|
129
|
+
const s = scene;
|
|
130
|
+
const g = geometry(s);
|
|
131
|
+
const ladder = (s.style ?? "ladder") === "ladder";
|
|
132
|
+
if (s.trace) {
|
|
133
|
+
const { corners } = traceOf(s, s.trace.from);
|
|
134
|
+
const points = corners.map(([lane, at]) => ({
|
|
135
|
+
x: g.x(lane),
|
|
136
|
+
y: at === 0 ? g.top : g.y(at),
|
|
137
|
+
}));
|
|
138
|
+
const last = corners[corners.length - 1];
|
|
139
|
+
points.push({ x: g.x(last[0]), y: g.bottom });
|
|
140
|
+
pen.el("path", {
|
|
141
|
+
d: points.map((p, i) => `${i === 0 ? "M" : "L"}${n(p.x)} ${n(p.y)}`).join(""),
|
|
142
|
+
fill: "none",
|
|
143
|
+
stroke: colors[s.trace.color ?? "orange"].fill,
|
|
144
|
+
"stroke-width": 13,
|
|
145
|
+
"stroke-linejoin": "round",
|
|
146
|
+
"stroke-linecap": "round",
|
|
147
|
+
class: "trace",
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
for (let lane = 1; lane <= g.lanes; lane++)
|
|
151
|
+
pen.el("path", {
|
|
152
|
+
d: `M${n(g.x(lane))} ${n(g.top)}V${n(g.bottom)}`,
|
|
153
|
+
stroke: ink,
|
|
154
|
+
"stroke-width": ladder ? 4 : 3,
|
|
155
|
+
"stroke-linecap": "round",
|
|
156
|
+
class: "lane",
|
|
157
|
+
});
|
|
158
|
+
for (const r of s.rungs) {
|
|
159
|
+
const y = g.y(r.at);
|
|
160
|
+
const [a, b] = r.between;
|
|
161
|
+
// A bar passes over the lanes between its ends: a white edge parts it from them.
|
|
162
|
+
if (!ladder && b > a + 1)
|
|
163
|
+
pen.el("path", {
|
|
164
|
+
d: `M${n(g.x(a) + 12)} ${n(y)}H${n(g.x(b) - 12)}`,
|
|
165
|
+
stroke: ground,
|
|
166
|
+
"stroke-width": 10,
|
|
167
|
+
});
|
|
168
|
+
pen.el("path", {
|
|
169
|
+
d: `M${n(g.x(a))} ${n(y)}H${n(g.x(b))}`,
|
|
170
|
+
stroke: ink,
|
|
171
|
+
"stroke-width": ladder ? 4 : 3.5,
|
|
172
|
+
"stroke-linecap": "round",
|
|
173
|
+
class: "rung",
|
|
174
|
+
});
|
|
175
|
+
if (!ladder)
|
|
176
|
+
for (const lane of [a, b])
|
|
177
|
+
pen.el("circle", { cx: g.x(lane), cy: y, r: 6.5, fill: ink });
|
|
178
|
+
}
|
|
179
|
+
s.tops.forEach((t, i) => {
|
|
180
|
+
pen.add('<g class="top">');
|
|
181
|
+
drawThing(pen, t, g.x(i + 1), margin + thing / 2, thing);
|
|
182
|
+
pen.add("</g>");
|
|
183
|
+
});
|
|
184
|
+
s.bottoms?.forEach((t, i) => {
|
|
185
|
+
pen.add('<g class="bottom">');
|
|
186
|
+
drawThing(pen, t, g.x(i + 1), g.bottom + 8 + thing / 2, thing);
|
|
187
|
+
pen.add("</g>");
|
|
188
|
+
});
|
|
189
|
+
return { width: g.width, height: g.height };
|
|
190
|
+
},
|
|
191
|
+
describe(scene) {
|
|
192
|
+
const s = scene;
|
|
193
|
+
const ladder = (s.style ?? "ladder") === "ladder";
|
|
194
|
+
const k = s.tops.length;
|
|
195
|
+
const out = [
|
|
196
|
+
ladder
|
|
197
|
+
? `A ladder of ${k} lanes going down, numbered 1 to ${k} from the left.`
|
|
198
|
+
: `A sorting network of ${k} lanes going down, numbered 1 to ${k} from the left.`,
|
|
199
|
+
`At the top, from the left: ${say(s.tops)}.`,
|
|
200
|
+
];
|
|
201
|
+
if (s.bottoms)
|
|
202
|
+
out.push(`At the bottom, from the left: ${say(s.bottoms)}.`);
|
|
203
|
+
const levels = [...new Set(s.rungs.map((r) => r.at))].sort((a, b) => a - b);
|
|
204
|
+
const rows = levels.map((at) => {
|
|
205
|
+
const here = s.rungs
|
|
206
|
+
.filter((r) => r.at === at)
|
|
207
|
+
.sort((p, q) => p.between[0] - q.between[0])
|
|
208
|
+
.map((r) => `lanes ${r.between[0]} and ${r.between[1]}`);
|
|
209
|
+
return here.length === 1 ? here[0] : `${here.join(" and ")}, at the same height`;
|
|
210
|
+
});
|
|
211
|
+
out.push(`${ladder ? "Rungs" : "Bars with a dot at each end"} join, from the top down: ${rows.join("; then ")}.`);
|
|
212
|
+
if (s.trace) {
|
|
213
|
+
const { end } = traceOf(s, s.trace.from);
|
|
214
|
+
const from = s.tops[s.trace.from - 1];
|
|
215
|
+
const to = s.bottoms?.[end - 1];
|
|
216
|
+
out.push(`A ${colors[s.trace.color ?? "orange"].word} line traces the way down from the ${wordOf(from)} at the top of lane ${s.trace.from} to the bottom of lane ${end}${to ? `, at the ${wordOf(to)}` : ""}.`);
|
|
217
|
+
}
|
|
218
|
+
return out.join(" ");
|
|
219
|
+
},
|
|
220
|
+
};
|