@beff/aula 0.8.1 → 0.9.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 (65) hide show
  1. package/dist/generated/aula.core5.wasm +0 -0
  2. package/dist/index.d.ts +7304 -6
  3. package/dist/index.js +14864 -11
  4. package/dist/index.js.map +1 -0
  5. package/package.json +8 -25
  6. package/dist/animations.d.ts +0 -635
  7. package/dist/animations.js +0 -1778
  8. package/dist/braces.d.ts +0 -84
  9. package/dist/braces.js +0 -253
  10. package/dist/card.d.ts +0 -17
  11. package/dist/card.js +0 -90
  12. package/dist/colors-palette.d.ts +0 -2242
  13. package/dist/colors-palette.js +0 -2241
  14. package/dist/colors.d.ts +0 -120
  15. package/dist/colors.js +0 -403
  16. package/dist/curves.d.ts +0 -10
  17. package/dist/curves.js +0 -128
  18. package/dist/engine.d.ts +0 -1705
  19. package/dist/engine.js +0 -3561
  20. package/dist/errors.d.ts +0 -19
  21. package/dist/errors.js +0 -25
  22. package/dist/geometry.d.ts +0 -551
  23. package/dist/geometry.js +0 -1555
  24. package/dist/graph.d.ts +0 -117
  25. package/dist/graph.js +0 -526
  26. package/dist/graphing.d.ts +0 -597
  27. package/dist/graphing.js +0 -1504
  28. package/dist/host.d.ts +0 -19
  29. package/dist/host.js +0 -85
  30. package/dist/lexicon.d.ts +0 -10
  31. package/dist/lexicon.js +0 -23
  32. package/dist/load.d.ts +0 -21
  33. package/dist/load.js +0 -109
  34. package/dist/logo.d.ts +0 -30
  35. package/dist/logo.js +0 -186
  36. package/dist/manim.d.ts +0 -22
  37. package/dist/manim.js +0 -19
  38. package/dist/media.d.ts +0 -34
  39. package/dist/media.js +0 -391
  40. package/dist/mobjects.d.ts +0 -127
  41. package/dist/mobjects.js +0 -316
  42. package/dist/picture.d.ts +0 -23
  43. package/dist/picture.js +0 -291
  44. package/dist/pointcloud.d.ts +0 -144
  45. package/dist/pointcloud.js +0 -384
  46. package/dist/rates.d.ts +0 -19
  47. package/dist/rates.js +0 -54
  48. package/dist/run.d.ts +0 -101
  49. package/dist/run.js +0 -323
  50. package/dist/tables.d.ts +0 -179
  51. package/dist/tables.js +0 -403
  52. package/dist/tex.d.ts +0 -8
  53. package/dist/tex.js +0 -257
  54. package/dist/text-layouts.d.ts +0 -64
  55. package/dist/text-layouts.js +0 -225
  56. package/dist/text.d.ts +0 -61
  57. package/dist/text.js +0 -78
  58. package/dist/theme.d.ts +0 -25
  59. package/dist/theme.js +0 -12
  60. package/dist/user-code.d.ts +0 -5
  61. package/dist/user-code.js +0 -20
  62. package/dist/utils.d.ts +0 -152
  63. package/dist/utils.js +0 -517
  64. package/dist/vector-field.d.ts +0 -132
  65. package/dist/vector-field.js +0 -361
package/dist/utils.js DELETED
@@ -1,517 +0,0 @@
1
- // Manim CE's helper functions and constants, for points with two numbers: `bezier`, `paths`,
2
- // `simple_functions`, the 2D part of `space_ops`, and the constants of `constants.py` that
3
- // engine.ts does not have.
4
- import { PI, RIGHT, TAU } from "./engine.js";
5
- import { fault } from "./errors.js";
6
- // --- constants -------------------------------------------------------------------------
7
- /** The weights and slants of `Text` (Manim's Pango names). The house font has only `NORMAL`
8
- * and `BOLD`: `Text` takes only those two. */
9
- export const ITALIC = "ITALIC";
10
- export const OBLIQUE = "OBLIQUE";
11
- export const THIN = "THIN";
12
- export const ULTRALIGHT = "ULTRALIGHT";
13
- export const LIGHT = "LIGHT";
14
- export const SEMILIGHT = "SEMILIGHT";
15
- export const BOOK = "BOOK";
16
- export const MEDIUM = "MEDIUM";
17
- export const SEMIBOLD = "SEMIBOLD";
18
- export const ULTRABOLD = "ULTRABOLD";
19
- export const HEAVY = "HEAVY";
20
- export const ULTRAHEAVY = "ULTRAHEAVY";
21
- export const X_AXIS = [1, 0];
22
- export const Y_AXIS = [0, 1];
23
- /** Manim's `START_X` and `START_Y` (Manim CE does not use them either). */
24
- export const START_X = 30;
25
- export const START_Y = 20;
26
- export const DEFAULT_DOT_RADIUS = 0.08;
27
- export const DEFAULT_SMALL_DOT_RADIUS = 0.04;
28
- export const DEFAULT_DASH_LENGTH = 0.05;
29
- export const DEFAULT_ARROW_TIP_LENGTH = 0.35;
30
- // Numbers, not MED_LARGE_BUFF and MED_SMALL_BUFF: engine.ts imports this module, so its top
31
- // level reads nothing of engine.ts.
32
- export const DEFAULT_MOBJECT_TO_EDGE_BUFFER = 0.5;
33
- export const DEFAULT_MOBJECT_TO_MOBJECT_BUFFER = 0.25;
34
- export const DEFAULT_POINTWISE_FUNCTION_RUN_TIME = 3.0;
35
- export const DEFAULT_WAIT_TIME = 1.0;
36
- export const DEFAULT_POINT_DENSITY_2D = 25;
37
- export const DEFAULT_POINT_DENSITY_1D = 10;
38
- export const DEFAULT_STROKE_WIDTH = 4;
39
- export const DEFAULT_FONT_SIZE = 48;
40
- export const SCALE_FACTOR_PER_FONT_POINT = 1 / 960;
41
- /** The corner styles of a stroke (Manim `LineJointType`). */
42
- export const LineJointType = { AUTO: "AUTO", ROUND: "ROUND", BEVEL: "BEVEL", MITER: "MITER" };
43
- /** The end styles of a stroke (Manim `CapStyleType`). */
44
- export const CapStyleType = { AUTO: "AUTO", ROUND: "ROUND", BUTT: "BUTT", SQUARE: "SQUARE" };
45
- // --- simple functions ------------------------------------------------------------------
46
- /** The `x` from `lowerBound` to `upperBound` where `f(x)` is `target`, to `tolerance`, for a
47
- * monotonic `f`; undefined when the target is not between `f` of the bounds (Manim `None`). */
48
- export function binarySearch(f, target, lowerBound, upperBound, tolerance = 1e-4) {
49
- let lh = lowerBound;
50
- let rh = upperBound;
51
- let mh = (lh + rh) / 2;
52
- while (Math.abs(rh - lh) > tolerance) {
53
- mh = (lh + rh) / 2;
54
- const lx = f(lh);
55
- const mx = f(mh);
56
- const rx = f(rh);
57
- if (lx === target)
58
- return lh;
59
- if (rx === target)
60
- return rh;
61
- if (lx <= target && target <= rx) {
62
- if (mx > target)
63
- rh = mh;
64
- else
65
- lh = mh;
66
- }
67
- else if (lx > target && target > rx) {
68
- [lh, rh] = [rh, lh];
69
- }
70
- else
71
- return undefined;
72
- }
73
- return mh;
74
- }
75
- /** The binomial coefficient: the number of ways to choose `k` of `n`. */
76
- export function choose(n, k) {
77
- if (k < 0 || k > n)
78
- return 0;
79
- let r = 1;
80
- for (let i = 1; i <= Math.min(k, n - k); i++)
81
- r = (r * (n - i + 1)) / i;
82
- return Math.round(r);
83
- }
84
- /** `a`, limited to `minA` … `maxA`. */
85
- export function clip(a, minA, maxA) {
86
- return a < minA ? minA : a > maxA ? maxA : a;
87
- }
88
- /** The logistic function `1 / (1 + e^-x)`. */
89
- export function sigmoid(x) {
90
- return 1 / (1 + Math.exp(-x));
91
- }
92
- // --- bezier ----------------------------------------------------------------------------
93
- function lerpPoint(a, b, t) {
94
- return [a[0] + t * (b[0] - a[0]), a[1] + t * (b[1] - a[1])];
95
- }
96
- /** The Bézier curve of the control points, as a function of `t` (Manim `bezier`). */
97
- export function bezier(points) {
98
- const p = [...points];
99
- return (t) => {
100
- const b = [...p];
101
- for (let i = b.length - 1; i > 0; i--) {
102
- for (let j = 0; j < i; j++)
103
- b[j] = lerpPoint(b[j] ?? [0, 0], b[j + 1] ?? [0, 0], t);
104
- }
105
- return b[0] ?? fault("bezier: give one or more control points");
106
- };
107
- }
108
- /** The control points of the part of the curve from `a` to `b` (Manim `partial_bezier_points`). */
109
- export function partialBezierPoints(points, a, b) {
110
- const last = points.at(-1);
111
- const first = points[0];
112
- if (a === 1 && last !== undefined)
113
- return points.map(() => last);
114
- if (b === 0 && first !== undefined)
115
- return points.map(() => first);
116
- const arr = [...points];
117
- const n = arr.length;
118
- if (a !== 0) {
119
- for (let i = 1; i < n; i++) {
120
- for (let j = 0; j < n - i; j++)
121
- arr[j] = lerpPoint(arr[j] ?? [0, 0], arr[j + 1] ?? [0, 0], a);
122
- }
123
- }
124
- if (b !== 1) {
125
- const mu = (1 - b) / (1 - a);
126
- for (let i = 1; i < n; i++) {
127
- for (let j = n - 1; j >= i; j--)
128
- arr[j] = lerpPoint(arr[j] ?? [0, 0], arr[j - 1] ?? [0, 0], mu);
129
- }
130
- }
131
- return arr;
132
- }
133
- /** The control points of the two halves at `t`, one after the other (Manim `split_bezier`). */
134
- export function splitBezier(points, t) {
135
- const n = points.length;
136
- const right = [...points];
137
- const left = [points[0] ?? fault("splitBezier: give one or more control points")];
138
- for (let i = 1; i < n; i++) {
139
- for (let j = 0; j < n - i; j++)
140
- right[j] = lerpPoint(right[j] ?? [0, 0], right[j + 1] ?? [0, 0], t);
141
- left.push(right[0] ?? [0, 0]);
142
- }
143
- return [...left, ...right];
144
- }
145
- /** The control points of `nDivisions` equal parts of the curve, one after the other (Manim
146
- * `subdivide_bezier`). */
147
- export function subdivideBezier(points, nDivisions) {
148
- if (nDivisions === 1)
149
- return [...points];
150
- const out = [];
151
- let rest = [...points];
152
- for (let k = nDivisions; k > 1; k--) {
153
- const halves = splitBezier(rest, 1 / k);
154
- out.push(halves.slice(0, points.length));
155
- rest = halves.slice(points.length);
156
- }
157
- out.push(rest);
158
- return out.flat();
159
- }
160
- /** Splits the curves into `newNumberOfCurves` curves, the earlier curves first (Manim
161
- * `bezier_remap`). */
162
- export function bezierRemap(bezierTuples, newNumberOfCurves) {
163
- const current = bezierTuples.length;
164
- const factors = Array.from({ length: current }, () => 0);
165
- for (let i = 0; i < newNumberOfCurves; i++) {
166
- const at = Math.floor((i * current) / newNumberOfCurves);
167
- factors[at] = (factors[at] ?? 0) + 1;
168
- }
169
- return bezierTuples.flatMap((curve, i) => {
170
- const sf = factors[i] ?? 0;
171
- if (sf === 0)
172
- return [];
173
- const pieces = subdivideBezier(curve, sf);
174
- return Array.from({ length: sf }, (_, k) => pieces.slice(k * curve.length, (k + 1) * curve.length));
175
- });
176
- }
177
- export function interpolate(start, end, alpha) {
178
- if (typeof start === "number" && typeof end === "number")
179
- return (1 - alpha) * start + alpha * end;
180
- if (typeof start !== "number" && typeof end !== "number")
181
- return lerpPoint(start, end, alpha);
182
- return fault("interpolate: give two numbers or two points");
183
- }
184
- /** The integer part and the fraction of `interpolate(start, end, alpha)`; at the end it is
185
- * `[end - 1, 1]`. */
186
- export function integerInterpolate(start, end, alpha) {
187
- if (alpha >= 1)
188
- return [Math.trunc(end - 1), 1];
189
- if (alpha <= 0)
190
- return [Math.trunc(start), 0];
191
- const value = Math.trunc(interpolate(start, end, alpha));
192
- const residue = ((((end - start) * alpha) % 1) + 1) % 1;
193
- return [value, residue];
194
- }
195
- export function mid(start, end) {
196
- if (typeof start === "number" && typeof end === "number")
197
- return (start + end) / 2;
198
- if (typeof start !== "number" && typeof end !== "number")
199
- return lerpPoint(start, end, 0.5);
200
- return fault("mid: give two numbers or two points");
201
- }
202
- export function inverseInterpolate(start, end, value) {
203
- if (typeof start === "number" && typeof end === "number" && typeof value === "number") {
204
- return (value - start) / (end - start);
205
- }
206
- if (typeof start !== "number" && typeof end !== "number" && typeof value !== "number") {
207
- return [(value[0] - start[0]) / (end[0] - start[0]), (value[1] - start[1]) / (end[1] - start[1])];
208
- }
209
- return fault("inverseInterpolate: give three numbers or three points");
210
- }
211
- /** Maps `oldValue` from the range `oldStart` … `oldEnd` to the range `newStart` … `newEnd`. */
212
- export function matchInterpolate(newStart, newEnd, oldStart, oldEnd, oldValue) {
213
- return interpolate(newStart, newEnd, inverseInterpolate(oldStart, oldEnd, oldValue));
214
- }
215
- /** True when the last point is the first point, within Manim's tolerance. */
216
- export function isClosed(points) {
217
- const start = points[0];
218
- const end = points.at(-1);
219
- if (start === undefined || end === undefined)
220
- return false;
221
- return [0, 1].every((i) => Math.abs((end[i] ?? 0) - (start[i] ?? 0)) <= 1e-8 + 1e-5 * Math.abs(start[i] ?? 0));
222
- }
223
- function add(p, q) {
224
- return [p[0] + q[0], p[1] + q[1]];
225
- }
226
- function scale(p, k) {
227
- return [p[0] * k, p[1] * k];
228
- }
229
- /** The handles of a smooth cubic spline through the anchors: `[firstHandles, secondHandles]`,
230
- * one of each for each curve (Manim `get_smooth_cubic_bezier_handle_points`). A closed list of
231
- * anchors gives a closed spline. */
232
- export function getSmoothCubicBezierHandlePoints(anchors) {
233
- const n = anchors.length - 1;
234
- const first = anchors[0];
235
- const second = anchors[1];
236
- if (first === undefined || n === 0)
237
- return [[], []];
238
- if (second === undefined || n === 1) {
239
- const b = second ?? first;
240
- return [[lerpPoint(first, b, 1 / 3)], [lerpPoint(first, b, 2 / 3)]];
241
- }
242
- const at = (i) => anchors[i] ?? first;
243
- const cp = [0.5];
244
- if (isClosed(anchors)) {
245
- cp[0] = 1 / 3;
246
- const up = [1 / 3];
247
- for (let i = 1; i < n - 1; i++) {
248
- cp[i] = 1 / (4 - (cp[i - 1] ?? 0));
249
- up[i] = -(cp[i] ?? 0) * (up[i - 1] ?? 0);
250
- }
251
- const cpLast = 1 / (3 - (cp[n - 2] ?? 0));
252
- const upLast = cpLast * (1 - (up[n - 2] ?? 0));
253
- const q = [];
254
- q[n - 1] = upLast;
255
- for (let i = n - 2; i >= 0; i--)
256
- q[i] = (up[i] ?? 0) - (cp[i] ?? 0) * (q[i + 1] ?? 0);
257
- const aux = Array.from({ length: n }, (_, i) => add(scale(at(i), 4), scale(at(i + 1), 2)));
258
- const dp = [scale(aux[0] ?? first, 1 / 3)];
259
- for (let i = 1; i < n - 1; i++)
260
- dp[i] = scale(add(aux[i] ?? first, scale(dp[i - 1] ?? first, -1)), cp[i] ?? 0);
261
- dp[n - 1] = scale(add(aux[n - 1] ?? first, scale(dp[n - 2] ?? first, -1)), cpLast);
262
- const y = [...dp];
263
- for (let i = n - 2; i >= 0; i--)
264
- y[i] = add(dp[i] ?? first, scale(y[i + 1] ?? first, -(cp[i] ?? 0)));
265
- const k = 1 / (1 + (q[0] ?? 0) + (q[n - 1] ?? 0));
266
- const ends = add(y[0] ?? first, y[n - 1] ?? first);
267
- const h1 = y.map((yi, i) => add(yi, scale(ends, -k * (q[i] ?? 0))));
268
- const h2 = h1.map((_, i) => add(scale(at(i + 1), 2), scale(h1[i + 1 === n ? 0 : i + 1] ?? first, -1)));
269
- return [h1, h2];
270
- }
271
- for (let i = 1; i < n - 1; i++)
272
- cp[i] = 1 / (4 - (cp[i - 1] ?? 0));
273
- const dp = [add(scale(at(0), 0.5), at(1))];
274
- for (let i = 1; i < n - 1; i++) {
275
- const aux = add(scale(at(i), 4), scale(at(i + 1), 2));
276
- dp[i] = scale(add(aux, scale(dp[i - 1] ?? first, -1)), cp[i] ?? 0);
277
- }
278
- dp[n - 1] = scale(add(add(scale(at(n - 1), 8), at(n)), scale(dp[n - 2] ?? first, -2)), 1 / (7 - 2 * (cp[n - 2] ?? 0)));
279
- const h1 = [...dp];
280
- for (let i = n - 2; i >= 0; i--)
281
- h1[i] = add(dp[i] ?? first, scale(h1[i + 1] ?? first, -(cp[i] ?? 0)));
282
- const h2 = h1.map((_, i) => i === n - 1 ? scale(add(at(n), h1[n - 1] ?? first), 0.5) : add(scale(at(i + 1), 2), scale(h1[i + 1] ?? first, -1)));
283
- return [h1, h2];
284
- }
285
- /** The real roots of the polynomial with coefficients `c` (lowest power first), by the
286
- * Durand-Kerner method. */
287
- function realRoots(c) {
288
- let coeffs = [...c];
289
- while (coeffs.length > 1 && coeffs.at(-1) === 0)
290
- coeffs = coeffs.slice(0, -1);
291
- const degree = coeffs.length - 1;
292
- if (degree < 1)
293
- return [];
294
- const lead = coeffs[degree] ?? 1;
295
- const monic = coeffs.map((x) => x / lead);
296
- const mulZ = (a, b) => [a[0] * b[0] - a[1] * b[1], a[0] * b[1] + a[1] * b[0]];
297
- const divZ = (a, b) => {
298
- const d = b[0] * b[0] + b[1] * b[1];
299
- return [(a[0] * b[0] + a[1] * b[1]) / d, (a[1] * b[0] - a[0] * b[1]) / d];
300
- };
301
- const evalZ = (z) => {
302
- let r = [1, 0];
303
- for (let i = degree - 1; i >= 0; i--)
304
- r = [mulZ(r, z)[0] + (monic[i] ?? 0), mulZ(r, z)[1]];
305
- return r;
306
- };
307
- let roots = Array.from({ length: degree }, (_, i) => {
308
- const p = [0.4, 0.9];
309
- let z = [1, 0];
310
- for (let k = 0; k < i; k++)
311
- z = mulZ(z, p);
312
- return z;
313
- });
314
- for (let iter = 0; iter < 500; iter++) {
315
- roots = roots.map((r, i) => {
316
- let d = [1, 0];
317
- roots.forEach((s, j) => {
318
- if (j !== i)
319
- d = mulZ(d, [r[0] - s[0], r[1] - s[1]]);
320
- });
321
- const step = divZ(evalZ(r), d);
322
- return [r[0] - step[0], r[1] - step[1]];
323
- });
324
- }
325
- return roots.filter((r) => Math.abs(r[1]) < 1e-7).map((r) => r[0]);
326
- }
327
- /** The proportions `t` from 0 to 1 where the curve of the control points passes through
328
- * `point`, rounded to `roundTo` (Manim `proportions_along_bezier_curve_for_point`). */
329
- export function proportionsAlongBezierCurveForPoint(point, controlPoints, roundTo = 1e-6) {
330
- const n = controlPoints.length - 1;
331
- const digits = Math.round(Math.log10(1 / roundTo));
332
- const rounded = (x) => Math.round(x * 10 ** digits) / 10 ** digits;
333
- const perAxis = [];
334
- for (const dim of [0, 1]) {
335
- const coords = controlPoints.map((p) => p[dim]);
336
- // The power basis: the coefficient of t^k is sum_i choose(n, k) choose(k, i) (-1)^(k - i) P_i.
337
- const terms = Array.from({ length: n + 1 }, (_, k) => {
338
- let sum = 0;
339
- for (let i = 0; i <= k; i++)
340
- sum += choose(k, i) * (-1) ** (k - i) * (coords[i] ?? 0);
341
- return choose(n, k) * sum;
342
- });
343
- terms[0] = (terms[0] ?? 0) - point[dim];
344
- if (terms.every((x) => x === 0))
345
- continue;
346
- perAxis.push(realRoots(terms).map(rounded));
347
- }
348
- const [firstAxis = [], ...others] = perAxis;
349
- const common = [...new Set(firstAxis)].filter((r) => others.every((a) => a.includes(r)));
350
- return common.filter((r) => r >= 0 && r <= 1).toSorted((a, b) => a - b);
351
- }
352
- /** True when the curve of the control points passes through `point`. */
353
- export function pointLiesOnBezier(point, controlPoints, roundTo = 1e-6) {
354
- return proportionsAlongBezierCurveForPoint(point, controlPoints, roundTo).length > 0;
355
- }
356
- /** Points move on straight lines. */
357
- export function straightPath() {
358
- return (startPoints, endPoints, alpha) => startPoints.map((p, i) => lerpPoint(p, endPoints[i] ?? p, alpha));
359
- }
360
- /** Points move on arcs of `arcAngle` (counter-clockwise for a positive angle). */
361
- export function pathAlongArc(arcAngle) {
362
- if (Math.abs(arcAngle) < 0.01)
363
- return straightPath();
364
- return (startPoints, endPoints, alpha) => startPoints.map((p, i) => {
365
- const q = endPoints[i] ?? p;
366
- const v = [q[0] - p[0], q[1] - p[1]];
367
- let center = [p[0] + v[0] / 2, p[1] + v[1] / 2];
368
- if (arcAngle !== PI) {
369
- const k = 1 / Math.tan(arcAngle / 2);
370
- center = [center[0] - (v[1] / 2) * k, center[1] + (v[0] / 2) * k];
371
- }
372
- return rotateVectorAbout(p, alpha * arcAngle, center);
373
- });
374
- }
375
- /** Points move on half circles, clockwise. */
376
- export function clockwisePath() {
377
- return pathAlongArc(-PI);
378
- }
379
- /** Points move on half circles, counter-clockwise. */
380
- export function counterclockwisePath() {
381
- return pathAlongArc(PI);
382
- }
383
- // --- space_ops -------------------------------------------------------------------------
384
- function rotateVectorAbout(p, angle, center) {
385
- const [x, y] = [p[0] - center[0], p[1] - center[1]];
386
- const c = Math.cos(angle);
387
- const s = Math.sin(angle);
388
- return [center[0] + c * x - s * y, center[1] + s * x + c * y];
389
- }
390
- /** `vector` turned by `angle`, counter-clockwise. */
391
- export function rotateVector(vector, angle) {
392
- return rotateVectorAbout(vector, angle, [0, 0]);
393
- }
394
- /** A `dim` × `dim` matrix of 1 where `|row - col| < thickness`, else 0. */
395
- export function thickDiagonal(dim, thickness = 2) {
396
- return Array.from({ length: dim }, (_, r) => Array.from({ length: dim }, (_c, c) => (Math.abs(r - c) < thickness ? 1 : 0)));
397
- }
398
- /** The 2 × 2 matrix that turns by `angle`, counter-clockwise. */
399
- export function rotationAboutZ(angle) {
400
- const c = Math.cos(angle);
401
- const s = Math.sin(angle);
402
- return [
403
- [c, -s],
404
- [s, c],
405
- ];
406
- }
407
- /** The angle of `vector` from the x axis, from -π to π. */
408
- export function angleOfVector(vector) {
409
- return Math.atan2(vector[1], vector[0]);
410
- }
411
- /** The angle between two vectors, from 0 to π. */
412
- export function angleBetweenVectors(v1, v2) {
413
- const a = normalize(v1);
414
- const b = normalize(v2);
415
- return 2 * Math.atan2(Math.hypot(a[0] - b[0], a[1] - b[1]), Math.hypot(a[0] + b[0], a[1] + b[1]));
416
- }
417
- /** `vect` with length 1; a zero vector gives `fallBack`, or `[0, 0]`. */
418
- export function normalize(vect, fallBack) {
419
- const n = Math.hypot(vect[0], vect[1]);
420
- return n > 0 ? [vect[0] / n, vect[1] / n] : (fallBack ?? [0, 0]);
421
- }
422
- /** `n` vectors, `startVect` turned by each multiple of `TAU / n`. */
423
- export function compassDirections(n = 4, startVect = RIGHT) {
424
- return Array.from({ length: n }, (_, k) => rotateVector(startVect, (k * TAU) / n));
425
- }
426
- /** The vertices of a regular `n`-gon and its start angle: `[vertices, startAngle]`. The start
427
- * angle is 0 for an even `n` and `TAU / 4` for an odd `n` by default. */
428
- export function regularVertices(n, opts = {}) {
429
- const { radius = 1, startAngle = n % 2 === 0 ? 0 : TAU / 4 } = opts;
430
- return [compassDirections(n, rotateVector([radius, 0], startAngle)), startAngle];
431
- }
432
- /** The point of a complex number. */
433
- export function complexToR3(complexNum) {
434
- return [complexNum[0], complexNum[1]];
435
- }
436
- /** The complex number of a point. */
437
- export function R3ToComplex(point) {
438
- return [point[0], point[1]];
439
- }
440
- /** A function of points from a function of complex numbers. */
441
- export function complexFuncToR3Func(complexFunc) {
442
- return (p) => complexToR3(complexFunc(R3ToComplex(p)));
443
- }
444
- /** The mean of the points. */
445
- export function centerOfMass(points) {
446
- const n = points.length;
447
- return [points.reduce((s, p) => s + p[0], 0) / n, points.reduce((s, p) => s + p[1], 0) / n];
448
- }
449
- export function midpoint(point1, point2) {
450
- return centerOfMass([point1, point2]);
451
- }
452
- /** For each `i`, the point where the line through `p0s[i]` along `v0s[i]` meets the line through
453
- * `p1s[i]` along `v1s[i]`. */
454
- export function findIntersection(p0s, v0s, p1s, v1s, threshold = 1e-5) {
455
- return p0s.map((p0, i) => {
456
- const v0 = v0s[i] ?? [0, 0];
457
- const p1 = p1s[i] ?? p0;
458
- const v1 = v1s[i] ?? [0, 0];
459
- const c = cross2d(v0, v1);
460
- const normal = [v1[1] * c, -v1[0] * c];
461
- const denom = Math.max(v0[0] * normal[0] + v0[1] * normal[1], threshold);
462
- const t = ((p1[0] - p0[0]) * normal[0] + (p1[1] - p0[1]) * normal[1]) / denom;
463
- return [p0[0] + t * v0[0], p0[1] + t * v0[1]];
464
- });
465
- }
466
- /** The point where the lines through `line1` and `line2` (each two points) meet. Parallel lines
467
- * are an error. */
468
- export function lineIntersection(line1, line2) {
469
- const [[x1, y1], [x2, y2]] = line1;
470
- const [[x3, y3], [x4, y4]] = line2;
471
- const a = [y1 - y2, x2 - x1, x1 * y2 - x2 * y1];
472
- const b = [y3 - y4, x4 - x3, x3 * y4 - x4 * y3];
473
- const x = a[1] * b[2] - a[2] * b[1];
474
- const y = a[2] * b[0] - a[0] * b[2];
475
- const z = a[0] * b[1] - a[1] * b[0];
476
- if (z === 0)
477
- return fault("lineIntersection: the lines are parallel, there is no unique intersection point");
478
- return [x / z, y / z];
479
- }
480
- /** How many times the closed polygon of `points` goes around the origin, counter-clockwise. */
481
- export function getWindingNumber(points) {
482
- let total = 0;
483
- points.forEach((p1, i) => {
484
- const p2 = points[(i + 1) % points.length] ?? p1;
485
- const d = angleOfVector(p2) - angleOfVector(p1);
486
- total += ((((d + PI) % TAU) + TAU) % TAU) - PI;
487
- });
488
- return total / TAU;
489
- }
490
- /** The trapezoid sum of the points (Manim `shoelace`): positive for a clockwise path. */
491
- export function shoelace(xY) {
492
- let sum = 0;
493
- for (let i = 0; i + 1 < xY.length; i++) {
494
- const [x0, y0] = xY[i] ?? [0, 0];
495
- const [x1, y1] = xY[i + 1] ?? [0, 0];
496
- sum += ((x1 - x0) * (y0 + y1)) / 2;
497
- }
498
- return sum;
499
- }
500
- /** `"CW"` or `"CCW"`: the direction of the path of the points. */
501
- export function shoelaceDirection(xY) {
502
- return shoelace(xY) > 0 ? "CW" : "CCW";
503
- }
504
- /** The z of the cross product of two vectors: `a.x * b.y - a.y * b.x`. */
505
- export function cross2d(a, b) {
506
- return a[0] * b[1] - b[0] * a[1];
507
- }
508
- /** Two points on the perpendicular bisector of the two points of `line`. */
509
- export function perpendicularBisector(line) {
510
- const [p1, p2] = line;
511
- const d = [p1[1] - p2[1], -(p1[0] - p2[0])];
512
- const m = midpoint(p1, p2);
513
- return [
514
- [m[0] + d[0], m[1] + d[1]],
515
- [m[0] - d[0], m[1] - d[1]],
516
- ];
517
- }
@@ -1,132 +0,0 @@
1
- import { type Animation, type AnimationOptions, type Color, type Point, type RateFunc, Mobject } from "./engine.ts";
2
- import { type ParsableManimColor } from "./colors.ts";
3
- import { type ArrowSettings } from "./geometry.ts";
4
- /** A vector field: the vector at each point. */
5
- export type VectorFunction = (p: Point) => Point;
6
- /** The options of VectorField (Manim's keyword arguments). */
7
- export interface VectorFieldOptions {
8
- /** One colour for the whole field. Without it, the colour comes from `colorScheme`. */
9
- color?: ParsableManimColor;
10
- /** A number for each vector, for the colour gradient (default: its length). */
11
- colorScheme?: (v: Point) => number;
12
- minColorSchemeValue?: number;
13
- maxColorSchemeValue?: number;
14
- colors?: readonly ParsableManimColor[];
15
- }
16
- /** Manim's `VectorField.shift_func`: the field moved by `shiftVector`. */
17
- declare function shiftFunc(func: VectorFunction, shiftVector: Point): VectorFunction;
18
- /** Manim's `VectorField.scale_func`: the field with its points scaled by `scalar`. */
19
- declare function scaleFunc(func: VectorFunction, scalar: number): VectorFunction;
20
- /** A vector field: Manim's `VectorField`. It has no picture of its own; it moves mobjects. */
21
- export declare class VectorFieldMobject extends Mobject {
22
- #private;
23
- /** The field (Manim `func`). */
24
- get func(): VectorFunction;
25
- /** True when `color` was given (Manim `single_color`). */
26
- get singleColor(): boolean;
27
- copy(): VectorFieldMobject;
28
- /** Gives `to`, a copy of the node, the field and the colours of this one. */
29
- protected copyField<T extends VectorFieldMobject>(to: T): T;
30
- /** Sets the field and its colours (the constructors call it). */
31
- setUp(func: VectorFunction, opts: VectorFieldOptions): this;
32
- /** The colour of the field at `pos` (Manim `pos_to_color`). */
33
- posToColor(pos: Point): Color;
34
- /** `func` moved by `shiftVector` (Manim `shift_func`). Also `m.VectorField.shiftFunc`. */
35
- shiftFunc(func: VectorFunction, shiftVector: Point): VectorFunction;
36
- /** `func` with its points scaled by `scalar` (Manim `scale_func`). Also `m.VectorField.scaleFunc`. */
37
- scaleFunc(func: VectorFunction, scalar: number): VectorFunction;
38
- /** Maps each point of the field through `coordinateSystem.coordsToPoint` (Manim
39
- * `fit_to_coordinate_system`). */
40
- fitToCoordinateSystem(coordinateSystem: {
41
- coordsToPoint(x: number, y: number): Point;
42
- }): this;
43
- /** Moves `mob` along the field for time `dt`, in `substeps` Runge-Kutta steps (Manim
44
- * `nudge`). With `pointwise`, each point of `mob` moves; else its centre. */
45
- nudge(mob: Mobject, dt?: number, substeps?: number, pointwise?: boolean): this;
46
- /** Nudges each submobject (Manim `nudge_submobjects`). */
47
- nudgeSubmobjects(dt?: number, substeps?: number, pointwise?: boolean): this;
48
- /** An updater that moves a mobject along the field at `speed` (Manim `get_nudge_updater`). */
49
- getNudgeUpdater(speed?: number, pointwise?: boolean): (mob: Mobject, dt: number) => void;
50
- /** Moves the submobjects along the field on every frame (Manim `start_submobject_movement`). */
51
- startSubmobjectMovement(speed?: number, pointwise?: boolean): this;
52
- /** Stops the movement of `startSubmobjectMovement` (Manim `stop_submobject_movement`). */
53
- stopSubmobjectMovement(): this;
54
- /** A function from values to `[r, g, b, a]` colours of the gradient of `colors` from `start`
55
- * to `end` (Manim `get_vectorized_rgba_gradient_function`). */
56
- getVectorizedRgbaGradientFunction(start: number, end: number, colors: readonly ParsableManimColor[]): (values: readonly number[], opacity?: number) => [number, number, number, number][];
57
- }
58
- /** A vector field with no picture (Manim `VectorField`): `VectorField(func, opts)`. */
59
- export declare const VectorField: ((func: VectorFunction, opts?: VectorFieldOptions) => VectorFieldMobject) & {
60
- shiftFunc: typeof shiftFunc;
61
- scaleFunc: typeof scaleFunc;
62
- };
63
- /** `[min, max]` or `[min, max, step]` of a field (the step defaults to 0.5). */
64
- export type FieldRange = readonly [number, number] | readonly [number, number, number];
65
- /** The options of ArrowVectorField (Manim's keyword arguments). */
66
- export interface ArrowVectorFieldOptions extends VectorFieldOptions {
67
- xRange?: FieldRange;
68
- yRange?: FieldRange;
69
- /** The length of the arrow of a vector of length `norm` (default `0.45 * sigmoid(norm)`). */
70
- lengthFunc?: (norm: number) => number;
71
- opacity?: number;
72
- /** The options of each Vector. */
73
- vectorConfig?: ArrowSettings;
74
- }
75
- /** A field of arrows: Manim's `ArrowVectorField`. */
76
- export declare class ArrowVectorFieldMobject extends VectorFieldMobject {
77
- lengthFunc: (norm: number) => number;
78
- vectorConfig: ArrowSettings;
79
- copy(): ArrowVectorFieldMobject;
80
- /** The arrow of the field at `point` (Manim `get_vector`). A vector that is not finite gives
81
- * a zero vector. With no point, the vector of the own path, as on every Mobject. */
82
- getVector(): Point;
83
- getVector(point: Point): Mobject;
84
- }
85
- /** Arrows of `func` on a grid (Manim `ArrowVectorField`): `ArrowVectorField(func, { xRange, yRange })`. */
86
- export declare function ArrowVectorField(func: VectorFunction, opts?: ArrowVectorFieldOptions): ArrowVectorFieldMobject;
87
- /** The options of StreamLines (Manim's keyword arguments). */
88
- export interface StreamLinesOptions extends VectorFieldOptions {
89
- xRange?: FieldRange;
90
- yRange?: FieldRange;
91
- /** How far each start point moves at random (default half the y step). */
92
- noiseFactor?: number;
93
- nRepeats?: number;
94
- dt?: number;
95
- /** The time that each agent moves; longer gives longer lines. */
96
- virtualTime?: number;
97
- maxAnchorsPerLine?: number;
98
- /** How far an agent can leave the area before it stops. */
99
- padding?: number;
100
- strokeWidth?: number;
101
- opacity?: number;
102
- }
103
- /** The traces of agents in a field: Manim's `StreamLines`. Its submobjects are the lines. */
104
- export declare class StreamLinesMobject extends VectorFieldMobject {
105
- #private;
106
- virtualTime: number;
107
- /** The lines (Manim `stream_lines`). */
108
- get streamLines(): Mobject[];
109
- /** The speed of the flow of `startAnimation` (Manim `flow_speed`). */
110
- get flowSpeed(): number | undefined;
111
- copy(): StreamLinesMobject;
112
- /** Adds a line, for the constructor. */
113
- addLine(mob: Mobject, duration: number): void;
114
- /** Each line is drawn, in random order (Manim `create`). `runTime` is the time of each line
115
- * (default `virtualTime`, or a function of it); `lagRatio` makes the whole 1.5 times as long. */
116
- create(lagRatio?: number, runTime?: number | ((virtualTime: number) => number), opts?: AnimationOptions): Animation;
117
- /** Makes the lines flow on every frame, with a passing flash of `timeWidth` along each line
118
- * (Manim `start_animation`). With `warmUp` the lines start one by one. */
119
- startAnimation(opts?: {
120
- warmUp?: boolean;
121
- flowSpeed?: number;
122
- timeWidth?: number;
123
- rateFunc?: RateFunc;
124
- }): this;
125
- /** Ends the flow smoothly: each line finishes its flash, then is drawn whole (Manim
126
- * `end_animation`). Call `startAnimation` first. */
127
- endAnimation(): Animation;
128
- }
129
- /** Stream lines of `func` (Manim `StreamLines`): each agent starts near a grid point and moves
130
- * with the field for `virtualTime`. */
131
- export declare function StreamLines(func: VectorFunction, opts?: StreamLinesOptions): StreamLinesMobject;
132
- export {};