@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,787 @@
|
|
|
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, textOn } from "../style.js";
|
|
5
|
+
import { byCodeUnit, n, poly } from "../svg.js";
|
|
6
|
+
/**
|
|
7
|
+
* Gears: toothed gears and smooth wheels on a board, each but the first set
|
|
8
|
+
* beside an earlier one, and belts round pairs of wheels, open or crossed. A
|
|
9
|
+
* gear set beside a gear with no gap meshes with it: the program puts it
|
|
10
|
+
* exactly where the teeth of the two fit, and draws every tooth. Some gears
|
|
11
|
+
* carry an arrow of the way they turn, and one may carry a "?". The check
|
|
12
|
+
* refuses wheels that overlap without meshing, a ring of wheels that locks,
|
|
13
|
+
* arrows that disagree with the gears, and belts that run over a wheel or
|
|
14
|
+
* another belt.
|
|
15
|
+
*/
|
|
16
|
+
export const directions = [
|
|
17
|
+
"up",
|
|
18
|
+
"up-right",
|
|
19
|
+
"right",
|
|
20
|
+
"down-right",
|
|
21
|
+
"down",
|
|
22
|
+
"down-left",
|
|
23
|
+
"left",
|
|
24
|
+
"up-left",
|
|
25
|
+
];
|
|
26
|
+
export const turns = ["clockwise", "anticlockwise"];
|
|
27
|
+
const members = ["kind", "gears", "belts"];
|
|
28
|
+
const wheelMembers = ["id", "teeth", "size", "color", "label", "next", "at", "gap", "turn", "ask"];
|
|
29
|
+
/** A gear is step pixels across, at its pitch circle, for each tooth. */
|
|
30
|
+
const step = 8;
|
|
31
|
+
const addendum = 5.5;
|
|
32
|
+
const dedendum = 7;
|
|
33
|
+
// The half width of a tooth at the pitch circle, and how much it widens per
|
|
34
|
+
// pixel towards the root: slim enough that two meshing teeth never touch,
|
|
35
|
+
// whatever the tooth counts and the angle they meet at.
|
|
36
|
+
const toothHalf = 4.6;
|
|
37
|
+
const flank = 0.12;
|
|
38
|
+
const pitch = Math.PI * step;
|
|
39
|
+
const most = 10;
|
|
40
|
+
const widest = 640;
|
|
41
|
+
const tallest = 600;
|
|
42
|
+
/** The free space wheels keep when they do not mesh, and what a belt needs. */
|
|
43
|
+
const clear = 6;
|
|
44
|
+
const beltHalf = 4.5;
|
|
45
|
+
const defaults = [
|
|
46
|
+
"orange",
|
|
47
|
+
"blue",
|
|
48
|
+
"green",
|
|
49
|
+
"red",
|
|
50
|
+
"purple",
|
|
51
|
+
"yellow",
|
|
52
|
+
"pink",
|
|
53
|
+
"brown",
|
|
54
|
+
"gray",
|
|
55
|
+
"white",
|
|
56
|
+
];
|
|
57
|
+
const bearingOf = {
|
|
58
|
+
up: 0,
|
|
59
|
+
"up-right": 45,
|
|
60
|
+
right: 90,
|
|
61
|
+
"down-right": 135,
|
|
62
|
+
down: 180,
|
|
63
|
+
"down-left": 225,
|
|
64
|
+
left: 270,
|
|
65
|
+
"up-left": 315,
|
|
66
|
+
};
|
|
67
|
+
const sideWords = {
|
|
68
|
+
up: "above",
|
|
69
|
+
"up-right": "up and to the right of",
|
|
70
|
+
right: "to the right of",
|
|
71
|
+
"down-right": "down and to the right of",
|
|
72
|
+
down: "below",
|
|
73
|
+
"down-left": "down and to the left of",
|
|
74
|
+
left: "to the left of",
|
|
75
|
+
"up-left": "up and to the left of",
|
|
76
|
+
};
|
|
77
|
+
const dist = (a, b) => Math.hypot(a.x - b.x, a.y - b.y);
|
|
78
|
+
const polar = (c, r, a) => ({
|
|
79
|
+
x: c.x + r * Math.cos(a),
|
|
80
|
+
y: c.y + r * Math.sin(a),
|
|
81
|
+
});
|
|
82
|
+
function segmentGap(p, a, b) {
|
|
83
|
+
const dx = b.x - a.x;
|
|
84
|
+
const dy = b.y - a.y;
|
|
85
|
+
const l = dx * dx + dy * dy;
|
|
86
|
+
const t = l === 0 ? 0 : Math.max(0, Math.min(1, ((p.x - a.x) * dx + (p.y - a.y) * dy) / l));
|
|
87
|
+
return Math.hypot(p.x - (a.x + t * dx), p.y - (a.y + t * dy));
|
|
88
|
+
}
|
|
89
|
+
function crosses(a, b, c, d) {
|
|
90
|
+
const side = (p, q, r) => (q.x - p.x) * (r.y - p.y) - (q.y - p.y) * (r.x - p.x);
|
|
91
|
+
return side(a, b, c) * side(a, b, d) < 0 && side(c, d, a) * side(c, d, b) < 0;
|
|
92
|
+
}
|
|
93
|
+
function sample(a, b, every = 6) {
|
|
94
|
+
const k = Math.max(1, Math.ceil(dist(a, b) / every));
|
|
95
|
+
return Array.from({ length: k + 1 }, (_, i) => ({
|
|
96
|
+
x: a.x + ((b.x - a.x) * i) / k,
|
|
97
|
+
y: a.y + ((b.y - a.y) * i) / k,
|
|
98
|
+
}));
|
|
99
|
+
}
|
|
100
|
+
function arcPoints(c, r, from, to) {
|
|
101
|
+
const k = Math.max(2, Math.ceil((Math.abs(to - from) * r) / 6));
|
|
102
|
+
return Array.from({ length: k + 1 }, (_, i) => polar(c, r, from + ((to - from) * i) / k));
|
|
103
|
+
}
|
|
104
|
+
/** bearing turns a direction, a word or degrees clockwise from up, into an angle on the screen. */
|
|
105
|
+
function angleOf(at) {
|
|
106
|
+
const degrees = typeof at === "number" ? at : bearingOf[at];
|
|
107
|
+
return ((degrees - 90) * Math.PI) / 180;
|
|
108
|
+
}
|
|
109
|
+
function sizeOf(wheel) {
|
|
110
|
+
if (wheel.teeth !== undefined) {
|
|
111
|
+
const r = (step * wheel.teeth) / 2;
|
|
112
|
+
return { r, outer: r + addendum, inner: r - dedendum };
|
|
113
|
+
}
|
|
114
|
+
const r = (step * (wheel.size ?? 6)) / 2;
|
|
115
|
+
return { r, outer: r, inner: r - 7 };
|
|
116
|
+
}
|
|
117
|
+
const capital = (words) => `${words[0].toUpperCase()}${words.slice(1)}`;
|
|
118
|
+
const nameOf = (w) => `${w.teeth !== undefined ? "gear" : "wheel"} ${w.id}`;
|
|
119
|
+
/**
|
|
120
|
+
* phaseBeside is the turn of a gear's first tooth that makes it mesh with a
|
|
121
|
+
* gear it meets along angle theta: the tooth of one in the gap of the other.
|
|
122
|
+
*/
|
|
123
|
+
function phaseBeside(a, b) {
|
|
124
|
+
const theta = Math.atan2(b.y - a.y, b.x - a.x);
|
|
125
|
+
return theta + Math.PI - (pitch / 2 + a.r * (a.phase - theta)) / b.r;
|
|
126
|
+
}
|
|
127
|
+
/** misfit is how far, along the pitch circles, the teeth of two meshing gears miss each other. */
|
|
128
|
+
function misfit(a, b) {
|
|
129
|
+
const theta = Math.atan2(b.y - a.y, b.x - a.x);
|
|
130
|
+
const m = a.r * (a.phase - theta) - b.r * (theta + Math.PI - b.phase) + pitch / 2;
|
|
131
|
+
const k = ((m % pitch) + pitch) % pitch;
|
|
132
|
+
return Math.min(k, pitch - k);
|
|
133
|
+
}
|
|
134
|
+
/** gearPath is the outline of a gear: a root arc, then a tooth, for each tooth. */
|
|
135
|
+
function gearPath(s, c) {
|
|
136
|
+
const t = s.wheel.teeth;
|
|
137
|
+
const T = (2 * Math.PI) / t;
|
|
138
|
+
const half = (rho) => (toothHalf + (s.r - rho) * flank) / rho;
|
|
139
|
+
const ar = half(s.inner);
|
|
140
|
+
const at = half(s.outer);
|
|
141
|
+
let d = "";
|
|
142
|
+
for (let j = 0; j < t; j++) {
|
|
143
|
+
const a = s.phase + j * T;
|
|
144
|
+
const p1 = polar(c, s.inner, a - ar);
|
|
145
|
+
const p2 = polar(c, s.outer, a - at);
|
|
146
|
+
const p3 = polar(c, s.outer, a + at);
|
|
147
|
+
const p4 = polar(c, s.inner, a + ar);
|
|
148
|
+
const p5 = polar(c, s.inner, a + T - ar);
|
|
149
|
+
if (j === 0)
|
|
150
|
+
d += `M${n(p1.x)} ${n(p1.y)}`;
|
|
151
|
+
d += `L${n(p2.x)} ${n(p2.y)}L${n(p3.x)} ${n(p3.y)}L${n(p4.x)} ${n(p4.y)}`;
|
|
152
|
+
d += `A${n(s.inner)} ${n(s.inner)} 0 0 1 ${n(p5.x)} ${n(p5.y)}`;
|
|
153
|
+
}
|
|
154
|
+
return `${d}Z`;
|
|
155
|
+
}
|
|
156
|
+
/** beltRadius is where a belt runs round a wheel: on a smooth wheel's rim, over a gear's teeth. */
|
|
157
|
+
const beltRadius = (s) => s.outer + 1.5;
|
|
158
|
+
function beltPath(belt, a, b) {
|
|
159
|
+
const ra = beltRadius(a);
|
|
160
|
+
const rb = beltRadius(b);
|
|
161
|
+
const d = dist(a, b);
|
|
162
|
+
const u = { x: (b.x - a.x) / d, y: (b.y - a.y) / d };
|
|
163
|
+
const v = { x: -u.y, y: u.x };
|
|
164
|
+
const crossed = belt.crossed === true;
|
|
165
|
+
const cos = (crossed ? ra + rb : ra - rb) / d;
|
|
166
|
+
if (Math.abs(cos) >= 0.97)
|
|
167
|
+
return undefined;
|
|
168
|
+
const sin = Math.sqrt(1 - cos * cos);
|
|
169
|
+
const n1 = { x: u.x * cos + v.x * sin, y: u.y * cos + v.y * sin };
|
|
170
|
+
const n2 = { x: u.x * cos - v.x * sin, y: u.y * cos - v.y * sin };
|
|
171
|
+
const beta = Math.acos(cos);
|
|
172
|
+
const p1 = polar(a, ra, Math.atan2(n1.y, n1.x));
|
|
173
|
+
const q1 = polar(a, ra, Math.atan2(n2.y, n2.x));
|
|
174
|
+
const sign = crossed ? -1 : 1;
|
|
175
|
+
const p2 = { x: b.x + sign * rb * n1.x, y: b.y + sign * rb * n1.y };
|
|
176
|
+
const q2 = { x: b.x + sign * rb * n2.x, y: b.y + sign * rb * n2.y };
|
|
177
|
+
// An open belt wraps each wheel on its far side, both arcs against the
|
|
178
|
+
// screen's angle; a crossed one wraps more than half of each wheel, the
|
|
179
|
+
// second wheel the other way round.
|
|
180
|
+
const spanB = crossed ? 2 * Math.PI - 2 * beta : 2 * beta;
|
|
181
|
+
const spanA = 2 * Math.PI - 2 * beta;
|
|
182
|
+
const sweepB = crossed ? 1 : 0;
|
|
183
|
+
const path = `M${n(p1.x)} ${n(p1.y)}L${n(p2.x)} ${n(p2.y)}` +
|
|
184
|
+
`A${n(rb)} ${n(rb)} 0 ${spanB > Math.PI ? 1 : 0} ${sweepB} ${n(q2.x)} ${n(q2.y)}` +
|
|
185
|
+
`L${n(q1.x)} ${n(q1.y)}` +
|
|
186
|
+
`A${n(ra)} ${n(ra)} 0 ${spanA > Math.PI ? 1 : 0} 0 ${n(p1.x)} ${n(p1.y)}Z`;
|
|
187
|
+
const angle = (c, p) => Math.atan2(p.y - c.y, p.x - c.x);
|
|
188
|
+
const aB = angle(b, p2);
|
|
189
|
+
const aA = angle(a, q1);
|
|
190
|
+
const points = [
|
|
191
|
+
...sample(p1, p2),
|
|
192
|
+
...arcPoints(b, rb, aB, aB + (sweepB === 1 ? spanB : -spanB)),
|
|
193
|
+
...sample(q2, q1),
|
|
194
|
+
...arcPoints(a, ra, aA, aA - spanA),
|
|
195
|
+
];
|
|
196
|
+
return {
|
|
197
|
+
belt,
|
|
198
|
+
d: path,
|
|
199
|
+
over: crossed ? `M${n(q2.x)} ${n(q2.y)}L${n(q1.x)} ${n(q1.y)}` : undefined,
|
|
200
|
+
strands: [
|
|
201
|
+
[p1, p2],
|
|
202
|
+
[q2, q1],
|
|
203
|
+
],
|
|
204
|
+
points,
|
|
205
|
+
ends: [
|
|
206
|
+
{ wheel: a.index, at: p1 },
|
|
207
|
+
{ wheel: a.index, at: q1 },
|
|
208
|
+
{ wheel: b.index, at: p2 },
|
|
209
|
+
{ wheel: b.index, at: q2 },
|
|
210
|
+
],
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
/** hubOf is the white disc at a wheel's centre and the size of what it shows. */
|
|
214
|
+
function hubOf(s) {
|
|
215
|
+
const room = s.gear ? s.inner - 6 : s.inner - 4;
|
|
216
|
+
if (s.wheel.ask)
|
|
217
|
+
return { r: Math.min(20, Math.max(12, room * 0.62)), size: 0 };
|
|
218
|
+
const label = s.wheel.label;
|
|
219
|
+
if (!label)
|
|
220
|
+
return { r: Math.min(14, Math.max(7, room * 0.3)), size: 0 };
|
|
221
|
+
for (const size of [Math.min(20, Math.max(14, room * 0.55)), 14]) {
|
|
222
|
+
const r = Math.max(textWidth(label, size) / 2 + 4, size * 0.72);
|
|
223
|
+
if (r <= room)
|
|
224
|
+
return { r, size };
|
|
225
|
+
}
|
|
226
|
+
return undefined;
|
|
227
|
+
}
|
|
228
|
+
/** order is where an arrow may stand round its wheel: on top first, then the sides, then below. */
|
|
229
|
+
const order = [0, 15, 345, 30, 330, 45, 315, 60, 300, 75, 285, 90, 270, 105, 255, 120, 240];
|
|
230
|
+
function arrowAt(s, bearing, fit, hub) {
|
|
231
|
+
const r = fit > 0 ? s.outer + 11 : (hub.r + s.inner) / 2;
|
|
232
|
+
const mid = ((bearing - 90) * Math.PI) / 180;
|
|
233
|
+
const half = fit > 0 ? Math.min(0.95, Math.max(0.45, 44 / r)) * fit : 0.95;
|
|
234
|
+
const cw = s.wheel.turn === "clockwise";
|
|
235
|
+
const from = cw ? mid - half : mid + half;
|
|
236
|
+
const to = cw ? mid + half : mid - half;
|
|
237
|
+
const a = polar(s, r, from);
|
|
238
|
+
const b = polar(s, r, to);
|
|
239
|
+
const tangent = cw
|
|
240
|
+
? { x: -Math.sin(to), y: Math.cos(to) }
|
|
241
|
+
: { x: Math.sin(to), y: -Math.cos(to) };
|
|
242
|
+
const size = 12;
|
|
243
|
+
// The arc stops short of the tip, so that the round end of the line stays inside the head.
|
|
244
|
+
const back = size / r;
|
|
245
|
+
const end = polar(s, r, cw ? to - back * 0.6 : to + back * 0.6);
|
|
246
|
+
const head = [
|
|
247
|
+
{ x: b.x, y: b.y },
|
|
248
|
+
{
|
|
249
|
+
x: b.x - tangent.x * size - tangent.y * size * 0.58,
|
|
250
|
+
y: b.y - tangent.y * size + tangent.x * size * 0.58,
|
|
251
|
+
},
|
|
252
|
+
{
|
|
253
|
+
x: b.x - tangent.x * size + tangent.y * size * 0.58,
|
|
254
|
+
y: b.y - tangent.y * size - tangent.x * size * 0.58,
|
|
255
|
+
},
|
|
256
|
+
];
|
|
257
|
+
return {
|
|
258
|
+
index: s.index,
|
|
259
|
+
bearing,
|
|
260
|
+
fit,
|
|
261
|
+
d: `M${n(a.x)} ${n(a.y)}A${n(r)} ${n(r)} 0 0 ${cw ? 1 : 0} ${n(end.x)} ${n(end.y)}`,
|
|
262
|
+
head,
|
|
263
|
+
points: [...arcPoints(s, r, from, to), ...head],
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
/** checkScene reads the members; it gives the scene typed when every member is right. */
|
|
267
|
+
function checkScene(scene, problems) {
|
|
268
|
+
only(scene, members, "scene", problems);
|
|
269
|
+
const ids = new Map();
|
|
270
|
+
const wheels = list(scene.gears, "gears", problems, 1, most);
|
|
271
|
+
wheels.forEach((w, i) => {
|
|
272
|
+
const path = `gears[${i}]`;
|
|
273
|
+
if (!isObj(w)) {
|
|
274
|
+
problems.push(`${path}: must be a gear with an id and teeth, or a wheel with a size`);
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
only(w, wheelMembers, path, problems);
|
|
278
|
+
const id = text(w.id, `${path}.id`, problems, 3);
|
|
279
|
+
if (id && ids.has(id))
|
|
280
|
+
problems.push(`${path}.id: "${id}" names another wheel too`);
|
|
281
|
+
if ((w.teeth === undefined) === (w.size === undefined))
|
|
282
|
+
problems.push(`${path}: give teeth for a gear or size for a smooth wheel, one of the two`);
|
|
283
|
+
if (w.teeth !== undefined)
|
|
284
|
+
int(w.teeth, `${path}.teeth`, problems, 8, 36);
|
|
285
|
+
if (w.size !== undefined)
|
|
286
|
+
int(w.size, `${path}.size`, problems, 6, 36);
|
|
287
|
+
if (w.color !== undefined && !isColor(w.color))
|
|
288
|
+
problems.push(`${path}.color: must be a colour: ${colorNames.join(", ")}`);
|
|
289
|
+
if (w.label !== undefined)
|
|
290
|
+
text(w.label, `${path}.label`, problems, 3);
|
|
291
|
+
if (i === 0) {
|
|
292
|
+
for (const key of ["next", "at", "gap"])
|
|
293
|
+
if (w[key] !== undefined)
|
|
294
|
+
problems.push(`${path}.${key}: the first wheel stands alone; the others stand by it`);
|
|
295
|
+
}
|
|
296
|
+
else {
|
|
297
|
+
if (typeof w.next !== "string" || !ids.has(w.next))
|
|
298
|
+
problems.push(`${path}.next: must name an earlier wheel it stands beside: ${[...ids.keys()].join(", ")}`);
|
|
299
|
+
if (typeof w.at === "number")
|
|
300
|
+
int(w.at, `${path}.at`, problems, 0, 359);
|
|
301
|
+
else
|
|
302
|
+
oneOf(w.at, directions, `${path}.at`, problems);
|
|
303
|
+
const gap = w.gap === undefined ? 0 : int(w.gap, `${path}.gap`, problems, 0, 20);
|
|
304
|
+
if (gap === 0 && w.size !== undefined)
|
|
305
|
+
problems.push(`${path}.gap: a smooth wheel does not mesh; give it a gap of at least 1 from ${String(w.next)}`);
|
|
306
|
+
if (gap === 0 && w.teeth !== undefined && typeof w.next === "string") {
|
|
307
|
+
const other = wheels[ids.get(w.next) ?? -1];
|
|
308
|
+
if (isObj(other) && other.size !== undefined)
|
|
309
|
+
problems.push(`${path}.gap: a gear meshes only with a gear; give it a gap of at least 1 from the smooth wheel ${w.next}`);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
if (w.turn !== undefined)
|
|
313
|
+
oneOf(w.turn, turns, `${path}.turn`, problems);
|
|
314
|
+
if (w.ask !== undefined && w.ask !== true)
|
|
315
|
+
problems.push(`${path}.ask: must be true, or left out`);
|
|
316
|
+
if (w.ask === true && w.turn !== undefined)
|
|
317
|
+
problems.push(`${path}.ask: an asked wheel has no arrow; leave out turn`);
|
|
318
|
+
if (w.ask === true && w.label !== undefined)
|
|
319
|
+
problems.push(`${path}.ask: an asked wheel shows "?" on its hub, so it has no label`);
|
|
320
|
+
if (id)
|
|
321
|
+
ids.set(id, i);
|
|
322
|
+
});
|
|
323
|
+
const pairs = new Set();
|
|
324
|
+
if (scene.belts !== undefined)
|
|
325
|
+
list(scene.belts, "belts", problems, 1, 6).forEach((b, i) => {
|
|
326
|
+
const path = `belts[${i}]`;
|
|
327
|
+
if (!isObj(b)) {
|
|
328
|
+
problems.push(`${path}: must be a belt with from and to`);
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
only(b, ["from", "to", "crossed", "color"], path, problems);
|
|
332
|
+
let ok = true;
|
|
333
|
+
for (const end of ["from", "to"])
|
|
334
|
+
if (typeof b[end] !== "string" || !ids.has(b[end])) {
|
|
335
|
+
problems.push(`${path}.${end}: must name a wheel: ${[...ids.keys()].join(", ")}`);
|
|
336
|
+
ok = false;
|
|
337
|
+
}
|
|
338
|
+
if (ok && b.from === b.to)
|
|
339
|
+
problems.push(`${path}.to: a belt joins two different wheels`);
|
|
340
|
+
else if (ok) {
|
|
341
|
+
const key = [String(b.from), String(b.to)].sort(byCodeUnit).join(" ");
|
|
342
|
+
if (pairs.has(key))
|
|
343
|
+
problems.push(`${path}: another belt joins ${b.from} and ${b.to}`);
|
|
344
|
+
pairs.add(key);
|
|
345
|
+
}
|
|
346
|
+
if (b.crossed !== undefined && typeof b.crossed !== "boolean")
|
|
347
|
+
problems.push(`${path}.crossed: must be true or false`);
|
|
348
|
+
if (b.color !== undefined && !isColor(b.color))
|
|
349
|
+
problems.push(`${path}.color: must be a colour: ${colorNames.join(", ")}`);
|
|
350
|
+
});
|
|
351
|
+
return problems.length > 0 ? undefined : scene;
|
|
352
|
+
}
|
|
353
|
+
/** plan lays a checked scene out; it records what cannot be drawn well. */
|
|
354
|
+
function plan(s, problems) {
|
|
355
|
+
const byId = new Map(s.gears.map((w, i) => [w.id, i]));
|
|
356
|
+
const spots = [];
|
|
357
|
+
s.gears.forEach((wheel, index) => {
|
|
358
|
+
const size = sizeOf(wheel);
|
|
359
|
+
const spot = {
|
|
360
|
+
wheel,
|
|
361
|
+
index,
|
|
362
|
+
x: 0,
|
|
363
|
+
y: 0,
|
|
364
|
+
gear: wheel.teeth !== undefined,
|
|
365
|
+
...size,
|
|
366
|
+
phase: -Math.PI / 2,
|
|
367
|
+
color: wheel.color ?? defaults[index % defaults.length],
|
|
368
|
+
};
|
|
369
|
+
if (index > 0) {
|
|
370
|
+
const by = spots[byId.get(wheel.next)];
|
|
371
|
+
const gap = wheel.gap ?? 0;
|
|
372
|
+
const reach = gap === 0 ? by.r + spot.r : by.outer + spot.outer + gap * step;
|
|
373
|
+
const a = angleOf(wheel.at);
|
|
374
|
+
spot.x = by.x + reach * Math.cos(a);
|
|
375
|
+
spot.y = by.y + reach * Math.sin(a);
|
|
376
|
+
if (gap === 0)
|
|
377
|
+
spot.phase = phaseBeside(by, spot);
|
|
378
|
+
}
|
|
379
|
+
spots.push(spot);
|
|
380
|
+
});
|
|
381
|
+
// Meshes and overlaps, every pair.
|
|
382
|
+
const meshes = [];
|
|
383
|
+
// Teeth that miss each other are told after a ring that locks, the deeper fault.
|
|
384
|
+
const misfits = [];
|
|
385
|
+
for (let j = 1; j < spots.length; j++)
|
|
386
|
+
for (let i = 0; i < j; i++) {
|
|
387
|
+
const a = spots[i];
|
|
388
|
+
const b = spots[j];
|
|
389
|
+
const d = dist(a, b);
|
|
390
|
+
const placed = b.wheel.next === a.wheel.id && (b.wheel.gap ?? 0) === 0;
|
|
391
|
+
if (a.gear && b.gear && Math.abs(d - a.r - b.r) < 0.5) {
|
|
392
|
+
if (!placed && misfit(a, b) > 1.2)
|
|
393
|
+
misfits.push(`gears[${j}]: meets ${nameOf(a.wheel)} too, but their teeth do not fit there; leave a gap between them or set the gears another way`);
|
|
394
|
+
meshes.push([i, j]);
|
|
395
|
+
}
|
|
396
|
+
else if (d < a.outer + b.outer + clear)
|
|
397
|
+
problems.push(`gears[${j}]: overlaps ${nameOf(a.wheel)} without meshing; move it, or give it a gap`);
|
|
398
|
+
}
|
|
399
|
+
if (problems.length > 0)
|
|
400
|
+
return undefined;
|
|
401
|
+
// Belts.
|
|
402
|
+
const belts = [];
|
|
403
|
+
const perWheel = new Map();
|
|
404
|
+
(s.belts ?? []).forEach((belt, k) => {
|
|
405
|
+
const path = `belts[${k}]`;
|
|
406
|
+
const ia = byId.get(belt.from);
|
|
407
|
+
const ib = byId.get(belt.to);
|
|
408
|
+
const a = spots[ia];
|
|
409
|
+
const b = spots[ib];
|
|
410
|
+
for (const i of [ia, ib])
|
|
411
|
+
perWheel.set(i, (perWheel.get(i) ?? 0) + 1);
|
|
412
|
+
if (meshes.some(([i, j]) => (i === ia && j === ib) || (i === ib && j === ia))) {
|
|
413
|
+
problems.push(`${path}: ${nameOf(a.wheel)} and ${nameOf(b.wheel)} mesh; a belt joins wheels that stand apart`);
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
const room = dist(a, b) - a.outer - b.outer;
|
|
417
|
+
const need = belt.crossed ? 4 : 2;
|
|
418
|
+
if (room < need * step - 0.5) {
|
|
419
|
+
problems.push(`${path}: ${nameOf(a.wheel)} and ${nameOf(b.wheel)} stand too close for ${belt.crossed ? "a crossed" : "a"} belt; keep a gap of at least ${need} between them`);
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
const drawn = beltPath(belt, a, b);
|
|
423
|
+
if (!drawn) {
|
|
424
|
+
problems.push(`${path}: the wheels are too unlike in size for a belt; move them apart`);
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
// The belt keeps clear of every other wheel, where it runs between its wheels and where it
|
|
428
|
+
// goes round them: a wheel with a belt meshes with nothing on the side the belt goes round.
|
|
429
|
+
for (const other of spots) {
|
|
430
|
+
if (other === a || other === b)
|
|
431
|
+
continue;
|
|
432
|
+
const near = (p) => dist(p, other) < other.outer + beltHalf + 4;
|
|
433
|
+
if (drawn.strands.some(([p, q]) => segmentGap(other, p, q) < other.outer + beltHalf + 4) ||
|
|
434
|
+
drawn.points.some(near))
|
|
435
|
+
problems.push(`${path}: runs over ${nameOf(other.wheel)}; move that wheel off the belt's way, or the belt to wheels that mesh with nothing`);
|
|
436
|
+
}
|
|
437
|
+
for (const [m, earlier] of belts.entries()) {
|
|
438
|
+
if (drawn.strands.some(([p, q]) => earlier.strands.some(([r, t]) => crosses(p, q, r, t))))
|
|
439
|
+
problems.push(`${path}: crosses belts[${m}]; set the wheels so that belts do not cross`);
|
|
440
|
+
for (const e of drawn.ends)
|
|
441
|
+
for (const f of earlier.ends)
|
|
442
|
+
if (e.wheel === f.wheel && dist(e.at, f.at) < 16)
|
|
443
|
+
problems.push(`${path}: leaves ${nameOf(spots[e.wheel].wheel)} where belts[${m}] does, so the two would look like one belt; set the wheels off a straight line`);
|
|
444
|
+
}
|
|
445
|
+
belts.push(drawn);
|
|
446
|
+
});
|
|
447
|
+
for (const [i, k] of perWheel)
|
|
448
|
+
if (k > 2)
|
|
449
|
+
problems.push(`belts: ${nameOf(spots[i].wheel)} carries ${k} belts; a wheel carries at most 2`);
|
|
450
|
+
if (problems.length > 0)
|
|
451
|
+
return undefined;
|
|
452
|
+
// Which way each wheel turns, against the first of its train.
|
|
453
|
+
const links = [
|
|
454
|
+
...meshes.map(([a, b]) => ({ a, b, flip: true })),
|
|
455
|
+
...(s.belts ?? []).map((belt) => ({
|
|
456
|
+
a: byId.get(belt.from),
|
|
457
|
+
b: byId.get(belt.to),
|
|
458
|
+
flip: belt.crossed === true,
|
|
459
|
+
})),
|
|
460
|
+
];
|
|
461
|
+
const side = spots.map(() => undefined);
|
|
462
|
+
const parent = spots.map((_, i) => i);
|
|
463
|
+
for (let root = 0; root < spots.length; root++) {
|
|
464
|
+
if (side[root] !== undefined)
|
|
465
|
+
continue;
|
|
466
|
+
side[root] = 0;
|
|
467
|
+
const queue = [root];
|
|
468
|
+
while (queue.length > 0) {
|
|
469
|
+
const at = queue.shift();
|
|
470
|
+
for (const link of links) {
|
|
471
|
+
if (link.a !== at && link.b !== at)
|
|
472
|
+
continue;
|
|
473
|
+
const other = link.a === at ? link.b : link.a;
|
|
474
|
+
const want = side[at] ^ (link.flip ? 1 : 0);
|
|
475
|
+
if (side[other] === undefined) {
|
|
476
|
+
side[other] = want;
|
|
477
|
+
parent[other] = at;
|
|
478
|
+
queue.push(other);
|
|
479
|
+
}
|
|
480
|
+
else if (side[other] !== want) {
|
|
481
|
+
const up = (i) => {
|
|
482
|
+
const out = [i];
|
|
483
|
+
while (parent[out.at(-1)] !== out.at(-1))
|
|
484
|
+
out.push(parent[out.at(-1)]);
|
|
485
|
+
return out;
|
|
486
|
+
};
|
|
487
|
+
const left = up(at);
|
|
488
|
+
const right = up(other);
|
|
489
|
+
const meet = left.find((i) => right.includes(i));
|
|
490
|
+
const ring = [
|
|
491
|
+
...left.slice(0, left.indexOf(meet) + 1),
|
|
492
|
+
...right.slice(0, right.indexOf(meet)).reverse(),
|
|
493
|
+
].map((i) => spots[i].wheel.id);
|
|
494
|
+
problems.push(`gears: the ring ${[...ring, ring[0]].join(" – ")} locks and cannot turn: each mesh and each crossed belt turns the next wheel the other way, and this ring has an odd number of them; take a wheel out of the ring`);
|
|
495
|
+
return undefined;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
problems.push(...misfits);
|
|
501
|
+
if (problems.length > 0)
|
|
502
|
+
return undefined;
|
|
503
|
+
const train = (i) => {
|
|
504
|
+
let at = i;
|
|
505
|
+
while (parent[at] !== at)
|
|
506
|
+
at = parent[at];
|
|
507
|
+
return at;
|
|
508
|
+
};
|
|
509
|
+
const shown = new Map();
|
|
510
|
+
for (const spot of spots) {
|
|
511
|
+
const turn = spot.wheel.turn;
|
|
512
|
+
if (!turn)
|
|
513
|
+
continue;
|
|
514
|
+
const root = train(spot.index);
|
|
515
|
+
const first = shown.get(root);
|
|
516
|
+
if (!first) {
|
|
517
|
+
shown.set(root, spot);
|
|
518
|
+
continue;
|
|
519
|
+
}
|
|
520
|
+
const same = side[first.index] === side[spot.index];
|
|
521
|
+
const should = same
|
|
522
|
+
? first.wheel.turn
|
|
523
|
+
: first.wheel.turn === "clockwise"
|
|
524
|
+
? "anticlockwise"
|
|
525
|
+
: "clockwise";
|
|
526
|
+
if (should !== turn)
|
|
527
|
+
problems.push(`gears[${spot.index}].turn: ${nameOf(spot.wheel)} turns ${should}, as ${nameOf(first.wheel)} turns ${first.wheel.turn}; leave the arrow out or turn it`);
|
|
528
|
+
}
|
|
529
|
+
// Hubs and the words on them.
|
|
530
|
+
const hubs = [];
|
|
531
|
+
for (const spot of spots) {
|
|
532
|
+
const hub = hubOf(spot);
|
|
533
|
+
if (!hub) {
|
|
534
|
+
problems.push(`gears[${spot.index}].label: "${spot.wheel.label}" does not fit on the hub of ${nameOf(spot.wheel)}; shorten it or make the wheel bigger`);
|
|
535
|
+
continue;
|
|
536
|
+
}
|
|
537
|
+
hubs.push(hub);
|
|
538
|
+
}
|
|
539
|
+
if (problems.length > 0)
|
|
540
|
+
return undefined;
|
|
541
|
+
// Arrows, each on the free side of its wheel nearest the top.
|
|
542
|
+
const arrows = [];
|
|
543
|
+
const beltPoints = belts.flatMap((b) => b.points);
|
|
544
|
+
for (const spot of spots) {
|
|
545
|
+
if (!spot.wheel.turn)
|
|
546
|
+
continue;
|
|
547
|
+
const hub = hubs[spot.index];
|
|
548
|
+
const tries = [1, 0.7].flatMap((fit) => order.map((bearing) => arrowAt(spot, bearing, fit, hub)));
|
|
549
|
+
// A wheel whose sides are taken by belts and neighbours carries its arrow on its face.
|
|
550
|
+
if (hub.r + 16 <= spot.inner)
|
|
551
|
+
tries.push(arrowAt(spot, 0, 0, hub));
|
|
552
|
+
const found = tries.find((arrow) => arrow.points.every((p) => arrow.fit === 0 ||
|
|
553
|
+
(spots.every((o) => o === spot || dist(p, o) > o.outer + 12) &&
|
|
554
|
+
beltPoints.every((q) => dist(p, q) > 11) &&
|
|
555
|
+
arrows.every((other) => other.points.every((q) => dist(p, q) > 14)))));
|
|
556
|
+
if (!found) {
|
|
557
|
+
problems.push(`gears[${spot.index}].turn: there is no room round ${nameOf(spot.wheel)} for its arrow; move its neighbours apart or put the arrow on another wheel`);
|
|
558
|
+
continue;
|
|
559
|
+
}
|
|
560
|
+
arrows.push(found);
|
|
561
|
+
}
|
|
562
|
+
if (problems.length > 0)
|
|
563
|
+
return undefined;
|
|
564
|
+
// The picture's box.
|
|
565
|
+
let minX = Number.POSITIVE_INFINITY;
|
|
566
|
+
let minY = Number.POSITIVE_INFINITY;
|
|
567
|
+
let maxX = Number.NEGATIVE_INFINITY;
|
|
568
|
+
let maxY = Number.NEGATIVE_INFINITY;
|
|
569
|
+
const grow = (p, r = 0) => {
|
|
570
|
+
minX = Math.min(minX, p.x - r);
|
|
571
|
+
maxX = Math.max(maxX, p.x + r);
|
|
572
|
+
minY = Math.min(minY, p.y - r);
|
|
573
|
+
maxY = Math.max(maxY, p.y + r);
|
|
574
|
+
};
|
|
575
|
+
for (const spot of spots)
|
|
576
|
+
grow(spot, spot.outer + outline);
|
|
577
|
+
for (const b of belts)
|
|
578
|
+
for (const p of b.points)
|
|
579
|
+
grow(p, beltHalf + 1);
|
|
580
|
+
for (const a of arrows)
|
|
581
|
+
for (const p of a.points)
|
|
582
|
+
grow(p, 2);
|
|
583
|
+
const width = maxX - minX + 2 * margin;
|
|
584
|
+
const height = maxY - minY + 2 * margin;
|
|
585
|
+
if (width > widest)
|
|
586
|
+
problems.push(`gears: the picture would be ${Math.round(width)} px wide, at most ${widest} fit; a gear is ${step} px across for each tooth, so use fewer or smaller wheels, or set some below others`);
|
|
587
|
+
if (height > tallest)
|
|
588
|
+
problems.push(`gears: the picture would be ${Math.round(height)} px tall, at most ${tallest} fit; use fewer or smaller wheels, or set some beside others`);
|
|
589
|
+
if (problems.length > 0)
|
|
590
|
+
return undefined;
|
|
591
|
+
return {
|
|
592
|
+
spots,
|
|
593
|
+
meshes,
|
|
594
|
+
belts,
|
|
595
|
+
arrows,
|
|
596
|
+
hubs,
|
|
597
|
+
width,
|
|
598
|
+
height,
|
|
599
|
+
dx: margin - minX,
|
|
600
|
+
dy: margin - minY,
|
|
601
|
+
};
|
|
602
|
+
}
|
|
603
|
+
function drawWheel(pen, s, hub, face) {
|
|
604
|
+
const c = { x: s.x, y: s.y };
|
|
605
|
+
const color = colors[s.color];
|
|
606
|
+
if (s.gear) {
|
|
607
|
+
pen.el("path", {
|
|
608
|
+
d: gearPath(s, c),
|
|
609
|
+
fill: color.fill,
|
|
610
|
+
stroke: ink,
|
|
611
|
+
"stroke-width": outline,
|
|
612
|
+
"stroke-linejoin": "round",
|
|
613
|
+
class: "gear",
|
|
614
|
+
});
|
|
615
|
+
if (!face && s.inner - hub.r > 18)
|
|
616
|
+
pen.el("circle", {
|
|
617
|
+
cx: c.x,
|
|
618
|
+
cy: c.y,
|
|
619
|
+
r: s.inner - 8,
|
|
620
|
+
fill: "none",
|
|
621
|
+
stroke: color.dark,
|
|
622
|
+
"stroke-width": 2.5,
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
else {
|
|
626
|
+
pen.el("circle", {
|
|
627
|
+
cx: c.x,
|
|
628
|
+
cy: c.y,
|
|
629
|
+
r: s.r,
|
|
630
|
+
fill: color.fill,
|
|
631
|
+
stroke: ink,
|
|
632
|
+
"stroke-width": outline,
|
|
633
|
+
class: "wheel",
|
|
634
|
+
});
|
|
635
|
+
pen.el("circle", {
|
|
636
|
+
cx: c.x,
|
|
637
|
+
cy: c.y,
|
|
638
|
+
r: s.inner,
|
|
639
|
+
fill: "none",
|
|
640
|
+
stroke: color.dark,
|
|
641
|
+
"stroke-width": 2.5,
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
function drawHub(pen, s, hub) {
|
|
646
|
+
const c = { x: s.x, y: s.y };
|
|
647
|
+
if (s.wheel.ask) {
|
|
648
|
+
pen.el("circle", {
|
|
649
|
+
cx: c.x,
|
|
650
|
+
cy: c.y,
|
|
651
|
+
r: hub.r,
|
|
652
|
+
fill: "#ffffff",
|
|
653
|
+
stroke: ink,
|
|
654
|
+
"stroke-width": 2,
|
|
655
|
+
"stroke-dasharray": "5 4",
|
|
656
|
+
class: "hub",
|
|
657
|
+
});
|
|
658
|
+
pen.text(c.x, c.y, "?", { size: hub.r * 1.3 });
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
pen.el("circle", {
|
|
662
|
+
cx: c.x,
|
|
663
|
+
cy: c.y,
|
|
664
|
+
r: hub.r,
|
|
665
|
+
fill: "#ffffff",
|
|
666
|
+
stroke: ink,
|
|
667
|
+
"stroke-width": outline,
|
|
668
|
+
class: "hub",
|
|
669
|
+
});
|
|
670
|
+
if (s.wheel.label)
|
|
671
|
+
pen.text(c.x, c.y, s.wheel.label, { size: hub.size });
|
|
672
|
+
else
|
|
673
|
+
pen.el("circle", { cx: c.x, cy: c.y, r: Math.max(2.5, hub.r * 0.3), fill: ink });
|
|
674
|
+
}
|
|
675
|
+
export const gears = {
|
|
676
|
+
kind: "gears",
|
|
677
|
+
members,
|
|
678
|
+
check(scene, problems) {
|
|
679
|
+
const typed = checkScene(scene, problems);
|
|
680
|
+
if (typed)
|
|
681
|
+
plan(typed, problems);
|
|
682
|
+
},
|
|
683
|
+
draw(scene, pen) {
|
|
684
|
+
const s = scene;
|
|
685
|
+
const p = plan(s, []);
|
|
686
|
+
// The plan lays the wheels out round the first; the picture moves them inside its margins.
|
|
687
|
+
const spots = p.spots.map((spot) => ({ ...spot, x: spot.x + p.dx, y: spot.y + p.dy }));
|
|
688
|
+
const onFace = new Set(p.arrows.filter((a) => a.fit === 0).map((a) => a.index));
|
|
689
|
+
spots.forEach((spot, i) => {
|
|
690
|
+
drawWheel(pen, spot, p.hubs[i], onFace.has(i));
|
|
691
|
+
});
|
|
692
|
+
for (const b of p.belts) {
|
|
693
|
+
const a = spots[b.ends[0].wheel];
|
|
694
|
+
const z = spots[b.ends[2].wheel];
|
|
695
|
+
const drawn = beltPath(b.belt, a, z);
|
|
696
|
+
const color = colors[b.belt.color ?? "gray"];
|
|
697
|
+
pen.add('<g class="belt">');
|
|
698
|
+
// The strand drawn again over the crossing ends flat, so that it joins the belt unseen.
|
|
699
|
+
for (const d of drawn.over ? [drawn.d, drawn.over] : [drawn.d]) {
|
|
700
|
+
pen.el("path", {
|
|
701
|
+
d,
|
|
702
|
+
fill: "none",
|
|
703
|
+
stroke: ink,
|
|
704
|
+
"stroke-width": 2 * beltHalf + 2 * outline - 1,
|
|
705
|
+
"stroke-linejoin": "round",
|
|
706
|
+
});
|
|
707
|
+
pen.el("path", {
|
|
708
|
+
d,
|
|
709
|
+
fill: "none",
|
|
710
|
+
stroke: color.fill,
|
|
711
|
+
"stroke-width": 2 * beltHalf - 1,
|
|
712
|
+
"stroke-linejoin": "round",
|
|
713
|
+
});
|
|
714
|
+
}
|
|
715
|
+
pen.add("</g>");
|
|
716
|
+
}
|
|
717
|
+
spots.forEach((spot, i) => {
|
|
718
|
+
drawHub(pen, spot, p.hubs[i]);
|
|
719
|
+
});
|
|
720
|
+
for (const planned of p.arrows) {
|
|
721
|
+
const spot = spots[planned.index];
|
|
722
|
+
const a = arrowAt(spot, planned.bearing, planned.fit, p.hubs[planned.index]);
|
|
723
|
+
// On a dark face the arrow is white, as a label would be.
|
|
724
|
+
const color = planned.fit === 0 ? textOn(spot.color) : ink;
|
|
725
|
+
pen.add('<g class="turn">');
|
|
726
|
+
pen.el("path", {
|
|
727
|
+
d: a.d,
|
|
728
|
+
fill: "none",
|
|
729
|
+
stroke: color,
|
|
730
|
+
"stroke-width": 3.5,
|
|
731
|
+
"stroke-linecap": "round",
|
|
732
|
+
});
|
|
733
|
+
pen.el("path", {
|
|
734
|
+
d: poly(a.head),
|
|
735
|
+
fill: color,
|
|
736
|
+
stroke: color,
|
|
737
|
+
"stroke-width": 1.5,
|
|
738
|
+
"stroke-linejoin": "round",
|
|
739
|
+
});
|
|
740
|
+
pen.add("</g>");
|
|
741
|
+
}
|
|
742
|
+
return { width: p.width, height: p.height };
|
|
743
|
+
},
|
|
744
|
+
describe(scene) {
|
|
745
|
+
const s = scene;
|
|
746
|
+
const p = plan(s, []);
|
|
747
|
+
const toothed = s.gears.filter((w) => w.teeth !== undefined).length;
|
|
748
|
+
const smooth = s.gears.length - toothed;
|
|
749
|
+
const parts = [
|
|
750
|
+
toothed > 0 ? `${toothed} gear${toothed === 1 ? "" : "s"}` : "",
|
|
751
|
+
smooth > 0 ? `${smooth} smooth wheel${smooth === 1 ? "" : "s"} without teeth` : "",
|
|
752
|
+
].filter(Boolean);
|
|
753
|
+
const out = [`${parts.join(" and ")}.`];
|
|
754
|
+
s.gears.forEach((w, i) => {
|
|
755
|
+
const color = colors[w.color ?? defaults[i % defaults.length]].word;
|
|
756
|
+
const what = w.teeth !== undefined ? `with ${w.teeth} teeth` : `of size ${w.size}`;
|
|
757
|
+
let where = "";
|
|
758
|
+
if (i > 0) {
|
|
759
|
+
const by = s.gears.find((o) => o.id === w.next);
|
|
760
|
+
const at = typeof w.at === "number" ? directions[Math.round(w.at / 45) % 8] : w.at;
|
|
761
|
+
const how = (w.gap ?? 0) === 0 ? "meshing with it" : "apart from it, not touching";
|
|
762
|
+
where = `, ${sideWords[at]} ${nameOf(by)}, ${how}`;
|
|
763
|
+
}
|
|
764
|
+
const label = w.label && w.label !== w.id ? `, marked ${w.label}` : "";
|
|
765
|
+
const name = nameOf(w);
|
|
766
|
+
const turn = w.turn ? `; an arrow shows it turning ${w.turn}` : "";
|
|
767
|
+
const ask = w.ask ? "; a question mark is on it" : "";
|
|
768
|
+
out.push(`${capital(name)}: ${color}, ${what}${label}${where}${turn}${ask}.`);
|
|
769
|
+
});
|
|
770
|
+
if (p) {
|
|
771
|
+
const extra = p.meshes.filter(([a, b]) => {
|
|
772
|
+
const later = s.gears[b];
|
|
773
|
+
return !(later.next === s.gears[a].id && (later.gap ?? 0) === 0);
|
|
774
|
+
});
|
|
775
|
+
for (const [a, b] of extra)
|
|
776
|
+
out.push(`${capital(nameOf(s.gears[b]))} also meshes with ${nameOf(s.gears[a])}.`);
|
|
777
|
+
}
|
|
778
|
+
for (const b of s.belts ?? []) {
|
|
779
|
+
const ends = `${nameOf(s.gears.find((w) => w.id === b.from))} and ${nameOf(s.gears.find((w) => w.id === b.to))}`;
|
|
780
|
+
const color = colors[b.color ?? "gray"].word;
|
|
781
|
+
out.push(b.crossed
|
|
782
|
+
? `A ${color} belt runs round ${ends}, crossed between them like a figure eight.`
|
|
783
|
+
: `A ${color} belt runs round ${ends}, not crossed.`);
|
|
784
|
+
}
|
|
785
|
+
return out.join(" ");
|
|
786
|
+
},
|
|
787
|
+
};
|