@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,713 @@
|
|
|
1
|
+
import { margin } from "../kind.js";
|
|
2
|
+
import { isObj, list, oneOf, only, text } from "../read.js";
|
|
3
|
+
import { colorNames, colors, ink, isColor, outline } from "../style.js";
|
|
4
|
+
import { poly } from "../svg.js";
|
|
5
|
+
/**
|
|
6
|
+
* Tangram: a figure laid from the pieces of a tangram — two large triangles,
|
|
7
|
+
* a medium one, two small ones, a square and a parallelogram, all cut from one
|
|
8
|
+
* square — or from a free set of such pieces and of rectangles and
|
|
9
|
+
* trapezoids; and pieces apart, in a row below it, such as the options of a
|
|
10
|
+
* question. The unit is the side of the square piece: a small triangle has
|
|
11
|
+
* legs of 1, a medium one legs of √2 (1.414), a large one legs of 2; the
|
|
12
|
+
* parallelogram has sides of 1 and √2, a rectangle is 2 by 1, a trapezoid has
|
|
13
|
+
* sides of 1 and 3 and is 1 high.
|
|
14
|
+
*
|
|
15
|
+
* A piece has a pose: turn, clockwise in steps of 45 degrees, after flip, a
|
|
16
|
+
* mirror image from left to right; and, in the figure, at: the top-left corner
|
|
17
|
+
* of the box round it, in units. Upright, a triangle has its right angle at
|
|
18
|
+
* the bottom left, the parallelogram its sides of 1 at the top and the bottom
|
|
19
|
+
* and leans to the right, a trapezoid its long side at the bottom. A piece of
|
|
20
|
+
* the figure may be missing: its place is left empty with a "?". The figure
|
|
21
|
+
* is drawn piece by piece, or as a silhouette in one colour, its pieces not
|
|
22
|
+
* shown. The check proves that the pieces of the figure hold together and
|
|
23
|
+
* nowhere overlap.
|
|
24
|
+
*/
|
|
25
|
+
export const pieceNames = [
|
|
26
|
+
"large triangle",
|
|
27
|
+
"medium triangle",
|
|
28
|
+
"small triangle",
|
|
29
|
+
"square",
|
|
30
|
+
"parallelogram",
|
|
31
|
+
"rectangle",
|
|
32
|
+
"trapezoid",
|
|
33
|
+
];
|
|
34
|
+
export const sets = ["tangram", "free"];
|
|
35
|
+
export const looks = ["pieces", "silhouette"];
|
|
36
|
+
export const turns = [0, 45, 90, 135, 180, 225, 270, 315];
|
|
37
|
+
const r2 = Math.SQRT2;
|
|
38
|
+
/** The corners of each piece upright, clockwise on the screen, in units. */
|
|
39
|
+
const outlines = {
|
|
40
|
+
"large triangle": [
|
|
41
|
+
{ x: 0, y: 0 },
|
|
42
|
+
{ x: 2, y: 2 },
|
|
43
|
+
{ x: 0, y: 2 },
|
|
44
|
+
],
|
|
45
|
+
"medium triangle": [
|
|
46
|
+
{ x: 0, y: 0 },
|
|
47
|
+
{ x: r2, y: r2 },
|
|
48
|
+
{ x: 0, y: r2 },
|
|
49
|
+
],
|
|
50
|
+
"small triangle": [
|
|
51
|
+
{ x: 0, y: 0 },
|
|
52
|
+
{ x: 1, y: 1 },
|
|
53
|
+
{ x: 0, y: 1 },
|
|
54
|
+
],
|
|
55
|
+
square: [
|
|
56
|
+
{ x: 0, y: 0 },
|
|
57
|
+
{ x: 1, y: 0 },
|
|
58
|
+
{ x: 1, y: 1 },
|
|
59
|
+
{ x: 0, y: 1 },
|
|
60
|
+
],
|
|
61
|
+
parallelogram: [
|
|
62
|
+
{ x: 1, y: 0 },
|
|
63
|
+
{ x: 2, y: 0 },
|
|
64
|
+
{ x: 1, y: 1 },
|
|
65
|
+
{ x: 0, y: 1 },
|
|
66
|
+
],
|
|
67
|
+
rectangle: [
|
|
68
|
+
{ x: 0, y: 0 },
|
|
69
|
+
{ x: 2, y: 0 },
|
|
70
|
+
{ x: 2, y: 1 },
|
|
71
|
+
{ x: 0, y: 1 },
|
|
72
|
+
],
|
|
73
|
+
trapezoid: [
|
|
74
|
+
{ x: 1, y: 0 },
|
|
75
|
+
{ x: 2, y: 0 },
|
|
76
|
+
{ x: 3, y: 1 },
|
|
77
|
+
{ x: 0, y: 1 },
|
|
78
|
+
],
|
|
79
|
+
};
|
|
80
|
+
const tangramSet = {
|
|
81
|
+
"large triangle": 2,
|
|
82
|
+
"medium triangle": 1,
|
|
83
|
+
"small triangle": 2,
|
|
84
|
+
square: 1,
|
|
85
|
+
parallelogram: 1,
|
|
86
|
+
};
|
|
87
|
+
/** The colours of a classic set, by piece and by how many of it came before. */
|
|
88
|
+
const classic = {
|
|
89
|
+
"large triangle": ["red", "blue"],
|
|
90
|
+
"medium triangle": ["purple"],
|
|
91
|
+
"small triangle": ["green", "orange"],
|
|
92
|
+
square: ["yellow"],
|
|
93
|
+
parallelogram: ["pink"],
|
|
94
|
+
rectangle: ["brown"],
|
|
95
|
+
trapezoid: ["gray"],
|
|
96
|
+
};
|
|
97
|
+
const most = { pieces: 12, apart: 8, extent: 12 };
|
|
98
|
+
const biggest = 52;
|
|
99
|
+
const smallest = 26;
|
|
100
|
+
const room = 640 - 2 * margin - 2 * outline;
|
|
101
|
+
const tallest = 400;
|
|
102
|
+
const gapApart = 28;
|
|
103
|
+
const labelRow = 30;
|
|
104
|
+
/** Lengths, in units, below which two corners are the same and an overlap is none. */
|
|
105
|
+
const near = 0.02;
|
|
106
|
+
const tiny = 0.004;
|
|
107
|
+
const cosines = [1, Math.SQRT1_2, 0, -Math.SQRT1_2, -1, -Math.SQRT1_2, 0, Math.SQRT1_2];
|
|
108
|
+
/** posed turns and flips a piece, its box's top-left corner at 0, 0. */
|
|
109
|
+
function posed(piece, turn = 0, flip = false) {
|
|
110
|
+
const k = (((turn / 45) % 8) + 8) % 8;
|
|
111
|
+
const cos = cosines[k];
|
|
112
|
+
const sin = cosines[(k + 6) % 8];
|
|
113
|
+
const pts = outlines[piece].map((p) => {
|
|
114
|
+
const x = flip ? -p.x : p.x;
|
|
115
|
+
return { x: x * cos - p.y * sin, y: x * sin + p.y * cos };
|
|
116
|
+
});
|
|
117
|
+
// A mirror image runs the other way round; reversed, it runs clockwise again.
|
|
118
|
+
if (flip)
|
|
119
|
+
pts.reverse();
|
|
120
|
+
const x0 = Math.min(...pts.map((p) => p.x));
|
|
121
|
+
const y0 = Math.min(...pts.map((p) => p.y));
|
|
122
|
+
return pts.map((p) => ({ x: p.x - x0, y: p.y - y0 }));
|
|
123
|
+
}
|
|
124
|
+
function placed(p) {
|
|
125
|
+
return posed(p.piece, p.turn, p.flip).map((q) => ({ x: q.x + p.at[0], y: q.y + p.at[1] }));
|
|
126
|
+
}
|
|
127
|
+
function boxOf(points) {
|
|
128
|
+
return {
|
|
129
|
+
x0: Math.min(...points.map((p) => p.x)),
|
|
130
|
+
y0: Math.min(...points.map((p) => p.y)),
|
|
131
|
+
x1: Math.max(...points.map((p) => p.x)),
|
|
132
|
+
y1: Math.max(...points.map((p) => p.y)),
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
function area(points) {
|
|
136
|
+
let sum = 0;
|
|
137
|
+
points.forEach((p, i) => {
|
|
138
|
+
const q = points[(i + 1) % points.length];
|
|
139
|
+
sum += p.x * q.y - q.x * p.y;
|
|
140
|
+
});
|
|
141
|
+
return Math.abs(sum) / 2;
|
|
142
|
+
}
|
|
143
|
+
/** overlap is the area two convex pieces share, by clipping one with each side of the other. */
|
|
144
|
+
function overlap(a, b) {
|
|
145
|
+
let out = [...a];
|
|
146
|
+
for (let i = 0; i < b.length && out.length > 0; i++) {
|
|
147
|
+
const p = b[i];
|
|
148
|
+
const q = b[(i + 1) % b.length];
|
|
149
|
+
const side = (s) => (q.x - p.x) * (s.y - p.y) - (q.y - p.y) * (s.x - p.x);
|
|
150
|
+
const next = [];
|
|
151
|
+
out.forEach((s, j) => {
|
|
152
|
+
const e = out[(j + 1) % out.length];
|
|
153
|
+
const ds = side(s);
|
|
154
|
+
const de = side(e);
|
|
155
|
+
if (ds >= 0)
|
|
156
|
+
next.push(s);
|
|
157
|
+
if (ds >= 0 !== de >= 0) {
|
|
158
|
+
const t = ds / (ds - de);
|
|
159
|
+
next.push({ x: s.x + t * (e.x - s.x), y: s.y + t * (e.y - s.y) });
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
out = next;
|
|
163
|
+
}
|
|
164
|
+
return out.length < 3 ? 0 : area(out);
|
|
165
|
+
}
|
|
166
|
+
function distanceTo(p, a, b) {
|
|
167
|
+
const dx = b.x - a.x;
|
|
168
|
+
const dy = b.y - a.y;
|
|
169
|
+
const t = Math.max(0, Math.min(1, ((p.x - a.x) * dx + (p.y - a.y) * dy) / (dx * dx + dy * dy)));
|
|
170
|
+
return Math.hypot(p.x - a.x - t * dx, p.y - a.y - t * dy);
|
|
171
|
+
}
|
|
172
|
+
/** touch tells whether two pieces meet, along a side or at a corner. */
|
|
173
|
+
function touch(a, b) {
|
|
174
|
+
const meets = (ps, qs) => ps.some((p) => qs.some((q, i) => distanceTo(p, q, qs[(i + 1) % qs.length]) < near));
|
|
175
|
+
return meets(a, b) || meets(b, a);
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* outlineOf is the outer outline of pieces laid side by side: every side of
|
|
179
|
+
* every piece, less the stretches another piece's side runs along.
|
|
180
|
+
*/
|
|
181
|
+
function outlineOf(pieces) {
|
|
182
|
+
const out = [];
|
|
183
|
+
pieces.forEach((piece, i) => {
|
|
184
|
+
piece.forEach((a, k) => {
|
|
185
|
+
const b = piece[(k + 1) % piece.length];
|
|
186
|
+
const len = Math.hypot(b.x - a.x, b.y - a.y);
|
|
187
|
+
const u = { x: (b.x - a.x) / len, y: (b.y - a.y) / len };
|
|
188
|
+
let left = [[0, len]];
|
|
189
|
+
pieces.forEach((other, j) => {
|
|
190
|
+
if (j === i)
|
|
191
|
+
return;
|
|
192
|
+
other.forEach((c, m) => {
|
|
193
|
+
const d = other[(m + 1) % other.length];
|
|
194
|
+
const off = (p) => Math.abs((p.x - a.x) * u.y - (p.y - a.y) * u.x);
|
|
195
|
+
if (off(c) > near || off(d) > near)
|
|
196
|
+
return;
|
|
197
|
+
const tc = (c.x - a.x) * u.x + (c.y - a.y) * u.y;
|
|
198
|
+
const td = (d.x - a.x) * u.x + (d.y - a.y) * u.y;
|
|
199
|
+
const lo = Math.min(tc, td);
|
|
200
|
+
const hi = Math.max(tc, td);
|
|
201
|
+
left = left.flatMap(([s, e]) => {
|
|
202
|
+
if (hi <= s + near || lo >= e - near)
|
|
203
|
+
return [[s, e]];
|
|
204
|
+
const keep = [];
|
|
205
|
+
if (lo > s + near)
|
|
206
|
+
keep.push([s, lo]);
|
|
207
|
+
if (hi < e - near)
|
|
208
|
+
keep.push([hi, e]);
|
|
209
|
+
return keep;
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
for (const [s, e] of left)
|
|
214
|
+
out.push([
|
|
215
|
+
{ x: a.x + u.x * s, y: a.y + u.y * s },
|
|
216
|
+
{ x: a.x + u.x * e, y: a.y + u.y * e },
|
|
217
|
+
]);
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
return out;
|
|
221
|
+
}
|
|
222
|
+
/** inCircle is the centre and radius of the largest circle in a piece, for its "?". */
|
|
223
|
+
function inCircle(points) {
|
|
224
|
+
if (points.length === 3) {
|
|
225
|
+
const [a, b, c] = points;
|
|
226
|
+
const la = Math.hypot(b.x - c.x, b.y - c.y);
|
|
227
|
+
const lb = Math.hypot(a.x - c.x, a.y - c.y);
|
|
228
|
+
const lc = Math.hypot(a.x - b.x, a.y - b.y);
|
|
229
|
+
const s = la + lb + lc;
|
|
230
|
+
return {
|
|
231
|
+
c: { x: (la * a.x + lb * b.x + lc * c.x) / s, y: (la * a.y + lb * b.y + lc * c.y) / s },
|
|
232
|
+
r: (2 * area(points)) / s,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
const c = {
|
|
236
|
+
x: points.reduce((sum, p) => sum + p.x, 0) / points.length,
|
|
237
|
+
y: points.reduce((sum, p) => sum + p.y, 0) / points.length,
|
|
238
|
+
};
|
|
239
|
+
const r = Math.min(...points.map((p, i) => distanceTo(c, p, points[(i + 1) % points.length])));
|
|
240
|
+
return { c, r };
|
|
241
|
+
}
|
|
242
|
+
/** colorsOf gives each piece its colour: its own, or the next of the classic set. */
|
|
243
|
+
function colorsOf(pieces, fallback) {
|
|
244
|
+
const used = {};
|
|
245
|
+
return pieces.map((p) => {
|
|
246
|
+
if (p.color)
|
|
247
|
+
return p.color;
|
|
248
|
+
if (fallback)
|
|
249
|
+
return fallback;
|
|
250
|
+
const k = used[p.piece] ?? 0;
|
|
251
|
+
used[p.piece] = k + 1;
|
|
252
|
+
const list_ = classic[p.piece];
|
|
253
|
+
return list_[k % list_.length];
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
function layoutOf(scene) {
|
|
257
|
+
const figurePts = (scene.pieces ?? []).flatMap(placed);
|
|
258
|
+
const figBox = figurePts.length > 0 ? boxOf(figurePts) : undefined;
|
|
259
|
+
const shapes = (scene.apart ?? []).map((p) => posed(p.piece, p.turn, p.flip));
|
|
260
|
+
const boxes = shapes.map(boxOf);
|
|
261
|
+
const apartUnits = boxes.reduce((sum, b) => sum + b.x1, 0);
|
|
262
|
+
const apartGaps = Math.max(0, shapes.length - 1) * gapApart;
|
|
263
|
+
const apartHigh = Math.max(0, ...boxes.map((b) => b.y1));
|
|
264
|
+
const figW = figBox ? figBox.x1 - figBox.x0 : 0;
|
|
265
|
+
const figH = figBox ? figBox.y1 - figBox.y0 : 0;
|
|
266
|
+
const labelled = (scene.apart ?? []).some((p) => p.label !== undefined);
|
|
267
|
+
const fixed = (figBox && shapes.length > 0 ? 30 : 0) + (labelled ? labelRow : 0);
|
|
268
|
+
const s = Math.min(biggest, figW > 0 ? room / figW : biggest, apartUnits > 0 ? (room - apartGaps) / apartUnits : biggest, (tallest - fixed) / (figH + apartHigh));
|
|
269
|
+
const inner = Math.max(figW * s, apartUnits * s + apartGaps);
|
|
270
|
+
const left = margin + outline;
|
|
271
|
+
let y = margin + outline;
|
|
272
|
+
const out = {
|
|
273
|
+
s,
|
|
274
|
+
apart: [],
|
|
275
|
+
apartY: 0,
|
|
276
|
+
apartH: apartHigh * s,
|
|
277
|
+
width: inner + 2 * left,
|
|
278
|
+
height: 0,
|
|
279
|
+
};
|
|
280
|
+
if (figBox) {
|
|
281
|
+
out.figure = { box: figBox, x: left + (inner - figW * s) / 2, y };
|
|
282
|
+
y += figH * s + (shapes.length > 0 ? 30 : 0);
|
|
283
|
+
}
|
|
284
|
+
let x = left + (inner - apartUnits * s - apartGaps) / 2;
|
|
285
|
+
shapes.forEach((pts, i) => {
|
|
286
|
+
const b = boxes[i];
|
|
287
|
+
out.apart.push({ pts, x, w: b.x1 * s });
|
|
288
|
+
x += b.x1 * s + gapApart;
|
|
289
|
+
});
|
|
290
|
+
out.apartY = y;
|
|
291
|
+
if (shapes.length > 0)
|
|
292
|
+
y += apartHigh * s + (labelled ? labelRow : 0);
|
|
293
|
+
out.height = y + outline + margin;
|
|
294
|
+
return out;
|
|
295
|
+
}
|
|
296
|
+
// Words.
|
|
297
|
+
const waysOf = [
|
|
298
|
+
"up",
|
|
299
|
+
"up and right",
|
|
300
|
+
"right",
|
|
301
|
+
"down and right",
|
|
302
|
+
"down",
|
|
303
|
+
"down and left",
|
|
304
|
+
"left",
|
|
305
|
+
"up and left",
|
|
306
|
+
];
|
|
307
|
+
function directionWords(v) {
|
|
308
|
+
const angle = Math.atan2(v.x, -v.y);
|
|
309
|
+
const k = ((Math.round(angle / (Math.PI / 4)) % 8) + 8) % 8;
|
|
310
|
+
return waysOf[k];
|
|
311
|
+
}
|
|
312
|
+
/** lineWords says which way a side runs: across, up and down, or aslant. */
|
|
313
|
+
function lineWords(v) {
|
|
314
|
+
const w = directionWords(v);
|
|
315
|
+
if (w === "left" || w === "right")
|
|
316
|
+
return "level";
|
|
317
|
+
if (w === "up" || w === "down")
|
|
318
|
+
return "upright";
|
|
319
|
+
return w === "up and right" || w === "down and left"
|
|
320
|
+
? "slanting up to the right"
|
|
321
|
+
: "slanting down to the right";
|
|
322
|
+
}
|
|
323
|
+
/** sides lists a piece's sides as vectors, with their lengths. */
|
|
324
|
+
function sides(pts) {
|
|
325
|
+
return pts.map((p, i) => {
|
|
326
|
+
const q = pts[(i + 1) % pts.length];
|
|
327
|
+
const v = { x: q.x - p.x, y: q.y - p.y };
|
|
328
|
+
return { v, len: Math.hypot(v.x, v.y), at: i };
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
const cornerWords = {
|
|
332
|
+
"up and right": "top right",
|
|
333
|
+
"down and right": "bottom right",
|
|
334
|
+
"down and left": "bottom left",
|
|
335
|
+
"up and left": "top left",
|
|
336
|
+
};
|
|
337
|
+
/** poseWords says how a piece lies, so that its pose can be drawn again from the words. */
|
|
338
|
+
function poseWords(name, pts) {
|
|
339
|
+
const all = sides(pts);
|
|
340
|
+
const longest = all.reduce((a, b) => (b.len > a.len + near ? b : a), all[0]);
|
|
341
|
+
const centre = {
|
|
342
|
+
x: pts.reduce((sum, p) => sum + p.x, 0) / pts.length,
|
|
343
|
+
y: pts.reduce((sum, p) => sum + p.y, 0) / pts.length,
|
|
344
|
+
};
|
|
345
|
+
if (name.endsWith("triangle")) {
|
|
346
|
+
// The right angle faces away from the long side, from its middle.
|
|
347
|
+
const a = pts[longest.at];
|
|
348
|
+
const right = pts[(longest.at + 2) % 3];
|
|
349
|
+
const way = directionWords({
|
|
350
|
+
x: right.x - (a.x + longest.v.x / 2),
|
|
351
|
+
y: right.y - (a.y + longest.v.y / 2),
|
|
352
|
+
});
|
|
353
|
+
return cornerWords[way]
|
|
354
|
+
? `its right angle at the ${cornerWords[way]}`
|
|
355
|
+
: `its right angle pointing ${way}`;
|
|
356
|
+
}
|
|
357
|
+
if (name === "square")
|
|
358
|
+
return lineWords(all[0].v) === "level" || lineWords(all[0].v) === "upright"
|
|
359
|
+
? "standing on a side"
|
|
360
|
+
: "standing on a corner";
|
|
361
|
+
if (name === "rectangle")
|
|
362
|
+
return `its long sides ${lineWords(longest.v)}`;
|
|
363
|
+
if (name === "trapezoid") {
|
|
364
|
+
const a = pts[longest.at];
|
|
365
|
+
const mid = { x: a.x + longest.v.x / 2, y: a.y + longest.v.y / 2 };
|
|
366
|
+
return `its long side facing ${directionWords({ x: mid.x - centre.x, y: mid.y - centre.y })}`;
|
|
367
|
+
}
|
|
368
|
+
// A parallelogram: the ways its sides of 1 and of √2 run tell its pose, and which of its mirror images it is.
|
|
369
|
+
const short = all.find((side) => side.len < longest.len - near);
|
|
370
|
+
return `its short sides ${lineWords(short.v)} and its long sides ${lineWords(longest.v)}`;
|
|
371
|
+
}
|
|
372
|
+
function placeWords(pts, box) {
|
|
373
|
+
const c = {
|
|
374
|
+
x: pts.reduce((s, p) => s + p.x, 0) / pts.length,
|
|
375
|
+
y: pts.reduce((s, p) => s + p.y, 0) / pts.length,
|
|
376
|
+
};
|
|
377
|
+
const x = box.x1 > box.x0 ? (c.x - box.x0) / (box.x1 - box.x0) : 0.5;
|
|
378
|
+
const y = box.y1 > box.y0 ? (c.y - box.y0) / (box.y1 - box.y0) : 0.5;
|
|
379
|
+
const across = x < 0.36 ? "left" : x > 0.64 ? "right" : "";
|
|
380
|
+
const down = y < 0.36 ? "top" : y > 0.64 ? "bottom" : "";
|
|
381
|
+
if (!across && !down)
|
|
382
|
+
return "in the middle";
|
|
383
|
+
if (!across)
|
|
384
|
+
return `at the ${down}`;
|
|
385
|
+
if (!down)
|
|
386
|
+
return `on the ${across}`;
|
|
387
|
+
return `at the ${down} ${across}`;
|
|
388
|
+
}
|
|
389
|
+
function number(v) {
|
|
390
|
+
return String(Math.round(v * 100) / 100);
|
|
391
|
+
}
|
|
392
|
+
function article(words) {
|
|
393
|
+
return `${/^[aeiou]/.test(words) ? "an" : "a"} ${words}`;
|
|
394
|
+
}
|
|
395
|
+
// Checking.
|
|
396
|
+
function checkPose(p, path, problems) {
|
|
397
|
+
oneOf(p.piece, pieceNames, `${path}.piece`, problems);
|
|
398
|
+
if (p.turn !== undefined && !turns.includes(p.turn))
|
|
399
|
+
problems.push(`${path}.turn: must be one of ${turns.join(", ")}, clockwise`);
|
|
400
|
+
if (p.flip !== undefined && typeof p.flip !== "boolean")
|
|
401
|
+
problems.push(`${path}.flip: must be true or false`);
|
|
402
|
+
if (p.color !== undefined && !isColor(p.color))
|
|
403
|
+
problems.push(`${path}.color: must be a colour: ${colorNames.join(", ")}`);
|
|
404
|
+
}
|
|
405
|
+
export const tangram = {
|
|
406
|
+
kind: "tangram",
|
|
407
|
+
members: ["kind", "set", "pieces", "apart", "look", "color", "name"],
|
|
408
|
+
check(scene, problems) {
|
|
409
|
+
only(scene, this.members, "scene", problems);
|
|
410
|
+
const set = scene.set === undefined ? "tangram" : oneOf(scene.set, sets, "set", problems);
|
|
411
|
+
const look = scene.look === undefined ? "pieces" : oneOf(scene.look, looks, "look", problems);
|
|
412
|
+
if (scene.color !== undefined && !isColor(scene.color))
|
|
413
|
+
problems.push(`color: must be a colour: ${colorNames.join(", ")}`);
|
|
414
|
+
if (scene.name !== undefined)
|
|
415
|
+
text(scene.name, "name", problems, 16);
|
|
416
|
+
const figure = scene.pieces === undefined ? [] : list(scene.pieces, "pieces", problems, 0, most.pieces);
|
|
417
|
+
const apart = scene.apart === undefined ? [] : list(scene.apart, "apart", problems, 0, most.apart);
|
|
418
|
+
if (figure.length === 0 && apart.length === 0)
|
|
419
|
+
problems.push("pieces: give the pieces of a figure, or pieces apart, or both");
|
|
420
|
+
figure.forEach((p, i) => {
|
|
421
|
+
const path = `pieces[${i}]`;
|
|
422
|
+
if (!isObj(p)) {
|
|
423
|
+
problems.push(`${path}: must be a piece: piece and at`);
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
only(p, ["piece", "at", "turn", "flip", "color", "missing"], path, problems);
|
|
427
|
+
checkPose(p, path, problems);
|
|
428
|
+
if (!Array.isArray(p.at) ||
|
|
429
|
+
p.at.length !== 2 ||
|
|
430
|
+
!p.at.every((v) => typeof v === "number" && Number.isFinite(v) && v >= 0 && v <= most.extent))
|
|
431
|
+
problems.push(`${path}.at: must be [x, y], the top-left corner of the box round the piece, in units from 0 to ${most.extent}`);
|
|
432
|
+
if (p.missing !== undefined && p.missing !== true)
|
|
433
|
+
problems.push(`${path}.missing: must be true`);
|
|
434
|
+
if (p.missing === true && look === "silhouette")
|
|
435
|
+
problems.push(`${path}.missing: a silhouette shows no pieces, so none is missing`);
|
|
436
|
+
});
|
|
437
|
+
apart.forEach((p, i) => {
|
|
438
|
+
const path = `apart[${i}]`;
|
|
439
|
+
if (!isObj(p)) {
|
|
440
|
+
problems.push(`${path}: must be a piece`);
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
only(p, ["piece", "turn", "flip", "color", "label"], path, problems);
|
|
444
|
+
checkPose(p, path, problems);
|
|
445
|
+
if (p.label !== undefined)
|
|
446
|
+
text(p.label, `${path}.label`, problems, 2);
|
|
447
|
+
});
|
|
448
|
+
if (problems.length > 0)
|
|
449
|
+
return;
|
|
450
|
+
const typed = scene;
|
|
451
|
+
const pieces = typed.pieces ?? [];
|
|
452
|
+
if (set === "tangram") {
|
|
453
|
+
for (const [where, group] of [
|
|
454
|
+
["pieces", pieces],
|
|
455
|
+
["apart", typed.apart ?? []],
|
|
456
|
+
]) {
|
|
457
|
+
const counts = {};
|
|
458
|
+
group.forEach((p, i) => {
|
|
459
|
+
const allowed = tangramSet[p.piece];
|
|
460
|
+
if (allowed === undefined) {
|
|
461
|
+
problems.push(`${where}[${i}].piece: a tangram has no ${p.piece}; use set "free" for other pieces`);
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
counts[p.piece] = (counts[p.piece] ?? 0) + 1;
|
|
465
|
+
if (where === "pieces" && counts[p.piece] > allowed)
|
|
466
|
+
problems.push(`${where}[${i}].piece: a tangram has ${allowed === 1 ? "one" : "two"} ${p.piece}${allowed === 1 ? "" : "s"}, and the figure uses more`);
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
const shapes = pieces.map(placed);
|
|
471
|
+
shapes.forEach((a, i) => {
|
|
472
|
+
const box = boxOf(a);
|
|
473
|
+
if (box.x1 > most.extent + near || box.y1 > most.extent + near)
|
|
474
|
+
problems.push(`pieces[${i}].at: the piece reaches past ${most.extent} units`);
|
|
475
|
+
shapes.forEach((b, j) => {
|
|
476
|
+
if (j >= i)
|
|
477
|
+
return;
|
|
478
|
+
if (overlap(a, b) > tiny)
|
|
479
|
+
problems.push(`pieces[${i}]: overlaps pieces[${j}]; move it so that they only touch`);
|
|
480
|
+
});
|
|
481
|
+
});
|
|
482
|
+
if (problems.length > 0)
|
|
483
|
+
return;
|
|
484
|
+
if (shapes.length > 1) {
|
|
485
|
+
const seen = new Set([0]);
|
|
486
|
+
const todo = [0];
|
|
487
|
+
while (todo.length > 0) {
|
|
488
|
+
const i = todo.pop();
|
|
489
|
+
shapes.forEach((b, j) => {
|
|
490
|
+
if (!seen.has(j) && touch(shapes[i], b)) {
|
|
491
|
+
seen.add(j);
|
|
492
|
+
todo.push(j);
|
|
493
|
+
}
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
const lost = shapes.findIndex((_, j) => !seen.has(j));
|
|
497
|
+
if (lost >= 0)
|
|
498
|
+
problems.push(`pieces[${lost}]: does not touch the rest of the figure; a figure is one shape, its pieces meeting side to side or corner to corner`);
|
|
499
|
+
}
|
|
500
|
+
if (problems.length > 0)
|
|
501
|
+
return;
|
|
502
|
+
const layout = layoutOf(typed);
|
|
503
|
+
if (layout.s < smallest)
|
|
504
|
+
problems.push(`pieces: the figure and the pieces apart do not fit with a unit of at least ${smallest} pixels; use fewer pieces apart, or a smaller figure`);
|
|
505
|
+
pieces.forEach((p, i) => {
|
|
506
|
+
if (p.missing && inCircle(shapes[i]).r * layout.s < 10)
|
|
507
|
+
problems.push(`pieces[${i}].missing: its place is too small to hold a question mark at this size`);
|
|
508
|
+
});
|
|
509
|
+
},
|
|
510
|
+
draw(scene, pen) {
|
|
511
|
+
const typed = scene;
|
|
512
|
+
const layout = layoutOf(typed);
|
|
513
|
+
const { s } = layout;
|
|
514
|
+
const pieces = typed.pieces ?? [];
|
|
515
|
+
const look = typed.look ?? "pieces";
|
|
516
|
+
if (layout.figure) {
|
|
517
|
+
const { box, x, y } = layout.figure;
|
|
518
|
+
const at = (p) => ({ x: x + (p.x - box.x0) * s, y: y + (p.y - box.y0) * s });
|
|
519
|
+
const shapes = pieces.map(placed);
|
|
520
|
+
if (look === "silhouette") {
|
|
521
|
+
const fill = colors[typed.color ?? "black"].fill;
|
|
522
|
+
// Each piece is edged in the fill itself, so that no seam shows between them.
|
|
523
|
+
for (const pts of shapes)
|
|
524
|
+
pen.el("path", {
|
|
525
|
+
class: "silhouette",
|
|
526
|
+
d: poly(pts.map(at)),
|
|
527
|
+
fill,
|
|
528
|
+
stroke: fill,
|
|
529
|
+
"stroke-width": 1.5,
|
|
530
|
+
"stroke-linejoin": "round",
|
|
531
|
+
});
|
|
532
|
+
pen.el("path", {
|
|
533
|
+
d: outlineOf(shapes)
|
|
534
|
+
.map(([a, b]) => poly([at(a), at(b)], false))
|
|
535
|
+
.join(""),
|
|
536
|
+
fill: "none",
|
|
537
|
+
stroke: ink,
|
|
538
|
+
"stroke-width": outline,
|
|
539
|
+
"stroke-linecap": "round",
|
|
540
|
+
});
|
|
541
|
+
}
|
|
542
|
+
else {
|
|
543
|
+
const fills = colorsOf(pieces);
|
|
544
|
+
shapes.forEach((pts, i) => {
|
|
545
|
+
const missing = pieces[i].missing === true;
|
|
546
|
+
pen.el("path", {
|
|
547
|
+
class: missing ? "gap" : "piece",
|
|
548
|
+
d: poly(pts.map(at)),
|
|
549
|
+
fill: missing ? "#ffffff" : colors[fills[i]].fill,
|
|
550
|
+
stroke: ink,
|
|
551
|
+
"stroke-width": outline,
|
|
552
|
+
"stroke-linejoin": "round",
|
|
553
|
+
});
|
|
554
|
+
});
|
|
555
|
+
shapes.forEach((pts, i) => {
|
|
556
|
+
if (!pieces[i].missing)
|
|
557
|
+
return;
|
|
558
|
+
const { c, r } = inCircle(pts);
|
|
559
|
+
const p = at(c);
|
|
560
|
+
pen.text(p.x, p.y, "?", { size: Math.min(30, r * s * 1.3) });
|
|
561
|
+
});
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
const apart = typed.apart ?? [];
|
|
565
|
+
const fills = colorsOf(apart, "gray");
|
|
566
|
+
layout.apart.forEach((one, i) => {
|
|
567
|
+
const item = apart[i];
|
|
568
|
+
const top = layout.apartY + (layout.apartH - Math.max(...one.pts.map((p) => p.y)) * s) / 2;
|
|
569
|
+
pen.el("path", {
|
|
570
|
+
class: "apart",
|
|
571
|
+
d: poly(one.pts.map((p) => ({ x: one.x + p.x * s, y: top + p.y * s }))),
|
|
572
|
+
fill: colors[fills[i]].fill,
|
|
573
|
+
stroke: ink,
|
|
574
|
+
"stroke-width": outline,
|
|
575
|
+
"stroke-linejoin": "round",
|
|
576
|
+
});
|
|
577
|
+
if (item.label !== undefined)
|
|
578
|
+
pen.text(one.x + one.w / 2, layout.apartY + layout.apartH + 18, item.label, { size: 18 });
|
|
579
|
+
});
|
|
580
|
+
return { width: layout.width, height: layout.height };
|
|
581
|
+
},
|
|
582
|
+
describe(scene) {
|
|
583
|
+
const typed = scene;
|
|
584
|
+
const pieces = typed.pieces ?? [];
|
|
585
|
+
const set = typed.set ?? "tangram";
|
|
586
|
+
const out = [];
|
|
587
|
+
if (pieces.length > 0) {
|
|
588
|
+
const shapes = pieces.map(placed);
|
|
589
|
+
const box = boxOf(shapes.flat());
|
|
590
|
+
const name = typed.name ? `a ${typed.name}` : "a figure";
|
|
591
|
+
const from = set === "tangram"
|
|
592
|
+
? pieces.length === 7
|
|
593
|
+
? "the 7 pieces of a tangram"
|
|
594
|
+
: `${pieces.length} of the 7 pieces of a tangram`
|
|
595
|
+
: `${pieces.length} piece${pieces.length === 1 ? "" : "s"}`;
|
|
596
|
+
const size = `${number(box.x1 - box.x0)} units wide and ${number(box.y1 - box.y0)} high, a unit being the side of the square piece`;
|
|
597
|
+
if ((typed.look ?? "pieces") === "silhouette") {
|
|
598
|
+
const loops = chain(outlineOf(shapes));
|
|
599
|
+
const outer = loops
|
|
600
|
+
.filter((loop) => !loop.hole)
|
|
601
|
+
.map((loop) => cornersFrom(loop.corners, box));
|
|
602
|
+
const holes = loops
|
|
603
|
+
.filter((loop) => loop.hole)
|
|
604
|
+
.map((loop) => cornersFrom(loop.corners, box));
|
|
605
|
+
const parts = outer.length === 1
|
|
606
|
+
? `Its outline goes round the corners ${outer[0]}`
|
|
607
|
+
: `It is ${outer.length} shapes that touch only at points: ${outer.map((c, i) => `${i === outer.length - 1 ? "and " : ""}one with the corners ${c}`).join("; ")}`;
|
|
608
|
+
const gaps = holes.length === 0
|
|
609
|
+
? ""
|
|
610
|
+
: `; inside it, ${holes.length === 1 ? "a hole is" : `${holes.length} holes are`} left open, ${holes.map((c) => `one with the corners ${c}`).join("; ")}`;
|
|
611
|
+
out.push(`The silhouette of ${name}, all ${colors[typed.color ?? "black"].word}, laid from ${from}, its pieces not shown; it is ${size}. ${parts}${gaps}; corners are counted from its top left, x to the right and y down.`);
|
|
612
|
+
}
|
|
613
|
+
else {
|
|
614
|
+
const fills = colorsOf(pieces);
|
|
615
|
+
const shown = pieces
|
|
616
|
+
.map((p, i) => ({ p, i }))
|
|
617
|
+
.filter(({ p }) => !p.missing)
|
|
618
|
+
.map(({ p, i }) => `${article(`${colors[fills[i]].word} ${p.piece}`)} ${placeWords(shapes[i], box)}, ${poseWords(p.piece, shapes[i])}`);
|
|
619
|
+
const gaps = pieces
|
|
620
|
+
.map((p, i) => ({ p, i }))
|
|
621
|
+
.filter(({ p }) => p.missing)
|
|
622
|
+
.map(({ i }) => `${placeWords(shapes[i], box)}, with the corners ${cornersFrom(shapes[i], box)}`);
|
|
623
|
+
out.push(`${cap(name)} laid from ${from}, ${size}, drawn piece by piece: ${shown.join("; ")}.${gaps.length > 0
|
|
624
|
+
? ` ${gaps.length === 1 ? "One piece is missing: its place is left empty, with a question mark," : `${gaps.length} pieces are missing: their places are left empty, each with a question mark,`} ${gaps.join("; ")}; corners are counted from the figure's top left, x to the right and y down.`
|
|
625
|
+
: ""}`);
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
const apart = typed.apart ?? [];
|
|
629
|
+
if (apart.length > 0) {
|
|
630
|
+
const fills = colorsOf(apart, "gray");
|
|
631
|
+
const items = apart.map((p, i) => {
|
|
632
|
+
const pts = posed(p.piece, p.turn, p.flip);
|
|
633
|
+
const what = `${article(`${colors[fills[i]].word} ${p.piece}`)}, ${poseWords(p.piece, pts)}`;
|
|
634
|
+
return p.label !== undefined ? `${p.label}: ${what}` : what;
|
|
635
|
+
});
|
|
636
|
+
out.push(`${pieces.length > 0 ? "Below it, " : ""}${apart.length} piece${apart.length === 1 ? "" : "s"} apart, from left to right: ${items.join("; ")}.`);
|
|
637
|
+
}
|
|
638
|
+
return out.join(" ");
|
|
639
|
+
},
|
|
640
|
+
};
|
|
641
|
+
/**
|
|
642
|
+
* chain joins the outline's sides into closed lines of corners, dropping the
|
|
643
|
+
* corners on straight sides. Every side runs clockwise round its piece, so a
|
|
644
|
+
* line round the figure runs clockwise and a line round a hole the other way;
|
|
645
|
+
* where two lines touch at a point, the sharpest turn to the right keeps them
|
|
646
|
+
* apart.
|
|
647
|
+
*/
|
|
648
|
+
function chain(segments) {
|
|
649
|
+
const same = (a, b) => Math.hypot(a.x - b.x, a.y - b.y) < near;
|
|
650
|
+
const used = new Set();
|
|
651
|
+
const loops = [];
|
|
652
|
+
// Start each line at its top-left corner, so that the words read from there.
|
|
653
|
+
const order = segments
|
|
654
|
+
.map((seg, i) => ({ seg, i }))
|
|
655
|
+
.sort((p, q) => p.seg[0].y - q.seg[0].y || p.seg[0].x - q.seg[0].x);
|
|
656
|
+
for (const { i: first } of order) {
|
|
657
|
+
if (used.has(first))
|
|
658
|
+
continue;
|
|
659
|
+
used.add(first);
|
|
660
|
+
const loop = [segments[first][0], segments[first][1]];
|
|
661
|
+
for (;;) {
|
|
662
|
+
const end = loop[loop.length - 1];
|
|
663
|
+
if (same(end, loop[0]))
|
|
664
|
+
break;
|
|
665
|
+
const from = loop[loop.length - 2];
|
|
666
|
+
const d = { x: end.x - from.x, y: end.y - from.y };
|
|
667
|
+
let next = -1;
|
|
668
|
+
let sharpest = -Infinity;
|
|
669
|
+
segments.forEach((seg, i) => {
|
|
670
|
+
if (used.has(i) || !same(seg[0], end))
|
|
671
|
+
return;
|
|
672
|
+
const v = { x: seg[1].x - seg[0].x, y: seg[1].y - seg[0].y };
|
|
673
|
+
const turn = Math.atan2(d.x * v.y - d.y * v.x, d.x * v.x + d.y * v.y);
|
|
674
|
+
if (turn > sharpest) {
|
|
675
|
+
sharpest = turn;
|
|
676
|
+
next = i;
|
|
677
|
+
}
|
|
678
|
+
});
|
|
679
|
+
if (next < 0)
|
|
680
|
+
break;
|
|
681
|
+
used.add(next);
|
|
682
|
+
loop.push(segments[next][1]);
|
|
683
|
+
}
|
|
684
|
+
if (same(loop[loop.length - 1], loop[0]))
|
|
685
|
+
loop.pop();
|
|
686
|
+
const kept = loop.filter((p, i) => {
|
|
687
|
+
const a = loop[(i + loop.length - 1) % loop.length];
|
|
688
|
+
const b = loop[(i + 1) % loop.length];
|
|
689
|
+
return Math.abs((p.x - a.x) * (b.y - p.y) - (p.y - a.y) * (b.x - p.x)) > 1e-6;
|
|
690
|
+
});
|
|
691
|
+
let sum = 0;
|
|
692
|
+
kept.forEach((p, i) => {
|
|
693
|
+
const q = kept[(i + 1) % kept.length];
|
|
694
|
+
sum += p.x * q.y - q.x * p.y;
|
|
695
|
+
});
|
|
696
|
+
loops.push({ corners: kept, hole: sum < 0 });
|
|
697
|
+
}
|
|
698
|
+
return loops;
|
|
699
|
+
}
|
|
700
|
+
/** cornersFrom lists corners from the top-left one on, counted from the figure's top left. */
|
|
701
|
+
function cornersFrom(pts, box) {
|
|
702
|
+
let first = 0;
|
|
703
|
+
pts.forEach((p, i) => {
|
|
704
|
+
const q = pts[first];
|
|
705
|
+
if (p.y < q.y - near || (Math.abs(p.y - q.y) <= near && p.x < q.x))
|
|
706
|
+
first = i;
|
|
707
|
+
});
|
|
708
|
+
const ordered = [...pts.slice(first), ...pts.slice(0, first)];
|
|
709
|
+
return ordered.map((p) => `(${number(p.x - box.x0)}, ${number(p.y - box.y0)})`).join(", ");
|
|
710
|
+
}
|
|
711
|
+
function cap(words) {
|
|
712
|
+
return `${words[0].toUpperCase()}${words.slice(1)}`;
|
|
713
|
+
}
|