@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.
- package/CHANGELOG.md +2 -0
- package/LICENSE +201 -201
- package/NOTICE +49 -49
- package/README.md +390 -391
- package/dist/annotate/angle-mark.js.map +1 -1
- package/dist/annotate/curly-brace.js.map +1 -1
- package/dist/annotate/emphasis-mark.js.map +1 -1
- package/dist/browser/chunks/{chunk-FETQZEN2.js → chunk-IRZCG6PM.js} +2 -2
- package/dist/browser/chunks/chunk-IRZCG6PM.js.map +7 -0
- package/dist/browser/chunks/{chunk-K77KLQX4.js → chunk-WMVW6ZIV.js} +2 -2
- package/dist/browser/chunks/chunk-WMVW6ZIV.js.map +7 -0
- package/dist/browser/chunks/{mathjax-DNP3YRES.js → mathjax-WV55AC2P.js} +1 -1
- package/dist/browser/chunks/{mathjax-DNP3YRES.js.map → mathjax-WV55AC2P.js.map} +1 -1
- package/dist/browser/index.js +1 -1
- package/dist/browser/index.js.map +1 -1
- package/dist/browser/kit.js +1 -1
- package/dist/browser/tex.js +1 -1
- package/dist/browser/tex.js.map +1 -1
- package/dist/engine.d.ts +1 -1
- package/dist/engine.js +1 -1
- package/dist/engine.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/kit/brace.js.map +1 -1
- package/dist/kit/cache.js.map +1 -1
- package/dist/kit/component.d.ts.map +1 -1
- package/dist/kit/component.js.map +1 -1
- package/dist/kit/contour.js.map +1 -1
- package/dist/kit/coords.js.map +1 -1
- package/dist/kit/curve.js.map +1 -1
- package/dist/kit/equation.js.map +1 -1
- package/dist/kit/equation3d.js.map +1 -1
- package/dist/kit/expression.js.map +1 -1
- package/dist/kit/field-mark.d.ts.map +1 -1
- package/dist/kit/field-mark.js.map +1 -1
- package/dist/kit/field.js.map +1 -1
- package/dist/kit/follow.d.ts.map +1 -1
- package/dist/kit/follow.js.map +1 -1
- package/dist/kit/furniture.js.map +1 -1
- package/dist/kit/geometry.js.map +1 -1
- package/dist/kit/graph.js.map +1 -1
- package/dist/kit/mark.js.map +1 -1
- package/dist/kit/part-fills.js.map +1 -1
- package/dist/kit/place.js.map +1 -1
- package/dist/kit/sampler.js.map +1 -1
- package/dist/kit/scale.js.map +1 -1
- package/dist/kit/source.js.map +1 -1
- package/dist/kit/system.js.map +1 -1
- package/dist/kit/ticks.js.map +1 -1
- package/dist/kit/withdrawn.js.map +1 -1
- package/dist/marks/curve-area.js.map +1 -1
- package/dist/marks/drop-lines.js.map +1 -1
- package/dist/marks/plot-point.js.map +1 -1
- package/dist/marks/riemann-sum.js.map +1 -1
- package/dist/marks/tangent-line.js.map +1 -1
- package/dist/marks/vector-arrow.js.map +1 -1
- package/dist/nodes.js.map +1 -1
- package/dist/plots/function-plot.d.ts.map +1 -1
- package/dist/plots/function-plot.js.map +1 -1
- package/dist/plots/points-plot.js.map +1 -1
- package/dist/plots/stream-lines.js.map +1 -1
- package/dist/plots/traced-path.js.map +1 -1
- package/dist/plots/vector-field.js.map +1 -1
- package/dist/structures/graph-layout.js.map +1 -1
- package/dist/structures/network-graph.js.map +1 -1
- package/dist/structures/sample-space.js.map +1 -1
- package/dist/systems/axes2d.d.ts.map +1 -1
- package/dist/systems/axes2d.js.map +1 -1
- package/dist/systems/number-line.js.map +1 -1
- package/dist/systems/number-plane.js.map +1 -1
- package/dist/systems/polar-plane.js.map +1 -1
- package/dist/tex/geometry.js.map +1 -1
- package/dist/tex/index.js.map +1 -1
- package/dist/tex/mathjax.js.map +1 -1
- package/dist/tex/matrix.js.map +1 -1
- package/dist/tex/node.js.map +1 -1
- package/dist/tex/tween.js.map +1 -1
- package/dist/tex/typeset.d.ts.map +1 -1
- package/dist/tex/typeset.js.map +1 -1
- package/dist/three/axes3d.js.map +1 -1
- package/dist/three/curve-plot3d.js.map +1 -1
- package/dist/three/entries.js.map +1 -1
- package/dist/three/label3d.js.map +1 -1
- package/dist/three/plot-point3d.js.map +1 -1
- package/dist/three/space.js.map +1 -1
- package/dist/three/surface-plot3d.js.map +1 -1
- package/dist/three/vector-arrow3d.js.map +1 -1
- package/package.json +4 -4
- package/registry.json +251 -251
- package/src/annotate/angle-mark.ts +137 -137
- package/src/annotate/curly-brace.ts +148 -148
- package/src/annotate/emphasis-mark.ts +118 -118
- package/src/engine.ts +86 -86
- package/src/index.ts +29 -29
- package/src/kit/brace.ts +37 -37
- package/src/kit/cache.ts +61 -61
- package/src/kit/component.ts +57 -57
- package/src/kit/contour.ts +94 -94
- package/src/kit/coords.ts +278 -278
- package/src/kit/curve.ts +47 -47
- package/src/kit/equation.ts +181 -181
- package/src/kit/equation3d.ts +137 -137
- package/src/kit/expression.ts +159 -159
- package/src/kit/field-mark.ts +171 -171
- package/src/kit/field.ts +50 -50
- package/src/kit/follow.ts +229 -229
- package/src/kit/furniture.ts +113 -113
- package/src/kit/geometry.ts +49 -49
- package/src/kit/graph.ts +116 -116
- package/src/kit/mark.ts +152 -152
- package/src/kit/part-fills.ts +61 -61
- package/src/kit/place.ts +77 -77
- package/src/kit/sampler.ts +281 -281
- package/src/kit/scale.ts +39 -39
- package/src/kit/source.ts +57 -57
- package/src/kit/system.ts +105 -105
- package/src/kit/ticks.ts +85 -85
- package/src/kit/withdrawn.ts +8 -8
- package/src/marks/curve-area.ts +89 -89
- package/src/marks/drop-lines.ts +71 -71
- package/src/marks/plot-point.ts +102 -102
- package/src/marks/riemann-sum.ts +99 -99
- package/src/marks/tangent-line.ts +119 -119
- package/src/marks/vector-arrow.ts +98 -98
- package/src/nodes.ts +99 -99
- package/src/plots/function-plot.ts +220 -220
- package/src/plots/points-plot.ts +103 -103
- package/src/plots/stream-lines.ts +160 -160
- package/src/plots/traced-path.ts +70 -70
- package/src/plots/vector-field.ts +107 -107
- package/src/structures/graph-layout.ts +145 -145
- package/src/structures/network-graph.ts +291 -291
- package/src/structures/sample-space.ts +210 -210
- package/src/systems/axes2d.ts +226 -226
- package/src/systems/number-line.ts +130 -130
- package/src/systems/number-plane.ts +232 -232
- package/src/systems/polar-plane.ts +177 -177
- package/src/tex/geometry.ts +703 -703
- package/src/tex/index.ts +15 -15
- package/src/tex/mathjax.ts +82 -82
- package/src/tex/matrix.ts +97 -97
- package/src/tex/node.ts +74 -74
- package/src/tex/tween.ts +308 -308
- package/src/tex/typeset.ts +461 -461
- package/src/three/axes3d.ts +267 -267
- package/src/three/curve-plot3d.ts +146 -146
- package/src/three/entries.ts +42 -42
- package/src/three/label3d.ts +52 -52
- package/src/three/plot-point3d.ts +63 -63
- package/src/three/space.ts +92 -92
- package/src/three/surface-plot3d.ts +318 -318
- package/src/three/vector-arrow3d.ts +88 -88
- package/dist/browser/chunks/chunk-FETQZEN2.js.map +0 -7
- package/dist/browser/chunks/chunk-K77KLQX4.js.map +0 -7
- package/dist/kit/placement.d.ts +0 -8
- package/dist/kit/placement.d.ts.map +0 -1
- package/dist/kit/placement.js +0 -8
- package/dist/kit/placement.js.map +0 -1
package/src/kit/coords.ts
CHANGED
|
@@ -1,278 +1,278 @@
|
|
|
1
|
-
import type { Extents, Vector2 } from "@motionscript/sdk";
|
|
2
|
-
|
|
3
|
-
/** A closed span of graph values, low to high. */
|
|
4
|
-
export type Interval = readonly [number, number];
|
|
5
|
-
|
|
6
|
-
export type AxisScaling = "linear" | "log";
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* How a coordinate system's graph values land on its pixels. Pixels are y-up
|
|
10
|
-
* from the centre of the system's own box, which is the frame a `Graphics2D` op
|
|
11
|
-
* is written in.
|
|
12
|
-
*/
|
|
13
|
-
export interface CoordinateMap {
|
|
14
|
-
/** Graph to pixels. Writes into `out` when given, so a sampler allocates nothing. */
|
|
15
|
-
toPoint(x: number, y: number, out?: Vector2): Vector2;
|
|
16
|
-
toCoords(x: number, y: number, out?: Vector2): Vector2;
|
|
17
|
-
/** The plot area. */
|
|
18
|
-
readonly area: Extents;
|
|
19
|
-
readonly xRange: Interval;
|
|
20
|
-
readonly yRange: Interval;
|
|
21
|
-
/** Equal for two maps that place every point alike: what a geometry cache keys on. */
|
|
22
|
-
readonly revision: string;
|
|
23
|
-
/** Set where a straight run of graph values is not a straight run of pixels, so a line through the map is sampled. */
|
|
24
|
-
readonly curved?: boolean;
|
|
25
|
-
/** Set where what is ruled over the ranges can leave the plot area, so the system keeps it in. */
|
|
26
|
-
readonly spills?: boolean;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/** One axis: a graph value to a pixel and back. */
|
|
30
|
-
export interface AxisScale {
|
|
31
|
-
to(value: number): number;
|
|
32
|
-
from(pixel: number): number;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/** `range`, made usable: finite, ordered, non-empty, and positive on a log axis. */
|
|
36
|
-
export function resolveInterval(range: Interval | undefined, scaling: AxisScaling = "linear"): Interval {
|
|
37
|
-
let [min, max] = range ?? [0, 1];
|
|
38
|
-
if (!Number.isFinite(min) || !Number.isFinite(max)) [min, max] = [0, 1];
|
|
39
|
-
if (min > max) [min, max] = [max, min];
|
|
40
|
-
if (scaling === "log") {
|
|
41
|
-
if (!(max > 0)) max = 10;
|
|
42
|
-
if (!(min > 0)) min = max / 1000;
|
|
43
|
-
}
|
|
44
|
-
return max > min ? [min, max] : [min, min + 1];
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/** `range` spread over the pixels from `lo` to `hi`. */
|
|
48
|
-
export function axisScale(range: Interval, lo: number, hi: number, scaling: AxisScaling = "linear"): AxisScale {
|
|
49
|
-
const [min, max] = range;
|
|
50
|
-
if (scaling === "log") {
|
|
51
|
-
const from = Math.log10(min);
|
|
52
|
-
const k = (hi - lo) / (Math.log10(max) - from);
|
|
53
|
-
return { to: (v) => lo + (Math.log10(v) - from) * k, from: (p) => 10 ** (from + (p - lo) / k) };
|
|
54
|
-
}
|
|
55
|
-
const k = (hi - lo) / (max - min);
|
|
56
|
-
return { to: (v) => lo + (v - min) * k, from: (p) => min + (p - lo) / k };
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export interface CartesianOptions {
|
|
60
|
-
xRange: Interval;
|
|
61
|
-
yRange: Interval;
|
|
62
|
-
area: Extents;
|
|
63
|
-
xScaling?: AxisScaling;
|
|
64
|
-
yScaling?: AxisScaling;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/** Two independent axes at right angles: what a number line, axes and a number plane share. */
|
|
68
|
-
export function cartesianMap({ xRange, yRange, area, xScaling = "linear", yScaling = "linear" }: CartesianOptions): CoordinateMap {
|
|
69
|
-
const x = axisScale(xRange, area.minX, area.maxX, xScaling);
|
|
70
|
-
const y = axisScale(yRange, area.minY, area.maxY, yScaling);
|
|
71
|
-
return {
|
|
72
|
-
toPoint: (gx, gy, out = { x: 0, y: 0 }) => {
|
|
73
|
-
out.x = x.to(gx);
|
|
74
|
-
out.y = y.to(gy);
|
|
75
|
-
return out;
|
|
76
|
-
},
|
|
77
|
-
toCoords: (px, py, out = { x: 0, y: 0 }) => {
|
|
78
|
-
out.x = x.from(px);
|
|
79
|
-
out.y = y.from(py);
|
|
80
|
-
return out;
|
|
81
|
-
},
|
|
82
|
-
area,
|
|
83
|
-
xRange,
|
|
84
|
-
yRange,
|
|
85
|
-
revision: [xRange, yRange, area.minX, area.minY, area.maxX, area.maxY, xScaling, yScaling].join("|"),
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const UNBOUNDED: Extents = { minX: -Infinity, minY: -Infinity, maxX: Infinity, maxY: Infinity };
|
|
90
|
-
|
|
91
|
-
/** One graph unit is one pixel: what a mark outside any system is drawn through. */
|
|
92
|
-
export const PIXEL_MAP: CoordinateMap = {
|
|
93
|
-
toPoint: (x, y, out = { x: 0, y: 0 }) => {
|
|
94
|
-
out.x = x;
|
|
95
|
-
out.y = y;
|
|
96
|
-
return out;
|
|
97
|
-
},
|
|
98
|
-
toCoords: (x, y, out = { x: 0, y: 0 }) => {
|
|
99
|
-
out.x = x;
|
|
100
|
-
out.y = y;
|
|
101
|
-
return out;
|
|
102
|
-
},
|
|
103
|
-
area: UNBOUNDED,
|
|
104
|
-
xRange: [-Infinity, Infinity],
|
|
105
|
-
yRange: [-Infinity, Infinity],
|
|
106
|
-
revision: "pixels",
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
/** A 2 × 2 matrix, row by row: `[a, b, c, d]` sends `(x, y)` to `(ax + by, cx + dy)`. */
|
|
110
|
-
export type Matrix2 = readonly [number, number, number, number];
|
|
111
|
-
|
|
112
|
-
export const IDENTITY_MATRIX: Matrix2 = [1, 0, 0, 1];
|
|
113
|
-
|
|
114
|
-
/** A matrix from its rows, or flat; anything else is the identity. */
|
|
115
|
-
export function readMatrix(value: unknown, previous: Matrix2 = IDENTITY_MATRIX): Matrix2 {
|
|
116
|
-
const flat = Array.isArray(value) ? value.flat() : [];
|
|
117
|
-
if (flat.length !== 4 || flat.some((entry) => typeof entry !== "number" || !Number.isFinite(entry))) return previous;
|
|
118
|
-
return [flat[0], flat[1], flat[2], flat[3]];
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
export function lerpMatrix(from: Matrix2, to: Matrix2, t: number): Matrix2 {
|
|
122
|
-
return [0, 1, 2, 3].map((i) => from[i] + (to[i] - from[i]) * t) as unknown as Matrix2;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
export function isIdentityMatrix([a, b, c, d]: Matrix2): boolean {
|
|
126
|
-
return a === 1 && b === 0 && c === 0 && d === 1;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* How far a transformed plane reaches, in half-widths of its own ranges about
|
|
131
|
-
* their middle. Finite on purpose: a plane squeezed towards a line shows the
|
|
132
|
-
* pre-image of its box growing as 1/det, and ruling all of it packs the box
|
|
133
|
-
* with lines, a solid wash just before the plane goes flat.
|
|
134
|
-
*/
|
|
135
|
-
const REACH = 4;
|
|
136
|
-
|
|
137
|
-
function reach([min, max]: Interval): Interval {
|
|
138
|
-
const middle = (min + max) / 2;
|
|
139
|
-
const half = ((max - min) / 2) * REACH;
|
|
140
|
-
return [middle - half, middle + half];
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* `base` with every graph point sent through `matrix` first: a plane sheared,
|
|
145
|
-
* turned or squeezed about its origin. Its ranges are what the plot area then
|
|
146
|
-
* shows, up to {@link REACH}, so a grid ruled over them fills the box under a
|
|
147
|
-
* turn, a shear or a fourfold shrink, and folds into a band as it goes flat.
|
|
148
|
-
*/
|
|
149
|
-
export function transformedMap(base: CoordinateMap, matrix: Matrix2): CoordinateMap {
|
|
150
|
-
const [a, b, c, d] = matrix;
|
|
151
|
-
const det = a * d - b * c;
|
|
152
|
-
// A plane squeezed onto a line has no way back: a point is read as the untransformed plane has it.
|
|
153
|
-
const flat = Math.abs(det) < 1e-9;
|
|
154
|
-
const back = (u: number, v: number, out: Vector2): Vector2 => {
|
|
155
|
-
if (flat) {
|
|
156
|
-
out.x = u;
|
|
157
|
-
out.y = v;
|
|
158
|
-
} else {
|
|
159
|
-
out.x = (d * u - b * v) / det;
|
|
160
|
-
out.y = (a * v - c * u) / det;
|
|
161
|
-
}
|
|
162
|
-
return out;
|
|
163
|
-
};
|
|
164
|
-
const xReach = reach(base.xRange);
|
|
165
|
-
const yReach = reach(base.yRange);
|
|
166
|
-
let xRange = xReach;
|
|
167
|
-
let yRange = yReach;
|
|
168
|
-
if (!flat) {
|
|
169
|
-
const corner: Vector2 = { x: 0, y: 0 };
|
|
170
|
-
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
171
|
-
for (const u of base.xRange) {
|
|
172
|
-
for (const v of base.yRange) {
|
|
173
|
-
back(u, v, corner);
|
|
174
|
-
minX = Math.min(minX, corner.x);
|
|
175
|
-
maxX = Math.max(maxX, corner.x);
|
|
176
|
-
minY = Math.min(minY, corner.y);
|
|
177
|
-
maxY = Math.max(maxY, corner.y);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
xRange = [Math.max(minX, xReach[0]), Math.min(maxX, xReach[1])];
|
|
181
|
-
yRange = [Math.max(minY, yReach[0]), Math.min(maxY, yReach[1])];
|
|
182
|
-
}
|
|
183
|
-
return {
|
|
184
|
-
toPoint: (x, y, out = { x: 0, y: 0 }) => base.toPoint(a * x + b * y, c * x + d * y, out),
|
|
185
|
-
toCoords: (px, py, out = { x: 0, y: 0 }) => {
|
|
186
|
-
base.toCoords(px, py, out);
|
|
187
|
-
return back(out.x, out.y, out);
|
|
188
|
-
},
|
|
189
|
-
area: base.area,
|
|
190
|
-
xRange,
|
|
191
|
-
yRange,
|
|
192
|
-
revision: `${base.revision}|m${matrix.join(",")}`,
|
|
193
|
-
spills: true,
|
|
194
|
-
};
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
/** Where a warp sends a graph point, written into `out`; `false` where it has no value. */
|
|
198
|
-
export type Warp = (x: number, y: number, out: Vector2) => boolean;
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* `base` with every graph point sent to where `warp` sends it: a plane bent by
|
|
202
|
-
* a function of its own points. `key` names the warp for the revision, since a
|
|
203
|
-
* function has no text of its own.
|
|
204
|
-
*/
|
|
205
|
-
export function warpedMap(base: CoordinateMap, warp: Warp, key: string): CoordinateMap {
|
|
206
|
-
const bend = (x: number, y: number, out: Vector2): Vector2 => {
|
|
207
|
-
// A point the warp has no value at stays where it is, so a pole does not tear the grid.
|
|
208
|
-
if (!warp(x, y, out)) {
|
|
209
|
-
out.x = x;
|
|
210
|
-
out.y = y;
|
|
211
|
-
}
|
|
212
|
-
return out;
|
|
213
|
-
};
|
|
214
|
-
const at: Vector2 = { x: 0, y: 0 };
|
|
215
|
-
const dx: Vector2 = { x: 0, y: 0 };
|
|
216
|
-
const dy: Vector2 = { x: 0, y: 0 };
|
|
217
|
-
const H = 1e-4;
|
|
218
|
-
return {
|
|
219
|
-
toPoint: (x, y, out = { x: 0, y: 0 }) => {
|
|
220
|
-
bend(x, y, out);
|
|
221
|
-
return base.toPoint(out.x, out.y, out);
|
|
222
|
-
},
|
|
223
|
-
// No closed form: a few Newton steps from where the unbent plane has the point.
|
|
224
|
-
toCoords: (px, py, out = { x: 0, y: 0 }) => {
|
|
225
|
-
const target = base.toCoords(px, py);
|
|
226
|
-
let gx = target.x;
|
|
227
|
-
let gy = target.y;
|
|
228
|
-
for (let i = 0; i < 8; i++) {
|
|
229
|
-
bend(gx, gy, at);
|
|
230
|
-
const ex = at.x - target.x;
|
|
231
|
-
const ey = at.y - target.y;
|
|
232
|
-
if (Math.abs(ex) + Math.abs(ey) < 1e-9) break;
|
|
233
|
-
bend(gx + H, gy, dx);
|
|
234
|
-
bend(gx, gy + H, dy);
|
|
235
|
-
const j00 = (dx.x - at.x) / H;
|
|
236
|
-
const j10 = (dx.y - at.y) / H;
|
|
237
|
-
const j01 = (dy.x - at.x) / H;
|
|
238
|
-
const j11 = (dy.y - at.y) / H;
|
|
239
|
-
const det = j00 * j11 - j01 * j10;
|
|
240
|
-
if (!(Math.abs(det) > 1e-12)) break;
|
|
241
|
-
gx -= (j11 * ex - j01 * ey) / det;
|
|
242
|
-
gy -= (j00 * ey - j10 * ex) / det;
|
|
243
|
-
}
|
|
244
|
-
out.x = Number.isFinite(gx) ? gx : target.x;
|
|
245
|
-
out.y = Number.isFinite(gy) ? gy : target.y;
|
|
246
|
-
return out;
|
|
247
|
-
},
|
|
248
|
-
area: base.area,
|
|
249
|
-
// A bent plane pulls its edges in somewhere: it is ruled a little past them so the box stays full.
|
|
250
|
-
xRange: widened(base.xRange),
|
|
251
|
-
yRange: widened(base.yRange),
|
|
252
|
-
revision: `${base.revision}|w${key}`,
|
|
253
|
-
curved: true,
|
|
254
|
-
spills: true,
|
|
255
|
-
};
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
/** How much further than its range, as a share of it, a warped plane is ruled each way. */
|
|
259
|
-
const WARP_MARGIN = 0.3;
|
|
260
|
-
|
|
261
|
-
function widened([min, max]: Interval): Interval {
|
|
262
|
-
const margin = (max - min) * WARP_MARGIN;
|
|
263
|
-
return Number.isFinite(margin) ? [min - margin, max + margin] : [min, max];
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
/** How many pieces a line through a curved map is drawn in. */
|
|
267
|
-
const CURVED_STEPS = 48;
|
|
268
|
-
|
|
269
|
-
/** The pixel points that draw the straight run of graph values from one point to another. */
|
|
270
|
-
export function lineThrough(map: CoordinateMap, x0: number, y0: number, x1: number, y1: number): Vector2[] {
|
|
271
|
-
if (!map.curved) return [map.toPoint(x0, y0), map.toPoint(x1, y1)];
|
|
272
|
-
const run: Vector2[] = [];
|
|
273
|
-
for (let i = 0; i <= CURVED_STEPS; i++) {
|
|
274
|
-
const t = i / CURVED_STEPS;
|
|
275
|
-
run.push(map.toPoint(x0 + (x1 - x0) * t, y0 + (y1 - y0) * t));
|
|
276
|
-
}
|
|
277
|
-
return run;
|
|
278
|
-
}
|
|
1
|
+
import type { Extents, Vector2 } from "@motionscript/sdk";
|
|
2
|
+
|
|
3
|
+
/** A closed span of graph values, low to high. */
|
|
4
|
+
export type Interval = readonly [number, number];
|
|
5
|
+
|
|
6
|
+
export type AxisScaling = "linear" | "log";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* How a coordinate system's graph values land on its pixels. Pixels are y-up
|
|
10
|
+
* from the centre of the system's own box, which is the frame a `Graphics2D` op
|
|
11
|
+
* is written in.
|
|
12
|
+
*/
|
|
13
|
+
export interface CoordinateMap {
|
|
14
|
+
/** Graph to pixels. Writes into `out` when given, so a sampler allocates nothing. */
|
|
15
|
+
toPoint(x: number, y: number, out?: Vector2): Vector2;
|
|
16
|
+
toCoords(x: number, y: number, out?: Vector2): Vector2;
|
|
17
|
+
/** The plot area. */
|
|
18
|
+
readonly area: Extents;
|
|
19
|
+
readonly xRange: Interval;
|
|
20
|
+
readonly yRange: Interval;
|
|
21
|
+
/** Equal for two maps that place every point alike: what a geometry cache keys on. */
|
|
22
|
+
readonly revision: string;
|
|
23
|
+
/** Set where a straight run of graph values is not a straight run of pixels, so a line through the map is sampled. */
|
|
24
|
+
readonly curved?: boolean;
|
|
25
|
+
/** Set where what is ruled over the ranges can leave the plot area, so the system keeps it in. */
|
|
26
|
+
readonly spills?: boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** One axis: a graph value to a pixel and back. */
|
|
30
|
+
export interface AxisScale {
|
|
31
|
+
to(value: number): number;
|
|
32
|
+
from(pixel: number): number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** `range`, made usable: finite, ordered, non-empty, and positive on a log axis. */
|
|
36
|
+
export function resolveInterval(range: Interval | undefined, scaling: AxisScaling = "linear"): Interval {
|
|
37
|
+
let [min, max] = range ?? [0, 1];
|
|
38
|
+
if (!Number.isFinite(min) || !Number.isFinite(max)) [min, max] = [0, 1];
|
|
39
|
+
if (min > max) [min, max] = [max, min];
|
|
40
|
+
if (scaling === "log") {
|
|
41
|
+
if (!(max > 0)) max = 10;
|
|
42
|
+
if (!(min > 0)) min = max / 1000;
|
|
43
|
+
}
|
|
44
|
+
return max > min ? [min, max] : [min, min + 1];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** `range` spread over the pixels from `lo` to `hi`. */
|
|
48
|
+
export function axisScale(range: Interval, lo: number, hi: number, scaling: AxisScaling = "linear"): AxisScale {
|
|
49
|
+
const [min, max] = range;
|
|
50
|
+
if (scaling === "log") {
|
|
51
|
+
const from = Math.log10(min);
|
|
52
|
+
const k = (hi - lo) / (Math.log10(max) - from);
|
|
53
|
+
return { to: (v) => lo + (Math.log10(v) - from) * k, from: (p) => 10 ** (from + (p - lo) / k) };
|
|
54
|
+
}
|
|
55
|
+
const k = (hi - lo) / (max - min);
|
|
56
|
+
return { to: (v) => lo + (v - min) * k, from: (p) => min + (p - lo) / k };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface CartesianOptions {
|
|
60
|
+
xRange: Interval;
|
|
61
|
+
yRange: Interval;
|
|
62
|
+
area: Extents;
|
|
63
|
+
xScaling?: AxisScaling;
|
|
64
|
+
yScaling?: AxisScaling;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Two independent axes at right angles: what a number line, axes and a number plane share. */
|
|
68
|
+
export function cartesianMap({ xRange, yRange, area, xScaling = "linear", yScaling = "linear" }: CartesianOptions): CoordinateMap {
|
|
69
|
+
const x = axisScale(xRange, area.minX, area.maxX, xScaling);
|
|
70
|
+
const y = axisScale(yRange, area.minY, area.maxY, yScaling);
|
|
71
|
+
return {
|
|
72
|
+
toPoint: (gx, gy, out = { x: 0, y: 0 }) => {
|
|
73
|
+
out.x = x.to(gx);
|
|
74
|
+
out.y = y.to(gy);
|
|
75
|
+
return out;
|
|
76
|
+
},
|
|
77
|
+
toCoords: (px, py, out = { x: 0, y: 0 }) => {
|
|
78
|
+
out.x = x.from(px);
|
|
79
|
+
out.y = y.from(py);
|
|
80
|
+
return out;
|
|
81
|
+
},
|
|
82
|
+
area,
|
|
83
|
+
xRange,
|
|
84
|
+
yRange,
|
|
85
|
+
revision: [xRange, yRange, area.minX, area.minY, area.maxX, area.maxY, xScaling, yScaling].join("|"),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const UNBOUNDED: Extents = { minX: -Infinity, minY: -Infinity, maxX: Infinity, maxY: Infinity };
|
|
90
|
+
|
|
91
|
+
/** One graph unit is one pixel: what a mark outside any system is drawn through. */
|
|
92
|
+
export const PIXEL_MAP: CoordinateMap = {
|
|
93
|
+
toPoint: (x, y, out = { x: 0, y: 0 }) => {
|
|
94
|
+
out.x = x;
|
|
95
|
+
out.y = y;
|
|
96
|
+
return out;
|
|
97
|
+
},
|
|
98
|
+
toCoords: (x, y, out = { x: 0, y: 0 }) => {
|
|
99
|
+
out.x = x;
|
|
100
|
+
out.y = y;
|
|
101
|
+
return out;
|
|
102
|
+
},
|
|
103
|
+
area: UNBOUNDED,
|
|
104
|
+
xRange: [-Infinity, Infinity],
|
|
105
|
+
yRange: [-Infinity, Infinity],
|
|
106
|
+
revision: "pixels",
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/** A 2 × 2 matrix, row by row: `[a, b, c, d]` sends `(x, y)` to `(ax + by, cx + dy)`. */
|
|
110
|
+
export type Matrix2 = readonly [number, number, number, number];
|
|
111
|
+
|
|
112
|
+
export const IDENTITY_MATRIX: Matrix2 = [1, 0, 0, 1];
|
|
113
|
+
|
|
114
|
+
/** A matrix from its rows, or flat; anything else is the identity. */
|
|
115
|
+
export function readMatrix(value: unknown, previous: Matrix2 = IDENTITY_MATRIX): Matrix2 {
|
|
116
|
+
const flat = Array.isArray(value) ? value.flat() : [];
|
|
117
|
+
if (flat.length !== 4 || flat.some((entry) => typeof entry !== "number" || !Number.isFinite(entry))) return previous;
|
|
118
|
+
return [flat[0], flat[1], flat[2], flat[3]];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function lerpMatrix(from: Matrix2, to: Matrix2, t: number): Matrix2 {
|
|
122
|
+
return [0, 1, 2, 3].map((i) => from[i] + (to[i] - from[i]) * t) as unknown as Matrix2;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function isIdentityMatrix([a, b, c, d]: Matrix2): boolean {
|
|
126
|
+
return a === 1 && b === 0 && c === 0 && d === 1;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* How far a transformed plane reaches, in half-widths of its own ranges about
|
|
131
|
+
* their middle. Finite on purpose: a plane squeezed towards a line shows the
|
|
132
|
+
* pre-image of its box growing as 1/det, and ruling all of it packs the box
|
|
133
|
+
* with lines, a solid wash just before the plane goes flat.
|
|
134
|
+
*/
|
|
135
|
+
const REACH = 4;
|
|
136
|
+
|
|
137
|
+
function reach([min, max]: Interval): Interval {
|
|
138
|
+
const middle = (min + max) / 2;
|
|
139
|
+
const half = ((max - min) / 2) * REACH;
|
|
140
|
+
return [middle - half, middle + half];
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* `base` with every graph point sent through `matrix` first: a plane sheared,
|
|
145
|
+
* turned or squeezed about its origin. Its ranges are what the plot area then
|
|
146
|
+
* shows, up to {@link REACH}, so a grid ruled over them fills the box under a
|
|
147
|
+
* turn, a shear or a fourfold shrink, and folds into a band as it goes flat.
|
|
148
|
+
*/
|
|
149
|
+
export function transformedMap(base: CoordinateMap, matrix: Matrix2): CoordinateMap {
|
|
150
|
+
const [a, b, c, d] = matrix;
|
|
151
|
+
const det = a * d - b * c;
|
|
152
|
+
// A plane squeezed onto a line has no way back: a point is read as the untransformed plane has it.
|
|
153
|
+
const flat = Math.abs(det) < 1e-9;
|
|
154
|
+
const back = (u: number, v: number, out: Vector2): Vector2 => {
|
|
155
|
+
if (flat) {
|
|
156
|
+
out.x = u;
|
|
157
|
+
out.y = v;
|
|
158
|
+
} else {
|
|
159
|
+
out.x = (d * u - b * v) / det;
|
|
160
|
+
out.y = (a * v - c * u) / det;
|
|
161
|
+
}
|
|
162
|
+
return out;
|
|
163
|
+
};
|
|
164
|
+
const xReach = reach(base.xRange);
|
|
165
|
+
const yReach = reach(base.yRange);
|
|
166
|
+
let xRange = xReach;
|
|
167
|
+
let yRange = yReach;
|
|
168
|
+
if (!flat) {
|
|
169
|
+
const corner: Vector2 = { x: 0, y: 0 };
|
|
170
|
+
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
171
|
+
for (const u of base.xRange) {
|
|
172
|
+
for (const v of base.yRange) {
|
|
173
|
+
back(u, v, corner);
|
|
174
|
+
minX = Math.min(minX, corner.x);
|
|
175
|
+
maxX = Math.max(maxX, corner.x);
|
|
176
|
+
minY = Math.min(minY, corner.y);
|
|
177
|
+
maxY = Math.max(maxY, corner.y);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
xRange = [Math.max(minX, xReach[0]), Math.min(maxX, xReach[1])];
|
|
181
|
+
yRange = [Math.max(minY, yReach[0]), Math.min(maxY, yReach[1])];
|
|
182
|
+
}
|
|
183
|
+
return {
|
|
184
|
+
toPoint: (x, y, out = { x: 0, y: 0 }) => base.toPoint(a * x + b * y, c * x + d * y, out),
|
|
185
|
+
toCoords: (px, py, out = { x: 0, y: 0 }) => {
|
|
186
|
+
base.toCoords(px, py, out);
|
|
187
|
+
return back(out.x, out.y, out);
|
|
188
|
+
},
|
|
189
|
+
area: base.area,
|
|
190
|
+
xRange,
|
|
191
|
+
yRange,
|
|
192
|
+
revision: `${base.revision}|m${matrix.join(",")}`,
|
|
193
|
+
spills: true,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** Where a warp sends a graph point, written into `out`; `false` where it has no value. */
|
|
198
|
+
export type Warp = (x: number, y: number, out: Vector2) => boolean;
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* `base` with every graph point sent to where `warp` sends it: a plane bent by
|
|
202
|
+
* a function of its own points. `key` names the warp for the revision, since a
|
|
203
|
+
* function has no text of its own.
|
|
204
|
+
*/
|
|
205
|
+
export function warpedMap(base: CoordinateMap, warp: Warp, key: string): CoordinateMap {
|
|
206
|
+
const bend = (x: number, y: number, out: Vector2): Vector2 => {
|
|
207
|
+
// A point the warp has no value at stays where it is, so a pole does not tear the grid.
|
|
208
|
+
if (!warp(x, y, out)) {
|
|
209
|
+
out.x = x;
|
|
210
|
+
out.y = y;
|
|
211
|
+
}
|
|
212
|
+
return out;
|
|
213
|
+
};
|
|
214
|
+
const at: Vector2 = { x: 0, y: 0 };
|
|
215
|
+
const dx: Vector2 = { x: 0, y: 0 };
|
|
216
|
+
const dy: Vector2 = { x: 0, y: 0 };
|
|
217
|
+
const H = 1e-4;
|
|
218
|
+
return {
|
|
219
|
+
toPoint: (x, y, out = { x: 0, y: 0 }) => {
|
|
220
|
+
bend(x, y, out);
|
|
221
|
+
return base.toPoint(out.x, out.y, out);
|
|
222
|
+
},
|
|
223
|
+
// No closed form: a few Newton steps from where the unbent plane has the point.
|
|
224
|
+
toCoords: (px, py, out = { x: 0, y: 0 }) => {
|
|
225
|
+
const target = base.toCoords(px, py);
|
|
226
|
+
let gx = target.x;
|
|
227
|
+
let gy = target.y;
|
|
228
|
+
for (let i = 0; i < 8; i++) {
|
|
229
|
+
bend(gx, gy, at);
|
|
230
|
+
const ex = at.x - target.x;
|
|
231
|
+
const ey = at.y - target.y;
|
|
232
|
+
if (Math.abs(ex) + Math.abs(ey) < 1e-9) break;
|
|
233
|
+
bend(gx + H, gy, dx);
|
|
234
|
+
bend(gx, gy + H, dy);
|
|
235
|
+
const j00 = (dx.x - at.x) / H;
|
|
236
|
+
const j10 = (dx.y - at.y) / H;
|
|
237
|
+
const j01 = (dy.x - at.x) / H;
|
|
238
|
+
const j11 = (dy.y - at.y) / H;
|
|
239
|
+
const det = j00 * j11 - j01 * j10;
|
|
240
|
+
if (!(Math.abs(det) > 1e-12)) break;
|
|
241
|
+
gx -= (j11 * ex - j01 * ey) / det;
|
|
242
|
+
gy -= (j00 * ey - j10 * ex) / det;
|
|
243
|
+
}
|
|
244
|
+
out.x = Number.isFinite(gx) ? gx : target.x;
|
|
245
|
+
out.y = Number.isFinite(gy) ? gy : target.y;
|
|
246
|
+
return out;
|
|
247
|
+
},
|
|
248
|
+
area: base.area,
|
|
249
|
+
// A bent plane pulls its edges in somewhere: it is ruled a little past them so the box stays full.
|
|
250
|
+
xRange: widened(base.xRange),
|
|
251
|
+
yRange: widened(base.yRange),
|
|
252
|
+
revision: `${base.revision}|w${key}`,
|
|
253
|
+
curved: true,
|
|
254
|
+
spills: true,
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/** How much further than its range, as a share of it, a warped plane is ruled each way. */
|
|
259
|
+
const WARP_MARGIN = 0.3;
|
|
260
|
+
|
|
261
|
+
function widened([min, max]: Interval): Interval {
|
|
262
|
+
const margin = (max - min) * WARP_MARGIN;
|
|
263
|
+
return Number.isFinite(margin) ? [min - margin, max + margin] : [min, max];
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** How many pieces a line through a curved map is drawn in. */
|
|
267
|
+
const CURVED_STEPS = 48;
|
|
268
|
+
|
|
269
|
+
/** The pixel points that draw the straight run of graph values from one point to another. */
|
|
270
|
+
export function lineThrough(map: CoordinateMap, x0: number, y0: number, x1: number, y1: number): Vector2[] {
|
|
271
|
+
if (!map.curved) return [map.toPoint(x0, y0), map.toPoint(x1, y1)];
|
|
272
|
+
const run: Vector2[] = [];
|
|
273
|
+
for (let i = 0; i <= CURVED_STEPS; i++) {
|
|
274
|
+
const t = i / CURVED_STEPS;
|
|
275
|
+
run.push(map.toPoint(x0 + (x1 - x0) * t, y0 + (y1 - y0) * t));
|
|
276
|
+
}
|
|
277
|
+
return run;
|
|
278
|
+
}
|
package/src/kit/curve.ts
CHANGED
|
@@ -1,47 +1,47 @@
|
|
|
1
|
-
import type { NodeView } from "@motionscript/sdk";
|
|
2
|
-
import { equationParams, parseEquation } from "./equation";
|
|
3
|
-
import type { ExpressionScope } from "./expression";
|
|
4
|
-
import { scopeFor, type PlotSource } from "./source";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* A curve as a mark reads it: what a plot is drawn from, its own `x`/`y`, and
|
|
8
|
-
* whether a system laid it out, which is when those are a shift in graph units.
|
|
9
|
-
* Data rather than a class, so a copied plot is ridden like the kit's own.
|
|
10
|
-
*/
|
|
11
|
-
export interface CurveValues {
|
|
12
|
-
readonly expression: PlotSource;
|
|
13
|
-
readonly params: ExpressionScope;
|
|
14
|
-
readonly x: number;
|
|
15
|
-
readonly y: number;
|
|
16
|
-
/** The plane the plot was laid out on; `null` outside any system. */
|
|
17
|
-
readonly graph?: object | null;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/** The node types a point, a tangent or an area can ride: they have a height at each `x`. */
|
|
21
|
-
export const CURVE_KEYS: readonly string[] = ["functionPlot", "tracedPath"];
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* The curve's height at graph `x`, or `NaN` where it has none or is not a
|
|
25
|
-
* function of `x`. What a point, a tangent or an area riding a plot reads.
|
|
26
|
-
*/
|
|
27
|
-
export function curveValue(curve: CurveValues, x: number): number {
|
|
28
|
-
const { source, blend } = curve.expression;
|
|
29
|
-
const shift = curve.graph ? curve : NO_SHIFT;
|
|
30
|
-
const value = heightOf(source, curve.params, x - shift.x) + shift.y;
|
|
31
|
-
if (!blend) return value;
|
|
32
|
-
const from = heightOf(blend.from, curve.params, x - shift.x) + shift.y;
|
|
33
|
-
return from + (value - from) * blend.t;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** Whether `view` is a node a mark can ride. */
|
|
37
|
-
export function isCurve(view: unknown): view is NodeView & { readonly state: CurveValues } {
|
|
38
|
-
return typeof view === "object" && view !== null && CURVE_KEYS.includes((view as { key?: string }).key ?? "");
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const NO_SHIFT = { x: 0, y: 0 };
|
|
42
|
-
|
|
43
|
-
function heightOf(source: string, params: ExpressionScope, x: number): number {
|
|
44
|
-
const equation = parseEquation(source, equationParams(source));
|
|
45
|
-
if (equation instanceof Error || equation.kind !== "function" || equation.of !== "x") return NaN;
|
|
46
|
-
return equation.f(x, 0, scopeFor(equationParams(source), params));
|
|
47
|
-
}
|
|
1
|
+
import type { NodeView } from "@motionscript/sdk";
|
|
2
|
+
import { equationParams, parseEquation } from "./equation";
|
|
3
|
+
import type { ExpressionScope } from "./expression";
|
|
4
|
+
import { scopeFor, type PlotSource } from "./source";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A curve as a mark reads it: what a plot is drawn from, its own `x`/`y`, and
|
|
8
|
+
* whether a system laid it out, which is when those are a shift in graph units.
|
|
9
|
+
* Data rather than a class, so a copied plot is ridden like the kit's own.
|
|
10
|
+
*/
|
|
11
|
+
export interface CurveValues {
|
|
12
|
+
readonly expression: PlotSource;
|
|
13
|
+
readonly params: ExpressionScope;
|
|
14
|
+
readonly x: number;
|
|
15
|
+
readonly y: number;
|
|
16
|
+
/** The plane the plot was laid out on; `null` outside any system. */
|
|
17
|
+
readonly graph?: object | null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** The node types a point, a tangent or an area can ride: they have a height at each `x`. */
|
|
21
|
+
export const CURVE_KEYS: readonly string[] = ["functionPlot", "tracedPath"];
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The curve's height at graph `x`, or `NaN` where it has none or is not a
|
|
25
|
+
* function of `x`. What a point, a tangent or an area riding a plot reads.
|
|
26
|
+
*/
|
|
27
|
+
export function curveValue(curve: CurveValues, x: number): number {
|
|
28
|
+
const { source, blend } = curve.expression;
|
|
29
|
+
const shift = curve.graph ? curve : NO_SHIFT;
|
|
30
|
+
const value = heightOf(source, curve.params, x - shift.x) + shift.y;
|
|
31
|
+
if (!blend) return value;
|
|
32
|
+
const from = heightOf(blend.from, curve.params, x - shift.x) + shift.y;
|
|
33
|
+
return from + (value - from) * blend.t;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Whether `view` is a node a mark can ride. */
|
|
37
|
+
export function isCurve(view: unknown): view is NodeView & { readonly state: CurveValues } {
|
|
38
|
+
return typeof view === "object" && view !== null && CURVE_KEYS.includes((view as { key?: string }).key ?? "");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const NO_SHIFT = { x: 0, y: 0 };
|
|
42
|
+
|
|
43
|
+
function heightOf(source: string, params: ExpressionScope, x: number): number {
|
|
44
|
+
const equation = parseEquation(source, equationParams(source));
|
|
45
|
+
if (equation instanceof Error || equation.kind !== "function" || equation.of !== "x") return NaN;
|
|
46
|
+
return equation.f(x, 0, scopeFor(equationParams(source), params));
|
|
47
|
+
}
|