@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.
Files changed (122) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +7 -0
  3. package/README.md +27 -2
  4. package/lib/emoji.d.ts +16 -0
  5. package/lib/emoji.js +64 -0
  6. package/lib/index.d.ts +9 -0
  7. package/lib/index.js +10 -0
  8. package/lib/kind.d.ts +22 -0
  9. package/lib/kind.js +2 -0
  10. package/lib/kinds/balance.d.ts +34 -0
  11. package/lib/kinds/balance.js +217 -0
  12. package/lib/kinds/branches.d.ts +33 -0
  13. package/lib/kinds/branches.js +473 -0
  14. package/lib/kinds/calendar.d.ts +55 -0
  15. package/lib/kinds/calendar.js +532 -0
  16. package/lib/kinds/chart.d.ts +48 -0
  17. package/lib/kinds/chart.js +645 -0
  18. package/lib/kinds/choices.d.ts +14 -0
  19. package/lib/kinds/choices.js +53 -0
  20. package/lib/kinds/clock.d.ts +36 -0
  21. package/lib/kinds/clock.js +219 -0
  22. package/lib/kinds/coins.d.ts +37 -0
  23. package/lib/kinds/coins.js +550 -0
  24. package/lib/kinds/column.d.ts +37 -0
  25. package/lib/kinds/column.js +410 -0
  26. package/lib/kinds/commands-parts.d.ts +12 -0
  27. package/lib/kinds/commands-parts.js +24 -0
  28. package/lib/kinds/commands.d.ts +66 -0
  29. package/lib/kinds/commands.js +789 -0
  30. package/lib/kinds/containers.d.ts +52 -0
  31. package/lib/kinds/containers.js +758 -0
  32. package/lib/kinds/countfig.d.ts +41 -0
  33. package/lib/kinds/countfig.js +372 -0
  34. package/lib/kinds/cubes.d.ts +70 -0
  35. package/lib/kinds/cubes.js +371 -0
  36. package/lib/kinds/die.d.ts +32 -0
  37. package/lib/kinds/die.js +301 -0
  38. package/lib/kinds/domino.d.ts +37 -0
  39. package/lib/kinds/domino.js +450 -0
  40. package/lib/kinds/dotgrid.d.ts +38 -0
  41. package/lib/kinds/dotgrid.js +411 -0
  42. package/lib/kinds/figure.d.ts +62 -0
  43. package/lib/kinds/figure.js +687 -0
  44. package/lib/kinds/fold-parts.d.ts +73 -0
  45. package/lib/kinds/fold-parts.js +315 -0
  46. package/lib/kinds/fold.d.ts +52 -0
  47. package/lib/kinds/fold.js +650 -0
  48. package/lib/kinds/fraction.d.ts +50 -0
  49. package/lib/kinds/fraction.js +568 -0
  50. package/lib/kinds/gauges.d.ts +53 -0
  51. package/lib/kinds/gauges.js +880 -0
  52. package/lib/kinds/gears.d.ts +40 -0
  53. package/lib/kinds/gears.js +787 -0
  54. package/lib/kinds/grid.d.ts +45 -0
  55. package/lib/kinds/grid.js +591 -0
  56. package/lib/kinds/groups.d.ts +23 -0
  57. package/lib/kinds/groups.js +335 -0
  58. package/lib/kinds/index.d.ts +3 -0
  59. package/lib/kinds/index.js +87 -0
  60. package/lib/kinds/lanes.d.ts +36 -0
  61. package/lib/kinds/lanes.js +220 -0
  62. package/lib/kinds/layers.d.ts +31 -0
  63. package/lib/kinds/layers.js +419 -0
  64. package/lib/kinds/line.d.ts +38 -0
  65. package/lib/kinds/line.js +739 -0
  66. package/lib/kinds/magic-parts.d.ts +72 -0
  67. package/lib/kinds/magic-parts.js +186 -0
  68. package/lib/kinds/magic.d.ts +66 -0
  69. package/lib/kinds/magic.js +664 -0
  70. package/lib/kinds/map.d.ts +32 -0
  71. package/lib/kinds/map.js +370 -0
  72. package/lib/kinds/matchsticks.d.ts +26 -0
  73. package/lib/kinds/matchsticks.js +434 -0
  74. package/lib/kinds/necklace.d.ts +27 -0
  75. package/lib/kinds/necklace.js +342 -0
  76. package/lib/kinds/net.d.ts +47 -0
  77. package/lib/kinds/net.js +241 -0
  78. package/lib/kinds/overlay.d.ts +50 -0
  79. package/lib/kinds/overlay.js +519 -0
  80. package/lib/kinds/pieces.d.ts +41 -0
  81. package/lib/kinds/pieces.js +420 -0
  82. package/lib/kinds/pipes.d.ts +46 -0
  83. package/lib/kinds/pipes.js +657 -0
  84. package/lib/kinds/plane-parts.d.ts +105 -0
  85. package/lib/kinds/plane-parts.js +372 -0
  86. package/lib/kinds/plane.d.ts +35 -0
  87. package/lib/kinds/plane.js +755 -0
  88. package/lib/kinds/pyramid.d.ts +26 -0
  89. package/lib/kinds/pyramid.js +228 -0
  90. package/lib/kinds/ropes.d.ts +30 -0
  91. package/lib/kinds/ropes.js +571 -0
  92. package/lib/kinds/shapes.d.ts +59 -0
  93. package/lib/kinds/shapes.js +490 -0
  94. package/lib/kinds/solids.d.ts +78 -0
  95. package/lib/kinds/solids.js +1058 -0
  96. package/lib/kinds/tangram.d.ts +52 -0
  97. package/lib/kinds/tangram.js +713 -0
  98. package/lib/kinds/target.d.ts +39 -0
  99. package/lib/kinds/target.js +761 -0
  100. package/lib/kinds/tiles.d.ts +55 -0
  101. package/lib/kinds/tiles.js +606 -0
  102. package/lib/kinds/venn.d.ts +38 -0
  103. package/lib/kinds/venn.js +710 -0
  104. package/lib/noto.d.ts +2 -0
  105. package/lib/noto.js +212 -0
  106. package/lib/objects.d.ts +7 -0
  107. package/lib/objects.js +226 -0
  108. package/lib/pen.d.ts +32 -0
  109. package/lib/pen.js +60 -0
  110. package/lib/read.d.ts +9 -0
  111. package/lib/read.js +49 -0
  112. package/lib/render.d.ts +24 -0
  113. package/lib/render.js +68 -0
  114. package/lib/solve/balance.d.ts +46 -0
  115. package/lib/solve/balance.js +118 -0
  116. package/lib/style.d.ts +70 -0
  117. package/lib/style.js +39 -0
  118. package/lib/svg.d.ts +22 -0
  119. package/lib/svg.js +48 -0
  120. package/lib/things.d.ts +40 -0
  121. package/lib/things.js +269 -0
  122. package/package.json +28 -4
@@ -0,0 +1,105 @@
1
+ import { type Pen } from "../pen.ts";
2
+ import { type Problems } from "../read.ts";
3
+ import type { P } from "../svg.ts";
4
+ export declare const sub: (a: P, b: P) => P;
5
+ export declare const dot: (a: P, b: P) => number;
6
+ export declare const crossOf: (a: P, b: P) => number;
7
+ export declare const distance: (a: P, b: P) => number;
8
+ export declare const lerp: (a: P, b: P, t: number) => P;
9
+ /** along is where p falls on the line from a to b: 0 at a, 1 at b. */
10
+ export declare function along(p: P, a: P, b: P): number;
11
+ /** toSegment is the distance from p to the segment from a to b. */
12
+ export declare function toSegment(p: P, a: P, b: P): number;
13
+ /** toLine is the distance from p to the whole straight line through a and b. */
14
+ export declare function toLine(p: P, a: P, b: P): number;
15
+ /**
16
+ * How two segments meet: not at all; at one point, s along the first and t
17
+ * along the second (0 at the start, 1 at the end); or along a stretch of the
18
+ * same line, from and to along the first.
19
+ */
20
+ export type Meeting = {
21
+ how: "apart";
22
+ } | {
23
+ how: "point";
24
+ at: P;
25
+ s: number;
26
+ t: number;
27
+ } | {
28
+ how: "along";
29
+ from: number;
30
+ to: number;
31
+ };
32
+ /** meet finds how the segments a–b and c–d meet; near is how far counts as touching. */
33
+ export declare function meet(a: P, b: P, c: P, d: P, near: number): Meeting;
34
+ /** inside tells whether p lies inside a polygon, by the even-odd rule; a point on an edge may go either way. */
35
+ export declare function inside(p: P, polygon: readonly P[]): boolean;
36
+ /** area is the area a polygon encloses, by the shoelace formula. */
37
+ export declare function area(polygon: readonly P[]): number;
38
+ /** centroid is the centre of mass of a polygon's area. */
39
+ export declare function centroid(polygon: readonly P[]): P;
40
+ export declare const edgesOf: (polygon: readonly P[]) => [P, P][];
41
+ /**
42
+ * crossing finds two sides of a polygon that cross or touch, other than
43
+ * neighbours at their common corner, as [i, j]; undefined when it is simple.
44
+ */
45
+ export declare function crossing(polygon: readonly P[], near: number): [number, number] | undefined;
46
+ /**
47
+ * overlap tells whether the insides of two simple polygons share some area.
48
+ * Every side of each is cut where it meets the other's sides; just off the
49
+ * middle of each piece, on both sides, a point inside both proves it. Where
50
+ * the insides meet, some such piece borders the common part, so none is missed.
51
+ */
52
+ export declare function overlap(a: readonly P[], b: readonly P[]): boolean;
53
+ /** covered tells whether the segment p–q lies wholly on the union of segments. */
54
+ export declare function covered(p: P, q: P, segments: readonly [P, P][], near: number): boolean;
55
+ /** decimal reads a number from least to most with at most `places` decimals. */
56
+ export declare function decimal(value: unknown, path: string, problems: Problems, least: number, most: number, places: number): number;
57
+ /** pointOf reads [x, y]: whole numbers when places is 0, else numbers with that many decimals. */
58
+ export declare function pointOf(value: unknown, path: string, problems: Problems, most: [number, number], places: number): P | undefined;
59
+ /** A box on the picture, from x0, y0 at the top left to x1, y1. */
60
+ export type Box = {
61
+ x0: number;
62
+ y0: number;
63
+ x1: number;
64
+ y1: number;
65
+ };
66
+ export declare const boxAt: (c: P, w: number, h: number) => Box;
67
+ /** gap is how far apart two boxes keep; less than 0 when they overlap. */
68
+ export declare function gap(a: Box, b: Box): number;
69
+ /** toBox is how far a point keeps from a box; 0 inside it. */
70
+ export declare function toBox(p: P, box: Box): number;
71
+ /** hits tells whether the segment a–b passes through a box. */
72
+ export declare function hits(a: P, b: P, box: Box): boolean;
73
+ /** boxToSegment is how far a box keeps from the segment a–b; 0 when they touch. */
74
+ export declare function boxToSegment(box: Box, a: P, b: P): number;
75
+ /** The size a label takes: a "?" stands in a dashed box a little larger than itself. */
76
+ export declare function labelSize(words: string, size: number): {
77
+ w: number;
78
+ h: number;
79
+ };
80
+ /** label writes a label centred on x, y; "?" stands in a dashed box, the place a question asks. */
81
+ export declare function label(pen: Pen, x: number, y: number, words: string, size: number, fill?: string): void;
82
+ /**
83
+ * around finds a place for a label of w × h beside an anchor: in sixteen
84
+ * directions, the preferred one first, at a few gaps from the anchor; the
85
+ * first place whose clearance — how far the box keeps from whatever is in
86
+ * the way — reaches want, else the clearest of all.
87
+ */
88
+ export declare function around(anchor: P, w: number, h: number, prefer: number, clearance: (box: Box) => number, want?: number, gaps?: readonly number[]): {
89
+ at: P;
90
+ clear: number;
91
+ };
92
+ /** widest is the direction, as an angle, through the middle of the widest gap between directions. */
93
+ export declare function widest(angles: readonly number[], otherwise: number): number;
94
+ /** say writes a number as a solver reads it: at most two decimals. */
95
+ export declare function say(value: number): string;
96
+ /** many says a count with its noun: "1 side", "3 sides". */
97
+ export declare function many(k: number, one: string, more?: string): string;
98
+ /** listOf joins words as English does: "a", "a and b", "a, b and c". */
99
+ export declare function listOf(items: readonly string[]): string;
100
+ /** runs says a sequence of words by runs: "2 gray, then 1 white, then 3 gray". */
101
+ export declare function runs(items: readonly string[]): string;
102
+ /** ordinal says 1 as "first", up to "eighth", then "9th". */
103
+ export declare function ordinal(k: number): string;
104
+ /** capital starts a sentence with a capital letter. */
105
+ export declare function capital(words: string): string;
@@ -0,0 +1,372 @@
1
+ import { textWidth } from "../pen.js";
2
+ import { int } from "../read.js";
3
+ import { ink } from "../style.js";
4
+ // The parts that the kinds of plane figures share — countfig, dotgrid, plane
5
+ // and fraction: geometry on points of the plane, reading a point of a scene,
6
+ // placing a label where it covers nothing, and the words that say a figure.
7
+ export const sub = (a, b) => ({ x: a.x - b.x, y: a.y - b.y });
8
+ export const dot = (a, b) => a.x * b.x + a.y * b.y;
9
+ export const crossOf = (a, b) => a.x * b.y - a.y * b.x;
10
+ export const distance = (a, b) => Math.hypot(a.x - b.x, a.y - b.y);
11
+ export const lerp = (a, b, t) => ({
12
+ x: a.x + (b.x - a.x) * t,
13
+ y: a.y + (b.y - a.y) * t,
14
+ });
15
+ /** along is where p falls on the line from a to b: 0 at a, 1 at b. */
16
+ export function along(p, a, b) {
17
+ const d = sub(b, a);
18
+ return dot(sub(p, a), d) / dot(d, d);
19
+ }
20
+ /** toSegment is the distance from p to the segment from a to b. */
21
+ export function toSegment(p, a, b) {
22
+ return distance(p, lerp(a, b, Math.max(0, Math.min(1, along(p, a, b)))));
23
+ }
24
+ /** toLine is the distance from p to the whole straight line through a and b. */
25
+ export function toLine(p, a, b) {
26
+ return Math.abs(crossOf(sub(b, a), sub(p, a))) / distance(a, b);
27
+ }
28
+ /** meet finds how the segments a–b and c–d meet; near is how far counts as touching. */
29
+ export function meet(a, b, c, d, near) {
30
+ const r = sub(b, a);
31
+ const q = sub(d, c);
32
+ const lr = Math.hypot(r.x, r.y);
33
+ const lq = Math.hypot(q.x, q.y);
34
+ const denom = crossOf(r, q);
35
+ if (Math.abs(denom) <= 1e-9 * lr * lq) {
36
+ if (toLine(c, a, b) > near)
37
+ return { how: "apart" };
38
+ const t0 = along(c, a, b);
39
+ const t1 = along(d, a, b);
40
+ const lo = Math.max(0, Math.min(t0, t1));
41
+ const hi = Math.min(1, Math.max(t0, t1));
42
+ if ((hi - lo) * lr > near)
43
+ return { how: "along", from: lo, to: hi };
44
+ if ((hi - lo) * lr < -near)
45
+ return { how: "apart" };
46
+ const s = Math.max(0, Math.min(1, (lo + hi) / 2));
47
+ const at = lerp(a, b, s);
48
+ return { how: "point", at, s, t: Math.max(0, Math.min(1, along(at, c, d))) };
49
+ }
50
+ const w = sub(c, a);
51
+ const s = crossOf(w, q) / denom;
52
+ const t = crossOf(w, r) / denom;
53
+ if (s * lr < -near || (s - 1) * lr > near || t * lq < -near || (t - 1) * lq > near)
54
+ return { how: "apart" };
55
+ const cs = Math.max(0, Math.min(1, s));
56
+ return { how: "point", at: lerp(a, b, cs), s: cs, t: Math.max(0, Math.min(1, t)) };
57
+ }
58
+ /** inside tells whether p lies inside a polygon, by the even-odd rule; a point on an edge may go either way. */
59
+ export function inside(p, polygon) {
60
+ let within = false;
61
+ for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
62
+ const a = polygon[i];
63
+ const b = polygon[j];
64
+ if (a.y > p.y !== b.y > p.y && p.x < ((b.x - a.x) * (p.y - a.y)) / (b.y - a.y) + a.x)
65
+ within = !within;
66
+ }
67
+ return within;
68
+ }
69
+ /** area is the area a polygon encloses, by the shoelace formula. */
70
+ export function area(polygon) {
71
+ let twice = 0;
72
+ polygon.forEach((p, i) => {
73
+ const q = polygon[(i + 1) % polygon.length];
74
+ twice += p.x * q.y - q.x * p.y;
75
+ });
76
+ return Math.abs(twice) / 2;
77
+ }
78
+ /** centroid is the centre of mass of a polygon's area. */
79
+ export function centroid(polygon) {
80
+ let a = 0;
81
+ let x = 0;
82
+ let y = 0;
83
+ polygon.forEach((p, i) => {
84
+ const q = polygon[(i + 1) % polygon.length];
85
+ const c = p.x * q.y - q.x * p.y;
86
+ a += c;
87
+ x += (p.x + q.x) * c;
88
+ y += (p.y + q.y) * c;
89
+ });
90
+ if (Math.abs(a) < 1e-12)
91
+ return {
92
+ x: polygon.reduce((s, p) => s + p.x, 0) / polygon.length,
93
+ y: polygon.reduce((s, p) => s + p.y, 0) / polygon.length,
94
+ };
95
+ return { x: x / (3 * a), y: y / (3 * a) };
96
+ }
97
+ export const edgesOf = (polygon) => polygon.map((p, i) => [p, polygon[(i + 1) % polygon.length]]);
98
+ /**
99
+ * crossing finds two sides of a polygon that cross or touch, other than
100
+ * neighbours at their common corner, as [i, j]; undefined when it is simple.
101
+ */
102
+ export function crossing(polygon, near) {
103
+ const edges = edgesOf(polygon);
104
+ const k = edges.length;
105
+ for (let i = 0; i < k; i++)
106
+ for (let j = i + 1; j < k; j++) {
107
+ const [a, b] = edges[i];
108
+ const [c, d] = edges[j];
109
+ const m = meet(a, b, c, d, near);
110
+ if (m.how === "apart")
111
+ continue;
112
+ const neighbours = j === i + 1 || (i === 0 && j === k - 1);
113
+ if (neighbours && m.how === "point")
114
+ continue;
115
+ return [i, j];
116
+ }
117
+ return undefined;
118
+ }
119
+ /**
120
+ * overlap tells whether the insides of two simple polygons share some area.
121
+ * Every side of each is cut where it meets the other's sides; just off the
122
+ * middle of each piece, on both sides, a point inside both proves it. Where
123
+ * the insides meet, some such piece borders the common part, so none is missed.
124
+ */
125
+ export function overlap(a, b) {
126
+ for (const [own, other] of [
127
+ [a, b],
128
+ [b, a],
129
+ ]) {
130
+ for (const [p, q] of edgesOf(own)) {
131
+ const cuts = [0, 1];
132
+ for (const [c, d] of edgesOf(other)) {
133
+ const m = meet(p, q, c, d, 1e-9);
134
+ if (m.how === "point")
135
+ cuts.push(m.s);
136
+ else if (m.how === "along")
137
+ cuts.push(m.from, m.to);
138
+ }
139
+ cuts.sort((x, y) => x - y);
140
+ const len = distance(p, q);
141
+ const normal = { x: -(q.y - p.y) / len, y: (q.x - p.x) / len };
142
+ for (let i = 0; i + 1 < cuts.length; i++) {
143
+ if (cuts[i + 1] - cuts[i] < 1e-9)
144
+ continue;
145
+ const mid = lerp(p, q, (cuts[i] + cuts[i + 1]) / 2);
146
+ for (const side of [1, -1]) {
147
+ const probe = { x: mid.x + normal.x * side * 1e-7, y: mid.y + normal.y * side * 1e-7 };
148
+ if (inside(probe, a) && inside(probe, b))
149
+ return true;
150
+ }
151
+ }
152
+ }
153
+ }
154
+ return false;
155
+ }
156
+ /** covered tells whether the segment p–q lies wholly on the union of segments. */
157
+ export function covered(p, q, segments, near) {
158
+ const len = distance(p, q);
159
+ const spans = [];
160
+ for (const [a, b] of segments) {
161
+ if (toLine(a, p, q) > near || toLine(b, p, q) > near)
162
+ continue;
163
+ const t0 = along(a, p, q);
164
+ const t1 = along(b, p, q);
165
+ spans.push([Math.min(t0, t1), Math.max(t0, t1)]);
166
+ }
167
+ spans.sort((x, y) => x[0] - y[0]);
168
+ let reach = 0;
169
+ for (const [lo, hi] of spans) {
170
+ if (lo * len > reach * len + near)
171
+ return false;
172
+ reach = Math.max(reach, hi);
173
+ if (reach * len >= len - near)
174
+ return true;
175
+ }
176
+ return reach * len >= len - near;
177
+ }
178
+ // --- reading -----------------------------------------------------------------
179
+ /** decimal reads a number from least to most with at most `places` decimals. */
180
+ export function decimal(value, path, problems, least, most, places) {
181
+ if (typeof value !== "number" || !Number.isFinite(value)) {
182
+ problems.push(`${path}: must be a number`);
183
+ return least;
184
+ }
185
+ if (value < least || value > most)
186
+ problems.push(`${path}: must be from ${least} to ${most}`);
187
+ else {
188
+ const scaled = value * 10 ** places;
189
+ if (Math.abs(scaled - Math.round(scaled)) > 1e-6)
190
+ problems.push(`${path}: at most ${places} decimal place${places === 1 ? "" : "s"}`);
191
+ }
192
+ return value;
193
+ }
194
+ /** pointOf reads [x, y]: whole numbers when places is 0, else numbers with that many decimals. */
195
+ export function pointOf(value, path, problems, most, places) {
196
+ if (!Array.isArray(value) || value.length !== 2) {
197
+ problems.push(`${path}: must be [x, y], two numbers`);
198
+ return undefined;
199
+ }
200
+ const before = problems.length;
201
+ const read = (v, at, top) => places === 0 ? int(v, at, problems, 0, top) : decimal(v, at, problems, 0, top, places);
202
+ const x = read(value[0], `${path}[0]`, most[0]);
203
+ const y = read(value[1], `${path}[1]`, most[1]);
204
+ return problems.length > before ? undefined : { x, y };
205
+ }
206
+ export const boxAt = (c, w, h) => ({
207
+ x0: c.x - w / 2,
208
+ y0: c.y - h / 2,
209
+ x1: c.x + w / 2,
210
+ y1: c.y + h / 2,
211
+ });
212
+ /** gap is how far apart two boxes keep; less than 0 when they overlap. */
213
+ export function gap(a, b) {
214
+ const dx = Math.max(a.x0 - b.x1, b.x0 - a.x1);
215
+ const dy = Math.max(a.y0 - b.y1, b.y0 - a.y1);
216
+ if (dx < 0 && dy < 0)
217
+ return Math.max(dx, dy);
218
+ return Math.hypot(Math.max(dx, 0), Math.max(dy, 0));
219
+ }
220
+ /** toBox is how far a point keeps from a box; 0 inside it. */
221
+ export function toBox(p, box) {
222
+ const dx = Math.max(box.x0 - p.x, 0, p.x - box.x1);
223
+ const dy = Math.max(box.y0 - p.y, 0, p.y - box.y1);
224
+ return Math.hypot(dx, dy);
225
+ }
226
+ /** hits tells whether the segment a–b passes through a box. */
227
+ export function hits(a, b, box) {
228
+ let t0 = 0;
229
+ let t1 = 1;
230
+ const dx = b.x - a.x;
231
+ const dy = b.y - a.y;
232
+ const clip = (p, q) => {
233
+ if (p === 0)
234
+ return q >= 0;
235
+ const r = q / p;
236
+ if (p < 0) {
237
+ if (r > t1)
238
+ return false;
239
+ if (r > t0)
240
+ t0 = r;
241
+ }
242
+ else {
243
+ if (r < t0)
244
+ return false;
245
+ if (r < t1)
246
+ t1 = r;
247
+ }
248
+ return true;
249
+ };
250
+ return (clip(-dx, a.x - box.x0) &&
251
+ clip(dx, box.x1 - a.x) &&
252
+ clip(-dy, a.y - box.y0) &&
253
+ clip(dy, box.y1 - a.y));
254
+ }
255
+ /** boxToSegment is how far a box keeps from the segment a–b; 0 when they touch. */
256
+ export function boxToSegment(box, a, b) {
257
+ if (hits(a, b, box))
258
+ return 0;
259
+ const corners = [
260
+ { x: box.x0, y: box.y0 },
261
+ { x: box.x1, y: box.y0 },
262
+ { x: box.x1, y: box.y1 },
263
+ { x: box.x0, y: box.y1 },
264
+ ];
265
+ return Math.min(...corners.map((c) => toSegment(c, a, b)), toBox(a, box), toBox(b, box));
266
+ }
267
+ /** The size a label takes: a "?" stands in a dashed box a little larger than itself. */
268
+ export function labelSize(words, size) {
269
+ if (words === "?")
270
+ return { w: size * 1.45, h: size * 1.45 };
271
+ return { w: textWidth(words, size) + 2, h: size * 0.78 };
272
+ }
273
+ /** label writes a label centred on x, y; "?" stands in a dashed box, the place a question asks. */
274
+ export function label(pen, x, y, words, size, fill = ink) {
275
+ if (words === "?") {
276
+ const s = size * 1.45;
277
+ pen.el("rect", {
278
+ x: x - s / 2,
279
+ y: y - s / 2,
280
+ width: s,
281
+ height: s,
282
+ rx: 6,
283
+ fill: "#ffffff",
284
+ stroke: ink,
285
+ "stroke-width": 2,
286
+ "stroke-dasharray": "4 3",
287
+ });
288
+ }
289
+ pen.text(x, y, words, { size, fill: words === "?" ? ink : fill });
290
+ }
291
+ /**
292
+ * around finds a place for a label of w × h beside an anchor: in sixteen
293
+ * directions, the preferred one first, at a few gaps from the anchor; the
294
+ * first place whose clearance — how far the box keeps from whatever is in
295
+ * the way — reaches want, else the clearest of all.
296
+ */
297
+ export function around(anchor, w, h, prefer, clearance, want = 6, gaps = [5, 9, 14]) {
298
+ let best = { at: anchor, clear: Number.NEGATIVE_INFINITY };
299
+ const turns = [0];
300
+ for (let k = 1; k <= 8; k++)
301
+ turns.push(k, -k);
302
+ for (const g of gaps)
303
+ for (const k of turns.slice(0, 16)) {
304
+ const angle = prefer + (k * Math.PI) / 8;
305
+ const ux = Math.cos(angle);
306
+ const uy = Math.sin(angle);
307
+ // Reach out to the side of the box that faces the anchor.
308
+ const reach = g +
309
+ Math.min(Math.abs(ux) > 1e-9 ? w / 2 / Math.abs(ux) : 1e9, Math.abs(uy) > 1e-9 ? h / 2 / Math.abs(uy) : 1e9);
310
+ const at = { x: anchor.x + ux * reach, y: anchor.y + uy * reach };
311
+ const clear = clearance(boxAt(at, w, h));
312
+ if (clear >= want)
313
+ return { at, clear };
314
+ if (clear > best.clear + 1e-9)
315
+ best = { at, clear };
316
+ }
317
+ return best;
318
+ }
319
+ /** widest is the direction, as an angle, through the middle of the widest gap between directions. */
320
+ export function widest(angles, otherwise) {
321
+ if (angles.length === 0)
322
+ return otherwise;
323
+ const sorted = [...angles].map((a) => ((a % (2 * Math.PI)) + 2 * Math.PI) % (2 * Math.PI));
324
+ sorted.sort((x, y) => x - y);
325
+ let best = otherwise;
326
+ let room = -1;
327
+ sorted.forEach((a, i) => {
328
+ const next = i + 1 < sorted.length ? sorted[i + 1] : sorted[0] + 2 * Math.PI;
329
+ if (next - a > room + 1e-9) {
330
+ room = next - a;
331
+ best = (a + next) / 2;
332
+ }
333
+ });
334
+ return best;
335
+ }
336
+ // --- words -------------------------------------------------------------------
337
+ /** say writes a number as a solver reads it: at most two decimals. */
338
+ export function say(value) {
339
+ const r = Math.round(value * 100) / 100;
340
+ return Object.is(r, -0) ? "0" : String(r);
341
+ }
342
+ /** many says a count with its noun: "1 side", "3 sides". */
343
+ export function many(k, one, more = `${one}s`) {
344
+ return `${k} ${k === 1 ? one : more}`;
345
+ }
346
+ /** listOf joins words as English does: "a", "a and b", "a, b and c". */
347
+ export function listOf(items) {
348
+ if (items.length <= 1)
349
+ return items[0] ?? "";
350
+ return `${items.slice(0, -1).join(", ")} and ${items[items.length - 1]}`;
351
+ }
352
+ /** runs says a sequence of words by runs: "2 gray, then 1 white, then 3 gray". */
353
+ export function runs(items) {
354
+ const out = [];
355
+ for (const word of items) {
356
+ const last = out[out.length - 1];
357
+ if (last && last.word === word)
358
+ last.k++;
359
+ else
360
+ out.push({ word, k: 1 });
361
+ }
362
+ return out.map((r) => `${r.k} ${r.word}`).join(", then ");
363
+ }
364
+ const ordinals = ["first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth"];
365
+ /** ordinal says 1 as "first", up to "eighth", then "9th". */
366
+ export function ordinal(k) {
367
+ return ordinals[k - 1] ?? `${k}th`;
368
+ }
369
+ /** capital starts a sentence with a capital letter. */
370
+ export function capital(words) {
371
+ return words.length > 0 ? `${words[0].toUpperCase()}${words.slice(1)}` : words;
372
+ }
@@ -0,0 +1,35 @@
1
+ import type { Kind } from "../kind.ts";
2
+ import { type ColorName } from "../style.ts";
3
+ /**
4
+ * Plane: plane figures with their marks — the lengths of sides, ticks that
5
+ * show equal sides, arcs and labels of angles, right-angle marks, letters at
6
+ * the corners and a text inside — one shape or several that touch along
7
+ * their sides, as a staircase or a rectangle cut into parts. A corner is
8
+ * [x, y], x to the right and y downward; sides[k] marks the side from
9
+ * corner k to the next one, angles[k] the angle at corner k. The picture
10
+ * need not be drawn to scale, but the check keeps it close: ticked sides
11
+ * are drawn equal, right-angle marks stand on right angles, a labelled angle
12
+ * is drawn near its size, and lengths that must add up do.
13
+ */
14
+ export type SideMark = {
15
+ label?: string;
16
+ ticks?: number;
17
+ };
18
+ export type AngleMark = {
19
+ right?: true;
20
+ arcs?: number;
21
+ label?: string;
22
+ };
23
+ export type PlaneShape = {
24
+ corners: [number, number][];
25
+ color?: ColorName;
26
+ letters?: string[];
27
+ sides?: (SideMark | null)[];
28
+ angles?: (AngleMark | null)[];
29
+ label?: string;
30
+ };
31
+ export type PlaneScene = {
32
+ kind: "plane";
33
+ shapes: PlaneShape[];
34
+ };
35
+ export declare const plane: Kind;