@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,342 @@
|
|
|
1
|
+
import { margin } from "../kind.js";
|
|
2
|
+
import { int, isObj, list, only, text } from "../read.js";
|
|
3
|
+
import { colors, ink } 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
|
+
const bead = 30;
|
|
8
|
+
/** A card bead is drawn a little bigger, so that its letters come out no smaller than 14 px. */
|
|
9
|
+
const cardBead = 32;
|
|
10
|
+
/** pitch is the length of string from a bead to the next. */
|
|
11
|
+
const pitch = 36;
|
|
12
|
+
/** Round a closed necklace, beads with square corners — cards and squares — need more string, so that two at a slant keep apart. */
|
|
13
|
+
const boxyPitch = 46;
|
|
14
|
+
const mostBeads = 16;
|
|
15
|
+
const wide = 616;
|
|
16
|
+
const rowGap = 18;
|
|
17
|
+
const colGap = 22;
|
|
18
|
+
const labelH = 30;
|
|
19
|
+
/** claspGap is the length of string between the beads on the two sides of a clasp. */
|
|
20
|
+
const claspGap = 62;
|
|
21
|
+
function isCard(b) {
|
|
22
|
+
return typeof b === "object" && "card" in b;
|
|
23
|
+
}
|
|
24
|
+
function beadSize(b) {
|
|
25
|
+
return isCard(b) ? cardBead : bead;
|
|
26
|
+
}
|
|
27
|
+
function isBoxy(b) {
|
|
28
|
+
return isCard(b) || (typeof b === "object" && "shape" in b && b.shape === "square");
|
|
29
|
+
}
|
|
30
|
+
/** ringPitch is the pitch of every closed necklace of a scene, so that they compare bead by bead. */
|
|
31
|
+
function ringPitch(s) {
|
|
32
|
+
return s.necklaces.some((k) => !k.open && k.beads.some(isBoxy)) ? boxyPitch : pitch;
|
|
33
|
+
}
|
|
34
|
+
/** radiusOf is the radius of every closed necklace of a scene: room for the beads of each, and its clasp. */
|
|
35
|
+
function radiusOf(s) {
|
|
36
|
+
const p = ringPitch(s);
|
|
37
|
+
const needs = s.necklaces
|
|
38
|
+
.filter((k) => !k.open)
|
|
39
|
+
.map((k) => (k.clasp ? (k.beads.length - 1) * p + claspGap : k.beads.length * p));
|
|
40
|
+
return Math.max(44, ...needs.map((length) => length / (2 * Math.PI)));
|
|
41
|
+
}
|
|
42
|
+
/** sizeOf is the box a necklace takes, its label under it included. */
|
|
43
|
+
function sizeOf(k, r) {
|
|
44
|
+
const label = k.label ? labelH : 0;
|
|
45
|
+
if (k.open) {
|
|
46
|
+
const ends = k.clasp ? 30 : 16;
|
|
47
|
+
return {
|
|
48
|
+
w: (k.beads.length - 1) * pitch + bead + 2 * ends,
|
|
49
|
+
h: bead + (k.cut ? 36 : 0) + 8 + label,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
const reach = r + (k.cut ? 46 : bead / 2 + 4);
|
|
53
|
+
return { w: 2 * reach, h: 2 * reach + label };
|
|
54
|
+
}
|
|
55
|
+
/** rowsOf lays the necklaces out in rows no wider than the picture. */
|
|
56
|
+
function rowsOf(s) {
|
|
57
|
+
const r = radiusOf(s);
|
|
58
|
+
const rows = [];
|
|
59
|
+
for (const k of s.necklaces) {
|
|
60
|
+
const { w, h } = sizeOf(k, r);
|
|
61
|
+
const row = rows[rows.length - 1];
|
|
62
|
+
if (row && !k.newRow && row.w + colGap + w <= wide) {
|
|
63
|
+
row.items.push({ k, w, h });
|
|
64
|
+
row.w += colGap + w;
|
|
65
|
+
row.h = Math.max(row.h, h);
|
|
66
|
+
}
|
|
67
|
+
else
|
|
68
|
+
rows.push({ items: [{ k, w, h }], w, h });
|
|
69
|
+
}
|
|
70
|
+
return rows;
|
|
71
|
+
}
|
|
72
|
+
/** anglesOf places the beads round the circle: the first at the top, or just after the clasp. */
|
|
73
|
+
function anglesOf(k, r) {
|
|
74
|
+
const count = k.beads.length;
|
|
75
|
+
const top = -Math.PI / 2;
|
|
76
|
+
if (!k.clasp)
|
|
77
|
+
return k.beads.map((_, i) => top + (i * 2 * Math.PI) / count);
|
|
78
|
+
const gap = claspGap / r;
|
|
79
|
+
const step = (2 * Math.PI - gap) / (count - 1);
|
|
80
|
+
return k.beads.map((_, i) => top + gap / 2 + i * step);
|
|
81
|
+
}
|
|
82
|
+
function at(c, r, a) {
|
|
83
|
+
return { x: c.x + r * Math.cos(a), y: c.y + r * Math.sin(a) };
|
|
84
|
+
}
|
|
85
|
+
/** clasp draws a small clasp: a ring and the oval catch that hooks into it, along the string. */
|
|
86
|
+
function claspAt(pen, c, angle) {
|
|
87
|
+
const ux = Math.cos(angle);
|
|
88
|
+
const uy = Math.sin(angle);
|
|
89
|
+
const deg = (angle * 180) / Math.PI;
|
|
90
|
+
pen.add('<g class="clasp">');
|
|
91
|
+
pen.el("ellipse", {
|
|
92
|
+
cx: c.x - ux * 5,
|
|
93
|
+
cy: c.y - uy * 5,
|
|
94
|
+
rx: 8,
|
|
95
|
+
ry: 5,
|
|
96
|
+
fill: colors.yellow.fill,
|
|
97
|
+
stroke: ink,
|
|
98
|
+
"stroke-width": 2,
|
|
99
|
+
transform: `rotate(${n(deg)} ${n(c.x - ux * 5)} ${n(c.y - uy * 5)})`,
|
|
100
|
+
});
|
|
101
|
+
pen.el("circle", {
|
|
102
|
+
cx: c.x + ux * 7,
|
|
103
|
+
cy: c.y + uy * 7,
|
|
104
|
+
r: 4.5,
|
|
105
|
+
fill: "none",
|
|
106
|
+
stroke: colors.yellow.dark,
|
|
107
|
+
"stroke-width": 3,
|
|
108
|
+
});
|
|
109
|
+
pen.add("</g>");
|
|
110
|
+
}
|
|
111
|
+
/** scissors mark a cut: a dashed line across the string and the scissors beside it. */
|
|
112
|
+
function scissors(pen, from, to, beside) {
|
|
113
|
+
pen.add('<g class="cut">');
|
|
114
|
+
pen.el("path", {
|
|
115
|
+
d: `M${n(from.x)} ${n(from.y)}L${n(to.x)} ${n(to.y)}`,
|
|
116
|
+
stroke: colors.red.fill,
|
|
117
|
+
"stroke-width": 3,
|
|
118
|
+
"stroke-dasharray": "4 3",
|
|
119
|
+
"stroke-linecap": "round",
|
|
120
|
+
});
|
|
121
|
+
drawThing(pen, "thing.scissors", beside.x, beside.y, 28);
|
|
122
|
+
pen.add("</g>");
|
|
123
|
+
}
|
|
124
|
+
function drawBeads(pen, beads, places) {
|
|
125
|
+
beads.forEach((b, i) => {
|
|
126
|
+
const p = places[i];
|
|
127
|
+
pen.add('<g class="bead">');
|
|
128
|
+
drawThing(pen, b, p.x, p.y, beadSize(b));
|
|
129
|
+
pen.add("</g>");
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
function drawClosed(pen, k, c, r) {
|
|
133
|
+
const angles = anglesOf(k, r);
|
|
134
|
+
const top = -Math.PI / 2;
|
|
135
|
+
if (k.clasp) {
|
|
136
|
+
// The string runs round from one side of the clasp to the other.
|
|
137
|
+
const gap = 9 / r;
|
|
138
|
+
const a = at(c, r, top + gap);
|
|
139
|
+
const b = at(c, r, top - gap);
|
|
140
|
+
pen.el("path", {
|
|
141
|
+
d: `M${n(a.x)} ${n(a.y)}A${n(r)} ${n(r)} 0 1 1 ${n(b.x)} ${n(b.y)}`,
|
|
142
|
+
fill: "none",
|
|
143
|
+
stroke: ink,
|
|
144
|
+
"stroke-width": 2.2,
|
|
145
|
+
class: "string",
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
else
|
|
149
|
+
pen.el("circle", {
|
|
150
|
+
cx: c.x,
|
|
151
|
+
cy: c.y,
|
|
152
|
+
r,
|
|
153
|
+
fill: "none",
|
|
154
|
+
stroke: ink,
|
|
155
|
+
"stroke-width": 2.2,
|
|
156
|
+
class: "string",
|
|
157
|
+
});
|
|
158
|
+
drawBeads(pen, k.beads, angles.map((a) => at(c, r, a)));
|
|
159
|
+
if (k.clasp)
|
|
160
|
+
claspAt(pen, at(c, r, top), 0);
|
|
161
|
+
if (k.cut !== undefined) {
|
|
162
|
+
const i = k.cut - 1;
|
|
163
|
+
const a = angles[i];
|
|
164
|
+
const b = i + 1 < angles.length ? angles[i + 1] : angles[0] + 2 * Math.PI;
|
|
165
|
+
const mid = (a + b) / 2;
|
|
166
|
+
scissors(pen, at(c, r - 11, mid), at(c, r + 11, mid), at(c, r + 31, mid));
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
function drawOpen(pen, k, x, y) {
|
|
170
|
+
const ends = k.clasp ? 30 : 16;
|
|
171
|
+
const first = x + ends + bead / 2;
|
|
172
|
+
const places = k.beads.map((_, i) => ({ x: first + i * pitch, y }));
|
|
173
|
+
const last = places[places.length - 1].x;
|
|
174
|
+
pen.el("path", {
|
|
175
|
+
d: `M${n(first - bead / 2 - ends + 6)} ${n(y)}H${n(last + bead / 2 + ends - 6)}`,
|
|
176
|
+
stroke: ink,
|
|
177
|
+
"stroke-width": 2.2,
|
|
178
|
+
"stroke-linecap": "round",
|
|
179
|
+
class: "string",
|
|
180
|
+
});
|
|
181
|
+
if (k.clasp) {
|
|
182
|
+
pen.add('<g class="clasp">');
|
|
183
|
+
pen.el("circle", {
|
|
184
|
+
cx: first - bead / 2 - ends + 9,
|
|
185
|
+
cy: y,
|
|
186
|
+
r: 5,
|
|
187
|
+
fill: "none",
|
|
188
|
+
stroke: colors.yellow.dark,
|
|
189
|
+
"stroke-width": 3,
|
|
190
|
+
});
|
|
191
|
+
pen.el("ellipse", {
|
|
192
|
+
cx: last + bead / 2 + ends - 11,
|
|
193
|
+
cy: y,
|
|
194
|
+
rx: 8,
|
|
195
|
+
ry: 5,
|
|
196
|
+
fill: colors.yellow.fill,
|
|
197
|
+
stroke: ink,
|
|
198
|
+
"stroke-width": 2,
|
|
199
|
+
});
|
|
200
|
+
pen.add("</g>");
|
|
201
|
+
}
|
|
202
|
+
else
|
|
203
|
+
for (const end of [first - bead / 2 - ends + 6, last + bead / 2 + ends - 6])
|
|
204
|
+
pen.el("circle", { cx: end, cy: y, r: 3.5, fill: ink });
|
|
205
|
+
drawBeads(pen, k.beads, places);
|
|
206
|
+
if (k.cut !== undefined) {
|
|
207
|
+
const cx = first + (k.cut - 0.5) * pitch;
|
|
208
|
+
scissors(pen, { x: cx, y: y - 12 }, { x: cx, y: y + 12 }, { x: cx, y: y + 34 });
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
function checkNecklace(value, path, problems) {
|
|
212
|
+
if (!isObj(value)) {
|
|
213
|
+
problems.push(`${path}: must be a necklace: an object with beads`);
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
only(value, ["beads", "open", "clasp", "cut", "label", "newRow"], path, problems);
|
|
217
|
+
if (value.newRow !== undefined && typeof value.newRow !== "boolean")
|
|
218
|
+
problems.push(`${path}.newRow: must be true or false`);
|
|
219
|
+
if (value.open !== undefined && typeof value.open !== "boolean")
|
|
220
|
+
problems.push(`${path}.open: must be true or false`);
|
|
221
|
+
const open = value.open === true;
|
|
222
|
+
const beads = list(value.beads, `${path}.beads`, problems, open ? 2 : 3, mostBeads);
|
|
223
|
+
beads.forEach((b, i) => {
|
|
224
|
+
const mark = problems.length;
|
|
225
|
+
checkThing(b, `${path}.beads[${i}]`, problems);
|
|
226
|
+
if (problems.length > mark)
|
|
227
|
+
return;
|
|
228
|
+
const small = smallText(b, beadSize(b), `${path}.beads[${i}]`);
|
|
229
|
+
if (small)
|
|
230
|
+
problems.push(`${small}; write on a card bead of one or two characters`);
|
|
231
|
+
});
|
|
232
|
+
if (value.clasp !== undefined && typeof value.clasp !== "boolean")
|
|
233
|
+
problems.push(`${path}.clasp: must be true or false`);
|
|
234
|
+
if (value.cut !== undefined && beads.length >= 2) {
|
|
235
|
+
if (!open && value.clasp === true && value.cut === beads.length)
|
|
236
|
+
problems.push(`${path}.cut: the clasp stands between the last bead and the first; cut after another bead`);
|
|
237
|
+
else
|
|
238
|
+
int(value.cut, `${path}.cut`, problems, 1, open ? beads.length - 1 : beads.length);
|
|
239
|
+
}
|
|
240
|
+
if (value.label !== undefined)
|
|
241
|
+
text(value.label, `${path}.label`, problems, 4);
|
|
242
|
+
}
|
|
243
|
+
const ordinals = [
|
|
244
|
+
"1st",
|
|
245
|
+
"2nd",
|
|
246
|
+
"3rd",
|
|
247
|
+
"4th",
|
|
248
|
+
"5th",
|
|
249
|
+
"6th",
|
|
250
|
+
"7th",
|
|
251
|
+
"8th",
|
|
252
|
+
"9th",
|
|
253
|
+
"10th",
|
|
254
|
+
"11th",
|
|
255
|
+
"12th",
|
|
256
|
+
"13th",
|
|
257
|
+
"14th",
|
|
258
|
+
"15th",
|
|
259
|
+
"16th",
|
|
260
|
+
];
|
|
261
|
+
function beadWord(b) {
|
|
262
|
+
if (typeof b !== "string" && "ball" in b)
|
|
263
|
+
return `${colors[b.ball].word} bead`;
|
|
264
|
+
return wordOf(b);
|
|
265
|
+
}
|
|
266
|
+
function sayOne(k) {
|
|
267
|
+
const count = k.beads.length;
|
|
268
|
+
const words = k.beads.map(beadWord).join(", ");
|
|
269
|
+
if (k.open) {
|
|
270
|
+
const clasp = k.clasp ? ", the two halves of a clasp at its ends" : "";
|
|
271
|
+
const cut = k.cut !== undefined
|
|
272
|
+
? ` Scissors mark a cut between the ${ordinals[k.cut - 1]} bead and the ${ordinals[k.cut]}.`
|
|
273
|
+
: "";
|
|
274
|
+
return `an open string of ${count} beads${clasp}, from the left: ${words}.${cut}`;
|
|
275
|
+
}
|
|
276
|
+
const cut = k.cut !== undefined
|
|
277
|
+
? ` Scissors mark a cut between the ${ordinals[k.cut - 1]} bead and the ${k.cut === count ? "1st" : ordinals[k.cut]}.`
|
|
278
|
+
: "";
|
|
279
|
+
return k.clasp
|
|
280
|
+
? `a closed necklace of ${count} beads with a clasp at the top; clockwise from the clasp: ${words}.${cut}`
|
|
281
|
+
: `a closed necklace of ${count} beads; clockwise from the top: ${words}.${cut}`;
|
|
282
|
+
}
|
|
283
|
+
export const necklace = {
|
|
284
|
+
kind: "necklace",
|
|
285
|
+
members: ["kind", "necklaces"],
|
|
286
|
+
check(scene, problems) {
|
|
287
|
+
only(scene, this.members, "scene", problems);
|
|
288
|
+
list(scene.necklaces, "necklaces", problems, 1, 6).forEach((k, i) => {
|
|
289
|
+
checkNecklace(k, `necklaces[${i}]`, problems);
|
|
290
|
+
});
|
|
291
|
+
if (problems.length > 0)
|
|
292
|
+
return;
|
|
293
|
+
const s = scene;
|
|
294
|
+
const r = radiusOf(s);
|
|
295
|
+
s.necklaces.forEach((k, i) => {
|
|
296
|
+
const { w } = sizeOf(k, r);
|
|
297
|
+
if (w > wide)
|
|
298
|
+
problems.push(`necklaces[${i}].beads: ${k.beads.length} beads make it ${Math.round(w)} px wide, more than ${wide}; use fewer beads${k.clasp ? " or no clasp" : ""}`);
|
|
299
|
+
});
|
|
300
|
+
},
|
|
301
|
+
draw(scene, pen) {
|
|
302
|
+
const s = scene;
|
|
303
|
+
const r = radiusOf(s);
|
|
304
|
+
const rows = rowsOf(s);
|
|
305
|
+
const width = Math.max(...rows.map((row) => row.w));
|
|
306
|
+
let y = margin;
|
|
307
|
+
for (const row of rows) {
|
|
308
|
+
let x = margin + (width - row.w) / 2;
|
|
309
|
+
for (const item of row.items) {
|
|
310
|
+
const k = item.k;
|
|
311
|
+
const body = row.h - (row.items.some((o) => o.k.label) ? labelH : 0);
|
|
312
|
+
const cy = y + body / 2;
|
|
313
|
+
pen.add('<g class="necklace">');
|
|
314
|
+
if (k.open)
|
|
315
|
+
drawOpen(pen, k, x, cy - (k.cut ? 18 : 0));
|
|
316
|
+
else
|
|
317
|
+
drawClosed(pen, k, { x: x + item.w / 2, y: cy }, r);
|
|
318
|
+
pen.add("</g>");
|
|
319
|
+
if (k.label)
|
|
320
|
+
pen.text(x + item.w / 2, y + body + labelH / 2, k.label, { size: 18 });
|
|
321
|
+
x += item.w + colGap;
|
|
322
|
+
}
|
|
323
|
+
y += row.h + rowGap;
|
|
324
|
+
}
|
|
325
|
+
return { width: width + 2 * margin, height: y - rowGap + margin };
|
|
326
|
+
},
|
|
327
|
+
describe(scene) {
|
|
328
|
+
const s = scene;
|
|
329
|
+
if (s.necklaces.length === 1) {
|
|
330
|
+
const one = sayOne(s.necklaces[0]);
|
|
331
|
+
return `${one[0].toUpperCase()}${one.slice(1)}${s.necklaces[0].label ? ` It is labelled ${s.necklaces[0].label}.` : ""}`;
|
|
332
|
+
}
|
|
333
|
+
const rows = rowsOf(s);
|
|
334
|
+
// A necklace with no label is named by its place, said not to be a label where others have one.
|
|
335
|
+
const labelled = s.necklaces.some((k) => k.label);
|
|
336
|
+
const nameOf = (k, i) => k.label ? `Necklace ${k.label}` : `Necklace ${i + 1}${labelled ? ", with no label" : ""}`;
|
|
337
|
+
return [
|
|
338
|
+
`${s.necklaces.length} necklaces${rows.length > 1 ? ` in ${rows.length} rows, read row by row` : ""}, from the left.`,
|
|
339
|
+
...s.necklaces.map((k, i) => `${nameOf(k, i)}: ${sayOne(k)}`),
|
|
340
|
+
].join(" ");
|
|
341
|
+
},
|
|
342
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { Kind } from "../kind.ts";
|
|
2
|
+
import { type ColorName } from "../style.ts";
|
|
3
|
+
import { type Thing } from "../things.ts";
|
|
4
|
+
/**
|
|
5
|
+
* Net: one to four nets of a cube side by side, each six squares that fold
|
|
6
|
+
* into a cube. rows draw the net, "#" a square and "." none, from the top row
|
|
7
|
+
* down; faces fill its squares in reading order, left to right, row by row.
|
|
8
|
+
* A face is blank, or coloured, and may carry pips, a short text, a thing (a
|
|
9
|
+
* sticker), or the "?" the question asks about. The check proves the six
|
|
10
|
+
* squares fold into a cube; with standard, the pips are a standard die's,
|
|
11
|
+
* opposite faces adding up to 7.
|
|
12
|
+
*/
|
|
13
|
+
export type NetFace = {
|
|
14
|
+
color?: ColorName;
|
|
15
|
+
pips?: number;
|
|
16
|
+
text?: string;
|
|
17
|
+
thing?: Thing;
|
|
18
|
+
ask?: true;
|
|
19
|
+
};
|
|
20
|
+
export type Net = {
|
|
21
|
+
rows: string[];
|
|
22
|
+
faces: NetFace[];
|
|
23
|
+
standard?: boolean;
|
|
24
|
+
label?: string;
|
|
25
|
+
};
|
|
26
|
+
export type NetScene = {
|
|
27
|
+
kind: "net";
|
|
28
|
+
nets: Net[];
|
|
29
|
+
};
|
|
30
|
+
/** cellsOf lists the squares of a net's rows, in reading order, as column and row. */
|
|
31
|
+
export declare function cellsOf(rows: readonly string[]): {
|
|
32
|
+
col: number;
|
|
33
|
+
row: number;
|
|
34
|
+
}[];
|
|
35
|
+
type Side = "U" | "D" | "N" | "S" | "E" | "W";
|
|
36
|
+
/**
|
|
37
|
+
* fold rolls a cube over the squares, from the first, and says which face of
|
|
38
|
+
* the cube lies on each: the squares make a net of a cube exactly when the
|
|
39
|
+
* six faces are all different. It gives undefined when the squares do not
|
|
40
|
+
* hold together edge to edge.
|
|
41
|
+
*/
|
|
42
|
+
export declare function fold(cells: readonly {
|
|
43
|
+
col: number;
|
|
44
|
+
row: number;
|
|
45
|
+
}[]): Side[] | undefined;
|
|
46
|
+
export declare const net: Kind;
|
|
47
|
+
export {};
|
package/lib/kinds/net.js
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { margin } from "../kind.js";
|
|
2
|
+
import { textWidth } from "../pen.js";
|
|
3
|
+
import { int, isObj, list, only, text } from "../read.js";
|
|
4
|
+
import { colorNames, colors, ink, isColor, outline, textOn } from "../style.js";
|
|
5
|
+
import { checkThing, drawThing, wordOf } from "../things.js";
|
|
6
|
+
import { article, box } from "./cubes.js";
|
|
7
|
+
import { pips } from "./die.js";
|
|
8
|
+
const biggest = 64;
|
|
9
|
+
const smallest = 34;
|
|
10
|
+
const room = 640 - 2 * margin;
|
|
11
|
+
const gap = 32;
|
|
12
|
+
const marks = ["pips", "text", "thing", "ask"];
|
|
13
|
+
/** cellsOf lists the squares of a net's rows, in reading order, as column and row. */
|
|
14
|
+
export function cellsOf(rows) {
|
|
15
|
+
return rows.flatMap((line, row) => [...line].flatMap((c, col) => (c === "#" ? [{ col, row }] : [])));
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* fold rolls a cube over the squares, from the first, and says which face of
|
|
19
|
+
* the cube lies on each: the squares make a net of a cube exactly when the
|
|
20
|
+
* six faces are all different. It gives undefined when the squares do not
|
|
21
|
+
* hold together edge to edge.
|
|
22
|
+
*/
|
|
23
|
+
export function fold(cells) {
|
|
24
|
+
const at = new Map(cells.map((c, i) => [`${c.col},${c.row}`, i]));
|
|
25
|
+
const got = cells.map(() => undefined);
|
|
26
|
+
const start = { U: "U", D: "D", N: "N", S: "S", E: "E", W: "W" };
|
|
27
|
+
const queue = [{ i: 0, cube: start }];
|
|
28
|
+
got[0] = "D";
|
|
29
|
+
const roll = (c, dir) => {
|
|
30
|
+
if (dir === "E")
|
|
31
|
+
return { ...c, D: c.E, E: c.U, U: c.W, W: c.D };
|
|
32
|
+
if (dir === "W")
|
|
33
|
+
return { ...c, D: c.W, W: c.U, U: c.E, E: c.D };
|
|
34
|
+
if (dir === "S")
|
|
35
|
+
return { ...c, D: c.S, S: c.U, U: c.N, N: c.D };
|
|
36
|
+
return { ...c, D: c.N, N: c.U, U: c.S, S: c.D };
|
|
37
|
+
};
|
|
38
|
+
const steps = [
|
|
39
|
+
["E", 1, 0],
|
|
40
|
+
["W", -1, 0],
|
|
41
|
+
["S", 0, 1],
|
|
42
|
+
["N", 0, -1],
|
|
43
|
+
];
|
|
44
|
+
while (queue.length > 0) {
|
|
45
|
+
const { i, cube } = queue.shift();
|
|
46
|
+
const c = cells[i];
|
|
47
|
+
for (const [dir, dc, dr] of steps) {
|
|
48
|
+
const j = at.get(`${c.col + dc},${c.row + dr}`);
|
|
49
|
+
if (j === undefined || got[j] !== undefined)
|
|
50
|
+
continue;
|
|
51
|
+
const next = roll(cube, dir);
|
|
52
|
+
got[j] = next.D;
|
|
53
|
+
queue.push({ i: j, cube: next });
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return got.every((g) => g !== undefined) ? got : undefined;
|
|
57
|
+
}
|
|
58
|
+
const opposite = { U: "D", D: "U", N: "S", S: "N", E: "W", W: "E" };
|
|
59
|
+
function checkFace(f, path, problems) {
|
|
60
|
+
if (!isObj(f)) {
|
|
61
|
+
problems.push(`${path}: must be a face: {} for a blank one, or color, pips, text, thing, ask`);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
only(f, ["color", ...marks], path, problems);
|
|
65
|
+
if (f.color !== undefined && !isColor(f.color))
|
|
66
|
+
problems.push(`${path}.color: must be a colour: ${colorNames.join(", ")}`);
|
|
67
|
+
const carried = marks.filter((m) => f[m] !== undefined);
|
|
68
|
+
if (carried.length > 1)
|
|
69
|
+
problems.push(`${path}: carries at most one of pips, text, thing, ask, not ${carried.join(" and ")}`);
|
|
70
|
+
if (f.pips !== undefined)
|
|
71
|
+
int(f.pips, `${path}.pips`, problems, 1, 6);
|
|
72
|
+
if (f.text !== undefined)
|
|
73
|
+
text(f.text, `${path}.text`, problems, 3);
|
|
74
|
+
if (f.thing !== undefined)
|
|
75
|
+
checkThing(f.thing, `${path}.thing`, problems);
|
|
76
|
+
if (f.ask !== undefined && f.ask !== true)
|
|
77
|
+
problems.push(`${path}.ask: must be true`);
|
|
78
|
+
}
|
|
79
|
+
function checkNet(net, path, problems) {
|
|
80
|
+
only(net, ["rows", "faces", "standard", "label"], path, problems);
|
|
81
|
+
if (net.label !== undefined)
|
|
82
|
+
text(net.label, `${path}.label`, problems, 10);
|
|
83
|
+
if (net.standard !== undefined && typeof net.standard !== "boolean")
|
|
84
|
+
problems.push(`${path}.standard: must be true or false`);
|
|
85
|
+
const before = problems.length;
|
|
86
|
+
const rows = list(net.rows, `${path}.rows`, problems, 1, 6);
|
|
87
|
+
rows.forEach((line, r) => {
|
|
88
|
+
if (typeof line !== "string" || !/^[#.]{1,6}$/.test(line))
|
|
89
|
+
problems.push(`${path}.rows[${r}]: must be 1 to 6 of "#" (a square) and "." (none)`);
|
|
90
|
+
});
|
|
91
|
+
list(net.faces, `${path}.faces`, problems, 6, 6).forEach((f, j) => {
|
|
92
|
+
checkFace(f, `${path}.faces[${j}]`, problems);
|
|
93
|
+
});
|
|
94
|
+
if (problems.length > before)
|
|
95
|
+
return;
|
|
96
|
+
const cells = cellsOf(rows);
|
|
97
|
+
if (cells.length !== 6) {
|
|
98
|
+
problems.push(`${path}.rows: must hold exactly 6 squares "#", not ${cells.length}`);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const sides = fold(cells);
|
|
102
|
+
if (!sides) {
|
|
103
|
+
problems.push(`${path}.rows: the squares must hold together edge to edge`);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
if (new Set(sides).size < 6) {
|
|
107
|
+
problems.push(`${path}.rows: these 6 squares do not fold into a cube; use one of the 11 nets of a cube`);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
if (net.standard !== true)
|
|
111
|
+
return;
|
|
112
|
+
const faces = net.faces;
|
|
113
|
+
faces.forEach((f, j) => {
|
|
114
|
+
if (f.pips === undefined && f.ask === undefined)
|
|
115
|
+
problems.push(`${path}.faces[${j}]: with standard, every face has pips or ask`);
|
|
116
|
+
faces.forEach((g, k) => {
|
|
117
|
+
if (k >= j || f.pips === undefined || g.pips === undefined)
|
|
118
|
+
return;
|
|
119
|
+
if (f.pips === g.pips)
|
|
120
|
+
problems.push(`${path}.faces[${j}].pips: ${f.pips} is on faces[${k}] too`);
|
|
121
|
+
else if (sides[k] === opposite[sides[j]] && f.pips + g.pips !== 7)
|
|
122
|
+
problems.push(`${path}.faces[${j}].pips: it folds opposite faces[${k}], and ${g.pips} + ${f.pips} is not 7`);
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
function spanOf(net) {
|
|
127
|
+
const cells = cellsOf(net.rows);
|
|
128
|
+
return {
|
|
129
|
+
cols: Math.max(...cells.map((c) => c.col)) - Math.min(...cells.map((c) => c.col)) + 1,
|
|
130
|
+
rows: Math.max(...cells.map((c) => c.row)) - Math.min(...cells.map((c) => c.row)) + 1,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
function sizeOf(nets) {
|
|
134
|
+
const cols = nets.reduce((sum, net) => sum + spanOf(net).cols, 0);
|
|
135
|
+
return Math.min(biggest, (room - gap * (nets.length - 1)) / cols);
|
|
136
|
+
}
|
|
137
|
+
function drawFace(pen, f, x, y, s) {
|
|
138
|
+
const color = f.color ?? "white";
|
|
139
|
+
pen.el("rect", {
|
|
140
|
+
x,
|
|
141
|
+
y,
|
|
142
|
+
width: s,
|
|
143
|
+
height: s,
|
|
144
|
+
fill: colors[color].fill,
|
|
145
|
+
stroke: ink,
|
|
146
|
+
"stroke-width": outline,
|
|
147
|
+
"stroke-linejoin": "round",
|
|
148
|
+
});
|
|
149
|
+
const [cx, cy] = [x + s / 2, y + s / 2];
|
|
150
|
+
if (f.pips !== undefined) {
|
|
151
|
+
const k = s / box;
|
|
152
|
+
for (const [px, py] of pips[f.pips])
|
|
153
|
+
pen.el("circle", {
|
|
154
|
+
cx: x + px * k,
|
|
155
|
+
cy: y + py * k,
|
|
156
|
+
r: 6.8 * k,
|
|
157
|
+
fill: textOn(color) === ink ? ink : "#ffffff",
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
else if (f.text !== undefined)
|
|
161
|
+
pen.text(cx, cy, f.text, {
|
|
162
|
+
size: Math.min(s * 0.5, (s * 0.8) / Math.max(1, textWidth(f.text, 1))),
|
|
163
|
+
fill: textOn(color),
|
|
164
|
+
});
|
|
165
|
+
else if (f.thing !== undefined)
|
|
166
|
+
drawThing(pen, f.thing, cx, cy, s * 0.72);
|
|
167
|
+
else if (f.ask)
|
|
168
|
+
drawThing(pen, { ask: true }, cx, cy, s * 0.8);
|
|
169
|
+
}
|
|
170
|
+
function sayFace(f) {
|
|
171
|
+
const color = f.color ? colors[f.color].word : "white";
|
|
172
|
+
if (f.pips !== undefined)
|
|
173
|
+
return `${color} with ${f.pips} pip${f.pips === 1 ? "" : "s"}`;
|
|
174
|
+
if (f.text !== undefined)
|
|
175
|
+
return `${color} with "${f.text}"`;
|
|
176
|
+
if (f.thing !== undefined)
|
|
177
|
+
return `${color} with ${article(wordOf(f.thing))}`;
|
|
178
|
+
if (f.ask)
|
|
179
|
+
return `${color} with a question mark`;
|
|
180
|
+
return `${color}, blank`;
|
|
181
|
+
}
|
|
182
|
+
function sayNet(net) {
|
|
183
|
+
const cells = cellsOf(net.rows);
|
|
184
|
+
const top = Math.min(...cells.map((c) => c.row));
|
|
185
|
+
const left = Math.min(...cells.map((c) => c.col));
|
|
186
|
+
const rows = [...new Set(cells.map((c) => c.row))].map((r) => {
|
|
187
|
+
const cols = cells.filter((c) => c.row === r).map((c) => c.col - left + 1);
|
|
188
|
+
return `row ${r - top + 1}: column${cols.length === 1 ? "" : "s"} ${cols.join(", ")}`;
|
|
189
|
+
});
|
|
190
|
+
const faces = net.faces.map((f, i) => `${i + 1}) ${sayFace(f)}`);
|
|
191
|
+
return `6 squares in ${rows.length} rows (${rows.join("; ")}). The squares in reading order, left to right and row by row: ${faces.join("; ")}.${net.standard ? " The pips are those of a standard die: opposite faces add up to 7." : ""}`;
|
|
192
|
+
}
|
|
193
|
+
export const net = {
|
|
194
|
+
kind: "net",
|
|
195
|
+
members: ["kind", "nets"],
|
|
196
|
+
check(scene, problems) {
|
|
197
|
+
only(scene, this.members, "scene", problems);
|
|
198
|
+
list(scene.nets, "nets", problems, 1, 4).forEach((one, i) => {
|
|
199
|
+
if (!isObj(one))
|
|
200
|
+
problems.push(`nets[${i}]: must be a net`);
|
|
201
|
+
else
|
|
202
|
+
checkNet(one, `nets[${i}]`, problems);
|
|
203
|
+
});
|
|
204
|
+
if (problems.length > 0)
|
|
205
|
+
return;
|
|
206
|
+
if (sizeOf(scene.nets) < smallest)
|
|
207
|
+
problems.push(`nets: too wide to draw side by side with squares of at least ${smallest} pixels; use fewer nets, or nets of fewer columns`);
|
|
208
|
+
},
|
|
209
|
+
draw(scene, pen) {
|
|
210
|
+
const nets = scene.nets;
|
|
211
|
+
const s = sizeOf(nets);
|
|
212
|
+
const high = Math.max(...nets.map((one) => spanOf(one).rows)) * s;
|
|
213
|
+
const labelled = nets.some((one) => one.label);
|
|
214
|
+
let x = margin;
|
|
215
|
+
for (const one of nets) {
|
|
216
|
+
const span = spanOf(one);
|
|
217
|
+
const cells = cellsOf(one.rows);
|
|
218
|
+
const top = Math.min(...cells.map((c) => c.row));
|
|
219
|
+
const left = Math.min(...cells.map((c) => c.col));
|
|
220
|
+
const w = span.cols * s;
|
|
221
|
+
const bw = Math.max(w, one.label ? textWidth(one.label, 18) : 0);
|
|
222
|
+
const ox = x + (bw - w) / 2;
|
|
223
|
+
const oy = margin + (high - span.rows * s) / 2;
|
|
224
|
+
cells.forEach((c, i) => {
|
|
225
|
+
drawFace(pen, one.faces[i], ox + (c.col - left) * s, oy + (c.row - top) * s, s);
|
|
226
|
+
});
|
|
227
|
+
if (one.label)
|
|
228
|
+
pen.text(x + bw / 2, margin + high + 24, one.label, { size: 18 });
|
|
229
|
+
x += bw + gap;
|
|
230
|
+
}
|
|
231
|
+
return { width: x - gap + margin, height: margin + high + (labelled ? 40 : 0) + margin };
|
|
232
|
+
},
|
|
233
|
+
describe(scene) {
|
|
234
|
+
const nets = scene.nets;
|
|
235
|
+
if (nets.length === 1)
|
|
236
|
+
return `A net of a cube: ${sayNet(nets[0])}`;
|
|
237
|
+
return `${nets.length} nets of a cube, side by side. ${nets
|
|
238
|
+
.map((one, i) => `Net ${one.label ?? i + 1}: ${sayNet(one)}`)
|
|
239
|
+
.join(" ")}`;
|
|
240
|
+
},
|
|
241
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { Kind } from "../kind.ts";
|
|
2
|
+
import { type ColorName } from "../style.ts";
|
|
3
|
+
import { type Part } from "./fold-parts.ts";
|
|
4
|
+
/**
|
|
5
|
+
* Overlay: two to four transparent cards of the same grid of squares, side by
|
|
6
|
+
* side, each with marks in its squares — a square or a part of one filled
|
|
7
|
+
* with a colour, or a shape in the middle of a square — and each maybe flipped
|
|
8
|
+
* over or turned before it is laid on the ones before it, as a sign under it
|
|
9
|
+
* shows. The picture may end with what is seen through the cards laid
|
|
10
|
+
* together, worked out by the program part by part, or with a "?"; or it may
|
|
11
|
+
* show that view alone, as an option of a choice. The marks are opaque: where
|
|
12
|
+
* two lie on each other, the one on the upper card is seen, and the first
|
|
13
|
+
* card lies at the bottom.
|
|
14
|
+
*
|
|
15
|
+
* A place is [row, column], counted from 1 at the top left of a card as it is
|
|
16
|
+
* drawn. A filled part is "whole" (the default), "nw", "ne", "se", "sw" — the
|
|
17
|
+
* half at that corner of the square cut along a diagonal — or "top", "right",
|
|
18
|
+
* "bottom", "left", the quarter between that side and the centre.
|
|
19
|
+
*/
|
|
20
|
+
export declare const markShapes: readonly ["circle", "triangle", "star", "heart", "diamond", "moon", "cross"];
|
|
21
|
+
export type MarkShape = (typeof markShapes)[number];
|
|
22
|
+
export declare const flips: readonly ["none", "horizontal", "vertical"];
|
|
23
|
+
export type Flip = (typeof flips)[number];
|
|
24
|
+
export declare const turns: readonly [0, 90, 180, 270];
|
|
25
|
+
export declare const shows: readonly ["ask", "result", "cards", "together"];
|
|
26
|
+
export type Show = (typeof shows)[number];
|
|
27
|
+
export type Mark = {
|
|
28
|
+
at: [number, number];
|
|
29
|
+
part?: Part;
|
|
30
|
+
color?: ColorName;
|
|
31
|
+
} | {
|
|
32
|
+
at: [number, number];
|
|
33
|
+
shape: MarkShape;
|
|
34
|
+
color?: ColorName;
|
|
35
|
+
turn?: number;
|
|
36
|
+
};
|
|
37
|
+
export type Card = {
|
|
38
|
+
marks: Mark[];
|
|
39
|
+
turn?: number;
|
|
40
|
+
flip?: Flip;
|
|
41
|
+
label?: string;
|
|
42
|
+
};
|
|
43
|
+
export type OverlayScene = {
|
|
44
|
+
kind: "overlay";
|
|
45
|
+
rows: number;
|
|
46
|
+
cols: number;
|
|
47
|
+
cards: Card[];
|
|
48
|
+
show?: Show;
|
|
49
|
+
};
|
|
50
|
+
export declare const overlay: Kind;
|