@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,519 @@
|
|
|
1
|
+
import { margin } from "../kind.js";
|
|
2
|
+
import { int, isObj, list, oneOf, only, text } from "../read.js";
|
|
3
|
+
import { colorNames, colors, faint, ink, isColor, outline, soft, } from "../style.js";
|
|
4
|
+
import { n, poly } from "../svg.js";
|
|
5
|
+
import { drawThing } from "../things.js";
|
|
6
|
+
import { andThen, article, atomsOf, cellWords, identity, key, loopsOf, mapAtom, parts, turnMat, turnOf, ways, } from "./fold-parts.js";
|
|
7
|
+
/**
|
|
8
|
+
* Overlay: two to four transparent cards of the same grid of squares, side by
|
|
9
|
+
* side, each with marks in its squares — a square or a part of one filled
|
|
10
|
+
* with a colour, or a shape in the middle of a square — and each maybe flipped
|
|
11
|
+
* over or turned before it is laid on the ones before it, as a sign under it
|
|
12
|
+
* shows. The picture may end with what is seen through the cards laid
|
|
13
|
+
* together, worked out by the program part by part, or with a "?"; or it may
|
|
14
|
+
* show that view alone, as an option of a choice. The marks are opaque: where
|
|
15
|
+
* two lie on each other, the one on the upper card is seen, and the first
|
|
16
|
+
* card lies at the bottom.
|
|
17
|
+
*
|
|
18
|
+
* A place is [row, column], counted from 1 at the top left of a card as it is
|
|
19
|
+
* drawn. A filled part is "whole" (the default), "nw", "ne", "se", "sw" — the
|
|
20
|
+
* half at that corner of the square cut along a diagonal — or "top", "right",
|
|
21
|
+
* "bottom", "left", the quarter between that side and the centre.
|
|
22
|
+
*/
|
|
23
|
+
export const markShapes = [
|
|
24
|
+
"circle",
|
|
25
|
+
"triangle",
|
|
26
|
+
"star",
|
|
27
|
+
"heart",
|
|
28
|
+
"diamond",
|
|
29
|
+
"moon",
|
|
30
|
+
"cross",
|
|
31
|
+
];
|
|
32
|
+
export const flips = ["none", "horizontal", "vertical"];
|
|
33
|
+
export const turns = [0, 90, 180, 270];
|
|
34
|
+
export const shows = ["ask", "result", "cards", "together"];
|
|
35
|
+
const most = { side: 6, cards: 4, marks: 36 };
|
|
36
|
+
const biggest = 44;
|
|
37
|
+
const smallest = 20;
|
|
38
|
+
const room = 640 - 2 * margin - 2 * outline;
|
|
39
|
+
const join = 44;
|
|
40
|
+
const labelRow = 28;
|
|
41
|
+
const signRow = 46;
|
|
42
|
+
/** The way each shape points upright, as an index of ways; none for one that looks the same turned. */
|
|
43
|
+
const shapeWay = {
|
|
44
|
+
circle: undefined,
|
|
45
|
+
diamond: undefined,
|
|
46
|
+
cross: undefined,
|
|
47
|
+
triangle: 0,
|
|
48
|
+
star: 0,
|
|
49
|
+
heart: 2,
|
|
50
|
+
moon: 1,
|
|
51
|
+
};
|
|
52
|
+
function isShape(mark) {
|
|
53
|
+
return "shape" in mark;
|
|
54
|
+
}
|
|
55
|
+
/** moveOf is the map that lays a card down: its flip first, then its turn. */
|
|
56
|
+
function moveOf(card, rows, cols) {
|
|
57
|
+
const flip = card.flip ?? "none";
|
|
58
|
+
let m = identity;
|
|
59
|
+
if (flip === "horizontal")
|
|
60
|
+
m = [-1, 0, 0, 1, cols, 0];
|
|
61
|
+
if (flip === "vertical")
|
|
62
|
+
m = [1, 0, 0, -1, 0, rows];
|
|
63
|
+
const turn = card.turn ?? 0;
|
|
64
|
+
if (turn === 90)
|
|
65
|
+
m = andThen(m, [0, 1, -1, 0, rows, 0]);
|
|
66
|
+
if (turn === 180)
|
|
67
|
+
m = andThen(m, [-1, 0, 0, -1, cols, rows]);
|
|
68
|
+
if (turn === 270)
|
|
69
|
+
m = andThen(m, [0, -1, 1, 0, 0, cols]);
|
|
70
|
+
return m;
|
|
71
|
+
}
|
|
72
|
+
function lay(card, m) {
|
|
73
|
+
const fills = new Map();
|
|
74
|
+
const shapes = new Map();
|
|
75
|
+
card.marks.forEach((mark, i) => {
|
|
76
|
+
const r = mark.at[0] - 1;
|
|
77
|
+
const c = mark.at[1] - 1;
|
|
78
|
+
if (isShape(mark)) {
|
|
79
|
+
const to = mapAtom({ r, c, q: 0 }, m);
|
|
80
|
+
const way = shapeWay[mark.shape];
|
|
81
|
+
const pose = andThen(turnMat(mark.turn ?? 0), [m[0], m[1], m[2], m[3], 0, 0]);
|
|
82
|
+
shapes.set(`${to.r},${to.c}`, {
|
|
83
|
+
shape: mark.shape,
|
|
84
|
+
color: mark.color ?? "black",
|
|
85
|
+
turn: way === undefined ? 0 : turnOf(pose, way),
|
|
86
|
+
mark: i,
|
|
87
|
+
});
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
for (const a of atomsOf(r, c, mark.part ?? "whole")) {
|
|
91
|
+
const to = mapAtom(a, m);
|
|
92
|
+
fills.set(key(to), { color: mark.color ?? "black", atom: to, mark: i });
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
return { fills, shapes };
|
|
96
|
+
}
|
|
97
|
+
/** together lays every card on the ones before it and says what is seen. */
|
|
98
|
+
function together(scene, moved = true) {
|
|
99
|
+
const out = { fills: new Map(), shapes: new Map() };
|
|
100
|
+
for (const card of scene.cards) {
|
|
101
|
+
const laid = lay(card, moved ? moveOf(card, scene.rows, scene.cols) : identity);
|
|
102
|
+
for (const [k, v] of laid.fills)
|
|
103
|
+
out.fills.set(k, v);
|
|
104
|
+
for (const [k, v] of laid.shapes)
|
|
105
|
+
out.shapes.set(k, v);
|
|
106
|
+
}
|
|
107
|
+
return out;
|
|
108
|
+
}
|
|
109
|
+
// Checking.
|
|
110
|
+
function checkMark(mark, path, scene, problems) {
|
|
111
|
+
if (!isObj(mark)) {
|
|
112
|
+
problems.push(`${path}: must be a mark: at, and a part or a shape`);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (!Array.isArray(mark.at) || mark.at.length !== 2)
|
|
116
|
+
problems.push(`${path}.at: must be [row, column], counted from 1 at the top left of the card`);
|
|
117
|
+
else {
|
|
118
|
+
int(mark.at[0], `${path}.at[0]`, problems, 1, Number(scene.rows));
|
|
119
|
+
int(mark.at[1], `${path}.at[1]`, problems, 1, Number(scene.cols));
|
|
120
|
+
}
|
|
121
|
+
if (mark.color !== undefined && !isColor(mark.color))
|
|
122
|
+
problems.push(`${path}.color: must be a colour: ${colorNames.join(", ")}`);
|
|
123
|
+
if ("shape" in mark) {
|
|
124
|
+
only(mark, ["at", "shape", "color", "turn"], path, problems);
|
|
125
|
+
oneOf(mark.shape, markShapes, `${path}.shape`, problems);
|
|
126
|
+
if (mark.turn !== undefined && !turns.includes(mark.turn))
|
|
127
|
+
problems.push(`${path}.turn: must be one of ${turns.join(", ")}, clockwise`);
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
only(mark, ["at", "part", "color"], path, problems);
|
|
131
|
+
if (mark.part !== undefined)
|
|
132
|
+
oneOf(mark.part, parts, `${path}.part`, problems);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/** checkLaid finds marks that would hide one another: on one card, or once the cards lie together. */
|
|
136
|
+
function checkLaid(scene, problems) {
|
|
137
|
+
scene.cards.forEach((card, i) => {
|
|
138
|
+
const own = new Map();
|
|
139
|
+
card.marks.forEach((mark, j) => {
|
|
140
|
+
const r = mark.at[0] - 1;
|
|
141
|
+
const c = mark.at[1] - 1;
|
|
142
|
+
for (const a of atomsOf(r, c, isShape(mark) ? "whole" : (mark.part ?? "whole"))) {
|
|
143
|
+
const other = own.get(key(a));
|
|
144
|
+
if (other !== undefined) {
|
|
145
|
+
problems.push(`cards[${i}].marks[${j}]: lies on marks[${other}] of the same card`);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
for (const a of atomsOf(r, c, isShape(mark) ? "whole" : (mark.part ?? "whole")))
|
|
150
|
+
own.set(key(a), j);
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
if (problems.length > 0)
|
|
154
|
+
return;
|
|
155
|
+
// Once laid, a shape may share its square only with the same shape, turned
|
|
156
|
+
// the same way: anything else would cover it, or show two things in one.
|
|
157
|
+
const laid = scene.cards.map((card) => lay(card, moveOf(card, scene.rows, scene.cols)));
|
|
158
|
+
laid.forEach((upper, i) => {
|
|
159
|
+
for (let k = 0; k < i; k++) {
|
|
160
|
+
const lower = laid[k];
|
|
161
|
+
for (const [cell, shape] of upper.shapes) {
|
|
162
|
+
const [r, c] = cell.split(",").map(Number);
|
|
163
|
+
const below = lower.shapes.get(cell);
|
|
164
|
+
const filled = [0, 1, 2, 3].some((q) => lower.fills.has(key({ r, c, q })));
|
|
165
|
+
if (filled ||
|
|
166
|
+
(below &&
|
|
167
|
+
(below.shape !== shape.shape ||
|
|
168
|
+
below.color !== shape.color ||
|
|
169
|
+
below.turn !== shape.turn)))
|
|
170
|
+
problems.push(`cards[${i}].marks[${shape.mark}]: laid on cards[${k}], it falls on row ${r + 1}, column ${c + 1}, where that card has ${filled ? "a filled part" : "another shape"}; a shape shares its square only with the same shape, turned the same way`);
|
|
171
|
+
}
|
|
172
|
+
for (const [cell, shape] of lower.shapes) {
|
|
173
|
+
const [r, c] = cell.split(",").map(Number);
|
|
174
|
+
if ([0, 1, 2, 3].some((q) => upper.fills.has(key({ r, c, q }))))
|
|
175
|
+
problems.push(`cards[${k}].marks[${shape.mark}]: cards[${i}], laid on it, fills a part of its square at row ${r + 1}, column ${c + 1} and would hide it; a shape shares its square only with the same shape`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
// Laying the picture out.
|
|
181
|
+
function itemsOf(scene) {
|
|
182
|
+
const show = scene.show ?? "ask";
|
|
183
|
+
if (show === "together")
|
|
184
|
+
return 1;
|
|
185
|
+
return scene.cards.length + (show === "cards" ? 0 : 1);
|
|
186
|
+
}
|
|
187
|
+
function sizeOf(scene) {
|
|
188
|
+
const items = itemsOf(scene);
|
|
189
|
+
return Math.min(biggest, (room - (items - 1) * join) / (items * scene.cols));
|
|
190
|
+
}
|
|
191
|
+
// Drawing.
|
|
192
|
+
/** drawCard draws a transparent card with what lies on it, its top left at x, y. */
|
|
193
|
+
function drawCard(pen, scene, laid, x, y, s) {
|
|
194
|
+
const w = scene.cols * s;
|
|
195
|
+
const h = scene.rows * s;
|
|
196
|
+
pen.el("rect", { x, y, width: w, height: h, fill: soft });
|
|
197
|
+
const at = (p) => ({ x: x + p.x * s, y: y + p.y * s });
|
|
198
|
+
// Each square's parts of one colour are drawn as one, edged in that
|
|
199
|
+
// colour's dark, so that filled squares side by side still count apart.
|
|
200
|
+
for (let r = 0; r < scene.rows; r++)
|
|
201
|
+
for (let c = 0; c < scene.cols; c++) {
|
|
202
|
+
const byColor = new Map();
|
|
203
|
+
for (let q = 0; q < 4; q++) {
|
|
204
|
+
const fill = laid.fills.get(key({ r, c, q }));
|
|
205
|
+
if (!fill)
|
|
206
|
+
continue;
|
|
207
|
+
const list_ = byColor.get(fill.color) ?? [];
|
|
208
|
+
list_.push({ r, c, q });
|
|
209
|
+
byColor.set(fill.color, list_);
|
|
210
|
+
}
|
|
211
|
+
for (const [color, atoms] of byColor)
|
|
212
|
+
pen.el("path", {
|
|
213
|
+
class: "fill",
|
|
214
|
+
d: loopsOf(atoms)
|
|
215
|
+
.map((loop) => poly(loop.map(at)))
|
|
216
|
+
.join(""),
|
|
217
|
+
fill: colors[color].fill,
|
|
218
|
+
stroke: colors[color].dark,
|
|
219
|
+
"stroke-width": 1.5,
|
|
220
|
+
"stroke-linejoin": "round",
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
const lines = [];
|
|
224
|
+
for (let c = 1; c < scene.cols; c++)
|
|
225
|
+
lines.push(`M${n(x + c * s)} ${n(y)}V${n(y + h)}`);
|
|
226
|
+
for (let r = 1; r < scene.rows; r++)
|
|
227
|
+
lines.push(`M${n(x)} ${n(y + r * s)}H${n(x + w)}`);
|
|
228
|
+
if (lines.length > 0)
|
|
229
|
+
pen.el("path", { d: lines.join(""), stroke: faint, "stroke-width": 1.5 });
|
|
230
|
+
for (const [cell, shape] of laid.shapes) {
|
|
231
|
+
const [r, c] = cell.split(",").map(Number);
|
|
232
|
+
const cx = x + (c + 0.5) * s;
|
|
233
|
+
const cy = y + (r + 0.5) * s;
|
|
234
|
+
if (shape.turn !== 0)
|
|
235
|
+
pen.add(`<g transform="rotate(${shape.turn} ${n(cx)} ${n(cy)})">`);
|
|
236
|
+
// A circle's arcs are written to a tenth of a pixel: a radius of whole tenths keeps it round.
|
|
237
|
+
const radius = Math.round(s * 0.8 * 0.45 * 10) / 10;
|
|
238
|
+
drawThing(pen, { shape: shape.shape, color: shape.color }, cx, cy, radius / 0.45);
|
|
239
|
+
if (shape.turn !== 0)
|
|
240
|
+
pen.add("</g>");
|
|
241
|
+
}
|
|
242
|
+
pen.el("rect", {
|
|
243
|
+
x,
|
|
244
|
+
y,
|
|
245
|
+
width: w,
|
|
246
|
+
height: h,
|
|
247
|
+
fill: "none",
|
|
248
|
+
stroke: ink,
|
|
249
|
+
"stroke-width": outline,
|
|
250
|
+
"stroke-linejoin": "round",
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
/** drawMove draws the signs of a card's move, centred at x on the line y: a flip, then a turn. */
|
|
254
|
+
function drawMove(pen, card, x, y) {
|
|
255
|
+
const signs = [];
|
|
256
|
+
const flip = card.flip ?? "none";
|
|
257
|
+
if (flip !== "none")
|
|
258
|
+
signs.push((cx) => {
|
|
259
|
+
const across = flip === "horizontal";
|
|
260
|
+
pen.el("path", {
|
|
261
|
+
d: across ? `M${n(cx)} ${n(y - 15)}V${n(y + 15)}` : `M${n(cx - 15)} ${n(y)}H${n(cx + 15)}`,
|
|
262
|
+
stroke: ink,
|
|
263
|
+
"stroke-width": 2,
|
|
264
|
+
"stroke-dasharray": "4 3",
|
|
265
|
+
});
|
|
266
|
+
const head = (tx, ty, dx, dy) => poly([
|
|
267
|
+
{ x: tx, y: ty },
|
|
268
|
+
{ x: tx - dx * 7 - dy * 5, y: ty - dy * 7 - dx * 5 },
|
|
269
|
+
{ x: tx - dx * 7 + dy * 5, y: ty - dy * 7 + dx * 5 },
|
|
270
|
+
]);
|
|
271
|
+
pen.el("path", {
|
|
272
|
+
class: "flip",
|
|
273
|
+
d: across
|
|
274
|
+
? `M${n(cx - 16)} ${n(y)}H${n(cx + 16)}${head(cx + 18, y, 1, 0)}${head(cx - 18, y, -1, 0)}`
|
|
275
|
+
: `M${n(cx)} ${n(y - 16)}V${n(y + 16)}${head(cx, y + 18, 0, 1)}${head(cx, y - 18, 0, -1)}`,
|
|
276
|
+
stroke: ink,
|
|
277
|
+
"stroke-width": 2.5,
|
|
278
|
+
fill: ink,
|
|
279
|
+
"stroke-linejoin": "round",
|
|
280
|
+
});
|
|
281
|
+
});
|
|
282
|
+
const turn = card.turn ?? 0;
|
|
283
|
+
if (turn !== 0)
|
|
284
|
+
signs.push((cx) => {
|
|
285
|
+
const back = turn === 270;
|
|
286
|
+
const r = 11;
|
|
287
|
+
const ox = cx - 16;
|
|
288
|
+
const start = back ? { x: ox - r, y } : { x: ox + r, y };
|
|
289
|
+
const end = { x: ox, y: y - r };
|
|
290
|
+
pen.el("path", {
|
|
291
|
+
class: "turn",
|
|
292
|
+
d: `M${n(start.x)} ${n(start.y)}A${r} ${r} 0 1 ${back ? 0 : 1} ${n(end.x)} ${n(end.y)}`,
|
|
293
|
+
fill: "none",
|
|
294
|
+
stroke: ink,
|
|
295
|
+
"stroke-width": 2.5,
|
|
296
|
+
});
|
|
297
|
+
const dir = back ? -1 : 1;
|
|
298
|
+
pen.el("path", {
|
|
299
|
+
d: poly([
|
|
300
|
+
{ x: end.x + dir * 7, y: end.y },
|
|
301
|
+
{ x: end.x - dir * 2, y: end.y - 6 },
|
|
302
|
+
{ x: end.x - dir * 2, y: end.y + 6 },
|
|
303
|
+
]),
|
|
304
|
+
fill: ink,
|
|
305
|
+
stroke: ink,
|
|
306
|
+
"stroke-width": 1.5,
|
|
307
|
+
"stroke-linejoin": "round",
|
|
308
|
+
});
|
|
309
|
+
pen.text(cx + 17, y, `${back ? 90 : turn}°`, { size: 15 });
|
|
310
|
+
});
|
|
311
|
+
const width = 56;
|
|
312
|
+
signs.forEach((sign, i) => {
|
|
313
|
+
sign(x + (i - (signs.length - 1) / 2) * width);
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
// Words.
|
|
317
|
+
const letters = "ABCD";
|
|
318
|
+
function nameOf(card, i) {
|
|
319
|
+
return `card ${card.label ?? letters[i]}`;
|
|
320
|
+
}
|
|
321
|
+
function shapeWords(shape, turn) {
|
|
322
|
+
const way = shapeWay[shape];
|
|
323
|
+
const pointing = way === undefined ? "" : ways[(way + turn / 90) % 4];
|
|
324
|
+
switch (shape) {
|
|
325
|
+
case "circle":
|
|
326
|
+
return "dot";
|
|
327
|
+
case "diamond":
|
|
328
|
+
return "diamond";
|
|
329
|
+
case "cross":
|
|
330
|
+
return "cross";
|
|
331
|
+
case "triangle":
|
|
332
|
+
return `triangle pointing ${pointing}`;
|
|
333
|
+
case "star":
|
|
334
|
+
return `star with a point ${pointing}`;
|
|
335
|
+
case "heart":
|
|
336
|
+
return `heart with its tip ${pointing}`;
|
|
337
|
+
case "moon":
|
|
338
|
+
return `crescent with its tips ${pointing}`;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
/** laidWords says what a card shows: its filled parts colour by colour, then its shapes. */
|
|
342
|
+
function laidWords(scene, laid) {
|
|
343
|
+
const byColor = new Map();
|
|
344
|
+
for (let r = 0; r < scene.rows; r++)
|
|
345
|
+
for (let c = 0; c < scene.cols; c++) {
|
|
346
|
+
const cell = new Map();
|
|
347
|
+
for (let q = 0; q < 4; q++) {
|
|
348
|
+
const fill = laid.fills.get(key({ r, c, q }));
|
|
349
|
+
if (fill)
|
|
350
|
+
cell.set(fill.color, [...(cell.get(fill.color) ?? []), q]);
|
|
351
|
+
}
|
|
352
|
+
for (const [color, qs] of cell) {
|
|
353
|
+
const words = byColor.get(color) ?? [];
|
|
354
|
+
const part = cellWords(qs);
|
|
355
|
+
words.push(`${part === "the whole square" ? "the square" : `${part} of the square`} at row ${r + 1}, column ${c + 1}`);
|
|
356
|
+
byColor.set(color, words);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
const out = [...byColor.entries()]
|
|
360
|
+
.sort((a, b) => colorNames.indexOf(a[0]) - colorNames.indexOf(b[0]))
|
|
361
|
+
.map(([color, words]) => `${colors[color].word}: ${words.join("; ")}`);
|
|
362
|
+
const shapes = [...laid.shapes.entries()]
|
|
363
|
+
.map(([cell, s]) => {
|
|
364
|
+
const [r, c] = cell.split(",").map(Number);
|
|
365
|
+
return {
|
|
366
|
+
r,
|
|
367
|
+
c,
|
|
368
|
+
words: `${article(`${colors[s.color].word} ${shapeWords(s.shape, s.turn)}`)} at row ${r + 1}, column ${c + 1}`,
|
|
369
|
+
};
|
|
370
|
+
})
|
|
371
|
+
.sort((a, b) => a.r - b.r || a.c - b.c)
|
|
372
|
+
.map((s) => s.words);
|
|
373
|
+
if (shapes.length > 0)
|
|
374
|
+
out.push(shapes.join("; "));
|
|
375
|
+
return out.length > 0 ? out.map((w, i) => (i === 0 ? w : cap(w))).join(". ") : "nothing";
|
|
376
|
+
}
|
|
377
|
+
function moveWords(card) {
|
|
378
|
+
const steps = [];
|
|
379
|
+
if (card.flip === "horizontal")
|
|
380
|
+
steps.push("flipped over from left to right, as in a mirror");
|
|
381
|
+
if (card.flip === "vertical")
|
|
382
|
+
steps.push("flipped over from top to bottom");
|
|
383
|
+
if (card.turn === 90)
|
|
384
|
+
steps.push("turned a quarter turn clockwise");
|
|
385
|
+
if (card.turn === 180)
|
|
386
|
+
steps.push("turned a half turn");
|
|
387
|
+
if (card.turn === 270)
|
|
388
|
+
steps.push("turned a quarter turn anticlockwise");
|
|
389
|
+
return steps.join(", then ");
|
|
390
|
+
}
|
|
391
|
+
function cap(words) {
|
|
392
|
+
return `${words[0].toUpperCase()}${words.slice(1)}`;
|
|
393
|
+
}
|
|
394
|
+
export const overlay = {
|
|
395
|
+
kind: "overlay",
|
|
396
|
+
members: ["kind", "rows", "cols", "cards", "show"],
|
|
397
|
+
check(scene, problems) {
|
|
398
|
+
only(scene, this.members, "scene", problems);
|
|
399
|
+
int(scene.rows, "rows", problems, 1, most.side);
|
|
400
|
+
int(scene.cols, "cols", problems, 1, most.side);
|
|
401
|
+
if (scene.show !== undefined)
|
|
402
|
+
oneOf(scene.show, shows, "show", problems);
|
|
403
|
+
if (problems.length > 0)
|
|
404
|
+
return;
|
|
405
|
+
const labels = new Map();
|
|
406
|
+
list(scene.cards, "cards", problems, 2, most.cards).forEach((card, i) => {
|
|
407
|
+
const path = `cards[${i}]`;
|
|
408
|
+
if (!isObj(card)) {
|
|
409
|
+
problems.push(`${path}: must be a card with marks`);
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
only(card, ["marks", "turn", "flip", "label"], path, problems);
|
|
413
|
+
list(card.marks, `${path}.marks`, problems, 0, most.marks).forEach((mark, j) => {
|
|
414
|
+
checkMark(mark, `${path}.marks[${j}]`, scene, problems);
|
|
415
|
+
});
|
|
416
|
+
if (card.turn !== undefined) {
|
|
417
|
+
if (!turns.includes(card.turn))
|
|
418
|
+
problems.push(`${path}.turn: must be one of ${turns.join(", ")}, clockwise`);
|
|
419
|
+
else if ((card.turn === 90 || card.turn === 270) && scene.rows !== scene.cols)
|
|
420
|
+
problems.push(`${path}.turn: a card of ${scene.cols} by ${scene.rows} squares turned a quarter turn would not lie on the others; turn it 180, or make the cards square`);
|
|
421
|
+
}
|
|
422
|
+
if (card.flip !== undefined)
|
|
423
|
+
oneOf(card.flip, flips, `${path}.flip`, problems);
|
|
424
|
+
if (card.label !== undefined) {
|
|
425
|
+
text(card.label, `${path}.label`, problems, 2);
|
|
426
|
+
const other = labels.get(card.label);
|
|
427
|
+
if (other !== undefined)
|
|
428
|
+
problems.push(`${path}.label: cards[${other}] has it already`);
|
|
429
|
+
labels.set(card.label, i);
|
|
430
|
+
}
|
|
431
|
+
});
|
|
432
|
+
if (problems.length > 0)
|
|
433
|
+
return;
|
|
434
|
+
const typed = scene;
|
|
435
|
+
checkLaid(typed, problems);
|
|
436
|
+
if (problems.length === 0 && sizeOf(typed) < smallest)
|
|
437
|
+
problems.push(`cols: the cards do not fit side by side with squares of at least ${smallest} pixels; use fewer columns or fewer cards`);
|
|
438
|
+
},
|
|
439
|
+
draw(scene, pen) {
|
|
440
|
+
const typed = scene;
|
|
441
|
+
const show = typed.show ?? "ask";
|
|
442
|
+
const s = sizeOf(typed);
|
|
443
|
+
const w = typed.cols * s;
|
|
444
|
+
const h = typed.rows * s;
|
|
445
|
+
const moves = show !== "together" &&
|
|
446
|
+
typed.cards.some((c) => (c.turn ?? 0) !== 0 || (c.flip ?? "none") !== "none");
|
|
447
|
+
const top = margin + outline + (show === "together" ? 0 : labelRow);
|
|
448
|
+
const mid = top + h / 2;
|
|
449
|
+
let x = margin + outline;
|
|
450
|
+
if (show === "together") {
|
|
451
|
+
drawCard(pen, typed, together(typed), x, top, s);
|
|
452
|
+
return { width: x + w + outline + margin, height: top + h + outline + margin };
|
|
453
|
+
}
|
|
454
|
+
typed.cards.forEach((card, i) => {
|
|
455
|
+
pen.text(x + w / 2, margin + 12, card.label ?? letters[i], { size: 18 });
|
|
456
|
+
drawCard(pen, typed, lay(card, identity), x, top, s);
|
|
457
|
+
if (moves)
|
|
458
|
+
drawMove(pen, card, x + w / 2, top + h + signRow / 2 + 4);
|
|
459
|
+
x += w;
|
|
460
|
+
if (i < typed.cards.length - 1 || show !== "cards") {
|
|
461
|
+
const sign = i < typed.cards.length - 1 ? (show === "cards" ? "" : "+") : "=";
|
|
462
|
+
if (sign)
|
|
463
|
+
pen.text(x + join / 2, mid, sign, { size: 30 });
|
|
464
|
+
x += join;
|
|
465
|
+
}
|
|
466
|
+
});
|
|
467
|
+
if (show === "result")
|
|
468
|
+
drawCard(pen, typed, together(typed), x, top, s);
|
|
469
|
+
if (show === "ask") {
|
|
470
|
+
pen.el("rect", {
|
|
471
|
+
x,
|
|
472
|
+
y: top,
|
|
473
|
+
width: w,
|
|
474
|
+
height: h,
|
|
475
|
+
fill: "#ffffff",
|
|
476
|
+
stroke: ink,
|
|
477
|
+
"stroke-width": outline,
|
|
478
|
+
"stroke-dasharray": "7 6",
|
|
479
|
+
});
|
|
480
|
+
pen.text(x + w / 2, mid, "?", { size: Math.max(28, Math.min(56, Math.min(w, h) * 0.5)) });
|
|
481
|
+
}
|
|
482
|
+
if (show !== "cards")
|
|
483
|
+
x += w;
|
|
484
|
+
return {
|
|
485
|
+
width: x + outline + margin,
|
|
486
|
+
height: top + h + (moves ? signRow : 0) + outline + margin,
|
|
487
|
+
};
|
|
488
|
+
},
|
|
489
|
+
describe(scene) {
|
|
490
|
+
const typed = scene;
|
|
491
|
+
const show = typed.show ?? "ask";
|
|
492
|
+
const grid = `${typed.cols} square${typed.cols === 1 ? "" : "s"} wide and ${typed.rows} high`;
|
|
493
|
+
if (show === "together")
|
|
494
|
+
return `A transparent card, ${grid}, showing ${laidWords(typed, together(typed))}.`;
|
|
495
|
+
const k = typed.cards.length;
|
|
496
|
+
const out = [`${k} transparent cards side by side, each ${grid}.`];
|
|
497
|
+
typed.cards.forEach((card, i) => {
|
|
498
|
+
out.push(`${cap(nameOf(card, i))} shows ${laidWords(typed, lay(card, identity))}.`);
|
|
499
|
+
});
|
|
500
|
+
const moved = typed.cards
|
|
501
|
+
.map((card, i) => ({ card, i }))
|
|
502
|
+
.filter(({ card }) => moveWords(card) !== "")
|
|
503
|
+
.map(({ card, i }) => `${cap(nameOf(card, i))} is ${moveWords(card)}.`);
|
|
504
|
+
out.push(...moved);
|
|
505
|
+
if (show === "cards")
|
|
506
|
+
return out.join(" ");
|
|
507
|
+
out.push(`Then the cards are laid one on another, edges matching: ${typed.cards
|
|
508
|
+
.map((card, i) => nameOf(card, i))
|
|
509
|
+
.join(", then ")} on top.`);
|
|
510
|
+
const many = new Set(typed.cards.flatMap((card) => card.marks.filter((m) => !isShape(m)).map((m) => m.color ?? "black")));
|
|
511
|
+
if (many.size > 1)
|
|
512
|
+
out.push("The marks are opaque: where two lie on each other, the upper one is seen.");
|
|
513
|
+
if (show === "ask")
|
|
514
|
+
out.push("A question mark stands for what is seen through them.");
|
|
515
|
+
else
|
|
516
|
+
out.push(`Seen through them: ${laidWords(typed, together(typed))}.`);
|
|
517
|
+
return out.join(" ");
|
|
518
|
+
},
|
|
519
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Kind } from "../kind.ts";
|
|
2
|
+
import { type ColorName } from "../style.ts";
|
|
3
|
+
/**
|
|
4
|
+
* Pieces: polyomino pieces cut from squared paper, drawn apart in a row, each
|
|
5
|
+
* a list of its squares, a colour and an optional label under it; and
|
|
6
|
+
* optionally a board, the outline of squares the pieces are to cover, drawn
|
|
7
|
+
* below at the same scale. A square is [row, column], counted from 0 at the
|
|
8
|
+
* top left of its piece, or [row, column, half] for the triangle left when a
|
|
9
|
+
* square is cut along a diagonal: "nw" keeps the half at the top-left corner,
|
|
10
|
+
* "ne", "sw" and "se" the others. Every piece is drawn square by square, its
|
|
11
|
+
* inner lines showing, so the squares can be counted.
|
|
12
|
+
*/
|
|
13
|
+
export declare const halves: readonly ["nw", "ne", "sw", "se"];
|
|
14
|
+
export type Half = (typeof halves)[number];
|
|
15
|
+
export type Square = [number, number] | [number, number, Half];
|
|
16
|
+
export type Piece = {
|
|
17
|
+
cells: Square[];
|
|
18
|
+
color: ColorName;
|
|
19
|
+
label?: string;
|
|
20
|
+
};
|
|
21
|
+
export type Board = {
|
|
22
|
+
rows?: number;
|
|
23
|
+
cols?: number;
|
|
24
|
+
cells?: Square[];
|
|
25
|
+
color?: ColorName;
|
|
26
|
+
label?: string;
|
|
27
|
+
};
|
|
28
|
+
export type PiecesScene = {
|
|
29
|
+
kind: "pieces";
|
|
30
|
+
pieces: Piece[];
|
|
31
|
+
board?: Board;
|
|
32
|
+
};
|
|
33
|
+
type Sq = {
|
|
34
|
+
r: number;
|
|
35
|
+
c: number;
|
|
36
|
+
form: Half | "full";
|
|
37
|
+
};
|
|
38
|
+
/** sayShape says a shape's squares, row by row, so that it can be drawn again from the words. */
|
|
39
|
+
export declare function sayShape(squares: readonly Sq[]): string;
|
|
40
|
+
export declare const pieces: Kind;
|
|
41
|
+
export {};
|