@motionscript/math 0.1.0-alpha.8 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (158) hide show
  1. package/CHANGELOG.md +6 -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/manifest.json +1 -1
  18. package/dist/browser/tex.js +1 -1
  19. package/dist/browser/tex.js.map +1 -1
  20. package/dist/engine.d.ts +1 -1
  21. package/dist/engine.js +1 -1
  22. package/dist/engine.js.map +1 -1
  23. package/dist/index.js.map +1 -1
  24. package/dist/kit/brace.js.map +1 -1
  25. package/dist/kit/cache.js.map +1 -1
  26. package/dist/kit/component.d.ts.map +1 -1
  27. package/dist/kit/component.js.map +1 -1
  28. package/dist/kit/contour.js.map +1 -1
  29. package/dist/kit/coords.js.map +1 -1
  30. package/dist/kit/curve.js.map +1 -1
  31. package/dist/kit/equation.js.map +1 -1
  32. package/dist/kit/equation3d.js.map +1 -1
  33. package/dist/kit/expression.js.map +1 -1
  34. package/dist/kit/field-mark.d.ts.map +1 -1
  35. package/dist/kit/field-mark.js.map +1 -1
  36. package/dist/kit/field.js.map +1 -1
  37. package/dist/kit/follow.d.ts.map +1 -1
  38. package/dist/kit/follow.js.map +1 -1
  39. package/dist/kit/furniture.js.map +1 -1
  40. package/dist/kit/geometry.js.map +1 -1
  41. package/dist/kit/graph.js.map +1 -1
  42. package/dist/kit/mark.js.map +1 -1
  43. package/dist/kit/part-fills.js.map +1 -1
  44. package/dist/kit/place.js.map +1 -1
  45. package/dist/kit/sampler.js.map +1 -1
  46. package/dist/kit/scale.js.map +1 -1
  47. package/dist/kit/source.js.map +1 -1
  48. package/dist/kit/system.js.map +1 -1
  49. package/dist/kit/ticks.js.map +1 -1
  50. package/dist/kit/withdrawn.js.map +1 -1
  51. package/dist/marks/curve-area.js.map +1 -1
  52. package/dist/marks/drop-lines.js.map +1 -1
  53. package/dist/marks/plot-point.js.map +1 -1
  54. package/dist/marks/riemann-sum.js.map +1 -1
  55. package/dist/marks/tangent-line.js.map +1 -1
  56. package/dist/marks/vector-arrow.js.map +1 -1
  57. package/dist/nodes.js.map +1 -1
  58. package/dist/plots/function-plot.d.ts.map +1 -1
  59. package/dist/plots/function-plot.js.map +1 -1
  60. package/dist/plots/points-plot.js.map +1 -1
  61. package/dist/plots/stream-lines.js.map +1 -1
  62. package/dist/plots/traced-path.js.map +1 -1
  63. package/dist/plots/vector-field.js.map +1 -1
  64. package/dist/structures/graph-layout.js.map +1 -1
  65. package/dist/structures/network-graph.js.map +1 -1
  66. package/dist/structures/sample-space.js.map +1 -1
  67. package/dist/systems/axes2d.d.ts.map +1 -1
  68. package/dist/systems/axes2d.js.map +1 -1
  69. package/dist/systems/number-line.js.map +1 -1
  70. package/dist/systems/number-plane.js.map +1 -1
  71. package/dist/systems/polar-plane.js.map +1 -1
  72. package/dist/tex/geometry.js.map +1 -1
  73. package/dist/tex/index.js.map +1 -1
  74. package/dist/tex/mathjax.js.map +1 -1
  75. package/dist/tex/matrix.js.map +1 -1
  76. package/dist/tex/node.js.map +1 -1
  77. package/dist/tex/tween.js.map +1 -1
  78. package/dist/tex/typeset.d.ts.map +1 -1
  79. package/dist/tex/typeset.js.map +1 -1
  80. package/dist/three/axes3d.js.map +1 -1
  81. package/dist/three/curve-plot3d.js.map +1 -1
  82. package/dist/three/entries.js.map +1 -1
  83. package/dist/three/label3d.js.map +1 -1
  84. package/dist/three/plot-point3d.js.map +1 -1
  85. package/dist/three/space.js.map +1 -1
  86. package/dist/three/surface-plot3d.js.map +1 -1
  87. package/dist/three/vector-arrow3d.js.map +1 -1
  88. package/package.json +4 -4
  89. package/registry.json +251 -251
  90. package/src/annotate/angle-mark.ts +137 -137
  91. package/src/annotate/curly-brace.ts +148 -148
  92. package/src/annotate/emphasis-mark.ts +118 -118
  93. package/src/engine.ts +86 -86
  94. package/src/index.ts +29 -29
  95. package/src/kit/brace.ts +37 -37
  96. package/src/kit/cache.ts +61 -61
  97. package/src/kit/component.ts +57 -57
  98. package/src/kit/contour.ts +94 -94
  99. package/src/kit/coords.ts +278 -278
  100. package/src/kit/curve.ts +47 -47
  101. package/src/kit/equation.ts +181 -181
  102. package/src/kit/equation3d.ts +137 -137
  103. package/src/kit/expression.ts +159 -159
  104. package/src/kit/field-mark.ts +171 -171
  105. package/src/kit/field.ts +50 -50
  106. package/src/kit/follow.ts +229 -229
  107. package/src/kit/furniture.ts +113 -113
  108. package/src/kit/geometry.ts +49 -49
  109. package/src/kit/graph.ts +116 -116
  110. package/src/kit/mark.ts +152 -152
  111. package/src/kit/part-fills.ts +61 -61
  112. package/src/kit/place.ts +77 -77
  113. package/src/kit/sampler.ts +281 -281
  114. package/src/kit/scale.ts +39 -39
  115. package/src/kit/source.ts +57 -57
  116. package/src/kit/system.ts +105 -105
  117. package/src/kit/ticks.ts +85 -85
  118. package/src/kit/withdrawn.ts +8 -8
  119. package/src/marks/curve-area.ts +89 -89
  120. package/src/marks/drop-lines.ts +71 -71
  121. package/src/marks/plot-point.ts +102 -102
  122. package/src/marks/riemann-sum.ts +99 -99
  123. package/src/marks/tangent-line.ts +119 -119
  124. package/src/marks/vector-arrow.ts +98 -98
  125. package/src/nodes.ts +99 -99
  126. package/src/plots/function-plot.ts +220 -220
  127. package/src/plots/points-plot.ts +103 -103
  128. package/src/plots/stream-lines.ts +160 -160
  129. package/src/plots/traced-path.ts +70 -70
  130. package/src/plots/vector-field.ts +107 -107
  131. package/src/structures/graph-layout.ts +145 -145
  132. package/src/structures/network-graph.ts +291 -291
  133. package/src/structures/sample-space.ts +210 -210
  134. package/src/systems/axes2d.ts +226 -226
  135. package/src/systems/number-line.ts +130 -130
  136. package/src/systems/number-plane.ts +232 -232
  137. package/src/systems/polar-plane.ts +177 -177
  138. package/src/tex/geometry.ts +703 -703
  139. package/src/tex/index.ts +15 -15
  140. package/src/tex/mathjax.ts +82 -82
  141. package/src/tex/matrix.ts +97 -97
  142. package/src/tex/node.ts +74 -74
  143. package/src/tex/tween.ts +308 -308
  144. package/src/tex/typeset.ts +461 -461
  145. package/src/three/axes3d.ts +267 -267
  146. package/src/three/curve-plot3d.ts +146 -146
  147. package/src/three/entries.ts +42 -42
  148. package/src/three/label3d.ts +52 -52
  149. package/src/three/plot-point3d.ts +63 -63
  150. package/src/three/space.ts +92 -92
  151. package/src/three/surface-plot3d.ts +318 -318
  152. package/src/three/vector-arrow3d.ts +88 -88
  153. package/dist/browser/chunks/chunk-FETQZEN2.js.map +0 -7
  154. package/dist/browser/chunks/chunk-K77KLQX4.js.map +0 -7
  155. package/dist/kit/placement.d.ts +0 -8
  156. package/dist/kit/placement.d.ts.map +0 -1
  157. package/dist/kit/placement.js +0 -8
  158. package/dist/kit/placement.js.map +0 -1
package/src/nodes.ts CHANGED
@@ -1,99 +1,99 @@
1
- import { AngleMark } from "./annotate/angle-mark";
2
- import { CurlyBrace } from "./annotate/curly-brace";
3
- import { EmphasisMark } from "./annotate/emphasis-mark";
4
- import { CurveArea } from "./marks/curve-area";
5
- import { DropLines } from "./marks/drop-lines";
6
- import { PlotPoint } from "./marks/plot-point";
7
- import { RiemannSum } from "./marks/riemann-sum";
8
- import { TangentLine } from "./marks/tangent-line";
9
- import { VectorArrow } from "./marks/vector-arrow";
10
- import { FunctionPlot } from "./plots/function-plot";
11
- import { PointsPlot } from "./plots/points-plot";
12
- import { StreamLines } from "./plots/stream-lines";
13
- import { TracedPath } from "./plots/traced-path";
14
- import { VectorField } from "./plots/vector-field";
15
- import { Axes2d } from "./systems/axes2d";
16
- import { NumberLine } from "./systems/number-line";
17
- import { NumberPlane } from "./systems/number-plane";
18
- import { PolarPlane } from "./systems/polar-plane";
19
- import { NetworkGraph } from "./structures/network-graph";
20
- import { SampleSpace } from "./structures/sample-space";
21
- import { LatexMatrix } from "./tex/matrix";
22
- import { Latex } from "./tex/node";
23
- import { Axes3d } from "./three/axes3d";
24
- import { CurvePlot3d } from "./three/curve-plot3d";
25
- import { PlotPoint3d } from "./three/plot-point3d";
26
- import { SurfacePlot3d } from "./three/surface-plot3d";
27
- import { VectorArrow3d } from "./three/vector-arrow3d";
28
-
29
- /**
30
- * Every node type this package publishes.
31
- *
32
- * A host registers these by handing the array to an engine —
33
- * `new Engine(platform, { nodes: NODES })` — which reads each component's key.
34
- * Defining a component only *declares* that key: nothing is registered by
35
- * importing a module, so a registry holds exactly what its owner asked for.
36
- *
37
- * Listing the component **values** is also what survives bundling: this package
38
- * declares `sideEffects: false`, and a document names a node type by string, so
39
- * an import kept only for its side effect is exactly what gets shaken out.
40
- */
41
- // Typed by name, so the declaration names each component rather than spelling out its type through core.
42
- type KitNode =
43
- | typeof Latex
44
- | typeof LatexMatrix
45
- | typeof NumberLine
46
- | typeof Axes2d
47
- | typeof NumberPlane
48
- | typeof FunctionPlot
49
- | typeof PointsPlot
50
- | typeof PlotPoint
51
- | typeof VectorArrow
52
- | typeof CurveArea
53
- | typeof RiemannSum
54
- | typeof TangentLine
55
- | typeof DropLines
56
- | typeof CurlyBrace
57
- | typeof AngleMark
58
- | typeof EmphasisMark
59
- | typeof Axes3d
60
- | typeof SurfacePlot3d
61
- | typeof CurvePlot3d
62
- | typeof PlotPoint3d
63
- | typeof VectorArrow3d
64
- | typeof PolarPlane
65
- | typeof VectorField
66
- | typeof StreamLines
67
- | typeof TracedPath
68
- | typeof NetworkGraph
69
- | typeof SampleSpace;
70
-
71
- export const NODES: KitNode[] = [
72
- Latex,
73
- LatexMatrix,
74
- NumberLine,
75
- Axes2d,
76
- NumberPlane,
77
- FunctionPlot,
78
- PointsPlot,
79
- PlotPoint,
80
- VectorArrow,
81
- CurveArea,
82
- RiemannSum,
83
- TangentLine,
84
- DropLines,
85
- CurlyBrace,
86
- AngleMark,
87
- EmphasisMark,
88
- Axes3d,
89
- SurfacePlot3d,
90
- CurvePlot3d,
91
- PlotPoint3d,
92
- VectorArrow3d,
93
- PolarPlane,
94
- VectorField,
95
- StreamLines,
96
- TracedPath,
97
- NetworkGraph,
98
- SampleSpace,
99
- ];
1
+ import { AngleMark } from "./annotate/angle-mark";
2
+ import { CurlyBrace } from "./annotate/curly-brace";
3
+ import { EmphasisMark } from "./annotate/emphasis-mark";
4
+ import { CurveArea } from "./marks/curve-area";
5
+ import { DropLines } from "./marks/drop-lines";
6
+ import { PlotPoint } from "./marks/plot-point";
7
+ import { RiemannSum } from "./marks/riemann-sum";
8
+ import { TangentLine } from "./marks/tangent-line";
9
+ import { VectorArrow } from "./marks/vector-arrow";
10
+ import { FunctionPlot } from "./plots/function-plot";
11
+ import { PointsPlot } from "./plots/points-plot";
12
+ import { StreamLines } from "./plots/stream-lines";
13
+ import { TracedPath } from "./plots/traced-path";
14
+ import { VectorField } from "./plots/vector-field";
15
+ import { Axes2d } from "./systems/axes2d";
16
+ import { NumberLine } from "./systems/number-line";
17
+ import { NumberPlane } from "./systems/number-plane";
18
+ import { PolarPlane } from "./systems/polar-plane";
19
+ import { NetworkGraph } from "./structures/network-graph";
20
+ import { SampleSpace } from "./structures/sample-space";
21
+ import { LatexMatrix } from "./tex/matrix";
22
+ import { Latex } from "./tex/node";
23
+ import { Axes3d } from "./three/axes3d";
24
+ import { CurvePlot3d } from "./three/curve-plot3d";
25
+ import { PlotPoint3d } from "./three/plot-point3d";
26
+ import { SurfacePlot3d } from "./three/surface-plot3d";
27
+ import { VectorArrow3d } from "./three/vector-arrow3d";
28
+
29
+ /**
30
+ * Every node type this package publishes.
31
+ *
32
+ * A host registers these by handing the array to an engine —
33
+ * `new Engine(platform, { nodes: NODES })` — which reads each component's key.
34
+ * Defining a component only *declares* that key: nothing is registered by
35
+ * importing a module, so a registry holds exactly what its owner asked for.
36
+ *
37
+ * Listing the component **values** is also what survives bundling: this package
38
+ * declares `sideEffects: false`, and a document names a node type by string, so
39
+ * an import kept only for its side effect is exactly what gets shaken out.
40
+ */
41
+ // Typed by name, so the declaration names each component rather than spelling out its type through core.
42
+ type KitNode =
43
+ | typeof Latex
44
+ | typeof LatexMatrix
45
+ | typeof NumberLine
46
+ | typeof Axes2d
47
+ | typeof NumberPlane
48
+ | typeof FunctionPlot
49
+ | typeof PointsPlot
50
+ | typeof PlotPoint
51
+ | typeof VectorArrow
52
+ | typeof CurveArea
53
+ | typeof RiemannSum
54
+ | typeof TangentLine
55
+ | typeof DropLines
56
+ | typeof CurlyBrace
57
+ | typeof AngleMark
58
+ | typeof EmphasisMark
59
+ | typeof Axes3d
60
+ | typeof SurfacePlot3d
61
+ | typeof CurvePlot3d
62
+ | typeof PlotPoint3d
63
+ | typeof VectorArrow3d
64
+ | typeof PolarPlane
65
+ | typeof VectorField
66
+ | typeof StreamLines
67
+ | typeof TracedPath
68
+ | typeof NetworkGraph
69
+ | typeof SampleSpace;
70
+
71
+ export const NODES: KitNode[] = [
72
+ Latex,
73
+ LatexMatrix,
74
+ NumberLine,
75
+ Axes2d,
76
+ NumberPlane,
77
+ FunctionPlot,
78
+ PointsPlot,
79
+ PlotPoint,
80
+ VectorArrow,
81
+ CurveArea,
82
+ RiemannSum,
83
+ TangentLine,
84
+ DropLines,
85
+ CurlyBrace,
86
+ AngleMark,
87
+ EmphasisMark,
88
+ Axes3d,
89
+ SurfacePlot3d,
90
+ CurvePlot3d,
91
+ PlotPoint3d,
92
+ VectorArrow3d,
93
+ PolarPlane,
94
+ VectorField,
95
+ StreamLines,
96
+ TracedPath,
97
+ NetworkGraph,
98
+ SampleSpace,
99
+ ];
@@ -1,220 +1,220 @@
1
- import {
2
- component2d,
3
- provides,
4
- Types,
5
- type EntryOptions,
6
- type NodeOf,
7
- type PaintProps,
8
- type Vector2,
9
- // Named so the emitted declaration reaches these through the sdk, not core.
10
- type Node2D as _Node2D,
11
- type Node2DProps as _Node2DProps,
12
- type Shadow as _Shadow,
13
- type ShadowResolved as _ShadowResolved,
14
- type Stroke as _Stroke,
15
- type StrokeResolved as _StrokeResolved,
16
- } from "@motionscript/sdk";
17
- import { createBoundedCache } from "../kit/cache";
18
- import { omit, PAINT, PAINT_COMMANDS } from "../kit/component";
19
- import { traceContour } from "../kit/contour";
20
- import type { CoordinateMap, Interval } from "../kit/coords";
21
- import { equationParams, parseEquation, type Equation } from "../kit/equation";
22
- import type { ExpressionScope } from "../kit/expression";
23
- import { PLOT_COLOR } from "../kit/furniture";
24
- import { PLOT, plotPlane, SYSTEM, SYSTEM_ENTRY } from "../kit/graph";
25
- import { drawMark, inkCentre, inkSize, reading, traced, type MarkReading } from "../kit/mark";
26
- import { openPlane, placeOnMap } from "../kit/place";
27
- import { sampleCurve, type CurveProjection } from "../kit/sampler";
28
- import { lerpDomain, lerpParams, lerpSource, readSource, scopeFor, type PlotSource } from "../kit/source";
29
-
30
- export interface FunctionPlotProps extends PaintProps {
31
- expression?: string | PlotSource;
32
- domain?: Interval | null;
33
- params?: ExpressionScope;
34
- }
35
-
36
- const TAU = Math.PI * 2;
37
-
38
- /** A plot's entries: what a traced path is built on too. It is only a line, so it has no fill. */
39
- export const PLOT_STATE = {
40
- ...omit(PAINT, "fill"),
41
- expression: Types.custom<string | PlotSource, PlotSource>("", { resolve: readSource, lerp: lerpSource, kind: "equation" }),
42
- /**
43
- * The span of the free variable that is drawn. Left out, a function runs the
44
- * width of its system, a polar curve one turn and a parametric one from 0 to 1.
45
- */
46
- domain: Types.custom<Interval | null, Interval | null, EntryOptions & { readonly defaultMin: number; readonly defaultMax: number }>(null, { lerp: lerpDomain, kind: "range", defaultMin: -5, defaultMax: 5 }),
47
- /**
48
- * The equation's named values: any name in it that is not a variable, a
49
- * function or a constant. One left unset is 1, and a formula can bind each
50
- * to a variable.
51
- */
52
- params: Types.custom<ExpressionScope>({}, { lerp: lerpParams }),
53
- graph: SYSTEM_ENTRY,
54
- };
55
-
56
- /** A plot's own commands, beside a line's paints. */
57
- export const PLOT_COMMANDS = {
58
- ...omit(PAINT_COMMANDS, "fillTo"),
59
- /** Change the equation, the curve bending from the one into the other. */
60
- morphTo: { args: ["expression"] as const },
61
- /** Animate the equation's named values. */
62
- paramsTo: { args: ["params"] as const },
63
- /** Animate the span that is drawn. */
64
- domainTo: { args: ["domain"] as const },
65
- };
66
-
67
- export const PLOT_STROKE = { weight: 3, fill: PLOT_COLOR, align: "center", cap: "round", join: "round" } as const;
68
-
69
- /** What a plot's curve is drawn from. */
70
- export interface PlotValues {
71
- readonly expression: PlotSource;
72
- readonly params: ExpressionScope;
73
- }
74
-
75
- /**
76
- * The curve of an equation, drawn in the coordinate system it is a child of.
77
- * How the equation is written says what it is: see `Equation`. A mark put
78
- * inside it reads it: the area under it, a tangent, a point that rides it.
79
- */
80
- export const FunctionPlot = traced(component2d({
81
- key: "functionPlot",
82
- state: PLOT_STATE,
83
- defaults: { stroke: PLOT_STROKE, width: "hug", height: "hug" },
84
- commands: PLOT_COMMANDS,
85
- // The marks inside it ride it, on its system's map.
86
- provide: (s, inherited) => [provides(PLOT, plotPlane(s, inherited(SYSTEM)))],
87
- measure: (_measurer, s) => inkSize(tracePlot(s, reading(s), s.domain)),
88
- layout: (box, s, children) => {
89
- const read = reading(s);
90
- return placeOnMap(children, read.map, inkCentre(tracePlot(s, read, s.domain)), box, plotPlane(s, s.graph));
91
- },
92
- plane: (_box, s) => {
93
- const read = reading(s);
94
- return openPlane(read.map, inkCentre(tracePlot(s, read, s.domain)));
95
- },
96
- render: (ctx, s) => drawMark(ctx, s, tracePlot(s, reading(s), s.domain)),
97
- }), (s: PlotValues & { readonly domain: Interval | null }, read: MarkReading) => tracePlot(s, read, s.domain));
98
-
99
- export type FunctionPlot = NodeOf<typeof FunctionPlot>;
100
-
101
- const traces = createBoundedCache<Vector2[][]>(256);
102
-
103
- /** The runs a plot of `s` draws through `read`, over `span` of its free variable (all of it for `null`). */
104
- export function tracePlot(s: PlotValues, read: MarkReading, span: Interval | null): Vector2[][] {
105
- const { map, shift } = read;
106
- const { area } = map;
107
- if (!Number.isFinite(area.maxX - area.minX)) return [];
108
- const { source, blend } = s.expression;
109
- const key = [source, blend?.from, blend?.t, map.revision, span, JSON.stringify(s.params), shift.x, shift.y].join("|");
110
- const known = traces.get(key);
111
- if (known) return known;
112
-
113
- let runs: Vector2[][];
114
- // A curve with no parameter to walk cannot bend into another: it changes over half way.
115
- const shown = blend && blend.t < 0.5 ? blend.from : source;
116
- if (equationOf(source)?.kind === "implicit" || (blend && equationOf(blend.from)?.kind === "implicit")) {
117
- runs = contour(shown, s.params, map, shift);
118
- } else {
119
- let curve = projectionOf(source, s.params, span, map, shift);
120
- if (curve && blend) {
121
- const from = projectionOf(blend.from, s.params, span, map, shift);
122
- if (from) curve = between(from, curve, blend.t);
123
- }
124
- const width = area.maxX - area.minX;
125
- runs = curve
126
- ? sampleCurve(curve, 0, 1, area, { seeds: Math.min(512, Math.max(64, Math.round(width / 8))), minStep: 0.5 / Math.max(1, width) })
127
- : [];
128
- }
129
- traces.set(key, runs);
130
- return runs;
131
- }
132
-
133
- function equationOf(source: string): Equation | null {
134
- const equation = parseEquation(source, equationParams(source));
135
- return equation instanceof Error ? null : equation;
136
- }
137
-
138
- /** Where an implicit `source` holds, over what the system shows, as pixel runs. */
139
- function contour(source: string, params: ExpressionScope, map: CoordinateMap, shift: Vector2): Vector2[][] {
140
- const equation = equationOf(source);
141
- if (equation?.kind !== "implicit") return [];
142
- const scope = scopeFor(equationParams(source), params);
143
- const { area } = map;
144
- const cells = (pixels: number) => Math.min(CONTOUR_MAX, Math.max(8, Math.round(pixels / CONTOUR_CELL)));
145
- const { f } = equation;
146
- const runs = traceContour(
147
- (x, y) => f(x - shift.x, y - shift.y, scope),
148
- map.xRange,
149
- map.yRange,
150
- cells(area.maxX - area.minX),
151
- cells(area.maxY - area.minY),
152
- );
153
- return runs.map((run) => run.map((point) => map.toPoint(point.x, point.y)));
154
- }
155
-
156
- /** `source` as a pixel curve over a parameter running 0 to 1. */
157
- function projectionOf(source: string, params: ExpressionScope, domain: Interval | null, map: CoordinateMap, shift: Vector2): CurveProjection | null {
158
- const equation = equationOf(source);
159
- if (!equation || equation.kind === "implicit") return null;
160
- const scope = scopeFor(equationParams(source), params);
161
- switch (equation.kind) {
162
- case "function": {
163
- const horizontal = equation.of === "x";
164
- const [lo, hi] = horizontal ? map.xRange : map.yRange;
165
- const offset = horizontal ? shift.x : shift.y;
166
- // Only what the system shows is sampled, however wide the domain.
167
- const a = Math.max(lo - offset, domain?.[0] ?? -Infinity);
168
- const b = Math.min(hi - offset, domain?.[1] ?? Infinity);
169
- if (!(b > a)) return null;
170
- const { f } = equation;
171
- return (u, out) => {
172
- const v = a + (b - a) * u;
173
- const w = f(v, 0, scope);
174
- if (!Number.isFinite(w)) return false;
175
- if (horizontal) map.toPoint(v + shift.x, w + shift.y, out);
176
- else map.toPoint(w + shift.x, v + shift.y, out);
177
- return true;
178
- };
179
- }
180
- case "polar": {
181
- const [a, b] = domain ?? [0, TAU];
182
- const { f } = equation;
183
- return (u, out) => {
184
- const theta = a + (b - a) * u;
185
- const r = f(theta, 0, scope);
186
- if (!Number.isFinite(r)) return false;
187
- map.toPoint(r * Math.cos(theta) + shift.x, r * Math.sin(theta) + shift.y, out);
188
- return true;
189
- };
190
- }
191
- case "parametric": {
192
- const [a, b] = domain ?? [0, 1];
193
- const { x, y } = equation;
194
- return (u, out) => {
195
- const t = a + (b - a) * u;
196
- const gx = x(t, 0, scope);
197
- const gy = y(t, 0, scope);
198
- if (!Number.isFinite(gx) || !Number.isFinite(gy)) return false;
199
- map.toPoint(gx + shift.x, gy + shift.y, out);
200
- return true;
201
- };
202
- }
203
- }
204
- }
205
-
206
- // Roughly one cell of the contour's grid to this many pixels, each way.
207
- const CONTOUR_CELL = 5;
208
- const CONTOUR_MAX = 220;
209
-
210
- // Two curves blended point for point along their parameters, in pixels, so any
211
- // kind bends into any other.
212
- function between(from: CurveProjection, to: CurveProjection, t: number): CurveProjection {
213
- const a: Vector2 = { x: 0, y: 0 };
214
- return (u, out) => {
215
- if (!from(u, a) || !to(u, out)) return false;
216
- out.x = a.x + (out.x - a.x) * t;
217
- out.y = a.y + (out.y - a.y) * t;
218
- return true;
219
- };
220
- }
1
+ import {
2
+ component2d,
3
+ provides,
4
+ Types,
5
+ type EntryOptions,
6
+ type NodeOf,
7
+ type PaintProps,
8
+ type Vector2,
9
+ // Named so the emitted declaration reaches these through the sdk, not core.
10
+ type Node2D as _Node2D,
11
+ type Node2DProps as _Node2DProps,
12
+ type Shadow as _Shadow,
13
+ type ShadowResolved as _ShadowResolved,
14
+ type Stroke as _Stroke,
15
+ type StrokeResolved as _StrokeResolved,
16
+ } from "@motionscript/sdk";
17
+ import { createBoundedCache } from "../kit/cache";
18
+ import { omit, PAINT, PAINT_COMMANDS } from "../kit/component";
19
+ import { traceContour } from "../kit/contour";
20
+ import type { CoordinateMap, Interval } from "../kit/coords";
21
+ import { equationParams, parseEquation, type Equation } from "../kit/equation";
22
+ import type { ExpressionScope } from "../kit/expression";
23
+ import { PLOT_COLOR } from "../kit/furniture";
24
+ import { PLOT, plotPlane, SYSTEM, SYSTEM_ENTRY } from "../kit/graph";
25
+ import { drawMark, inkCentre, inkSize, reading, traced, type MarkReading } from "../kit/mark";
26
+ import { openPlane, placeOnMap } from "../kit/place";
27
+ import { sampleCurve, type CurveProjection } from "../kit/sampler";
28
+ import { lerpDomain, lerpParams, lerpSource, readSource, scopeFor, type PlotSource } from "../kit/source";
29
+
30
+ export interface FunctionPlotProps extends PaintProps {
31
+ expression?: string | PlotSource;
32
+ domain?: Interval | null;
33
+ params?: ExpressionScope;
34
+ }
35
+
36
+ const TAU = Math.PI * 2;
37
+
38
+ /** A plot's entries: what a traced path is built on too. It is only a line, so it has no fill. */
39
+ export const PLOT_STATE = {
40
+ ...omit(PAINT, "fill"),
41
+ expression: Types.custom<string | PlotSource, PlotSource>("", { resolve: readSource, lerp: lerpSource, kind: "equation" }),
42
+ /**
43
+ * The span of the free variable that is drawn. Left out, a function runs the
44
+ * width of its system, a polar curve one turn and a parametric one from 0 to 1.
45
+ */
46
+ domain: Types.custom<Interval | null, Interval | null, EntryOptions & { readonly defaultMin: number; readonly defaultMax: number }>(null, { lerp: lerpDomain, kind: "range", defaultMin: -5, defaultMax: 5 }),
47
+ /**
48
+ * The equation's named values: any name in it that is not a variable, a
49
+ * function or a constant. One left unset is 1, and a formula can bind each
50
+ * to a variable.
51
+ */
52
+ params: Types.custom<ExpressionScope>({}, { lerp: lerpParams }),
53
+ graph: SYSTEM_ENTRY,
54
+ };
55
+
56
+ /** A plot's own commands, beside a line's paints. */
57
+ export const PLOT_COMMANDS = {
58
+ ...omit(PAINT_COMMANDS, "fillTo"),
59
+ /** Change the equation, the curve bending from the one into the other. */
60
+ morphTo: { args: ["expression"] as const },
61
+ /** Animate the equation's named values. */
62
+ paramsTo: { args: ["params"] as const },
63
+ /** Animate the span that is drawn. */
64
+ domainTo: { args: ["domain"] as const },
65
+ };
66
+
67
+ export const PLOT_STROKE = { weight: 3, fill: PLOT_COLOR, align: "center", cap: "round", join: "round" } as const;
68
+
69
+ /** What a plot's curve is drawn from. */
70
+ export interface PlotValues {
71
+ readonly expression: PlotSource;
72
+ readonly params: ExpressionScope;
73
+ }
74
+
75
+ /**
76
+ * The curve of an equation, drawn in the coordinate system it is a child of.
77
+ * How the equation is written says what it is: see `Equation`. A mark put
78
+ * inside it reads it: the area under it, a tangent, a point that rides it.
79
+ */
80
+ export const FunctionPlot = traced(component2d({
81
+ key: "functionPlot",
82
+ state: PLOT_STATE,
83
+ defaults: { stroke: PLOT_STROKE, width: "hug", height: "hug" },
84
+ commands: PLOT_COMMANDS,
85
+ // The marks inside it ride it, on its system's map.
86
+ provide: (s, inherited) => [provides(PLOT, plotPlane(s, inherited(SYSTEM)))],
87
+ measure: (_measurer, s) => inkSize(tracePlot(s, reading(s), s.domain)),
88
+ layout: (box, s, children) => {
89
+ const read = reading(s);
90
+ return placeOnMap(children, read.map, inkCentre(tracePlot(s, read, s.domain)), box, plotPlane(s, s.graph));
91
+ },
92
+ plane: (_box, s) => {
93
+ const read = reading(s);
94
+ return openPlane(read.map, inkCentre(tracePlot(s, read, s.domain)));
95
+ },
96
+ render: (ctx, s) => drawMark(ctx, s, tracePlot(s, reading(s), s.domain)),
97
+ }), (s: PlotValues & { readonly domain: Interval | null }, read: MarkReading) => tracePlot(s, read, s.domain));
98
+
99
+ export type FunctionPlot = NodeOf<typeof FunctionPlot>;
100
+
101
+ const traces = createBoundedCache<Vector2[][]>(256);
102
+
103
+ /** The runs a plot of `s` draws through `read`, over `span` of its free variable (all of it for `null`). */
104
+ export function tracePlot(s: PlotValues, read: MarkReading, span: Interval | null): Vector2[][] {
105
+ const { map, shift } = read;
106
+ const { area } = map;
107
+ if (!Number.isFinite(area.maxX - area.minX)) return [];
108
+ const { source, blend } = s.expression;
109
+ const key = [source, blend?.from, blend?.t, map.revision, span, JSON.stringify(s.params), shift.x, shift.y].join("|");
110
+ const known = traces.get(key);
111
+ if (known) return known;
112
+
113
+ let runs: Vector2[][];
114
+ // A curve with no parameter to walk cannot bend into another: it changes over half way.
115
+ const shown = blend && blend.t < 0.5 ? blend.from : source;
116
+ if (equationOf(source)?.kind === "implicit" || (blend && equationOf(blend.from)?.kind === "implicit")) {
117
+ runs = contour(shown, s.params, map, shift);
118
+ } else {
119
+ let curve = projectionOf(source, s.params, span, map, shift);
120
+ if (curve && blend) {
121
+ const from = projectionOf(blend.from, s.params, span, map, shift);
122
+ if (from) curve = between(from, curve, blend.t);
123
+ }
124
+ const width = area.maxX - area.minX;
125
+ runs = curve
126
+ ? sampleCurve(curve, 0, 1, area, { seeds: Math.min(512, Math.max(64, Math.round(width / 8))), minStep: 0.5 / Math.max(1, width) })
127
+ : [];
128
+ }
129
+ traces.set(key, runs);
130
+ return runs;
131
+ }
132
+
133
+ function equationOf(source: string): Equation | null {
134
+ const equation = parseEquation(source, equationParams(source));
135
+ return equation instanceof Error ? null : equation;
136
+ }
137
+
138
+ /** Where an implicit `source` holds, over what the system shows, as pixel runs. */
139
+ function contour(source: string, params: ExpressionScope, map: CoordinateMap, shift: Vector2): Vector2[][] {
140
+ const equation = equationOf(source);
141
+ if (equation?.kind !== "implicit") return [];
142
+ const scope = scopeFor(equationParams(source), params);
143
+ const { area } = map;
144
+ const cells = (pixels: number) => Math.min(CONTOUR_MAX, Math.max(8, Math.round(pixels / CONTOUR_CELL)));
145
+ const { f } = equation;
146
+ const runs = traceContour(
147
+ (x, y) => f(x - shift.x, y - shift.y, scope),
148
+ map.xRange,
149
+ map.yRange,
150
+ cells(area.maxX - area.minX),
151
+ cells(area.maxY - area.minY),
152
+ );
153
+ return runs.map((run) => run.map((point) => map.toPoint(point.x, point.y)));
154
+ }
155
+
156
+ /** `source` as a pixel curve over a parameter running 0 to 1. */
157
+ function projectionOf(source: string, params: ExpressionScope, domain: Interval | null, map: CoordinateMap, shift: Vector2): CurveProjection | null {
158
+ const equation = equationOf(source);
159
+ if (!equation || equation.kind === "implicit") return null;
160
+ const scope = scopeFor(equationParams(source), params);
161
+ switch (equation.kind) {
162
+ case "function": {
163
+ const horizontal = equation.of === "x";
164
+ const [lo, hi] = horizontal ? map.xRange : map.yRange;
165
+ const offset = horizontal ? shift.x : shift.y;
166
+ // Only what the system shows is sampled, however wide the domain.
167
+ const a = Math.max(lo - offset, domain?.[0] ?? -Infinity);
168
+ const b = Math.min(hi - offset, domain?.[1] ?? Infinity);
169
+ if (!(b > a)) return null;
170
+ const { f } = equation;
171
+ return (u, out) => {
172
+ const v = a + (b - a) * u;
173
+ const w = f(v, 0, scope);
174
+ if (!Number.isFinite(w)) return false;
175
+ if (horizontal) map.toPoint(v + shift.x, w + shift.y, out);
176
+ else map.toPoint(w + shift.x, v + shift.y, out);
177
+ return true;
178
+ };
179
+ }
180
+ case "polar": {
181
+ const [a, b] = domain ?? [0, TAU];
182
+ const { f } = equation;
183
+ return (u, out) => {
184
+ const theta = a + (b - a) * u;
185
+ const r = f(theta, 0, scope);
186
+ if (!Number.isFinite(r)) return false;
187
+ map.toPoint(r * Math.cos(theta) + shift.x, r * Math.sin(theta) + shift.y, out);
188
+ return true;
189
+ };
190
+ }
191
+ case "parametric": {
192
+ const [a, b] = domain ?? [0, 1];
193
+ const { x, y } = equation;
194
+ return (u, out) => {
195
+ const t = a + (b - a) * u;
196
+ const gx = x(t, 0, scope);
197
+ const gy = y(t, 0, scope);
198
+ if (!Number.isFinite(gx) || !Number.isFinite(gy)) return false;
199
+ map.toPoint(gx + shift.x, gy + shift.y, out);
200
+ return true;
201
+ };
202
+ }
203
+ }
204
+ }
205
+
206
+ // Roughly one cell of the contour's grid to this many pixels, each way.
207
+ const CONTOUR_CELL = 5;
208
+ const CONTOUR_MAX = 220;
209
+
210
+ // Two curves blended point for point along their parameters, in pixels, so any
211
+ // kind bends into any other.
212
+ function between(from: CurveProjection, to: CurveProjection, t: number): CurveProjection {
213
+ const a: Vector2 = { x: 0, y: 0 };
214
+ return (u, out) => {
215
+ if (!from(u, a) || !to(u, out)) return false;
216
+ out.x = a.x + (out.x - a.x) * t;
217
+ out.y = a.y + (out.y - a.y) * t;
218
+ return true;
219
+ };
220
+ }