@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,657 @@
|
|
|
1
|
+
import { margin } from "../kind.js";
|
|
2
|
+
import { textWidth } from "../pen.js";
|
|
3
|
+
import { int, isObj, list, oneOf, only, text } from "../read.js";
|
|
4
|
+
import { colorNames, colors, ink, isColor, outline, soft } from "../style.js";
|
|
5
|
+
import { n, poly } from "../svg.js";
|
|
6
|
+
/**
|
|
7
|
+
* Pipes: water or balls poured into funnels at the top run down through pipes
|
|
8
|
+
* that fork at junctions into buckets at the bottom. A funnel shows how much
|
|
9
|
+
* is poured in, as words or as the balls themselves; a junction splits what
|
|
10
|
+
* comes in among its pipes, evenly, by the marks written on them, or — for
|
|
11
|
+
* balls — taking turns, a switch pointing the way the next ball goes; a pipe
|
|
12
|
+
* may be closed. The program lays the junctions out in rows from the pipes
|
|
13
|
+
* alone: each stands above the middle of the places its pipes lead to.
|
|
14
|
+
*/
|
|
15
|
+
export const flows = ["water", "balls"];
|
|
16
|
+
export const rules = ["even", "turns"];
|
|
17
|
+
const members = ["kind", "flow", "inlets", "junctions", "buckets"];
|
|
18
|
+
const spacing = 100;
|
|
19
|
+
const rowHeight = 84;
|
|
20
|
+
const pipeWidth = 16;
|
|
21
|
+
const joint = 13;
|
|
22
|
+
const switchSize = 17;
|
|
23
|
+
const funnelHeight = 34;
|
|
24
|
+
const ball = 18;
|
|
25
|
+
const mouth = 60;
|
|
26
|
+
const bucketHeight = 48;
|
|
27
|
+
const widest = 640;
|
|
28
|
+
const tallest = 620;
|
|
29
|
+
const water = "#9fd3f4";
|
|
30
|
+
const bucketColors = ["blue", "red", "green", "yellow", "purple", "orange"];
|
|
31
|
+
const dist = (a, b) => Math.hypot(a.x - b.x, a.y - b.y);
|
|
32
|
+
function segmentGap(p, a, b) {
|
|
33
|
+
const dx = b.x - a.x;
|
|
34
|
+
const dy = b.y - a.y;
|
|
35
|
+
const l = dx * dx + dy * dy;
|
|
36
|
+
const t = l === 0 ? 0 : Math.max(0, Math.min(1, ((p.x - a.x) * dx + (p.y - a.y) * dy) / l));
|
|
37
|
+
return Math.hypot(p.x - (a.x + t * dx), p.y - (a.y + t * dy));
|
|
38
|
+
}
|
|
39
|
+
/** meet is where two segments cross, strictly inside both, or undefined. */
|
|
40
|
+
function meet(a, b, c, d) {
|
|
41
|
+
const r = { x: b.x - a.x, y: b.y - a.y };
|
|
42
|
+
const s = { x: d.x - c.x, y: d.y - c.y };
|
|
43
|
+
const cross = r.x * s.y - r.y * s.x;
|
|
44
|
+
if (Math.abs(cross) < 1e-9)
|
|
45
|
+
return undefined;
|
|
46
|
+
const t = ((c.x - a.x) * s.y - (c.y - a.y) * s.x) / cross;
|
|
47
|
+
const u = ((c.x - a.x) * r.y - (c.y - a.y) * r.x) / cross;
|
|
48
|
+
if (t <= 0.02 || t >= 0.98 || u <= 0.02 || u >= 0.98)
|
|
49
|
+
return undefined;
|
|
50
|
+
return { x: a.x + t * r.x, y: a.y + t * r.y };
|
|
51
|
+
}
|
|
52
|
+
const wayTo = (w) => (typeof w === "string" ? w : w.to);
|
|
53
|
+
/** label tags stand on a pipe a little below its junction. */
|
|
54
|
+
function along(pipe, t) {
|
|
55
|
+
return { x: pipe.a.x + (pipe.b.x - pipe.a.x) * t, y: pipe.a.y + (pipe.b.y - pipe.a.y) * t };
|
|
56
|
+
}
|
|
57
|
+
/** A pipe's mark stands on its middle; a closed pipe that is marked too has its sign further down. */
|
|
58
|
+
const tagAt = (pipe) => along(pipe, pipe.closed ? 0.4 : 0.5);
|
|
59
|
+
const signAt = (pipe) => along(pipe, pipe.label ? 0.72 : 0.5);
|
|
60
|
+
const tagWidth = (label) => Math.max(28, textWidth(label, 15) + 12);
|
|
61
|
+
function hopperOf(k) {
|
|
62
|
+
const cols = Math.min(k, 4);
|
|
63
|
+
const rows = Math.ceil(k / 4);
|
|
64
|
+
return { cols, rows, width: cols * ball + 10, height: rows * ball + 8 };
|
|
65
|
+
}
|
|
66
|
+
function checkScene(scene, problems) {
|
|
67
|
+
only(scene, members, "scene", problems);
|
|
68
|
+
const flow = scene.flow === undefined ? "water" : oneOf(scene.flow, flows, "flow", problems);
|
|
69
|
+
const ids = new Map();
|
|
70
|
+
const name = (value, path, what) => {
|
|
71
|
+
const id = text(value, `${path}.id`, problems, 3);
|
|
72
|
+
if (id && ids.has(id))
|
|
73
|
+
problems.push(`${path}.id: "${id}" names ${ids.get(id)} too`);
|
|
74
|
+
if (id)
|
|
75
|
+
ids.set(id, what);
|
|
76
|
+
};
|
|
77
|
+
const inlets = list(scene.inlets, "inlets", problems, 1, 3);
|
|
78
|
+
inlets.forEach((t, i) => {
|
|
79
|
+
const path = `inlets[${i}]`;
|
|
80
|
+
if (!isObj(t)) {
|
|
81
|
+
problems.push(`${path}: must be an inlet with an id and to`);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
only(t, ["id", "to", "amount", "balls", "color"], path, problems);
|
|
85
|
+
name(t.id, path, "an inlet");
|
|
86
|
+
if (t.amount !== undefined)
|
|
87
|
+
text(t.amount, `${path}.amount`, problems, 6);
|
|
88
|
+
if (t.balls !== undefined) {
|
|
89
|
+
if (flow !== "balls")
|
|
90
|
+
problems.push(`${path}.balls: only a scene of flow "balls" pours balls`);
|
|
91
|
+
else
|
|
92
|
+
int(t.balls, `${path}.balls`, problems, 1, 12);
|
|
93
|
+
if (t.amount !== undefined)
|
|
94
|
+
problems.push(`${path}: show the balls or write the amount, not both`);
|
|
95
|
+
}
|
|
96
|
+
if (t.color !== undefined) {
|
|
97
|
+
if (t.balls === undefined)
|
|
98
|
+
problems.push(`${path}.color: only balls have a colour`);
|
|
99
|
+
else if (!isColor(t.color))
|
|
100
|
+
problems.push(`${path}.color: must be a colour: ${colorNames.join(", ")}`);
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
const junctions = scene.junctions === undefined ? [] : list(scene.junctions, "junctions", problems, 1, 12);
|
|
104
|
+
junctions.forEach((j, i) => {
|
|
105
|
+
const path = `junctions[${i}]`;
|
|
106
|
+
if (!isObj(j)) {
|
|
107
|
+
problems.push(`${path}: must be a junction with an id and to`);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
only(j, ["id", "to", "rule"], path, problems);
|
|
111
|
+
name(j.id, path, "a junction");
|
|
112
|
+
list(j.to, `${path}.to`, problems, 2, 3).forEach((w, k) => {
|
|
113
|
+
if (typeof w === "string")
|
|
114
|
+
return;
|
|
115
|
+
const wpath = `${path}.to[${k}]`;
|
|
116
|
+
if (!isObj(w)) {
|
|
117
|
+
problems.push(`${wpath}: must be an id, or a way with to, label and closed`);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
only(w, ["to", "label", "closed"], wpath, problems);
|
|
121
|
+
if (typeof w.to !== "string")
|
|
122
|
+
problems.push(`${wpath}.to: must be an id`);
|
|
123
|
+
if (w.label !== undefined)
|
|
124
|
+
text(w.label, `${wpath}.label`, problems, 4);
|
|
125
|
+
if (w.closed !== undefined && w.closed !== true)
|
|
126
|
+
problems.push(`${wpath}.closed: must be true, or left out`);
|
|
127
|
+
});
|
|
128
|
+
if (j.rule !== undefined) {
|
|
129
|
+
const rule = oneOf(j.rule, rules, `${path}.rule`, problems);
|
|
130
|
+
if (rule === "turns" && flow !== "balls")
|
|
131
|
+
problems.push(`${path}.rule: water does not take turns; only balls do`);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
const buckets = list(scene.buckets, "buckets", problems, 1, 6);
|
|
135
|
+
buckets.forEach((b, i) => {
|
|
136
|
+
const path = `buckets[${i}]`;
|
|
137
|
+
if (!isObj(b)) {
|
|
138
|
+
problems.push(`${path}: must be a bucket with an id`);
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
only(b, ["id", "label", "ask", "color"], path, problems);
|
|
142
|
+
name(b.id, path, "a bucket");
|
|
143
|
+
if (b.label !== undefined)
|
|
144
|
+
text(b.label, `${path}.label`, problems, 3);
|
|
145
|
+
if (b.ask !== undefined && b.ask !== true)
|
|
146
|
+
problems.push(`${path}.ask: must be true, or left out`);
|
|
147
|
+
if (b.color !== undefined && !isColor(b.color))
|
|
148
|
+
problems.push(`${path}.color: must be a colour: ${colorNames.join(", ")}`);
|
|
149
|
+
});
|
|
150
|
+
if (problems.length > 0)
|
|
151
|
+
return undefined;
|
|
152
|
+
// Where each pipe goes.
|
|
153
|
+
const known = (id, path, allowed) => {
|
|
154
|
+
const what = typeof id === "string" ? ids.get(id) : undefined;
|
|
155
|
+
if (!what || !allowed.includes(what)) {
|
|
156
|
+
const can = [...ids].filter(([, w]) => allowed.includes(w)).map(([k]) => k);
|
|
157
|
+
problems.push(`${path}: must name a junction or a bucket: ${can.join(", ")}`);
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
inlets.forEach((t, i) => {
|
|
161
|
+
known(t.to, `inlets[${i}].to`, ["a junction", "a bucket"]);
|
|
162
|
+
});
|
|
163
|
+
junctions.forEach((j, i) => {
|
|
164
|
+
const seen = new Set();
|
|
165
|
+
j.to.forEach((w, k) => {
|
|
166
|
+
const to = wayTo(w);
|
|
167
|
+
known(to, `junctions[${i}].to[${k}]`, ["a junction", "a bucket"]);
|
|
168
|
+
if (to === j.id)
|
|
169
|
+
problems.push(`junctions[${i}].to[${k}]: a pipe cannot lead back to its own junction`);
|
|
170
|
+
if (seen.has(to))
|
|
171
|
+
problems.push(`junctions[${i}].to[${k}]: another pipe of the junction leads to ${to}`);
|
|
172
|
+
seen.add(to);
|
|
173
|
+
});
|
|
174
|
+
const ways = j.to;
|
|
175
|
+
if (ways.every((w) => typeof w !== "string" && w.closed))
|
|
176
|
+
problems.push(`junctions[${i}].to: at least one pipe must stay open`);
|
|
177
|
+
});
|
|
178
|
+
return problems.length > 0 ? undefined : scene;
|
|
179
|
+
}
|
|
180
|
+
/** plan lays the pipes out in rows and records what cannot be drawn well. */
|
|
181
|
+
function plan(s, problems) {
|
|
182
|
+
const junctions = s.junctions ?? [];
|
|
183
|
+
const nodes = new Map();
|
|
184
|
+
s.inlets.forEach((t, index) => {
|
|
185
|
+
nodes.set(t.id, { id: t.id, what: "inlet", index, rank: 0, x: 0, y: 0 });
|
|
186
|
+
});
|
|
187
|
+
junctions.forEach((j, index) => {
|
|
188
|
+
nodes.set(j.id, { id: j.id, what: "junction", index, rank: 1, x: 0, y: 0 });
|
|
189
|
+
});
|
|
190
|
+
s.buckets.forEach((b, index) => {
|
|
191
|
+
nodes.set(b.id, { id: b.id, what: "bucket", index, rank: 0, x: 0, y: 0 });
|
|
192
|
+
});
|
|
193
|
+
const edges = [
|
|
194
|
+
...s.inlets.map((t) => ({ from: t.id, to: t.to, first: false })),
|
|
195
|
+
...junctions.flatMap((j) => j.to.map((w, k) => ({
|
|
196
|
+
from: j.id,
|
|
197
|
+
to: wayTo(w),
|
|
198
|
+
way: w,
|
|
199
|
+
first: j.rule === "turns" && k === 0,
|
|
200
|
+
}))),
|
|
201
|
+
];
|
|
202
|
+
// Rows: a junction stands a row below every place that feeds it.
|
|
203
|
+
for (let round = 0; round <= junctions.length; round++) {
|
|
204
|
+
let moved = false;
|
|
205
|
+
for (const e of edges) {
|
|
206
|
+
const a = nodes.get(e.from);
|
|
207
|
+
const b = nodes.get(e.to);
|
|
208
|
+
if (b.what === "junction" && b.rank < a.rank + 1) {
|
|
209
|
+
b.rank = a.rank + 1;
|
|
210
|
+
moved = true;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (!moved)
|
|
214
|
+
break;
|
|
215
|
+
if (round === junctions.length) {
|
|
216
|
+
problems.push("junctions: the pipes go round in a loop; water only runs down");
|
|
217
|
+
return undefined;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
junctions.forEach((j, i) => {
|
|
221
|
+
if (!edges.some((e) => e.to === j.id))
|
|
222
|
+
problems.push(`junctions[${i}]: no pipe leads into ${j.id}; join it to an inlet or a junction above`);
|
|
223
|
+
});
|
|
224
|
+
if (problems.length > 0)
|
|
225
|
+
return undefined;
|
|
226
|
+
const last = Math.max(0, ...junctions.map((j) => nodes.get(j.id).rank)) + 1;
|
|
227
|
+
for (const b of s.buckets)
|
|
228
|
+
nodes.get(b.id).rank = last;
|
|
229
|
+
// Places across: buckets in their order, every other place above the middle of where it leads.
|
|
230
|
+
s.buckets.forEach((b, i) => {
|
|
231
|
+
nodes.get(b.id).x = i * spacing;
|
|
232
|
+
});
|
|
233
|
+
for (let rank = last - 1; rank >= 0; rank--)
|
|
234
|
+
for (const node of nodes.values()) {
|
|
235
|
+
if (node.rank !== rank || node.what === "bucket")
|
|
236
|
+
continue;
|
|
237
|
+
const outs = edges.filter((e) => e.from === node.id).map((e) => nodes.get(e.to).x);
|
|
238
|
+
node.x = outs.reduce((sum, x) => sum + x, 0) / outs.length;
|
|
239
|
+
}
|
|
240
|
+
// Room above the funnels for what they show.
|
|
241
|
+
const head = Math.max(0, ...s.inlets.map((t) => t.balls !== undefined ? hopperOf(t.balls).height + 6 : t.amount !== undefined ? 34 : 0));
|
|
242
|
+
const minX = Math.min(...[...nodes.values()].map((nd) => nd.x));
|
|
243
|
+
const maxX = Math.max(...[...nodes.values()].map((nd) => nd.x));
|
|
244
|
+
const reach = Math.max(mouth / 2 + 6, ...s.inlets.map((t) => (t.balls !== undefined ? hopperOf(t.balls).width / 2 : 0)), ...s.inlets.map((t) => (t.amount !== undefined ? tagWidth(t.amount) / 2 : 0)), funnelHeight * 0.8);
|
|
245
|
+
const left = margin + reach + 4 - minX;
|
|
246
|
+
const top = margin + head + funnelHeight;
|
|
247
|
+
for (const node of nodes.values()) {
|
|
248
|
+
node.x += left;
|
|
249
|
+
node.y = top + node.rank * rowHeight;
|
|
250
|
+
}
|
|
251
|
+
// Places of a row keep apart.
|
|
252
|
+
const placed = [...nodes.values()];
|
|
253
|
+
for (const a of placed)
|
|
254
|
+
for (const b of placed) {
|
|
255
|
+
if (a === b || a.rank !== b.rank || a.id > b.id)
|
|
256
|
+
continue;
|
|
257
|
+
const need = a.what === "inlet"
|
|
258
|
+
? Math.max(70, ...[a, b].map((nd) => roomOf(s, nd)))
|
|
259
|
+
: a.what === "junction"
|
|
260
|
+
? 46
|
|
261
|
+
: spacing;
|
|
262
|
+
if (Math.abs(a.x - b.x) < need - 0.5)
|
|
263
|
+
problems.push(`${placeName(a)} and ${placeName(b)} would stand on one spot of row ${a.rank + 1}; lead their pipes to different places, or list the buckets in another order`);
|
|
264
|
+
}
|
|
265
|
+
if (problems.length > 0)
|
|
266
|
+
return undefined;
|
|
267
|
+
const pipes = edges.map((e) => {
|
|
268
|
+
const from = nodes.get(e.from);
|
|
269
|
+
const to = nodes.get(e.to);
|
|
270
|
+
const way = typeof e.way === "object" ? e.way : undefined;
|
|
271
|
+
const pipe = {
|
|
272
|
+
from,
|
|
273
|
+
to,
|
|
274
|
+
a: { x: from.x, y: from.y },
|
|
275
|
+
b: { x: to.x, y: to.what === "bucket" ? to.y + 6 : to.y },
|
|
276
|
+
closed: way?.closed === true,
|
|
277
|
+
first: e.first,
|
|
278
|
+
};
|
|
279
|
+
if (way?.label !== undefined)
|
|
280
|
+
pipe.label = way.label;
|
|
281
|
+
return pipe;
|
|
282
|
+
});
|
|
283
|
+
// A pipe passes over no place it does not join, and pipes cross clearly or not at all.
|
|
284
|
+
for (const pipe of pipes) {
|
|
285
|
+
for (const node of nodes.values()) {
|
|
286
|
+
if (node === pipe.from || node === pipe.to)
|
|
287
|
+
continue;
|
|
288
|
+
const at = node.what === "bucket" ? { x: node.x, y: node.y + bucketHeight / 2 } : node;
|
|
289
|
+
const room = node.what === "bucket" ? mouth / 2 + 8 : node.what === "inlet" ? 30 : joint + 12;
|
|
290
|
+
if (segmentGap(at, pipe.a, pipe.b) < room)
|
|
291
|
+
problems.push(`the pipe from ${pipe.from.id} to ${pipe.to.id} passes over ${placeName(node)}; lead it through a junction or list the buckets in another order`);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
for (let i = 0; i < pipes.length; i++)
|
|
295
|
+
for (let j = 0; j < i; j++) {
|
|
296
|
+
const p = pipes[i];
|
|
297
|
+
const q = pipes[j];
|
|
298
|
+
const at = meet(p.a, p.b, q.a, q.b);
|
|
299
|
+
if (!at)
|
|
300
|
+
continue;
|
|
301
|
+
const angle = Math.abs(Math.atan2(p.b.y - p.a.y, p.b.x - p.a.x) - Math.atan2(q.b.y - q.a.y, q.b.x - q.a.x));
|
|
302
|
+
const sharp = Math.min(angle, Math.PI - (angle % Math.PI));
|
|
303
|
+
if (sharp < (25 * Math.PI) / 180 || [...nodes.values()].some((nd) => dist(nd, at) < 34))
|
|
304
|
+
problems.push(`the pipes from ${p.from.id} to ${p.to.id} and from ${q.from.id} to ${q.to.id} cross where it cannot be seen clearly; list the buckets in another order`);
|
|
305
|
+
}
|
|
306
|
+
// Marks on pipes keep clear of other pipes and of each other.
|
|
307
|
+
const marks = pipes.flatMap((pipe) => [
|
|
308
|
+
...(pipe.label ? [{ pipe, at: tagAt(pipe), r: tagWidth(pipe.label) / 2 + 2 }] : []),
|
|
309
|
+
...(pipe.closed ? [{ pipe, at: signAt(pipe), r: 13 }] : []),
|
|
310
|
+
]);
|
|
311
|
+
for (const m of marks) {
|
|
312
|
+
for (const other of pipes)
|
|
313
|
+
if (other !== m.pipe &&
|
|
314
|
+
other.from !== m.pipe.from &&
|
|
315
|
+
segmentGap(m.at, other.a, other.b) < m.r + pipeWidth / 2)
|
|
316
|
+
problems.push(`the mark on the pipe from ${m.pipe.from.id} to ${m.pipe.to.id} would cover the pipe from ${other.from.id} to ${other.to.id}; list the buckets in another order`);
|
|
317
|
+
for (const other of marks)
|
|
318
|
+
if (other !== m && other.pipe.from === m.pipe.from && dist(other.at, m.at) < m.r + other.r)
|
|
319
|
+
problems.push(`the marks on the pipes of ${m.pipe.from.id} would overlap; spread its pipes wider or shorten the marks`);
|
|
320
|
+
}
|
|
321
|
+
if (problems.length > 0)
|
|
322
|
+
return undefined;
|
|
323
|
+
const width = maxX + left + reach + 4 + margin;
|
|
324
|
+
const height = top + last * rowHeight + bucketHeight + (s.buckets.some((b) => b.label) ? 30 : 4) + margin;
|
|
325
|
+
if (width > widest)
|
|
326
|
+
problems.push(`buckets: the picture would be ${Math.round(width)} px wide, at most ${widest} fit; use fewer buckets or fewer balls on a funnel`);
|
|
327
|
+
if (height > tallest)
|
|
328
|
+
problems.push(`junctions: the picture would be ${Math.round(height)} px tall, at most ${tallest} fit; use fewer rows of junctions`);
|
|
329
|
+
if (problems.length > 0)
|
|
330
|
+
return undefined;
|
|
331
|
+
return { nodes, pipes, width, height, top };
|
|
332
|
+
}
|
|
333
|
+
function roomOf(s, node) {
|
|
334
|
+
if (node.what !== "inlet")
|
|
335
|
+
return 0;
|
|
336
|
+
const t = s.inlets[node.index];
|
|
337
|
+
if (t.balls !== undefined)
|
|
338
|
+
return hopperOf(t.balls).width + 12;
|
|
339
|
+
if (t.amount !== undefined)
|
|
340
|
+
return tagWidth(t.amount) + 12;
|
|
341
|
+
return 70;
|
|
342
|
+
}
|
|
343
|
+
function placeName(node) {
|
|
344
|
+
return `${node.what} ${node.id}`;
|
|
345
|
+
}
|
|
346
|
+
function drawFunnel(pen, s, t, at) {
|
|
347
|
+
const top = at.y - funnelHeight;
|
|
348
|
+
const w = 56;
|
|
349
|
+
const spout = pipeWidth;
|
|
350
|
+
const body = [
|
|
351
|
+
{ x: at.x - w / 2, y: top },
|
|
352
|
+
{ x: at.x + w / 2, y: top },
|
|
353
|
+
{ x: at.x + spout / 2, y: at.y - 8 },
|
|
354
|
+
{ x: at.x + spout / 2, y: at.y + 2 },
|
|
355
|
+
{ x: at.x - spout / 2, y: at.y + 2 },
|
|
356
|
+
{ x: at.x - spout / 2, y: at.y - 8 },
|
|
357
|
+
];
|
|
358
|
+
const wet = (s.flow ?? "water") === "water";
|
|
359
|
+
pen.el("path", {
|
|
360
|
+
d: poly(body),
|
|
361
|
+
fill: wet ? water : soft,
|
|
362
|
+
stroke: ink,
|
|
363
|
+
"stroke-width": outline,
|
|
364
|
+
"stroke-linejoin": "round",
|
|
365
|
+
class: "funnel",
|
|
366
|
+
});
|
|
367
|
+
if (wet)
|
|
368
|
+
pen.el("path", {
|
|
369
|
+
d: `M${n(at.x - w * 0.32)} ${n(top + 8)}q7 -5 14 0q7 5 14 0`,
|
|
370
|
+
fill: "none",
|
|
371
|
+
stroke: "#ffffff",
|
|
372
|
+
"stroke-width": 2.5,
|
|
373
|
+
"stroke-linecap": "round",
|
|
374
|
+
});
|
|
375
|
+
if (t.balls !== undefined) {
|
|
376
|
+
// The balls wait in a box on the funnel, in rows from the bottom up.
|
|
377
|
+
const h = hopperOf(t.balls);
|
|
378
|
+
const color = colors[t.color ?? "red"];
|
|
379
|
+
pen.el("rect", {
|
|
380
|
+
x: at.x - h.width / 2,
|
|
381
|
+
y: top - h.height + 2,
|
|
382
|
+
width: h.width,
|
|
383
|
+
height: h.height,
|
|
384
|
+
rx: 6,
|
|
385
|
+
fill: soft,
|
|
386
|
+
stroke: ink,
|
|
387
|
+
"stroke-width": outline,
|
|
388
|
+
class: "hopper",
|
|
389
|
+
});
|
|
390
|
+
for (let i = 0; i < t.balls; i++) {
|
|
391
|
+
const row = Math.floor(i / h.cols);
|
|
392
|
+
const inRow = Math.min(h.cols, t.balls - row * h.cols);
|
|
393
|
+
const cx = at.x + ((i % h.cols) - (inRow - 1) / 2) * ball;
|
|
394
|
+
const cy = top - 2 - ball / 2 - row * ball;
|
|
395
|
+
pen.el("circle", {
|
|
396
|
+
cx,
|
|
397
|
+
cy,
|
|
398
|
+
r: ball / 2 - 1.2,
|
|
399
|
+
fill: color.fill,
|
|
400
|
+
stroke: ink,
|
|
401
|
+
"stroke-width": 2,
|
|
402
|
+
class: "ball",
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
else if (t.amount !== undefined) {
|
|
407
|
+
const w2 = tagWidth(t.amount);
|
|
408
|
+
pen.el("rect", {
|
|
409
|
+
x: at.x - w2 / 2,
|
|
410
|
+
y: top - 32,
|
|
411
|
+
width: w2,
|
|
412
|
+
height: 26,
|
|
413
|
+
rx: 7,
|
|
414
|
+
fill: "#ffffff",
|
|
415
|
+
stroke: ink,
|
|
416
|
+
"stroke-width": 1.4,
|
|
417
|
+
});
|
|
418
|
+
pen.text(at.x, top - 19, t.amount, { size: 16 });
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
/** handle is a bucket's handle, drawn before the pipes so that the pipe into it passes in front. */
|
|
422
|
+
function handle(pen, at) {
|
|
423
|
+
pen.el("path", {
|
|
424
|
+
d: `M${n(at.x - mouth * 0.42)} ${n(at.y + 2)}C${n(at.x - mouth * 0.42)} ${n(at.y - 30)} ${n(at.x + mouth * 0.42)} ${n(at.y - 30)} ${n(at.x + mouth * 0.42)} ${n(at.y + 2)}`,
|
|
425
|
+
fill: "none",
|
|
426
|
+
stroke: ink,
|
|
427
|
+
"stroke-width": 3,
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
function drawBucket(pen, s, b, index, at) {
|
|
431
|
+
const color = colors[b.color ?? bucketColors[index % bucketColors.length]];
|
|
432
|
+
const top = at.y;
|
|
433
|
+
const bottomY = at.y + bucketHeight;
|
|
434
|
+
const balls = s.flow === "balls";
|
|
435
|
+
if (balls) {
|
|
436
|
+
pen.el("rect", {
|
|
437
|
+
x: at.x - mouth / 2,
|
|
438
|
+
y: top,
|
|
439
|
+
width: mouth,
|
|
440
|
+
height: bucketHeight,
|
|
441
|
+
rx: 4,
|
|
442
|
+
fill: color.fill,
|
|
443
|
+
stroke: ink,
|
|
444
|
+
"stroke-width": outline,
|
|
445
|
+
class: "bucket",
|
|
446
|
+
});
|
|
447
|
+
pen.el("path", {
|
|
448
|
+
d: `M${n(at.x - mouth / 2)} ${n(top + 10)}H${n(at.x + mouth / 2)}`,
|
|
449
|
+
stroke: color.dark,
|
|
450
|
+
"stroke-width": 2.5,
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
else {
|
|
454
|
+
const bottom = mouth * 0.74;
|
|
455
|
+
pen.el("path", {
|
|
456
|
+
d: poly([
|
|
457
|
+
{ x: at.x - mouth / 2, y: top },
|
|
458
|
+
{ x: at.x + mouth / 2, y: top },
|
|
459
|
+
{ x: at.x + bottom / 2, y: bottomY },
|
|
460
|
+
{ x: at.x - bottom / 2, y: bottomY },
|
|
461
|
+
]),
|
|
462
|
+
fill: color.fill,
|
|
463
|
+
stroke: ink,
|
|
464
|
+
"stroke-width": outline,
|
|
465
|
+
"stroke-linejoin": "round",
|
|
466
|
+
class: "bucket",
|
|
467
|
+
});
|
|
468
|
+
pen.el("rect", {
|
|
469
|
+
x: at.x - mouth / 2 - 3,
|
|
470
|
+
y: top - 4,
|
|
471
|
+
width: mouth + 6,
|
|
472
|
+
height: 9,
|
|
473
|
+
rx: 4.5,
|
|
474
|
+
fill: color.dark,
|
|
475
|
+
stroke: ink,
|
|
476
|
+
"stroke-width": outline,
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
if (b.ask) {
|
|
480
|
+
const r = 14;
|
|
481
|
+
const cy = top + bucketHeight / 2 + 4;
|
|
482
|
+
pen.el("circle", {
|
|
483
|
+
cx: at.x,
|
|
484
|
+
cy,
|
|
485
|
+
r,
|
|
486
|
+
fill: "#ffffff",
|
|
487
|
+
stroke: ink,
|
|
488
|
+
"stroke-width": 2,
|
|
489
|
+
"stroke-dasharray": "4 3",
|
|
490
|
+
});
|
|
491
|
+
pen.text(at.x, cy, "?", { size: 19 });
|
|
492
|
+
}
|
|
493
|
+
if (b.label)
|
|
494
|
+
pen.text(at.x, bottomY + 18, b.label, { size: 18 });
|
|
495
|
+
}
|
|
496
|
+
export const pipes = {
|
|
497
|
+
kind: "pipes",
|
|
498
|
+
members,
|
|
499
|
+
check(scene, problems) {
|
|
500
|
+
const typed = checkScene(scene, problems);
|
|
501
|
+
if (typed)
|
|
502
|
+
plan(typed, problems);
|
|
503
|
+
},
|
|
504
|
+
draw(scene, pen) {
|
|
505
|
+
const s = scene;
|
|
506
|
+
const p = plan(s, []);
|
|
507
|
+
const wet = (s.flow ?? "water") === "water";
|
|
508
|
+
if (wet)
|
|
509
|
+
for (const b of s.buckets)
|
|
510
|
+
handle(pen, p.nodes.get(b.id));
|
|
511
|
+
// Each pipe whole, outline then inside, so a later pipe that crosses an earlier one
|
|
512
|
+
// passes over it; the junctions then cover where pipes meet.
|
|
513
|
+
for (const pipe of p.pipes) {
|
|
514
|
+
const d = `M${n(pipe.a.x)} ${n(pipe.a.y)}L${n(pipe.b.x)} ${n(pipe.b.y)}`;
|
|
515
|
+
pen.add('<g class="pipe">');
|
|
516
|
+
pen.el("path", { d, stroke: ink, "stroke-width": pipeWidth + 2 * outline - 1 });
|
|
517
|
+
pen.el("path", { d, stroke: wet ? water : soft, "stroke-width": pipeWidth - outline - 1 });
|
|
518
|
+
pen.add("</g>");
|
|
519
|
+
}
|
|
520
|
+
for (const node of p.nodes.values()) {
|
|
521
|
+
if (node.what !== "junction")
|
|
522
|
+
continue;
|
|
523
|
+
const first = p.pipes.find((pipe) => pipe.from === node && pipe.first);
|
|
524
|
+
pen.el("circle", {
|
|
525
|
+
cx: node.x,
|
|
526
|
+
cy: node.y,
|
|
527
|
+
r: first ? switchSize : joint,
|
|
528
|
+
fill: first ? "#ffffff" : wet ? water : soft,
|
|
529
|
+
stroke: ink,
|
|
530
|
+
"stroke-width": outline,
|
|
531
|
+
class: "junction",
|
|
532
|
+
});
|
|
533
|
+
if (first) {
|
|
534
|
+
// The switch: an arrow from the middle towards the pipe the next ball takes.
|
|
535
|
+
const l = dist(first.a, first.b);
|
|
536
|
+
const ux = (first.b.x - first.a.x) / l;
|
|
537
|
+
const uy = (first.b.y - first.a.y) / l;
|
|
538
|
+
const tip = { x: node.x + ux * (switchSize - 3), y: node.y + uy * (switchSize - 3) };
|
|
539
|
+
const back = { x: tip.x - ux * 9, y: tip.y - uy * 9 };
|
|
540
|
+
pen.add('<g class="switch">');
|
|
541
|
+
pen.el("path", {
|
|
542
|
+
d: `M${n(node.x - ux * 7)} ${n(node.y - uy * 7)}L${n(back.x)} ${n(back.y)}`,
|
|
543
|
+
stroke: colors.red.fill,
|
|
544
|
+
"stroke-width": 4.5,
|
|
545
|
+
"stroke-linecap": "round",
|
|
546
|
+
});
|
|
547
|
+
pen.el("path", {
|
|
548
|
+
d: poly([
|
|
549
|
+
tip,
|
|
550
|
+
{ x: back.x - uy * 6, y: back.y + ux * 6 },
|
|
551
|
+
{ x: back.x + uy * 6, y: back.y - ux * 6 },
|
|
552
|
+
]),
|
|
553
|
+
fill: colors.red.fill,
|
|
554
|
+
stroke: colors.red.fill,
|
|
555
|
+
"stroke-width": 1.5,
|
|
556
|
+
"stroke-linejoin": "round",
|
|
557
|
+
});
|
|
558
|
+
pen.add("</g>");
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
for (const pipe of p.pipes) {
|
|
562
|
+
if (pipe.label) {
|
|
563
|
+
const at = tagAt(pipe);
|
|
564
|
+
const w = tagWidth(pipe.label);
|
|
565
|
+
pen.el("rect", {
|
|
566
|
+
x: at.x - w / 2,
|
|
567
|
+
y: at.y - 12,
|
|
568
|
+
width: w,
|
|
569
|
+
height: 24,
|
|
570
|
+
rx: 7,
|
|
571
|
+
fill: "#ffffff",
|
|
572
|
+
stroke: ink,
|
|
573
|
+
"stroke-width": 1.4,
|
|
574
|
+
});
|
|
575
|
+
pen.text(at.x, at.y, pipe.label, { size: 15 });
|
|
576
|
+
}
|
|
577
|
+
if (pipe.closed) {
|
|
578
|
+
const at = signAt(pipe);
|
|
579
|
+
pen.el("circle", {
|
|
580
|
+
cx: at.x,
|
|
581
|
+
cy: at.y,
|
|
582
|
+
r: 12,
|
|
583
|
+
fill: colors.red.fill,
|
|
584
|
+
stroke: ink,
|
|
585
|
+
"stroke-width": outline,
|
|
586
|
+
class: "closed",
|
|
587
|
+
});
|
|
588
|
+
pen.el("rect", {
|
|
589
|
+
x: at.x - 7,
|
|
590
|
+
y: at.y - 2.5,
|
|
591
|
+
width: 14,
|
|
592
|
+
height: 5,
|
|
593
|
+
rx: 1,
|
|
594
|
+
fill: "#ffffff",
|
|
595
|
+
});
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
s.inlets.forEach((t) => {
|
|
599
|
+
drawFunnel(pen, s, t, p.nodes.get(t.id));
|
|
600
|
+
});
|
|
601
|
+
s.buckets.forEach((b, i) => {
|
|
602
|
+
drawBucket(pen, s, b, i, p.nodes.get(b.id));
|
|
603
|
+
});
|
|
604
|
+
return { width: p.width, height: p.height };
|
|
605
|
+
},
|
|
606
|
+
describe(scene) {
|
|
607
|
+
const s = scene;
|
|
608
|
+
const p = plan(s, []);
|
|
609
|
+
const wet = (s.flow ?? "water") === "water";
|
|
610
|
+
const junctions = s.junctions ?? [];
|
|
611
|
+
const box = wet ? "bucket" : "box";
|
|
612
|
+
const boxes = wet ? "buckets" : "boxes";
|
|
613
|
+
const capital = (words) => `${words[0].toUpperCase()}${words.slice(1)}`;
|
|
614
|
+
const boxName = (b, i) => `${box} ${b.label ?? i + 1}`;
|
|
615
|
+
// Funnels are told from left to right, as the picture lays them out.
|
|
616
|
+
const funnels = [...s.inlets].sort((a, b) => p.nodes.get(a.id).x - p.nodes.get(b.id).x);
|
|
617
|
+
const name = (id) => {
|
|
618
|
+
const b = s.buckets.findIndex((x) => x.id === id);
|
|
619
|
+
if (b >= 0)
|
|
620
|
+
return boxName(s.buckets[b], b);
|
|
621
|
+
const j = junctions.findIndex((x) => x.id === id);
|
|
622
|
+
if (j >= 0)
|
|
623
|
+
return `junction ${j + 1}`;
|
|
624
|
+
return funnels.length === 1
|
|
625
|
+
? "the funnel"
|
|
626
|
+
: `funnel ${funnels.findIndex((t) => t.id === id) + 1}`;
|
|
627
|
+
};
|
|
628
|
+
const out = [];
|
|
629
|
+
const list_ = s.buckets
|
|
630
|
+
.map((b, i) => `${boxName(b, i)} (${colors[b.color ?? bucketColors[i % bucketColors.length]].word})`)
|
|
631
|
+
.join(", ");
|
|
632
|
+
out.push(`Pipes carry ${wet ? "water" : "balls"} down from ${funnels.length === 1 ? "a funnel" : `${funnels.length} funnels`} at the top into ${s.buckets.length === 1 ? `1 ${box}` : `${s.buckets.length} ${boxes}`} at the bottom, from left to right: ${list_}.`);
|
|
633
|
+
funnels.forEach((t) => {
|
|
634
|
+
const poured = t.balls !== undefined
|
|
635
|
+
? ` holds ${t.balls} ${colors[t.color ?? "red"].word} ball${t.balls === 1 ? "" : "s"}`
|
|
636
|
+
: t.amount !== undefined
|
|
637
|
+
? ` is marked ${t.amount}`
|
|
638
|
+
: "";
|
|
639
|
+
out.push(`${capital(name(t.id))}${poured}; its pipe leads to ${name(t.to)}.`);
|
|
640
|
+
});
|
|
641
|
+
for (const j of junctions) {
|
|
642
|
+
const ways = j.to.map((w) => {
|
|
643
|
+
const to = `to ${name(wayTo(w))}`;
|
|
644
|
+
if (typeof w === "string")
|
|
645
|
+
return to;
|
|
646
|
+
return `${to}${w.label ? ` marked ${w.label}` : ""}${w.closed ? ", closed" : ""}`;
|
|
647
|
+
});
|
|
648
|
+
const turns = j.rule === "turns" ? ` A switch in it points to the pipe ${ways[0]}.` : "";
|
|
649
|
+
out.push(`${capital(name(j.id))} splits into ${j.to.length} pipes: ${ways.join("; ")}.${turns}`);
|
|
650
|
+
}
|
|
651
|
+
s.buckets.forEach((b, i) => {
|
|
652
|
+
if (b.ask)
|
|
653
|
+
out.push(`${capital(boxName(b, i))} has a question mark.`);
|
|
654
|
+
});
|
|
655
|
+
return out.join(" ");
|
|
656
|
+
},
|
|
657
|
+
};
|