@motionscript/math 0.0.0-stage → 0.1.0-alpha.5

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 (322) hide show
  1. package/CHANGELOG.md +110 -0
  2. package/LICENSE +201 -0
  3. package/NOTICE +48 -0
  4. package/README.md +385 -3
  5. package/dist/annotate/angle-mark.d.ts +46 -0
  6. package/dist/annotate/angle-mark.d.ts.map +1 -0
  7. package/dist/annotate/angle-mark.js +132 -0
  8. package/dist/annotate/angle-mark.js.map +1 -0
  9. package/dist/annotate/curly-brace.d.ts +46 -0
  10. package/dist/annotate/curly-brace.d.ts.map +1 -0
  11. package/dist/annotate/curly-brace.js +114 -0
  12. package/dist/annotate/curly-brace.js.map +1 -0
  13. package/dist/annotate/emphasis-mark.d.ts +20 -0
  14. package/dist/annotate/emphasis-mark.d.ts.map +1 -0
  15. package/dist/annotate/emphasis-mark.js +81 -0
  16. package/dist/annotate/emphasis-mark.js.map +1 -0
  17. package/dist/browser/THIRD_PARTY_LICENSES.txt +420 -0
  18. package/dist/browser/chunks/chunk-3GJB6AVO.js +2 -0
  19. package/dist/browser/chunks/chunk-3GJB6AVO.js.map +7 -0
  20. package/dist/browser/chunks/chunk-D5WHCVBC.js +2 -0
  21. package/dist/browser/chunks/chunk-D5WHCVBC.js.map +7 -0
  22. package/dist/browser/chunks/chunk-WL4KEX7Y.js +2 -0
  23. package/dist/browser/chunks/chunk-WL4KEX7Y.js.map +7 -0
  24. package/dist/browser/chunks/mathjax-DNP3YRES.js +61 -0
  25. package/dist/browser/chunks/mathjax-DNP3YRES.js.map +7 -0
  26. package/dist/browser/index.js +2 -0
  27. package/dist/browser/index.js.map +7 -0
  28. package/dist/browser/kit.js +2 -0
  29. package/dist/browser/kit.js.map +7 -0
  30. package/dist/browser/manifest.json +30 -0
  31. package/dist/browser/tex.js +2 -0
  32. package/dist/browser/tex.js.map +7 -0
  33. package/dist/engine.d.ts +64 -0
  34. package/dist/engine.d.ts.map +1 -0
  35. package/dist/engine.js +42 -0
  36. package/dist/engine.js.map +1 -0
  37. package/dist/index.d.ts +30 -0
  38. package/dist/index.d.ts.map +1 -0
  39. package/dist/index.js +30 -0
  40. package/dist/index.js.map +1 -0
  41. package/dist/kit/brace.d.ts +8 -0
  42. package/dist/kit/brace.d.ts.map +1 -0
  43. package/dist/kit/brace.js +36 -0
  44. package/dist/kit/brace.js.map +1 -0
  45. package/dist/kit/cache.d.ts +30 -0
  46. package/dist/kit/cache.d.ts.map +1 -0
  47. package/dist/kit/cache.js +53 -0
  48. package/dist/kit/cache.js.map +1 -0
  49. package/dist/kit/contour.d.ts +8 -0
  50. package/dist/kit/contour.d.ts.map +1 -0
  51. package/dist/kit/contour.js +93 -0
  52. package/dist/kit/contour.js.map +1 -0
  53. package/dist/kit/coords.d.ts +69 -0
  54. package/dist/kit/coords.d.ts.map +1 -0
  55. package/dist/kit/coords.js +224 -0
  56. package/dist/kit/coords.js.map +1 -0
  57. package/dist/kit/curve.d.ts +17 -0
  58. package/dist/kit/curve.d.ts.map +1 -0
  59. package/dist/kit/curve.js +22 -0
  60. package/dist/kit/curve.js.map +1 -0
  61. package/dist/kit/equation.d.ts +61 -0
  62. package/dist/kit/equation.d.ts.map +1 -0
  63. package/dist/kit/equation.js +168 -0
  64. package/dist/kit/equation.js.map +1 -0
  65. package/dist/kit/equation3d.d.ts +50 -0
  66. package/dist/kit/equation3d.d.ts.map +1 -0
  67. package/dist/kit/equation3d.js +103 -0
  68. package/dist/kit/equation3d.js.map +1 -0
  69. package/dist/kit/expression.d.ts +35 -0
  70. package/dist/kit/expression.d.ts.map +1 -0
  71. package/dist/kit/expression.js +124 -0
  72. package/dist/kit/expression.js.map +1 -0
  73. package/dist/kit/field-mark.d.ts +58 -0
  74. package/dist/kit/field-mark.d.ts.map +1 -0
  75. package/dist/kit/field-mark.js +129 -0
  76. package/dist/kit/field-mark.js.map +1 -0
  77. package/dist/kit/field.d.ts +14 -0
  78. package/dist/kit/field.d.ts.map +1 -0
  79. package/dist/kit/field.js +46 -0
  80. package/dist/kit/field.js.map +1 -0
  81. package/dist/kit/follow.d.ts +71 -0
  82. package/dist/kit/follow.d.ts.map +1 -0
  83. package/dist/kit/follow.js +159 -0
  84. package/dist/kit/follow.js.map +1 -0
  85. package/dist/kit/furniture.d.ts +30 -0
  86. package/dist/kit/furniture.d.ts.map +1 -0
  87. package/dist/kit/furniture.js +87 -0
  88. package/dist/kit/furniture.js.map +1 -0
  89. package/dist/kit/geometry.d.ts +13 -0
  90. package/dist/kit/geometry.d.ts.map +1 -0
  91. package/dist/kit/geometry.js +54 -0
  92. package/dist/kit/geometry.js.map +1 -0
  93. package/dist/kit/mark.d.ts +47 -0
  94. package/dist/kit/mark.d.ts.map +1 -0
  95. package/dist/kit/mark.js +103 -0
  96. package/dist/kit/mark.js.map +1 -0
  97. package/dist/kit/part-fills.d.ts +23 -0
  98. package/dist/kit/part-fills.d.ts.map +1 -0
  99. package/dist/kit/part-fills.js +46 -0
  100. package/dist/kit/part-fills.js.map +1 -0
  101. package/dist/kit/place.d.ts +18 -0
  102. package/dist/kit/place.d.ts.map +1 -0
  103. package/dist/kit/place.js +68 -0
  104. package/dist/kit/place.js.map +1 -0
  105. package/dist/kit/placement.d.ts +8 -0
  106. package/dist/kit/placement.d.ts.map +1 -0
  107. package/dist/kit/placement.js +8 -0
  108. package/dist/kit/placement.js.map +1 -0
  109. package/dist/kit/sampler.d.ts +17 -0
  110. package/dist/kit/sampler.d.ts.map +1 -0
  111. package/dist/kit/sampler.js +250 -0
  112. package/dist/kit/sampler.js.map +1 -0
  113. package/dist/kit/scale.d.ts +12 -0
  114. package/dist/kit/scale.d.ts.map +1 -0
  115. package/dist/kit/scale.js +37 -0
  116. package/dist/kit/scale.js.map +1 -0
  117. package/dist/kit/source.d.ts +22 -0
  118. package/dist/kit/source.d.ts.map +1 -0
  119. package/dist/kit/source.js +45 -0
  120. package/dist/kit/source.js.map +1 -0
  121. package/dist/kit/system.d.ts +33 -0
  122. package/dist/kit/system.d.ts.map +1 -0
  123. package/dist/kit/system.js +67 -0
  124. package/dist/kit/system.js.map +1 -0
  125. package/dist/kit/ticks.d.ts +19 -0
  126. package/dist/kit/ticks.d.ts.map +1 -0
  127. package/dist/kit/ticks.js +78 -0
  128. package/dist/kit/ticks.js.map +1 -0
  129. package/dist/kit/withdrawn.d.ts +9 -0
  130. package/dist/kit/withdrawn.d.ts.map +1 -0
  131. package/dist/kit/withdrawn.js +9 -0
  132. package/dist/kit/withdrawn.js.map +1 -0
  133. package/dist/marks/curve-area.d.ts +32 -0
  134. package/dist/marks/curve-area.d.ts.map +1 -0
  135. package/dist/marks/curve-area.js +82 -0
  136. package/dist/marks/curve-area.js.map +1 -0
  137. package/dist/marks/drop-lines.d.ts +22 -0
  138. package/dist/marks/drop-lines.d.ts.map +1 -0
  139. package/dist/marks/drop-lines.js +58 -0
  140. package/dist/marks/drop-lines.js.map +1 -0
  141. package/dist/marks/plot-point.d.ts +35 -0
  142. package/dist/marks/plot-point.d.ts.map +1 -0
  143. package/dist/marks/plot-point.js +97 -0
  144. package/dist/marks/plot-point.js.map +1 -0
  145. package/dist/marks/riemann-sum.d.ts +37 -0
  146. package/dist/marks/riemann-sum.d.ts.map +1 -0
  147. package/dist/marks/riemann-sum.js +91 -0
  148. package/dist/marks/riemann-sum.js.map +1 -0
  149. package/dist/marks/tangent-line.d.ts +27 -0
  150. package/dist/marks/tangent-line.d.ts.map +1 -0
  151. package/dist/marks/tangent-line.js +103 -0
  152. package/dist/marks/tangent-line.js.map +1 -0
  153. package/dist/marks/vector-arrow.d.ts +32 -0
  154. package/dist/marks/vector-arrow.d.ts.map +1 -0
  155. package/dist/marks/vector-arrow.js +87 -0
  156. package/dist/marks/vector-arrow.js.map +1 -0
  157. package/dist/nodes.d.ts +35 -0
  158. package/dist/nodes.d.ts.map +1 -0
  159. package/dist/nodes.js +63 -0
  160. package/dist/nodes.js.map +1 -0
  161. package/dist/plots/function-plot.d.ts +67 -0
  162. package/dist/plots/function-plot.d.ts.map +1 -0
  163. package/dist/plots/function-plot.js +215 -0
  164. package/dist/plots/function-plot.js.map +1 -0
  165. package/dist/plots/points-plot.d.ts +28 -0
  166. package/dist/plots/points-plot.d.ts.map +1 -0
  167. package/dist/plots/points-plot.js +99 -0
  168. package/dist/plots/points-plot.js.map +1 -0
  169. package/dist/plots/stream-lines.d.ts +35 -0
  170. package/dist/plots/stream-lines.d.ts.map +1 -0
  171. package/dist/plots/stream-lines.js +141 -0
  172. package/dist/plots/stream-lines.js.map +1 -0
  173. package/dist/plots/traced-path.d.ts +31 -0
  174. package/dist/plots/traced-path.d.ts.map +1 -0
  175. package/dist/plots/traced-path.js +56 -0
  176. package/dist/plots/traced-path.js.map +1 -0
  177. package/dist/plots/vector-field.d.ts +23 -0
  178. package/dist/plots/vector-field.d.ts.map +1 -0
  179. package/dist/plots/vector-field.js +91 -0
  180. package/dist/plots/vector-field.js.map +1 -0
  181. package/dist/structures/graph-layout.d.ts +11 -0
  182. package/dist/structures/graph-layout.d.ts.map +1 -0
  183. package/dist/structures/graph-layout.js +135 -0
  184. package/dist/structures/graph-layout.js.map +1 -0
  185. package/dist/structures/network-graph.d.ts +83 -0
  186. package/dist/structures/network-graph.d.ts.map +1 -0
  187. package/dist/structures/network-graph.js +289 -0
  188. package/dist/structures/network-graph.js.map +1 -0
  189. package/dist/structures/sample-space.d.ts +62 -0
  190. package/dist/structures/sample-space.d.ts.map +1 -0
  191. package/dist/structures/sample-space.js +199 -0
  192. package/dist/structures/sample-space.js.map +1 -0
  193. package/dist/systems/axes2d.d.ts +75 -0
  194. package/dist/systems/axes2d.d.ts.map +1 -0
  195. package/dist/systems/axes2d.js +193 -0
  196. package/dist/systems/axes2d.js.map +1 -0
  197. package/dist/systems/number-line.d.ts +49 -0
  198. package/dist/systems/number-line.d.ts.map +1 -0
  199. package/dist/systems/number-line.js +127 -0
  200. package/dist/systems/number-line.js.map +1 -0
  201. package/dist/systems/number-plane.d.ts +59 -0
  202. package/dist/systems/number-plane.d.ts.map +1 -0
  203. package/dist/systems/number-plane.js +193 -0
  204. package/dist/systems/number-plane.js.map +1 -0
  205. package/dist/systems/polar-plane.d.ts +48 -0
  206. package/dist/systems/polar-plane.d.ts.map +1 -0
  207. package/dist/systems/polar-plane.js +165 -0
  208. package/dist/systems/polar-plane.js.map +1 -0
  209. package/dist/tex/geometry.d.ts +85 -0
  210. package/dist/tex/geometry.d.ts.map +1 -0
  211. package/dist/tex/geometry.js +632 -0
  212. package/dist/tex/geometry.js.map +1 -0
  213. package/dist/tex/index.d.ts +11 -0
  214. package/dist/tex/index.d.ts.map +1 -0
  215. package/dist/tex/index.js +9 -0
  216. package/dist/tex/index.js.map +1 -0
  217. package/dist/tex/mathjax.d.ts +33 -0
  218. package/dist/tex/mathjax.d.ts.map +1 -0
  219. package/dist/tex/mathjax.js +74 -0
  220. package/dist/tex/mathjax.js.map +1 -0
  221. package/dist/tex/matrix.d.ts +31 -0
  222. package/dist/tex/matrix.d.ts.map +1 -0
  223. package/dist/tex/matrix.js +73 -0
  224. package/dist/tex/matrix.js.map +1 -0
  225. package/dist/tex/node.d.ts +215 -0
  226. package/dist/tex/node.d.ts.map +1 -0
  227. package/dist/tex/node.js +595 -0
  228. package/dist/tex/node.js.map +1 -0
  229. package/dist/tex/tween.d.ts +50 -0
  230. package/dist/tex/tween.d.ts.map +1 -0
  231. package/dist/tex/tween.js +253 -0
  232. package/dist/tex/tween.js.map +1 -0
  233. package/dist/three/axes3d.d.ts +87 -0
  234. package/dist/three/axes3d.d.ts.map +1 -0
  235. package/dist/three/axes3d.js +228 -0
  236. package/dist/three/axes3d.js.map +1 -0
  237. package/dist/three/curve-plot3d.d.ts +60 -0
  238. package/dist/three/curve-plot3d.d.ts.map +1 -0
  239. package/dist/three/curve-plot3d.js +143 -0
  240. package/dist/three/curve-plot3d.js.map +1 -0
  241. package/dist/three/label3d.d.ts +11 -0
  242. package/dist/three/label3d.d.ts.map +1 -0
  243. package/dist/three/label3d.js +40 -0
  244. package/dist/three/label3d.js.map +1 -0
  245. package/dist/three/plot-point3d.d.ts +26 -0
  246. package/dist/three/plot-point3d.d.ts.map +1 -0
  247. package/dist/three/plot-point3d.js +60 -0
  248. package/dist/three/plot-point3d.js.map +1 -0
  249. package/dist/three/space.d.ts +28 -0
  250. package/dist/three/space.d.ts.map +1 -0
  251. package/dist/three/space.js +47 -0
  252. package/dist/three/space.js.map +1 -0
  253. package/dist/three/surface-plot3d.d.ts +80 -0
  254. package/dist/three/surface-plot3d.d.ts.map +1 -0
  255. package/dist/three/surface-plot3d.js +290 -0
  256. package/dist/three/surface-plot3d.js.map +1 -0
  257. package/dist/three/vector-arrow3d.d.ts +25 -0
  258. package/dist/three/vector-arrow3d.d.ts.map +1 -0
  259. package/dist/three/vector-arrow3d.js +72 -0
  260. package/dist/three/vector-arrow3d.js.map +1 -0
  261. package/package.json +79 -3
  262. package/registry.json +251 -0
  263. package/src/annotate/angle-mark.ts +147 -0
  264. package/src/annotate/curly-brace.ts +126 -0
  265. package/src/annotate/emphasis-mark.ts +89 -0
  266. package/src/engine.ts +72 -0
  267. package/src/index.ts +29 -0
  268. package/src/kit/brace.ts +37 -0
  269. package/src/kit/cache.ts +61 -0
  270. package/src/kit/contour.ts +94 -0
  271. package/src/kit/coords.ts +278 -0
  272. package/src/kit/curve.ts +29 -0
  273. package/src/kit/equation.ts +181 -0
  274. package/src/kit/equation3d.ts +137 -0
  275. package/src/kit/expression.ts +159 -0
  276. package/src/kit/field-mark.ts +155 -0
  277. package/src/kit/field.ts +50 -0
  278. package/src/kit/follow.ts +203 -0
  279. package/src/kit/furniture.ts +108 -0
  280. package/src/kit/geometry.ts +49 -0
  281. package/src/kit/mark.ts +131 -0
  282. package/src/kit/part-fills.ts +61 -0
  283. package/src/kit/place.ts +101 -0
  284. package/src/kit/placement.ts +8 -0
  285. package/src/kit/sampler.ts +281 -0
  286. package/src/kit/scale.ts +39 -0
  287. package/src/kit/source.ts +57 -0
  288. package/src/kit/system.ts +99 -0
  289. package/src/kit/ticks.ts +85 -0
  290. package/src/kit/withdrawn.ts +8 -0
  291. package/src/marks/curve-area.ts +89 -0
  292. package/src/marks/drop-lines.ts +63 -0
  293. package/src/marks/plot-point.ts +107 -0
  294. package/src/marks/riemann-sum.ts +98 -0
  295. package/src/marks/tangent-line.ts +103 -0
  296. package/src/marks/vector-arrow.ts +98 -0
  297. package/src/nodes.ts +63 -0
  298. package/src/plots/function-plot.ts +236 -0
  299. package/src/plots/points-plot.ts +107 -0
  300. package/src/plots/stream-lines.ts +151 -0
  301. package/src/plots/traced-path.ts +52 -0
  302. package/src/plots/vector-field.ts +88 -0
  303. package/src/structures/graph-layout.ts +145 -0
  304. package/src/structures/network-graph.ts +308 -0
  305. package/src/structures/sample-space.ts +209 -0
  306. package/src/systems/axes2d.ts +217 -0
  307. package/src/systems/number-line.ts +131 -0
  308. package/src/systems/number-plane.ts +203 -0
  309. package/src/systems/polar-plane.ts +176 -0
  310. package/src/tex/geometry.ts +703 -0
  311. package/src/tex/index.ts +14 -0
  312. package/src/tex/mathjax.ts +82 -0
  313. package/src/tex/matrix.ts +75 -0
  314. package/src/tex/node.ts +641 -0
  315. package/src/tex/tween.ts +308 -0
  316. package/src/three/axes3d.ts +240 -0
  317. package/src/three/curve-plot3d.ts +147 -0
  318. package/src/three/label3d.ts +50 -0
  319. package/src/three/plot-point3d.ts +66 -0
  320. package/src/three/space.ts +67 -0
  321. package/src/three/surface-plot3d.ts +323 -0
  322. package/src/three/vector-arrow3d.ts +77 -0
@@ -0,0 +1,103 @@
1
+ import {
2
+ node,
3
+ property,
4
+ strokeProperty,
5
+ withdraw,
6
+ type NodeConfig,
7
+ type PaintProps,
8
+ type Stroke,
9
+ type Vector2,
10
+ } from "@motionscript/sdk";
11
+ import type { CoordinateMap } from "../kit/coords";
12
+ import { Mark } from "../kit/mark";
13
+ import { curveOf, type Curve } from "../kit/curve";
14
+ import { FILL_PAINT } from "../kit/withdrawn";
15
+
16
+ export interface TangentLineProps extends PaintProps {
17
+ on?: string;
18
+ length?: number;
19
+ dx?: number;
20
+ legs?: boolean;
21
+ }
22
+
23
+ /** The step a slope is measured over, as a share of the system's width. */
24
+ const SLOPE_STEP = 1e-4;
25
+
26
+ /**
27
+ * The tangent to a plot at this node's `x`, a fixed length on screen. With a
28
+ * `dx` it is the secant through `x` and `x + dx` instead, and `legs` draws that
29
+ * secant's run and rise.
30
+ */
31
+ @node({
32
+ key: "tangentLine",
33
+ within: ["functionPlot"],
34
+ parentKey: "shape",
35
+ forkable: true,
36
+ layout: { children: "freeform", defaultWidthMode: "hug", defaultHeightMode: "hug", acceptsChildren: false },
37
+ })
38
+ @withdraw(...FILL_PAINT)
39
+ export class TangentLine extends Mark<TangentLineProps> {
40
+ @property({ default: "", kind: "nodeRef", nodeTypes: ["functionPlot"], animatable: false }) declare on: string;
41
+ @property({ default: 260, min: 0, unit: "px" }) declare length: number;
42
+ /** The second point's distance along x; zero is the tangent itself. */
43
+ @property({ default: 0, min: 0 }) declare dx: number;
44
+ @property({ default: false, animatable: false }) declare legs: boolean;
45
+
46
+ @strokeProperty({ default: { weight: 3, fill: "#f59e0b", align: "center", cap: "round", join: "round" } })
47
+ declare stroke: Stroke;
48
+
49
+ constructor(props?: NodeConfig<TangentLine, TangentLineProps>) {
50
+ super(props ?? {});
51
+ }
52
+
53
+ /** The slope drawn, in graph units: the curve's at `x`, or the secant's when there is a `dx`. */
54
+ get slope(): number {
55
+ const plot = curveOf(this, this.on);
56
+ if (!plot) return NaN;
57
+ if (this.dx > 0) return (plot.valueAt(this.x + this.dx) - plot.valueAt(this.x)) / this.dx;
58
+ return slopeAt(plot, this.x, 1e-6 * Math.max(1, Math.abs(this.x)));
59
+ }
60
+
61
+ protected trace(map: CoordinateMap): Vector2[][] {
62
+ const plot = curveOf(this, this.on);
63
+ if (!plot) return [];
64
+ const x = this.x;
65
+ const y = plot.valueAt(x);
66
+ if (!Number.isFinite(y)) return [];
67
+ const half = this.length / 2;
68
+
69
+ if (this.dx > 0) {
70
+ const y2 = plot.valueAt(x + this.dx);
71
+ if (!Number.isFinite(y2)) return [];
72
+ const a = map.toPoint(x, y);
73
+ const b = map.toPoint(x + this.dx, y2);
74
+ const span = Math.hypot(b.x - a.x, b.y - a.y);
75
+ if (span === 0) return [];
76
+ const reach = Math.max(half, span / 2 + 16);
77
+ const ux = (b.x - a.x) / span;
78
+ const uy = (b.y - a.y) / span;
79
+ const mx = (a.x + b.x) / 2;
80
+ const my = (a.y + b.y) / 2;
81
+ const secant = [{ x: mx - ux * reach, y: my - uy * reach }, { x: mx + ux * reach, y: my + uy * reach }];
82
+ return this.legs ? [secant, [a, map.toPoint(x + this.dx, y), b]] : [secant];
83
+ }
84
+
85
+ const [xMin, xMax] = map.xRange;
86
+ const h = Number.isFinite(xMax - xMin) ? (xMax - xMin) * SLOPE_STEP : 1e-4;
87
+ const before = plot.valueAt(x - h);
88
+ const after = plot.valueAt(x + h);
89
+ if (!Number.isFinite(before) || !Number.isFinite(after)) return [];
90
+ const p = map.toPoint(x, y);
91
+ const a = map.toPoint(x - h, before);
92
+ const b = map.toPoint(x + h, after);
93
+ const span = Math.hypot(b.x - a.x, b.y - a.y);
94
+ if (span === 0) return [];
95
+ const ux = (b.x - a.x) / span;
96
+ const uy = (b.y - a.y) / span;
97
+ return [[{ x: p.x - ux * half, y: p.y - uy * half }, { x: p.x + ux * half, y: p.y + uy * half }]];
98
+ }
99
+ }
100
+
101
+ function slopeAt(plot: Curve, x: number, h: number): number {
102
+ return (plot.valueAt(x + h) - plot.valueAt(x - h)) / (2 * h);
103
+ }
@@ -0,0 +1,98 @@
1
+ import {
2
+ command,
3
+ node,
4
+ property,
5
+ strokeProperty,
6
+ vector2Property,
7
+ withdraw,
8
+ type Command,
9
+ type CommandArgs,
10
+ type NodeConfig,
11
+ type NodeHandle,
12
+ type PaintProps,
13
+ type Stroke,
14
+ type Vector2,
15
+ } from "@motionscript/sdk";
16
+ import type { CoordinateMap } from "../kit/coords";
17
+ import { stated } from "../kit/geometry";
18
+ import { Mark } from "../kit/mark";
19
+ import { SYSTEMS } from "../kit/placement";
20
+ import { FILL_PAINT } from "../kit/withdrawn";
21
+
22
+ export interface VectorArrowProps extends PaintProps {
23
+ tail?: Vector2;
24
+ head?: Vector2;
25
+ bend?: number;
26
+ }
27
+
28
+ /** Degrees of arc per drawn segment of a bent arrow. */
29
+ const BEND_STEP = 4;
30
+
31
+ /**
32
+ * An arrow from `tail` to `head`, two graph points. Its arrowhead is the stroke's
33
+ * `endCap`, so a plain segment, a double arrow or a dashed one is a change of
34
+ * stroke. (`from` and `to` are not its names because every node has a `to`.)
35
+ */
36
+ @node({
37
+ key: "vectorArrow",
38
+ within: SYSTEMS,
39
+ parentKey: "shape",
40
+ forkable: true,
41
+ layout: { children: "freeform", defaultWidthMode: "hug", defaultHeightMode: "hug", acceptsChildren: false },
42
+ })
43
+ @withdraw(...FILL_PAINT)
44
+ export class VectorArrow extends Mark<VectorArrowProps> {
45
+ @vector2Property() declare tail: Vector2;
46
+ @vector2Property({ default: { x: 1, y: 1 } }) declare head: Vector2;
47
+ /** The arc the arrow sweeps, in degrees: zero is straight, positive bows to its left. */
48
+ @property({ default: 0, min: -180, max: 180, unit: "°" }) declare bend: number;
49
+
50
+ @strokeProperty({ default: { weight: 4, fill: "#e5e7eb", align: "center", endCap: "triangle" } })
51
+ declare stroke: Stroke;
52
+
53
+ constructor(props?: NodeConfig<VectorArrow, VectorArrowProps>) {
54
+ super(props ?? {});
55
+ }
56
+
57
+ /** Move either end, in graph coordinates. */
58
+ @command({ args: ["tail", "head"] })
59
+ endpointsTo(args: CommandArgs<{ tail?: Vector2; head?: Vector2 }> & { duration: number }): Command<VectorArrowProps> {
60
+ return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
61
+ }
62
+
63
+ protected trace(map: CoordinateMap): Vector2[][] {
64
+ const shift = this.shift();
65
+ const a = map.toPoint(this.tail.x + shift.x, this.tail.y + shift.y);
66
+ const b = map.toPoint(this.head.x + shift.x, this.head.y + shift.y);
67
+ const sweep = (this.bend * Math.PI) / 180;
68
+ const chord = Math.hypot(b.x - a.x, b.y - a.y);
69
+ if (Math.abs(sweep) < 1e-3 || chord === 0) return [[a, b]];
70
+
71
+ // The circle through both ends that subtends `sweep` at its centre.
72
+ const radius = chord / (2 * Math.sin(Math.abs(sweep) / 2));
73
+ const side = Math.sign(sweep);
74
+ const reach = radius * Math.cos(sweep / 2);
75
+ // The centre sits to the arrow's right for a positive sweep, so the arc bows to its left.
76
+ const cx = (a.x + b.x) / 2 + (side * (b.y - a.y) * reach) / chord;
77
+ const cy = (a.y + b.y) / 2 - (side * (b.x - a.x) * reach) / chord;
78
+ const from = Math.atan2(a.y - cy, a.x - cx);
79
+ const steps = Math.max(8, Math.ceil(Math.abs(this.bend) / BEND_STEP));
80
+ const run: Vector2[] = [];
81
+ for (let i = 0; i <= steps; i++) {
82
+ const angle = from - (sweep * i) / steps;
83
+ run.push({ x: cx + radius * Math.cos(angle), y: cy + radius * Math.sin(angle) });
84
+ }
85
+ return [run];
86
+ }
87
+
88
+ override _handles(): readonly NodeHandle[] {
89
+ return [
90
+ { prop: "tail", at: this.ownPoint(this.tail) },
91
+ { prop: "head", at: this.ownPoint(this.head) },
92
+ ];
93
+ }
94
+
95
+ override _handleValue(prop: string, at: Vector2): Vector2 | null {
96
+ return prop === "tail" || prop === "head" ? this.graphPoint(at) : null;
97
+ }
98
+ }
package/src/nodes.ts ADDED
@@ -0,0 +1,63 @@
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. A host registers them by handing the
31
+ * array to an engine (`nodes: NODES`); `@node()` only declares each key. Class
32
+ * values rather than side-effect imports, because the package is
33
+ * `sideEffects: false` and a document names a type by string.
34
+ */
35
+ export const NODES = [
36
+ Latex,
37
+ LatexMatrix,
38
+ NumberLine,
39
+ Axes2d,
40
+ NumberPlane,
41
+ FunctionPlot,
42
+ PointsPlot,
43
+ PlotPoint,
44
+ VectorArrow,
45
+ CurveArea,
46
+ RiemannSum,
47
+ TangentLine,
48
+ DropLines,
49
+ CurlyBrace,
50
+ AngleMark,
51
+ EmphasisMark,
52
+ Axes3d,
53
+ SurfacePlot3d,
54
+ CurvePlot3d,
55
+ PlotPoint3d,
56
+ VectorArrow3d,
57
+ PolarPlane,
58
+ VectorField,
59
+ StreamLines,
60
+ TracedPath,
61
+ NetworkGraph,
62
+ SampleSpace,
63
+ ];
@@ -0,0 +1,236 @@
1
+ import {
2
+ command,
3
+ node,
4
+ property,
5
+ strokeProperty,
6
+ withdraw,
7
+ type Command,
8
+ type CommandArgs,
9
+ type NodeConfig,
10
+ type PaintProps,
11
+ type Stroke,
12
+ type Vector2,
13
+ } from "@motionscript/sdk";
14
+ import { traceContour } from "../kit/contour";
15
+ import type { CoordinateMap, Interval } from "../kit/coords";
16
+ import type { Curve } from "../kit/curve";
17
+ import { equationParams, parseEquation, type Equation } from "../kit/equation";
18
+ import type { ExpressionScope } from "../kit/expression";
19
+ import { PLOT_COLOR } from "../kit/furniture";
20
+ import { stated } from "../kit/geometry";
21
+ import { Mark } from "../kit/mark";
22
+ import { sampleCurve, type CurveProjection } from "../kit/sampler";
23
+ import { lerpDomain, lerpParams, lerpSource, readSource, scopeFor, type PlotSource } from "../kit/source";
24
+ import { SYSTEMS } from "../kit/placement";
25
+ import { FILL_PAINT } from "../kit/withdrawn";
26
+
27
+ export interface FunctionPlotProps extends PaintProps {
28
+ expression?: string | PlotSource;
29
+ domain?: Interval | null;
30
+ params?: ExpressionScope;
31
+ }
32
+
33
+ const TAU = Math.PI * 2;
34
+
35
+ /**
36
+ * The curve of an equation, drawn in the coordinate system it is a child of.
37
+ * How the equation is written says what it is: see `Equation`. A mark put
38
+ * inside it reads it: the area under it, a tangent, a point that rides it.
39
+ */
40
+ @node({
41
+ key: "functionPlot",
42
+ within: SYSTEMS,
43
+ closed: true,
44
+ parentKey: "shape",
45
+ forkable: true,
46
+ layout: { children: "freeform", defaultWidthMode: "hug", defaultHeightMode: "hug", acceptsChildren: true },
47
+ seed: { expression: "sin(x)" },
48
+ })
49
+ @withdraw(...FILL_PAINT)
50
+ export class FunctionPlot<P extends FunctionPlotProps = FunctionPlotProps> extends Mark<P> implements Curve {
51
+ @property({ default: "", mapper: readSource, tween: lerpSource, kind: "equation" })
52
+ declare expression: PlotSource;
53
+
54
+ /**
55
+ * The span of the free variable that is drawn. Left out, a function runs the
56
+ * width of its system, a polar curve one turn and a parametric one from 0 to 1.
57
+ */
58
+ @property({ default: null, tween: lerpDomain, kind: "range", defaultMin: -5, defaultMax: 5 }) declare domain: Interval | null;
59
+
60
+ /**
61
+ * The equation's named values: any name in it that is not a variable, a
62
+ * function or a constant. One left unset is 1, and a formula can bind each
63
+ * to a variable.
64
+ */
65
+ @property({ default: {}, tween: lerpParams }) declare params: ExpressionScope;
66
+
67
+ @strokeProperty({ default: { weight: 3, fill: PLOT_COLOR, align: "center", cap: "round", join: "round" } })
68
+ declare stroke: Stroke;
69
+
70
+ constructor(props?: NodeConfig<FunctionPlot<P>, P>) {
71
+ super((props ?? {}) as NodeConfig<FunctionPlot<P>, P>);
72
+ }
73
+
74
+ /** Change the equation, the curve bending from the one into the other. */
75
+ @command({ args: ["expression"] })
76
+ morphTo(args: CommandArgs<{ expression: string }> & { duration: number }): Command<P> {
77
+ return this.to({ data: stated(args.data) as Partial<P>, duration: args.duration, easing: args.easing });
78
+ }
79
+
80
+ /** Animate the equation's named values. */
81
+ @command({ args: ["params"] })
82
+ paramsTo(args: CommandArgs<{ params: ExpressionScope }> & { duration: number }): Command<P> {
83
+ return this.to({ data: stated(args.data) as Partial<P>, duration: args.duration, easing: args.easing });
84
+ }
85
+
86
+ /** Animate the span that is drawn. */
87
+ @command({ args: ["domain"] })
88
+ domainTo(args: CommandArgs<{ domain: Interval | null }> & { duration: number }): Command<P> {
89
+ return this.to({ data: stated(args.data) as Partial<P>, duration: args.duration, easing: args.easing });
90
+ }
91
+
92
+ /**
93
+ * The curve's height at graph `x`, or `NaN` where it has none or is not a
94
+ * function of `x`. What a point, a tangent or an area riding this plot reads.
95
+ */
96
+ valueAt(x: number): number {
97
+ const { source, blend } = this.expression;
98
+ const shift = this.shift();
99
+ const value = this._heightOf(source, x - shift.x) + shift.y;
100
+ if (!blend) return value;
101
+ const from = this._heightOf(blend.from, x - shift.x) + shift.y;
102
+ return from + (value - from) * blend.t;
103
+ }
104
+
105
+ /** The span of the free variable that is drawn: `domain`, unless a node built on this one says otherwise. */
106
+ protected span(): Interval | null {
107
+ return this.domain;
108
+ }
109
+
110
+ protected trace(map: CoordinateMap): Vector2[][] {
111
+ const { area } = map;
112
+ if (!Number.isFinite(area.maxX - area.minX)) return [];
113
+ const { source, blend } = this.expression;
114
+ const shift = this.shift();
115
+ const key = [source, blend?.from, blend?.t, map.revision, this.span(), JSON.stringify(this.params), shift.x, shift.y].join("|");
116
+ if (key === this._key) return this._runs;
117
+ this._key = key;
118
+
119
+ // A curve with no parameter to walk cannot bend into another: it changes over half way.
120
+ const shown = blend && blend.t < 0.5 ? blend.from : source;
121
+ if (this._equation(source)?.kind === "implicit" || (blend && this._equation(blend.from)?.kind === "implicit")) {
122
+ this._runs = this._contour(shown, map, shift);
123
+ return this._runs;
124
+ }
125
+
126
+ let curve = this._curve(source, map, shift);
127
+ if (curve && blend) {
128
+ const from = this._curve(blend.from, map, shift);
129
+ if (from) curve = between(from, curve, blend.t);
130
+ }
131
+ const width = area.maxX - area.minX;
132
+ this._runs = curve
133
+ ? sampleCurve(curve, 0, 1, area, { seeds: Math.min(512, Math.max(64, Math.round(width / 8))), minStep: 0.5 / Math.max(1, width) })
134
+ : [];
135
+ return this._runs;
136
+ }
137
+
138
+ private _key = "";
139
+ private _runs: Vector2[][] = [];
140
+
141
+ private _equation(source: string): Equation | null {
142
+ const equation = parseEquation(source, equationParams(source));
143
+ return equation instanceof Error ? null : equation;
144
+ }
145
+
146
+ private _heightOf(source: string, x: number): number {
147
+ const equation = this._equation(source);
148
+ if (equation?.kind !== "function" || equation.of !== "x") return NaN;
149
+ return equation.f(x, 0, scopeFor(equationParams(source), this.params));
150
+ }
151
+
152
+ /** Where an implicit `source` holds, over what the system shows, as pixel runs. */
153
+ private _contour(source: string, map: CoordinateMap, shift: Vector2): Vector2[][] {
154
+ const equation = this._equation(source);
155
+ if (equation?.kind !== "implicit") return [];
156
+ const params = scopeFor(equationParams(source), this.params);
157
+ const { area } = map;
158
+ const cells = (pixels: number) => Math.min(CONTOUR_MAX, Math.max(8, Math.round(pixels / CONTOUR_CELL)));
159
+ const { f } = equation;
160
+ const runs = traceContour(
161
+ (x, y) => f(x - shift.x, y - shift.y, params),
162
+ map.xRange,
163
+ map.yRange,
164
+ cells(area.maxX - area.minX),
165
+ cells(area.maxY - area.minY),
166
+ );
167
+ return runs.map((run) => run.map((point) => map.toPoint(point.x, point.y)));
168
+ }
169
+
170
+ /** `source` as a pixel curve over a parameter running 0 to 1. */
171
+ private _curve(source: string, map: CoordinateMap, shift: Vector2): CurveProjection | null {
172
+ const equation = this._equation(source);
173
+ if (!equation || equation.kind === "implicit") return null;
174
+ const params = scopeFor(equationParams(source), this.params);
175
+ const domain = this.span();
176
+ switch (equation.kind) {
177
+ case "function": {
178
+ const horizontal = equation.of === "x";
179
+ const [lo, hi] = horizontal ? map.xRange : map.yRange;
180
+ const offset = horizontal ? shift.x : shift.y;
181
+ // Only what the system shows is sampled, however wide the domain.
182
+ const a = Math.max(lo - offset, domain?.[0] ?? -Infinity);
183
+ const b = Math.min(hi - offset, domain?.[1] ?? Infinity);
184
+ if (!(b > a)) return null;
185
+ const { f } = equation;
186
+ return (u, out) => {
187
+ const v = a + (b - a) * u;
188
+ const w = f(v, 0, params);
189
+ if (!Number.isFinite(w)) return false;
190
+ if (horizontal) map.toPoint(v + shift.x, w + shift.y, out);
191
+ else map.toPoint(w + shift.x, v + shift.y, out);
192
+ return true;
193
+ };
194
+ }
195
+ case "polar": {
196
+ const [a, b] = domain ?? [0, TAU];
197
+ const { f } = equation;
198
+ return (u, out) => {
199
+ const theta = a + (b - a) * u;
200
+ const r = f(theta, 0, params);
201
+ if (!Number.isFinite(r)) return false;
202
+ map.toPoint(r * Math.cos(theta) + shift.x, r * Math.sin(theta) + shift.y, out);
203
+ return true;
204
+ };
205
+ }
206
+ case "parametric": {
207
+ const [a, b] = domain ?? [0, 1];
208
+ const { x, y } = equation;
209
+ return (u, out) => {
210
+ const t = a + (b - a) * u;
211
+ const gx = x(t, 0, params);
212
+ const gy = y(t, 0, params);
213
+ if (!Number.isFinite(gx) || !Number.isFinite(gy)) return false;
214
+ map.toPoint(gx + shift.x, gy + shift.y, out);
215
+ return true;
216
+ };
217
+ }
218
+ }
219
+ }
220
+ }
221
+
222
+ // Roughly one cell of the contour's grid to this many pixels, each way.
223
+ const CONTOUR_CELL = 5;
224
+ const CONTOUR_MAX = 220;
225
+
226
+ // Two curves blended point for point along their parameters, in pixels, so any
227
+ // kind bends into any other.
228
+ function between(from: CurveProjection, to: CurveProjection, t: number): CurveProjection {
229
+ const a: Vector2 = { x: 0, y: 0 };
230
+ return (u, out) => {
231
+ if (!from(u, a) || !to(u, out)) return false;
232
+ out.x = a.x + (out.x - a.x) * t;
233
+ out.y = a.y + (out.y - a.y) * t;
234
+ return true;
235
+ };
236
+ }
@@ -0,0 +1,107 @@
1
+ import {
2
+ command,
3
+ node,
4
+ property,
5
+ strokeProperty,
6
+ type Command,
7
+ type CommandArgs,
8
+ type NodeConfig,
9
+ type PaintProps,
10
+ type Stroke,
11
+ type Vector2,
12
+ } from "@motionscript/sdk";
13
+ import type { CoordinateMap } from "../kit/coords";
14
+ import { stated } from "../kit/geometry";
15
+ import { Mark } from "../kit/mark";
16
+ import { PLOT_COLOR } from "../kit/furniture";
17
+ import { SYSTEMS } from "../kit/placement";
18
+
19
+ export interface PointsPlotProps extends PaintProps {
20
+ points?: Vector2[];
21
+ closed?: boolean;
22
+ smooth?: boolean;
23
+ }
24
+
25
+ // A shorter list is padded with its own last point, so a line grows new points out of its end.
26
+ function lerpPoints(from: Vector2[], to: Vector2[], t: number): Vector2[] {
27
+ const count = Math.max(from.length, to.length);
28
+ if (from.length === 0 || to.length === 0) return t < 1 ? from : to;
29
+ const out = new Array<Vector2>(count);
30
+ for (let i = 0; i < count; i++) {
31
+ const a = from[Math.min(i, from.length - 1)];
32
+ const b = to[Math.min(i, to.length - 1)];
33
+ out[i] = { x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t };
34
+ }
35
+ return out;
36
+ }
37
+
38
+ /** Points on the curve between each pair of a smooth line's points. */
39
+ const SMOOTH_STEPS = 16;
40
+
41
+ /** A line through listed graph points: data, or a shape drawn in graph coordinates when closed, which is when it is filled. */
42
+ @node({
43
+ key: "pointsPlot",
44
+ within: SYSTEMS,
45
+ parentKey: "shape",
46
+ forkable: true,
47
+ layout: { children: "freeform", defaultWidthMode: "hug", defaultHeightMode: "hug", acceptsChildren: false },
48
+ seed: { points: [{ x: -2, y: -1 }, { x: 0, y: 1 }, { x: 2, y: 0 }] },
49
+ })
50
+ export class PointsPlot extends Mark<PointsPlotProps> {
51
+ @property({ default: [], tween: lerpPoints }) declare points: Vector2[];
52
+ /** Join the last point to the first. */
53
+ @property({ default: false, animatable: false }) declare closed: boolean;
54
+ /** Curve through the points instead of turning a corner at each. */
55
+ @property({ default: false, animatable: false }) declare smooth: boolean;
56
+
57
+ @strokeProperty({ default: { weight: 3, fill: PLOT_COLOR, align: "center", cap: "round", join: "round" } })
58
+ declare stroke: Stroke;
59
+
60
+ constructor(props?: NodeConfig<PointsPlot, PointsPlotProps>) {
61
+ super(props ?? {});
62
+ }
63
+
64
+ /** Move the points to new places; extra ones grow from the line's end. */
65
+ @command({ args: ["points"] })
66
+ pointsTo(args: CommandArgs<{ points: Vector2[] }> & { duration: number }): Command<PointsPlotProps> {
67
+ return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
68
+ }
69
+
70
+ protected override isClosed(): boolean {
71
+ return this.closed;
72
+ }
73
+
74
+ // An open line has no inside, so its fill waits until it is closed.
75
+ protected override paintsFill(): boolean {
76
+ return this.closed;
77
+ }
78
+
79
+ protected trace(map: CoordinateMap): Vector2[][] {
80
+ const shift = this.shift();
81
+ const pixels = this.points.map((p) => map.toPoint(p.x + shift.x, p.y + shift.y));
82
+ if (pixels.length < 2) return [];
83
+ return [this.smooth && pixels.length > 2 ? spline(pixels, this.closed) : pixels];
84
+ }
85
+ }
86
+
87
+ // Catmull–Rom, so the curve passes through every point it was given.
88
+ function spline(points: readonly Vector2[], closed: boolean): Vector2[] {
89
+ const n = points.length;
90
+ const at = (i: number): Vector2 => (closed ? points[((i % n) + n) % n] : points[Math.max(0, Math.min(n - 1, i))]);
91
+ const out: Vector2[] = [];
92
+ const spans = closed ? n : n - 1;
93
+ for (let i = 0; i < spans; i++) {
94
+ const p0 = at(i - 1), p1 = at(i), p2 = at(i + 1), p3 = at(i + 2);
95
+ for (let s = 0; s < SMOOTH_STEPS; s++) {
96
+ const t = s / SMOOTH_STEPS;
97
+ const t2 = t * t;
98
+ const t3 = t2 * t;
99
+ out.push({
100
+ x: 0.5 * (2 * p1.x + (p2.x - p0.x) * t + (2 * p0.x - 5 * p1.x + 4 * p2.x - p3.x) * t2 + (3 * p1.x - p0.x - 3 * p2.x + p3.x) * t3),
101
+ y: 0.5 * (2 * p1.y + (p2.y - p0.y) * t + (2 * p0.y - 5 * p1.y + 4 * p2.y - p3.y) * t2 + (3 * p1.y - p0.y - 3 * p2.y + p3.y) * t3),
102
+ });
103
+ }
104
+ }
105
+ if (!closed) out.push(points[n - 1]);
106
+ return out;
107
+ }