@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,41 @@
|
|
|
1
|
+
import type { Kind } from "../kind.ts";
|
|
2
|
+
import { type ColorName } from "../style.ts";
|
|
3
|
+
/**
|
|
4
|
+
* Countfig: a figure of points joined by straight segments, the picture of a
|
|
5
|
+
* task that counts the triangles, squares, rectangles or segments in it. A
|
|
6
|
+
* point has a name and a place [x, y], x to the right and y downward, in
|
|
7
|
+
* units of the figure; a segment joins two points by their names. Every
|
|
8
|
+
* crossing of two segments is a point of the scene, so that every corner of
|
|
9
|
+
* every shape in the figure has a name and the words can list each straight
|
|
10
|
+
* line with the points on it, in order. Letters and dots show on demand, and
|
|
11
|
+
* one shape may be shaded. The scene may carry its facts — how many
|
|
12
|
+
* triangles, squares, rectangles and segments the figure holds — and the
|
|
13
|
+
* check counts them itself.
|
|
14
|
+
*/
|
|
15
|
+
export type FigPoint = {
|
|
16
|
+
name: string;
|
|
17
|
+
at: [number, number];
|
|
18
|
+
};
|
|
19
|
+
export type Highlight = {
|
|
20
|
+
corners: string[];
|
|
21
|
+
color?: ColorName;
|
|
22
|
+
};
|
|
23
|
+
export declare const countables: readonly ["triangles", "squares", "rectangles", "segments"];
|
|
24
|
+
export type Countable = (typeof countables)[number];
|
|
25
|
+
export type CountfigScene = {
|
|
26
|
+
kind: "countfig";
|
|
27
|
+
points: FigPoint[];
|
|
28
|
+
segments: [string, string][];
|
|
29
|
+
letters?: boolean;
|
|
30
|
+
dots?: boolean;
|
|
31
|
+
highlight?: Highlight;
|
|
32
|
+
facts?: Partial<Record<Countable, number>>;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* countShapes counts what a task may ask about: the triangles, squares and
|
|
36
|
+
* rectangles whose sides lie along the lines of the figure, with corners at
|
|
37
|
+
* its points, and the segments between two points along its lines. A square
|
|
38
|
+
* is a rectangle too.
|
|
39
|
+
*/
|
|
40
|
+
export declare function countShapes(f: CountfigScene): Record<Countable, number>;
|
|
41
|
+
export declare const countfig: Kind;
|
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
import { margin } from "../kind.js";
|
|
2
|
+
import { textWidth } from "../pen.js";
|
|
3
|
+
import { int, isObj, list, only } from "../read.js";
|
|
4
|
+
import { colorNames, colors, ink, isColor, outline } from "../style.js";
|
|
5
|
+
import { byCodeUnit, poly } from "../svg.js";
|
|
6
|
+
import { area, around, boxAt, boxToSegment, covered, crossing, distance, dot, gap, listOf, many, meet, pointOf, say, sub, toBox, toLine, toSegment, widest, } from "./plane-parts.js";
|
|
7
|
+
export const countables = ["triangles", "squares", "rectangles", "segments"];
|
|
8
|
+
/** The figure lies in a square of this many units a side. */
|
|
9
|
+
const span = 20;
|
|
10
|
+
/** How far, in units, a point may be from a line and still lie on it. */
|
|
11
|
+
const near = 0.01;
|
|
12
|
+
const letterSize = 18;
|
|
13
|
+
const dotR = 4.5;
|
|
14
|
+
/** Points keep this many pixels apart, a point off a line this many from it. */
|
|
15
|
+
const apart = 16;
|
|
16
|
+
const off = 9;
|
|
17
|
+
/** The narrowest angle, in degrees, between two lines leaving one point. */
|
|
18
|
+
const narrowest = 10;
|
|
19
|
+
const names = /^[A-Z][0-9]?$/;
|
|
20
|
+
const placeOf = (p) => ({ x: p.at[0], y: p.at[1] });
|
|
21
|
+
/** unitOf is how many pixels a unit of the figure takes: the figure fits 480 × 340. */
|
|
22
|
+
function unitOf(points) {
|
|
23
|
+
const xs = points.map((p) => p.at[0]);
|
|
24
|
+
const ys = points.map((p) => p.at[1]);
|
|
25
|
+
const w = Math.max(...xs) - Math.min(...xs);
|
|
26
|
+
const h = Math.max(...ys) - Math.min(...ys);
|
|
27
|
+
return Math.min(60, w > 0 ? 480 / w : 60, h > 0 ? 340 / h : 60);
|
|
28
|
+
}
|
|
29
|
+
function figureOf(f) {
|
|
30
|
+
const index = new Map(f.points.map((p, i) => [p.name, i]));
|
|
31
|
+
const at = f.points.map(placeOf);
|
|
32
|
+
const ends = f.segments.map(([a, b]) => [index.get(a), index.get(b)]);
|
|
33
|
+
return { at, ends, segs: ends.map(([a, b]) => [at[a], at[b]]) };
|
|
34
|
+
}
|
|
35
|
+
/** directions are the angles, on the screen, of the lines that leave point k. */
|
|
36
|
+
function directions(fig, k) {
|
|
37
|
+
const p = fig.at[k];
|
|
38
|
+
const out = [];
|
|
39
|
+
const toward = (q) => Math.atan2(q.y - p.y, q.x - p.x);
|
|
40
|
+
fig.ends.forEach(([a, b], s) => {
|
|
41
|
+
if (a === k)
|
|
42
|
+
out.push(toward(fig.at[b]));
|
|
43
|
+
else if (b === k)
|
|
44
|
+
out.push(toward(fig.at[a]));
|
|
45
|
+
else if (toSegment(p, ...fig.segs[s]) <= near) {
|
|
46
|
+
out.push(toward(fig.at[a]), toward(fig.at[b]));
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
/** alone tells which points lie on no segment: they show as dots, or they would not show. */
|
|
52
|
+
function alone(fig) {
|
|
53
|
+
return fig.at.map((p) => fig.segs.every(([a, b]) => toSegment(p, a, b) > near));
|
|
54
|
+
}
|
|
55
|
+
/** letterPlaces finds a place for each point's letter, in pixels from the figure's top left. */
|
|
56
|
+
function letterPlaces(f, fig, unit) {
|
|
57
|
+
const px = fig.at.map((p) => ({ x: p.x * unit, y: p.y * unit }));
|
|
58
|
+
const segs = fig.ends.map(([a, b]) => [px[a], px[b]]);
|
|
59
|
+
const placed = [];
|
|
60
|
+
return f.points.map((point, k) => {
|
|
61
|
+
const w = textWidth(point.name, letterSize) + 2;
|
|
62
|
+
const h = letterSize * 0.78;
|
|
63
|
+
const prefer = widest(directions(fig, k), -Math.PI / 4);
|
|
64
|
+
const spot = around(px[k], w, h, prefer, (box) => Math.min(...segs.map(([a, b]) => boxToSegment(box, a, b) - outline / 2), ...px.map((q, j) => (j === k ? Number.POSITIVE_INFINITY : toBox(q, box) - dotR)), ...placed.map((other) => gap(box, other))), 6, [7, 11, 16]);
|
|
65
|
+
placed.push(boxAt(spot.at, w, h));
|
|
66
|
+
return spot;
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* countShapes counts what a task may ask about: the triangles, squares and
|
|
71
|
+
* rectangles whose sides lie along the lines of the figure, with corners at
|
|
72
|
+
* its points, and the segments between two points along its lines. A square
|
|
73
|
+
* is a rectangle too.
|
|
74
|
+
*/
|
|
75
|
+
export function countShapes(f) {
|
|
76
|
+
const fig = figureOf(f);
|
|
77
|
+
const k = fig.at.length;
|
|
78
|
+
const joined = fig.at.map((p, i) => fig.at.map((q, j) => i !== j && covered(p, q, fig.segs, near)));
|
|
79
|
+
const at = fig.at;
|
|
80
|
+
const counts = { triangles: 0, squares: 0, rectangles: 0, segments: 0 };
|
|
81
|
+
for (let i = 0; i < k; i++)
|
|
82
|
+
for (let j = i + 1; j < k; j++) {
|
|
83
|
+
if (!joined[i][j])
|
|
84
|
+
continue;
|
|
85
|
+
counts.segments++;
|
|
86
|
+
for (let l = j + 1; l < k; l++)
|
|
87
|
+
if (joined[j][l] && joined[i][l] && toLine(at[l], at[i], at[j]) > near)
|
|
88
|
+
counts.triangles++;
|
|
89
|
+
}
|
|
90
|
+
const right = (a, b, c) => {
|
|
91
|
+
const u = sub(b, a);
|
|
92
|
+
const v = sub(c, b);
|
|
93
|
+
return Math.abs(dot(u, v)) <= 1e-3 * Math.hypot(u.x, u.y) * Math.hypot(v.x, v.y);
|
|
94
|
+
};
|
|
95
|
+
const rectangle = (o) => o.every((v, i) => joined[v][o[(i + 1) % 4]]) &&
|
|
96
|
+
o.every((v, i) => right(at[v], at[o[(i + 1) % 4]], at[o[(i + 2) % 4]]));
|
|
97
|
+
for (let a = 0; a < k; a++)
|
|
98
|
+
for (let b = a + 1; b < k; b++)
|
|
99
|
+
for (let c = b + 1; c < k; c++)
|
|
100
|
+
for (let d = c + 1; d < k; d++) {
|
|
101
|
+
const order = [
|
|
102
|
+
[a, b, c, d],
|
|
103
|
+
[a, b, d, c],
|
|
104
|
+
[a, c, b, d],
|
|
105
|
+
].find(rectangle);
|
|
106
|
+
if (!order)
|
|
107
|
+
continue;
|
|
108
|
+
counts.rectangles++;
|
|
109
|
+
const s1 = distance(at[order[0]], at[order[1]]);
|
|
110
|
+
const s2 = distance(at[order[1]], at[order[2]]);
|
|
111
|
+
if (Math.abs(s1 - s2) <= 1e-3 * Math.max(s1, s2))
|
|
112
|
+
counts.squares++;
|
|
113
|
+
}
|
|
114
|
+
return counts;
|
|
115
|
+
}
|
|
116
|
+
/** lines are the straight lines of the figure, each with the names of the points on it in order. */
|
|
117
|
+
function lines(f) {
|
|
118
|
+
const fig = figureOf(f);
|
|
119
|
+
const group = fig.segs.map((_, i) => i);
|
|
120
|
+
const root = (i) => (group[i] === i ? i : root(group[i]));
|
|
121
|
+
fig.segs.forEach(([a, b], i) => {
|
|
122
|
+
for (let j = 0; j < i; j++) {
|
|
123
|
+
const [c, d] = fig.segs[j];
|
|
124
|
+
if (toLine(c, a, b) > near || toLine(d, a, b) > near)
|
|
125
|
+
continue;
|
|
126
|
+
if (meet(a, b, c, d, near).how !== "apart")
|
|
127
|
+
group[root(i)] = root(j);
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
const out = [];
|
|
131
|
+
const seen = new Set();
|
|
132
|
+
fig.segs.forEach((_, i) => {
|
|
133
|
+
const r = root(i);
|
|
134
|
+
if (seen.has(r))
|
|
135
|
+
return;
|
|
136
|
+
seen.add(r);
|
|
137
|
+
const mine = fig.segs.filter((_, j) => root(j) === r);
|
|
138
|
+
const on = fig.at
|
|
139
|
+
.map((p, k) => ({ p, k }))
|
|
140
|
+
.filter(({ p }) => mine.some(([a, b]) => toSegment(p, a, b) <= near));
|
|
141
|
+
// From the leftmost point, or the topmost of a line that goes straight down.
|
|
142
|
+
const start = on.reduce((s, q) => q.p.x < s.p.x - near || (Math.abs(q.p.x - s.p.x) <= near && q.p.y < s.p.y) ? q : s, on[0]);
|
|
143
|
+
on.sort((q, r2) => distance(q.p, start.p) - distance(r2.p, start.p));
|
|
144
|
+
out.push(on.map(({ k }) => f.points[k].name));
|
|
145
|
+
});
|
|
146
|
+
return out;
|
|
147
|
+
}
|
|
148
|
+
function sayLine(names_) {
|
|
149
|
+
if (names_.length === 2)
|
|
150
|
+
return `from ${names_[0]} to ${names_[1]}`;
|
|
151
|
+
return `from ${names_[0]} through ${listOf(names_.slice(1, -1))} to ${names_[names_.length - 1]}`;
|
|
152
|
+
}
|
|
153
|
+
export const countfig = {
|
|
154
|
+
kind: "countfig",
|
|
155
|
+
members: ["kind", "points", "segments", "letters", "dots", "highlight", "facts"],
|
|
156
|
+
check(scene, problems) {
|
|
157
|
+
only(scene, this.members, "scene", problems);
|
|
158
|
+
const before = problems.length;
|
|
159
|
+
const known = new Map();
|
|
160
|
+
list(scene.points, "points", problems, 2, 30).forEach((p, i) => {
|
|
161
|
+
const path = `points[${i}]`;
|
|
162
|
+
if (!isObj(p)) {
|
|
163
|
+
problems.push(`${path}: must be a point, as {"name": "A", "at": [0, 4]}`);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
only(p, ["name", "at"], path, problems);
|
|
167
|
+
if (typeof p.name !== "string" || !names.test(p.name))
|
|
168
|
+
problems.push(`${path}.name: must be a capital letter, maybe with one digit after it, as "A" or "B1"`);
|
|
169
|
+
else if (known.has(p.name))
|
|
170
|
+
problems.push(`${path}.name: ${p.name} names points[${known.get(p.name)}] already`);
|
|
171
|
+
else
|
|
172
|
+
known.set(p.name, i);
|
|
173
|
+
pointOf(p.at, `${path}.at`, problems, [span, span], 2);
|
|
174
|
+
});
|
|
175
|
+
const pairs = new Map();
|
|
176
|
+
list(scene.segments, "segments", problems, 1, 60).forEach((s, i) => {
|
|
177
|
+
const path = `segments[${i}]`;
|
|
178
|
+
if (!Array.isArray(s) || s.length !== 2 || s.some((x) => typeof x !== "string")) {
|
|
179
|
+
problems.push(`${path}: must be the names of the two points it joins, as ["A", "B"]`);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
const [a, b] = s;
|
|
183
|
+
[a, b].forEach((name, k) => {
|
|
184
|
+
if (!known.has(name))
|
|
185
|
+
problems.push(`${path}[${k}]: ${name} is no point of the scene`);
|
|
186
|
+
});
|
|
187
|
+
if (a === b)
|
|
188
|
+
problems.push(`${path}: joins ${a} to itself; a segment joins two points`);
|
|
189
|
+
const key = [a, b].sort(byCodeUnit).join(" ");
|
|
190
|
+
if (pairs.has(key))
|
|
191
|
+
problems.push(`${path}: joins ${a} and ${b} as segments[${pairs.get(key)}] does`);
|
|
192
|
+
else
|
|
193
|
+
pairs.set(key, i);
|
|
194
|
+
});
|
|
195
|
+
for (const member of ["letters", "dots"])
|
|
196
|
+
if (scene[member] !== undefined && typeof scene[member] !== "boolean")
|
|
197
|
+
problems.push(`${member}: must be true or false`);
|
|
198
|
+
const h = scene.highlight;
|
|
199
|
+
if (h !== undefined) {
|
|
200
|
+
if (!isObj(h))
|
|
201
|
+
problems.push('highlight: must be a shape, as {"corners": ["A", "B", "C"]}');
|
|
202
|
+
else {
|
|
203
|
+
only(h, ["corners", "color"], "highlight", problems);
|
|
204
|
+
const seen = new Set();
|
|
205
|
+
list(h.corners, "highlight.corners", problems, 3, 8).forEach((c, i) => {
|
|
206
|
+
if (typeof c !== "string" || !known.has(c))
|
|
207
|
+
problems.push(`highlight.corners[${i}]: must be the name of a point of the scene`);
|
|
208
|
+
else if (seen.has(c))
|
|
209
|
+
problems.push(`highlight.corners[${i}]: ${c} is a corner already`);
|
|
210
|
+
else
|
|
211
|
+
seen.add(c);
|
|
212
|
+
});
|
|
213
|
+
if (h.color !== undefined && !isColor(h.color))
|
|
214
|
+
problems.push(`highlight.color: must be a colour: ${colorNames.join(", ")}`);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
const facts = scene.facts;
|
|
218
|
+
if (facts !== undefined) {
|
|
219
|
+
if (!isObj(facts))
|
|
220
|
+
problems.push('facts: must hold counts, as {"triangles": 8}');
|
|
221
|
+
else {
|
|
222
|
+
only(facts, countables, "facts", problems);
|
|
223
|
+
for (const c of countables)
|
|
224
|
+
if (facts[c] !== undefined)
|
|
225
|
+
int(facts[c], `facts.${c}`, problems, 0, 100000);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
if (problems.length > before)
|
|
229
|
+
return;
|
|
230
|
+
const f = scene;
|
|
231
|
+
const fig = figureOf(f);
|
|
232
|
+
const unit = unitOf(f.points);
|
|
233
|
+
const name = (k) => f.points[k].name;
|
|
234
|
+
const segName = (s) => `segments[${s}] (${f.segments[s].join("–")})`;
|
|
235
|
+
for (let i = 0; i < fig.at.length; i++)
|
|
236
|
+
for (let j = 0; j < i; j++) {
|
|
237
|
+
const d = distance(fig.at[i], fig.at[j]) * unit;
|
|
238
|
+
if (d < apart)
|
|
239
|
+
problems.push(`points[${i}]: ${name(i)} is ${d < 0.5 ? "in the same place as" : "too close to"} ${name(j)}; keep points at least ${say(apart / unit)} apart`);
|
|
240
|
+
}
|
|
241
|
+
if (problems.length > before)
|
|
242
|
+
return;
|
|
243
|
+
fig.segs.forEach(([a, b], i) => {
|
|
244
|
+
for (let j = 0; j < i; j++) {
|
|
245
|
+
const [c, d] = fig.segs[j];
|
|
246
|
+
const m = meet(c, d, a, b, near);
|
|
247
|
+
if (m.how === "along")
|
|
248
|
+
problems.push(`segments[${i}]: runs along ${segName(j)} for a stretch; draw each stretch of a line once`);
|
|
249
|
+
else if (m.how === "point" && fig.at.every((p) => distance(p, m.at) > 2 * near))
|
|
250
|
+
problems.push(`${segName(j)} and ${segName(i)} cross at [${say(m.at.x)}, ${say(m.at.y)}], which is no point of the scene; add a point there`);
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
fig.at.forEach((p, k) => {
|
|
254
|
+
fig.segs.forEach(([a, b], s) => {
|
|
255
|
+
const d = toSegment(p, a, b);
|
|
256
|
+
if (d > near && d * unit < off)
|
|
257
|
+
problems.push(`points[${k}]: ${name(k)} lies close to ${segName(s)} but not on it; put it on that line or farther from it`);
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
if (problems.length > before)
|
|
261
|
+
return;
|
|
262
|
+
fig.at.forEach((_, k) => {
|
|
263
|
+
const ways = directions(fig, k)
|
|
264
|
+
.map((a) => ((((a * 180) / Math.PI) % 360) + 360) % 360)
|
|
265
|
+
.sort((x, y) => x - y);
|
|
266
|
+
ways.forEach((a, i) => {
|
|
267
|
+
const next = i + 1 < ways.length ? ways[i + 1] : ways[0] + 360;
|
|
268
|
+
if (ways.length > 1 && next - a < narrowest)
|
|
269
|
+
problems.push(`points[${k}]: two lines leave ${name(k)} only ${say(next - a)}° apart, too narrow to tell them apart; make it at least ${narrowest}°`);
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
if (f.highlight) {
|
|
273
|
+
const corners = f.highlight.corners.map((c) => fig.at[f.points.findIndex((p) => p.name === c)]);
|
|
274
|
+
const k = corners.length;
|
|
275
|
+
if (crossing(corners, near))
|
|
276
|
+
problems.push("highlight.corners: its sides cross; list the corners in order round the shape");
|
|
277
|
+
else if (area(corners) * unit * unit < 80)
|
|
278
|
+
problems.push("highlight.corners: the shape has no room inside to shade");
|
|
279
|
+
else
|
|
280
|
+
corners.forEach((c, i) => {
|
|
281
|
+
if (!covered(c, corners[(i + 1) % k], fig.segs, near))
|
|
282
|
+
problems.push(`highlight.corners: its side from ${f.highlight.corners[i]} to ${f.highlight.corners[(i + 1) % k]} does not run along the lines of the figure`);
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
if (f.letters)
|
|
286
|
+
letterPlaces(f, fig, unit).forEach((spot, k) => {
|
|
287
|
+
if (spot.clear < 2)
|
|
288
|
+
problems.push(`points[${k}].name: no room for the letter ${name(k)} beside its point; move the lines near it apart, or leave the letters out`);
|
|
289
|
+
});
|
|
290
|
+
if (problems.length > before || !f.facts)
|
|
291
|
+
return;
|
|
292
|
+
const counts = countShapes(f);
|
|
293
|
+
for (const c of countables) {
|
|
294
|
+
const given = f.facts[c];
|
|
295
|
+
if (given !== undefined && given !== counts[c])
|
|
296
|
+
problems.push(`facts.${c}: the figure holds ${counts[c]} ${c}, not ${given}`);
|
|
297
|
+
}
|
|
298
|
+
},
|
|
299
|
+
draw(scene, pen) {
|
|
300
|
+
const f = scene;
|
|
301
|
+
const fig = figureOf(f);
|
|
302
|
+
const unit = unitOf(f.points);
|
|
303
|
+
const px = fig.at.map((p) => ({ x: p.x * unit, y: p.y * unit }));
|
|
304
|
+
const lonely = alone(fig);
|
|
305
|
+
const dotted = (k) => f.dots === true || lonely[k] === true;
|
|
306
|
+
const pad = Math.max(outline / 2, ...px.map((_, k) => (dotted(k) ? dotR : 0)));
|
|
307
|
+
const box = {
|
|
308
|
+
x0: Math.min(...px.map((p) => p.x)) - pad,
|
|
309
|
+
y0: Math.min(...px.map((p) => p.y)) - pad,
|
|
310
|
+
x1: Math.max(...px.map((p) => p.x)) + pad,
|
|
311
|
+
y1: Math.max(...px.map((p) => p.y)) + pad,
|
|
312
|
+
};
|
|
313
|
+
const spots = f.letters ? letterPlaces(f, fig, unit) : [];
|
|
314
|
+
spots.forEach((spot, k) => {
|
|
315
|
+
const b = boxAt(spot.at, textWidth(f.points[k].name, letterSize) + 2, letterSize * 0.78);
|
|
316
|
+
box.x0 = Math.min(box.x0, b.x0);
|
|
317
|
+
box.y0 = Math.min(box.y0, b.y0);
|
|
318
|
+
box.x1 = Math.max(box.x1, b.x1);
|
|
319
|
+
box.y1 = Math.max(box.y1, b.y1);
|
|
320
|
+
});
|
|
321
|
+
const at = (p) => ({ x: p.x + margin - box.x0, y: p.y + margin - box.y0 });
|
|
322
|
+
if (f.highlight) {
|
|
323
|
+
const corners = f.highlight.corners.map((c) => px[f.points.findIndex((p) => p.name === c)]);
|
|
324
|
+
pen.el("path", {
|
|
325
|
+
class: "highlight",
|
|
326
|
+
d: poly(corners.map(at)),
|
|
327
|
+
fill: colors[f.highlight.color ?? "yellow"].fill,
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
fig.ends.forEach(([a, b]) => {
|
|
331
|
+
pen.el("path", {
|
|
332
|
+
class: "segment",
|
|
333
|
+
d: poly([at(px[a]), at(px[b])], false),
|
|
334
|
+
fill: "none",
|
|
335
|
+
stroke: ink,
|
|
336
|
+
"stroke-width": outline,
|
|
337
|
+
"stroke-linecap": "round",
|
|
338
|
+
});
|
|
339
|
+
});
|
|
340
|
+
px.forEach((p, k) => {
|
|
341
|
+
if (!dotted(k))
|
|
342
|
+
return;
|
|
343
|
+
const q = at(p);
|
|
344
|
+
pen.el("circle", { class: "dot", cx: q.x, cy: q.y, r: dotR, fill: ink });
|
|
345
|
+
});
|
|
346
|
+
spots.forEach((spot, k) => {
|
|
347
|
+
const q = at(spot.at);
|
|
348
|
+
pen.text(q.x, q.y, f.points[k].name, { size: letterSize });
|
|
349
|
+
});
|
|
350
|
+
return { width: box.x1 - box.x0 + 2 * margin, height: box.y1 - box.y0 + 2 * margin };
|
|
351
|
+
},
|
|
352
|
+
describe(scene) {
|
|
353
|
+
const f = scene;
|
|
354
|
+
const fig = figureOf(f);
|
|
355
|
+
const out = [];
|
|
356
|
+
const shown = [f.letters ? "lettered" : "", f.dots ? "marked with a dot" : ""].filter(Boolean);
|
|
357
|
+
out.push(`A figure of ${many(f.points.length, "point")} joined by straight lines${shown.length > 0 ? `; each point is ${shown.join(" and ")}` : ""}.`);
|
|
358
|
+
if (!f.letters)
|
|
359
|
+
out.push("The picture shows no letters; the names here only tell the points apart.");
|
|
360
|
+
out.push(`The points, at (x, y) with x to the right and y downward: ${f.points
|
|
361
|
+
.map((p) => `${p.name} (${say(p.at[0])}, ${say(p.at[1])})`)
|
|
362
|
+
.join(", ")}.`);
|
|
363
|
+
out.push(`The straight lines: ${lines(f).map(sayLine).join("; ")}.`);
|
|
364
|
+
const lonely = alone(fig);
|
|
365
|
+
const loose = f.points.filter((_, k) => lonely[k]).map((p) => p.name);
|
|
366
|
+
if (loose.length > 0)
|
|
367
|
+
out.push(`${listOf(loose)} ${loose.length === 1 ? "is a dot" : "are dots"} on no line.`);
|
|
368
|
+
if (f.highlight)
|
|
369
|
+
out.push(`The shape with the corners ${listOf(f.highlight.corners)} is shaded ${colors[f.highlight.color ?? "yellow"].word}.`);
|
|
370
|
+
return out.join(" ");
|
|
371
|
+
},
|
|
372
|
+
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { Kind } from "../kind.ts";
|
|
2
|
+
import { type ColorName } from "../style.ts";
|
|
3
|
+
import { type P } from "../svg.ts";
|
|
4
|
+
import { type Thing } from "../things.ts";
|
|
5
|
+
/**
|
|
6
|
+
* Cubes: one to four structures of unit cubes, side by side, drawn in
|
|
7
|
+
* isometric projection. x runs to the right along a row, y from the back row
|
|
8
|
+
* to the front, z up from the table. A structure gives its cubes by place, or
|
|
9
|
+
* as a height map: rows from the back, places from the left, the number of
|
|
10
|
+
* cubes stacked in each. Each cube is one colour of the palette; its top is
|
|
11
|
+
* the colour itself, its front a darker tone, its right side the darkest.
|
|
12
|
+
* A face shows exactly where no nearer cube covers it, so the picture is the
|
|
13
|
+
* structure seen from the front, the right and above.
|
|
14
|
+
*/
|
|
15
|
+
export type At = [number, number, number];
|
|
16
|
+
export type CubeItem = At | {
|
|
17
|
+
at: At;
|
|
18
|
+
color?: ColorName;
|
|
19
|
+
};
|
|
20
|
+
export declare const faces: readonly ["top", "front", "right"];
|
|
21
|
+
export type Face = (typeof faces)[number];
|
|
22
|
+
export type Mark = {
|
|
23
|
+
cube: At;
|
|
24
|
+
face: Face;
|
|
25
|
+
thing: Thing;
|
|
26
|
+
};
|
|
27
|
+
export type Structure = {
|
|
28
|
+
cubes?: CubeItem[];
|
|
29
|
+
heights?: number[][];
|
|
30
|
+
color?: ColorName;
|
|
31
|
+
marks?: Mark[];
|
|
32
|
+
label?: string;
|
|
33
|
+
};
|
|
34
|
+
export type CubesScene = {
|
|
35
|
+
kind: "cubes";
|
|
36
|
+
structures: Structure[];
|
|
37
|
+
};
|
|
38
|
+
export type Cube = {
|
|
39
|
+
x: number;
|
|
40
|
+
y: number;
|
|
41
|
+
z: number;
|
|
42
|
+
color: ColorName;
|
|
43
|
+
};
|
|
44
|
+
/** The side of the square a sticker on a face is drawn in, before it is laid on the face. */
|
|
45
|
+
export declare const box = 64;
|
|
46
|
+
/** mix is a colour between a and b, t of the way to b. */
|
|
47
|
+
export declare function mix(a: string, b: string, t: number): string;
|
|
48
|
+
/** tones are the fills of a cube's three faces: lightest on top, darker in front, darkest on the right. */
|
|
49
|
+
export declare function tones(name: ColorName): Record<Face, string>;
|
|
50
|
+
/** cubesOf lists the cubes of a checked structure, each with its colour. */
|
|
51
|
+
export declare function cubesOf(st: Structure): Cube[];
|
|
52
|
+
/** The isometric point of a corner of the grid, cube edge s, origin ox, oy. */
|
|
53
|
+
export type Projector = (x: number, y: number, z: number) => P;
|
|
54
|
+
export declare function projector(s: number, ox: number, oy: number): Projector;
|
|
55
|
+
/**
|
|
56
|
+
* cornersOf are the corners of a face of the cube at x, y, z, in the order a
|
|
57
|
+
* sticker on it reads: top left, top right, bottom right, bottom left.
|
|
58
|
+
*/
|
|
59
|
+
export declare function cornersOf(c: {
|
|
60
|
+
x: number;
|
|
61
|
+
y: number;
|
|
62
|
+
z: number;
|
|
63
|
+
}, face: Face, at: Projector): P[];
|
|
64
|
+
/** onFace is the transform that lays a box × box square on a face given by its corners. */
|
|
65
|
+
export declare function onFace(corners: readonly P[]): string;
|
|
66
|
+
/** sight says, for each cube and face, how many of its two triangles are in sight. */
|
|
67
|
+
export declare function sight(cubes: readonly Cube[]): Record<Face, number>[];
|
|
68
|
+
/** article is "a" or "an" before a word. */
|
|
69
|
+
export declare function article(word: string): string;
|
|
70
|
+
export declare const cubes: Kind;
|