@motionscript/math 0.1.0-alpha.8 → 0.1.0-alpha.9

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 (157) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/LICENSE +201 -201
  3. package/NOTICE +49 -49
  4. package/README.md +390 -391
  5. package/dist/annotate/angle-mark.js.map +1 -1
  6. package/dist/annotate/curly-brace.js.map +1 -1
  7. package/dist/annotate/emphasis-mark.js.map +1 -1
  8. package/dist/browser/chunks/{chunk-FETQZEN2.js → chunk-IRZCG6PM.js} +2 -2
  9. package/dist/browser/chunks/chunk-IRZCG6PM.js.map +7 -0
  10. package/dist/browser/chunks/{chunk-K77KLQX4.js → chunk-WMVW6ZIV.js} +2 -2
  11. package/dist/browser/chunks/chunk-WMVW6ZIV.js.map +7 -0
  12. package/dist/browser/chunks/{mathjax-DNP3YRES.js → mathjax-WV55AC2P.js} +1 -1
  13. package/dist/browser/chunks/{mathjax-DNP3YRES.js.map → mathjax-WV55AC2P.js.map} +1 -1
  14. package/dist/browser/index.js +1 -1
  15. package/dist/browser/index.js.map +1 -1
  16. package/dist/browser/kit.js +1 -1
  17. package/dist/browser/tex.js +1 -1
  18. package/dist/browser/tex.js.map +1 -1
  19. package/dist/engine.d.ts +1 -1
  20. package/dist/engine.js +1 -1
  21. package/dist/engine.js.map +1 -1
  22. package/dist/index.js.map +1 -1
  23. package/dist/kit/brace.js.map +1 -1
  24. package/dist/kit/cache.js.map +1 -1
  25. package/dist/kit/component.d.ts.map +1 -1
  26. package/dist/kit/component.js.map +1 -1
  27. package/dist/kit/contour.js.map +1 -1
  28. package/dist/kit/coords.js.map +1 -1
  29. package/dist/kit/curve.js.map +1 -1
  30. package/dist/kit/equation.js.map +1 -1
  31. package/dist/kit/equation3d.js.map +1 -1
  32. package/dist/kit/expression.js.map +1 -1
  33. package/dist/kit/field-mark.d.ts.map +1 -1
  34. package/dist/kit/field-mark.js.map +1 -1
  35. package/dist/kit/field.js.map +1 -1
  36. package/dist/kit/follow.d.ts.map +1 -1
  37. package/dist/kit/follow.js.map +1 -1
  38. package/dist/kit/furniture.js.map +1 -1
  39. package/dist/kit/geometry.js.map +1 -1
  40. package/dist/kit/graph.js.map +1 -1
  41. package/dist/kit/mark.js.map +1 -1
  42. package/dist/kit/part-fills.js.map +1 -1
  43. package/dist/kit/place.js.map +1 -1
  44. package/dist/kit/sampler.js.map +1 -1
  45. package/dist/kit/scale.js.map +1 -1
  46. package/dist/kit/source.js.map +1 -1
  47. package/dist/kit/system.js.map +1 -1
  48. package/dist/kit/ticks.js.map +1 -1
  49. package/dist/kit/withdrawn.js.map +1 -1
  50. package/dist/marks/curve-area.js.map +1 -1
  51. package/dist/marks/drop-lines.js.map +1 -1
  52. package/dist/marks/plot-point.js.map +1 -1
  53. package/dist/marks/riemann-sum.js.map +1 -1
  54. package/dist/marks/tangent-line.js.map +1 -1
  55. package/dist/marks/vector-arrow.js.map +1 -1
  56. package/dist/nodes.js.map +1 -1
  57. package/dist/plots/function-plot.d.ts.map +1 -1
  58. package/dist/plots/function-plot.js.map +1 -1
  59. package/dist/plots/points-plot.js.map +1 -1
  60. package/dist/plots/stream-lines.js.map +1 -1
  61. package/dist/plots/traced-path.js.map +1 -1
  62. package/dist/plots/vector-field.js.map +1 -1
  63. package/dist/structures/graph-layout.js.map +1 -1
  64. package/dist/structures/network-graph.js.map +1 -1
  65. package/dist/structures/sample-space.js.map +1 -1
  66. package/dist/systems/axes2d.d.ts.map +1 -1
  67. package/dist/systems/axes2d.js.map +1 -1
  68. package/dist/systems/number-line.js.map +1 -1
  69. package/dist/systems/number-plane.js.map +1 -1
  70. package/dist/systems/polar-plane.js.map +1 -1
  71. package/dist/tex/geometry.js.map +1 -1
  72. package/dist/tex/index.js.map +1 -1
  73. package/dist/tex/mathjax.js.map +1 -1
  74. package/dist/tex/matrix.js.map +1 -1
  75. package/dist/tex/node.js.map +1 -1
  76. package/dist/tex/tween.js.map +1 -1
  77. package/dist/tex/typeset.d.ts.map +1 -1
  78. package/dist/tex/typeset.js.map +1 -1
  79. package/dist/three/axes3d.js.map +1 -1
  80. package/dist/three/curve-plot3d.js.map +1 -1
  81. package/dist/three/entries.js.map +1 -1
  82. package/dist/three/label3d.js.map +1 -1
  83. package/dist/three/plot-point3d.js.map +1 -1
  84. package/dist/three/space.js.map +1 -1
  85. package/dist/three/surface-plot3d.js.map +1 -1
  86. package/dist/three/vector-arrow3d.js.map +1 -1
  87. package/package.json +4 -4
  88. package/registry.json +251 -251
  89. package/src/annotate/angle-mark.ts +137 -137
  90. package/src/annotate/curly-brace.ts +148 -148
  91. package/src/annotate/emphasis-mark.ts +118 -118
  92. package/src/engine.ts +86 -86
  93. package/src/index.ts +29 -29
  94. package/src/kit/brace.ts +37 -37
  95. package/src/kit/cache.ts +61 -61
  96. package/src/kit/component.ts +57 -57
  97. package/src/kit/contour.ts +94 -94
  98. package/src/kit/coords.ts +278 -278
  99. package/src/kit/curve.ts +47 -47
  100. package/src/kit/equation.ts +181 -181
  101. package/src/kit/equation3d.ts +137 -137
  102. package/src/kit/expression.ts +159 -159
  103. package/src/kit/field-mark.ts +171 -171
  104. package/src/kit/field.ts +50 -50
  105. package/src/kit/follow.ts +229 -229
  106. package/src/kit/furniture.ts +113 -113
  107. package/src/kit/geometry.ts +49 -49
  108. package/src/kit/graph.ts +116 -116
  109. package/src/kit/mark.ts +152 -152
  110. package/src/kit/part-fills.ts +61 -61
  111. package/src/kit/place.ts +77 -77
  112. package/src/kit/sampler.ts +281 -281
  113. package/src/kit/scale.ts +39 -39
  114. package/src/kit/source.ts +57 -57
  115. package/src/kit/system.ts +105 -105
  116. package/src/kit/ticks.ts +85 -85
  117. package/src/kit/withdrawn.ts +8 -8
  118. package/src/marks/curve-area.ts +89 -89
  119. package/src/marks/drop-lines.ts +71 -71
  120. package/src/marks/plot-point.ts +102 -102
  121. package/src/marks/riemann-sum.ts +99 -99
  122. package/src/marks/tangent-line.ts +119 -119
  123. package/src/marks/vector-arrow.ts +98 -98
  124. package/src/nodes.ts +99 -99
  125. package/src/plots/function-plot.ts +220 -220
  126. package/src/plots/points-plot.ts +103 -103
  127. package/src/plots/stream-lines.ts +160 -160
  128. package/src/plots/traced-path.ts +70 -70
  129. package/src/plots/vector-field.ts +107 -107
  130. package/src/structures/graph-layout.ts +145 -145
  131. package/src/structures/network-graph.ts +291 -291
  132. package/src/structures/sample-space.ts +210 -210
  133. package/src/systems/axes2d.ts +226 -226
  134. package/src/systems/number-line.ts +130 -130
  135. package/src/systems/number-plane.ts +232 -232
  136. package/src/systems/polar-plane.ts +177 -177
  137. package/src/tex/geometry.ts +703 -703
  138. package/src/tex/index.ts +15 -15
  139. package/src/tex/mathjax.ts +82 -82
  140. package/src/tex/matrix.ts +97 -97
  141. package/src/tex/node.ts +74 -74
  142. package/src/tex/tween.ts +308 -308
  143. package/src/tex/typeset.ts +461 -461
  144. package/src/three/axes3d.ts +267 -267
  145. package/src/three/curve-plot3d.ts +146 -146
  146. package/src/three/entries.ts +42 -42
  147. package/src/three/label3d.ts +52 -52
  148. package/src/three/plot-point3d.ts +63 -63
  149. package/src/three/space.ts +92 -92
  150. package/src/three/surface-plot3d.ts +318 -318
  151. package/src/three/vector-arrow3d.ts +88 -88
  152. package/dist/browser/chunks/chunk-FETQZEN2.js.map +0 -7
  153. package/dist/browser/chunks/chunk-K77KLQX4.js.map +0 -7
  154. package/dist/kit/placement.d.ts +0 -8
  155. package/dist/kit/placement.d.ts.map +0 -1
  156. package/dist/kit/placement.js +0 -8
  157. package/dist/kit/placement.js.map +0 -1
@@ -1,210 +1,210 @@
1
- import {
2
- component2d,
3
- fillOps,
4
- Graphics2D,
5
- Types,
6
- type Color,
7
- type Fill,
8
- type FillResolved,
9
- type NodeOf,
10
- type NormalizedColor,
11
- type PaintProps,
12
- type RenderContext,
13
- type ShadowResolved,
14
- type Size2D,
15
- type StrokeResolved,
16
- type Values,
17
- type Vector2,
18
- // Named so the emitted declaration reaches these through the sdk, not core.
19
- type Node2D as _Node2D,
20
- type Node2DProps as _Node2DProps,
21
- type Shadow as _Shadow,
22
- type Stroke as _Stroke,
23
- } from "@motionscript/sdk";
24
- import { braceOutline } from "../kit/brace";
25
- import { omit, PAINT, PAINT_COMMANDS, painted } from "../kit/component";
26
- import { drawLabels, loadFormulas, type Label } from "../kit/furniture";
27
- import { lerpFillScale, readFillScale } from "../kit/scale";
28
- import { formatTick } from "../kit/ticks";
29
-
30
- export interface SampleSpaceProps extends PaintProps {
31
- columnShares?: number[];
32
- rowShares?: number[];
33
- columnFills?: Fill[];
34
- columnLabels?: string[];
35
- rowLabels?: string[];
36
- values?: boolean;
37
- labelSize?: number;
38
- labelColor?: Color;
39
- fontFamily?: string;
40
- }
41
-
42
- /** The parts' paints until told otherwise, a column each, round again past the last. */
43
- const PALETTE = ["#58a6ff", "#f59e0b", "#4ade80", "#f472b6", "#a78bfa", "#2dd4bf"];
44
-
45
- /** How strongly the lower part of a split column shows its paint. */
46
- const LOWER = 0.45;
47
-
48
- function readShares(value: readonly number[] | undefined): number[] {
49
- return (Array.isArray(value) ? value : []).map((share) => (Number.isFinite(share) && share > 0 ? share : 0));
50
- }
51
-
52
- // Shares tween one by one; a different count has nothing to pair with, and changes at the end.
53
- function lerpShares(from: number[], to: number[], t: number): number[] {
54
- if (from.length !== to.length) return t < 1 ? from : to;
55
- return to.map((share, i) => from[i] + (share - from[i]) * t);
56
- }
57
-
58
- const state = {
59
- // Its parts are painted from `columnFills`, and it trims no outline of its own.
60
- ...omit(PAINT, "fill", "start", "end"),
61
- /** How the whole is shared across, left to right; any numbers, read as shares of their sum. */
62
- columnShares: Types.custom<number[]>([1], { resolve: readShares, lerp: lerpShares, kind: "vector" }),
63
- /** Each column's upper share, 0 to 1; a column without one is not cut across. */
64
- rowShares: Types.custom<number[]>([], { resolve: readShares, lerp: lerpShares, kind: "vector" }),
65
- /** Each column's paint, left to right, round again past the last; its lower part shows it fainter. */
66
- columnFills: Types.custom<Fill[], FillResolved[][]>([], { resolve: readFillScale, lerp: lerpFillScale, kind: "vector" }),
67
- columnLabels: Types.custom<string[]>([], { animatable: false, kind: "vector" }),
68
- /** The upper part's name and the lower's, written on the left. */
69
- rowLabels: Types.custom<string[]>([], { animatable: false, kind: "vector" }),
70
- /** Write each part's probability in it. */
71
- values: Types.boolean(false, { animatable: false }),
72
- labelSize: Types.number(20, { min: 1, unit: "px" }),
73
- labelColor: Types.color("#e5e7eb"),
74
- fontFamily: Types.text("", { animatable: false }),
75
- };
76
-
77
- /** What a sample space is drawn from: its own entries, as its fields and its node both read them. */
78
- export type SampleSpaceValues = Values<typeof state>;
79
-
80
- /** One part of a sample space: its box in pixels from the node's centre, y up, and its probability. */
81
- export interface SamplePart {
82
- x: number;
83
- y: number;
84
- width: number;
85
- height: number;
86
- probability: number;
87
- column: number;
88
- upper: boolean;
89
- }
90
-
91
- /**
92
- * A probability's whole, as a box cut into columns by `columnShares` (shares of
93
- * the whole, P(A) and P(not A)) and each column cut again by its `rowShares` entry
94
- * (P(B | A) above, the rest below): each part's area is its probability.
95
- * Braces name the columns below and the rows on the left; `divideTo` moves the
96
- * cuts.
97
- */
98
- export const SampleSpace = component2d({
99
- key: "sampleSpace",
100
- state,
101
- defaults: { stroke: { weight: 2, fill: "#0b0d12", align: "center" } },
102
- load: (loader, s) => {
103
- if (s.fontFamily !== "") loader.font(s.fontFamily);
104
- loadFormulas(loader, [...s.columnLabels, ...s.rowLabels]);
105
- },
106
- commands: {
107
- ...omit(PAINT_COMMANDS, "fillTo", "progressTo", "flash"),
108
- /** Move the cuts: the columns' shares, each column's upper share, or both. */
109
- divideTo: { args: ["columnShares", "rowShares"] },
110
- },
111
- render: (ctx, s) => painted(ctx, () => drawSpace(ctx, s)),
112
- });
113
-
114
- export type SampleSpace = NodeOf<typeof SampleSpace>;
115
-
116
- /** Each part of a sample space of `size`: columns left to right, upper part first. */
117
- export function sampleParts(space: SampleSpaceValues, size: Size2D): SamplePart[] {
118
- const box = boxOf(space, size);
119
- const shares = space.columnShares;
120
- const total = shares.reduce((sum, share) => sum + share, 0);
121
- if (!(total > 0) || box.width <= 0 || box.height <= 0) return [];
122
- const out: SamplePart[] = [];
123
- let left = box.x - box.width / 2;
124
- shares.forEach((share, column) => {
125
- const width = (box.width * share) / total;
126
- const p = share / total;
127
- const cut = space.rowShares[column];
128
- const top = box.y + box.height / 2;
129
- if (cut === undefined) {
130
- out.push({ x: left + width / 2, y: box.y, width, height: box.height, probability: p, column, upper: true });
131
- } else {
132
- const upper = Math.min(1, Math.max(0, cut));
133
- const h = box.height * upper;
134
- out.push({ x: left + width / 2, y: top - h / 2, width, height: h, probability: p * upper, column, upper: true });
135
- out.push({ x: left + width / 2, y: top - h - (box.height - h) / 2, width, height: box.height - h, probability: p * (1 - upper), column, upper: false });
136
- }
137
- left += width;
138
- });
139
- return out;
140
- }
141
-
142
- function drawSpace(ctx: RenderContext, space: SampleSpaceValues): void {
143
- const parts = sampleParts(space, ctx.size);
144
- if (parts.length === 0) return;
145
- const fills = space.columnFills;
146
- const stroke = space.stroke as StrokeResolved[];
147
- const labels: Label[] = [];
148
- for (const part of parts) {
149
- if (!(part.width > 0) || !(part.height > 0)) continue;
150
- const base = fills.length > 0 ? fills[part.column % fills.length] : fillOps.resolve(PALETTE[part.column % PALETTE.length]);
151
- const paint = part.upper ? base : base.map((layer) => ({ ...layer, opacity: (layer.opacity ?? 1) * LOWER }));
152
- const g = new Graphics2D().rect({ x: part.x, y: part.y, width: part.width, height: part.height });
153
- ctx.draw(g.shadow(space.shadow as ShadowResolved[]).fill(paint));
154
- if (stroke.length > 0) ctx.draw(new Graphics2D().rect({ x: part.x, y: part.y, width: part.width, height: part.height }).stroke(stroke));
155
- if (space.values && part.height > space.labelSize && part.width > space.labelSize) {
156
- labels.push({ text: formatTick(Math.round(part.probability * 1000) / 1000, 0.001), at: { x: part.x, y: part.y }, align: "center" });
157
- }
158
- }
159
- labels.push(...braces(ctx, space, parts));
160
- const style = { fontSize: space.labelSize, fontFamily: space.fontFamily, color: space.labelColor as NormalizedColor };
161
- for (const part of drawLabels(labels, style)) ctx.draw(part);
162
- }
163
-
164
- /** Room for the braces and their names, below and on the left, taken from the box. */
165
- function boxOf(space: SampleSpaceValues, size: Size2D): { x: number; y: number; width: number; height: number } {
166
- const { width, height } = size;
167
- const room = space.labelSize * 2.6;
168
- const below = space.columnLabels.some((label) => label !== "") ? room : 0;
169
- const left = space.rowLabels.some((label) => label !== "") ? room * 1.6 : 0;
170
- return { x: left / 2, y: below / 2, width: Math.max(0, width - left), height: Math.max(0, height - below) };
171
- }
172
-
173
- /** Braces under each column and beside the first column's parts, and the labels that name them. */
174
- function braces(ctx: RenderContext, space: SampleSpaceValues, parts: readonly SamplePart[]): Label[] {
175
- const box = boxOf(space, ctx.size);
176
- const reach = space.labelSize * 0.8;
177
- const gap = 6;
178
- const runs = new Graphics2D();
179
- const labels: Label[] = [];
180
- let drawn = false;
181
- const brace = (from: Vector2, to: Vector2, out: Vector2, text: string, align: Label["align"]) => {
182
- if (text === "") return;
183
- // A frame centred on the node's own centre, so the path is drawn where it is and not moved onto it.
184
- runs.path({ data: braceOutline(from, to, out, reach), centerBounds: [-1, -1, 1, 1] });
185
- drawn = true;
186
- const mid = { x: (from.x + to.x) / 2, y: (from.y + to.y) / 2 };
187
- const far = reach + space.labelSize * 0.75;
188
- labels.push({ text, at: { x: mid.x + out.x * far, y: mid.y + out.y * far }, align });
189
- };
190
- const bottom = box.y - box.height / 2 - gap;
191
- for (const part of parts) {
192
- if (!part.upper && space.rowShares[part.column] !== undefined) continue;
193
- const text = space.columnLabels[part.column] ?? "";
194
- // Left to right along the bottom, so the bulge, to the edge's right, points down.
195
- brace({ x: part.x + part.width / 2, y: bottom }, { x: part.x - part.width / 2, y: bottom }, { x: 0, y: -1 }, text, "center");
196
- }
197
- const first = parts.filter((part) => part.column === 0);
198
- const left = box.x - box.width / 2 - gap;
199
- if (space.rowShares[0] !== undefined) {
200
- first.forEach((part) => {
201
- const text = space.rowLabels[part.upper ? 0 : 1] ?? "";
202
- brace({ x: left, y: part.y - part.height / 2 }, { x: left, y: part.y + part.height / 2 }, { x: -1, y: 0 }, text, "right");
203
- });
204
- }
205
- if (drawn) {
206
- const fill = fillOps.resolve({ type: "solid", color: space.labelColor as NormalizedColor });
207
- ctx.draw(runs.stroke({ weight: 2, fill, align: "center", cap: "round", join: "round" }));
208
- }
209
- return labels;
210
- }
1
+ import {
2
+ component2d,
3
+ fillOps,
4
+ Graphics2D,
5
+ Types,
6
+ type Color,
7
+ type Fill,
8
+ type FillResolved,
9
+ type NodeOf,
10
+ type NormalizedColor,
11
+ type PaintProps,
12
+ type RenderContext,
13
+ type ShadowResolved,
14
+ type Size2D,
15
+ type StrokeResolved,
16
+ type Values,
17
+ type Vector2,
18
+ // Named so the emitted declaration reaches these through the sdk, not core.
19
+ type Node2D as _Node2D,
20
+ type Node2DProps as _Node2DProps,
21
+ type Shadow as _Shadow,
22
+ type Stroke as _Stroke,
23
+ } from "@motionscript/sdk";
24
+ import { braceOutline } from "../kit/brace";
25
+ import { omit, PAINT, PAINT_COMMANDS, painted } from "../kit/component";
26
+ import { drawLabels, loadFormulas, type Label } from "../kit/furniture";
27
+ import { lerpFillScale, readFillScale } from "../kit/scale";
28
+ import { formatTick } from "../kit/ticks";
29
+
30
+ export interface SampleSpaceProps extends PaintProps {
31
+ columnShares?: number[];
32
+ rowShares?: number[];
33
+ columnFills?: Fill[];
34
+ columnLabels?: string[];
35
+ rowLabels?: string[];
36
+ values?: boolean;
37
+ labelSize?: number;
38
+ labelColor?: Color;
39
+ fontFamily?: string;
40
+ }
41
+
42
+ /** The parts' paints until told otherwise, a column each, round again past the last. */
43
+ const PALETTE = ["#58a6ff", "#f59e0b", "#4ade80", "#f472b6", "#a78bfa", "#2dd4bf"];
44
+
45
+ /** How strongly the lower part of a split column shows its paint. */
46
+ const LOWER = 0.45;
47
+
48
+ function readShares(value: readonly number[] | undefined): number[] {
49
+ return (Array.isArray(value) ? value : []).map((share) => (Number.isFinite(share) && share > 0 ? share : 0));
50
+ }
51
+
52
+ // Shares tween one by one; a different count has nothing to pair with, and changes at the end.
53
+ function lerpShares(from: number[], to: number[], t: number): number[] {
54
+ if (from.length !== to.length) return t < 1 ? from : to;
55
+ return to.map((share, i) => from[i] + (share - from[i]) * t);
56
+ }
57
+
58
+ const state = {
59
+ // Its parts are painted from `columnFills`, and it trims no outline of its own.
60
+ ...omit(PAINT, "fill", "start", "end"),
61
+ /** How the whole is shared across, left to right; any numbers, read as shares of their sum. */
62
+ columnShares: Types.custom<number[]>([1], { resolve: readShares, lerp: lerpShares, kind: "vector" }),
63
+ /** Each column's upper share, 0 to 1; a column without one is not cut across. */
64
+ rowShares: Types.custom<number[]>([], { resolve: readShares, lerp: lerpShares, kind: "vector" }),
65
+ /** Each column's paint, left to right, round again past the last; its lower part shows it fainter. */
66
+ columnFills: Types.custom<Fill[], FillResolved[][]>([], { resolve: readFillScale, lerp: lerpFillScale, kind: "vector" }),
67
+ columnLabels: Types.custom<string[]>([], { animatable: false, kind: "vector" }),
68
+ /** The upper part's name and the lower's, written on the left. */
69
+ rowLabels: Types.custom<string[]>([], { animatable: false, kind: "vector" }),
70
+ /** Write each part's probability in it. */
71
+ values: Types.boolean(false, { animatable: false }),
72
+ labelSize: Types.number(20, { min: 1, unit: "px" }),
73
+ labelColor: Types.color("#e5e7eb"),
74
+ fontFamily: Types.text("", { animatable: false }),
75
+ };
76
+
77
+ /** What a sample space is drawn from: its own entries, as its fields and its node both read them. */
78
+ export type SampleSpaceValues = Values<typeof state>;
79
+
80
+ /** One part of a sample space: its box in pixels from the node's centre, y up, and its probability. */
81
+ export interface SamplePart {
82
+ x: number;
83
+ y: number;
84
+ width: number;
85
+ height: number;
86
+ probability: number;
87
+ column: number;
88
+ upper: boolean;
89
+ }
90
+
91
+ /**
92
+ * A probability's whole, as a box cut into columns by `columnShares` (shares of
93
+ * the whole, P(A) and P(not A)) and each column cut again by its `rowShares` entry
94
+ * (P(B | A) above, the rest below): each part's area is its probability.
95
+ * Braces name the columns below and the rows on the left; `divideTo` moves the
96
+ * cuts.
97
+ */
98
+ export const SampleSpace = component2d({
99
+ key: "sampleSpace",
100
+ state,
101
+ defaults: { stroke: { weight: 2, fill: "#0b0d12", align: "center" } },
102
+ load: (loader, s) => {
103
+ if (s.fontFamily !== "") loader.font(s.fontFamily);
104
+ loadFormulas(loader, [...s.columnLabels, ...s.rowLabels]);
105
+ },
106
+ commands: {
107
+ ...omit(PAINT_COMMANDS, "fillTo", "progressTo", "flash"),
108
+ /** Move the cuts: the columns' shares, each column's upper share, or both. */
109
+ divideTo: { args: ["columnShares", "rowShares"] },
110
+ },
111
+ render: (ctx, s) => painted(ctx, () => drawSpace(ctx, s)),
112
+ });
113
+
114
+ export type SampleSpace = NodeOf<typeof SampleSpace>;
115
+
116
+ /** Each part of a sample space of `size`: columns left to right, upper part first. */
117
+ export function sampleParts(space: SampleSpaceValues, size: Size2D): SamplePart[] {
118
+ const box = boxOf(space, size);
119
+ const shares = space.columnShares;
120
+ const total = shares.reduce((sum, share) => sum + share, 0);
121
+ if (!(total > 0) || box.width <= 0 || box.height <= 0) return [];
122
+ const out: SamplePart[] = [];
123
+ let left = box.x - box.width / 2;
124
+ shares.forEach((share, column) => {
125
+ const width = (box.width * share) / total;
126
+ const p = share / total;
127
+ const cut = space.rowShares[column];
128
+ const top = box.y + box.height / 2;
129
+ if (cut === undefined) {
130
+ out.push({ x: left + width / 2, y: box.y, width, height: box.height, probability: p, column, upper: true });
131
+ } else {
132
+ const upper = Math.min(1, Math.max(0, cut));
133
+ const h = box.height * upper;
134
+ out.push({ x: left + width / 2, y: top - h / 2, width, height: h, probability: p * upper, column, upper: true });
135
+ out.push({ x: left + width / 2, y: top - h - (box.height - h) / 2, width, height: box.height - h, probability: p * (1 - upper), column, upper: false });
136
+ }
137
+ left += width;
138
+ });
139
+ return out;
140
+ }
141
+
142
+ function drawSpace(ctx: RenderContext, space: SampleSpaceValues): void {
143
+ const parts = sampleParts(space, ctx.size);
144
+ if (parts.length === 0) return;
145
+ const fills = space.columnFills;
146
+ const stroke = space.stroke as StrokeResolved[];
147
+ const labels: Label[] = [];
148
+ for (const part of parts) {
149
+ if (!(part.width > 0) || !(part.height > 0)) continue;
150
+ const base = fills.length > 0 ? fills[part.column % fills.length] : fillOps.resolve(PALETTE[part.column % PALETTE.length]);
151
+ const paint = part.upper ? base : base.map((layer) => ({ ...layer, opacity: (layer.opacity ?? 1) * LOWER }));
152
+ const g = new Graphics2D().rect({ x: part.x, y: part.y, width: part.width, height: part.height });
153
+ ctx.draw(g.shadow(space.shadow as ShadowResolved[]).fill(paint));
154
+ if (stroke.length > 0) ctx.draw(new Graphics2D().rect({ x: part.x, y: part.y, width: part.width, height: part.height }).stroke(stroke));
155
+ if (space.values && part.height > space.labelSize && part.width > space.labelSize) {
156
+ labels.push({ text: formatTick(Math.round(part.probability * 1000) / 1000, 0.001), at: { x: part.x, y: part.y }, align: "center" });
157
+ }
158
+ }
159
+ labels.push(...braces(ctx, space, parts));
160
+ const style = { fontSize: space.labelSize, fontFamily: space.fontFamily, color: space.labelColor as NormalizedColor };
161
+ for (const part of drawLabels(labels, style)) ctx.draw(part);
162
+ }
163
+
164
+ /** Room for the braces and their names, below and on the left, taken from the box. */
165
+ function boxOf(space: SampleSpaceValues, size: Size2D): { x: number; y: number; width: number; height: number } {
166
+ const { width, height } = size;
167
+ const room = space.labelSize * 2.6;
168
+ const below = space.columnLabels.some((label) => label !== "") ? room : 0;
169
+ const left = space.rowLabels.some((label) => label !== "") ? room * 1.6 : 0;
170
+ return { x: left / 2, y: below / 2, width: Math.max(0, width - left), height: Math.max(0, height - below) };
171
+ }
172
+
173
+ /** Braces under each column and beside the first column's parts, and the labels that name them. */
174
+ function braces(ctx: RenderContext, space: SampleSpaceValues, parts: readonly SamplePart[]): Label[] {
175
+ const box = boxOf(space, ctx.size);
176
+ const reach = space.labelSize * 0.8;
177
+ const gap = 6;
178
+ const runs = new Graphics2D();
179
+ const labels: Label[] = [];
180
+ let drawn = false;
181
+ const brace = (from: Vector2, to: Vector2, out: Vector2, text: string, align: Label["align"]) => {
182
+ if (text === "") return;
183
+ // A frame centred on the node's own centre, so the path is drawn where it is and not moved onto it.
184
+ runs.path({ data: braceOutline(from, to, out, reach), centerBounds: [-1, -1, 1, 1] });
185
+ drawn = true;
186
+ const mid = { x: (from.x + to.x) / 2, y: (from.y + to.y) / 2 };
187
+ const far = reach + space.labelSize * 0.75;
188
+ labels.push({ text, at: { x: mid.x + out.x * far, y: mid.y + out.y * far }, align });
189
+ };
190
+ const bottom = box.y - box.height / 2 - gap;
191
+ for (const part of parts) {
192
+ if (!part.upper && space.rowShares[part.column] !== undefined) continue;
193
+ const text = space.columnLabels[part.column] ?? "";
194
+ // Left to right along the bottom, so the bulge, to the edge's right, points down.
195
+ brace({ x: part.x + part.width / 2, y: bottom }, { x: part.x - part.width / 2, y: bottom }, { x: 0, y: -1 }, text, "center");
196
+ }
197
+ const first = parts.filter((part) => part.column === 0);
198
+ const left = box.x - box.width / 2 - gap;
199
+ if (space.rowShares[0] !== undefined) {
200
+ first.forEach((part) => {
201
+ const text = space.rowLabels[part.upper ? 0 : 1] ?? "";
202
+ brace({ x: left, y: part.y - part.height / 2 }, { x: left, y: part.y + part.height / 2 }, { x: -1, y: 0 }, text, "right");
203
+ });
204
+ }
205
+ if (drawn) {
206
+ const fill = fillOps.resolve({ type: "solid", color: space.labelColor as NormalizedColor });
207
+ ctx.draw(runs.stroke({ weight: 2, fill, align: "center", cap: "round", join: "round" }));
208
+ }
209
+ return labels;
210
+ }