@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,151 @@
1
+ import {
2
+ command,
3
+ node,
4
+ property,
5
+ strokeProperty,
6
+ type Command,
7
+ type CommandArgs,
8
+ type NodeConfig,
9
+ type Stroke,
10
+ type Vector2,
11
+ } from "@motionscript/sdk";
12
+ import type { CoordinateMap } from "../kit/coords";
13
+ import { FieldMark, type FieldProps, type FieldSampler, type FieldStroke } from "../kit/field-mark";
14
+ import { PLOT_COLOR } from "../kit/furniture";
15
+ import { clamp, stated } from "../kit/geometry";
16
+ import { SYSTEMS } from "../kit/placement";
17
+
18
+ export interface StreamLinesProps extends FieldProps {
19
+ spacing?: number;
20
+ length?: number;
21
+ window?: number;
22
+ phase?: number;
23
+ }
24
+
25
+ const MAX_LINES = 1600;
26
+ /** Steps a line takes across one spacing: what keeps a tight swirl round. */
27
+ const STEPS_PER_SPACING = 8;
28
+ const MAX_STEPS = 600;
29
+
30
+ /**
31
+ * The lines a vector field carries a point along, one from each point of a
32
+ * grid: `(-y, x)` draws circles. With a `window` below one, each line shows
33
+ * only a stretch of itself, and `flowTo` sends those stretches along.
34
+ */
35
+ @node({
36
+ key: "streamLines",
37
+ within: SYSTEMS,
38
+ parentKey: "shape",
39
+ forkable: true,
40
+ layout: { children: "freeform", defaultWidthMode: "hug", defaultHeightMode: "hug", acceptsChildren: false },
41
+ seed: { expression: "(sin(y), cos(x))" },
42
+ })
43
+ export class StreamLines extends FieldMark<StreamLinesProps> {
44
+ /** How far apart the lines start, in graph units. */
45
+ @property({ default: 1, min: 0.01, step: 0.1 }) declare spacing: number;
46
+ /** How far each line runs from where it starts, in graph units along itself. */
47
+ @property({ default: 3, min: 0, step: 0.1 }) declare length: number;
48
+ /** The share of each line that is shown at once; one shows all of it. */
49
+ @property({ default: 1, min: 0.01, max: 1, step: 0.01 }) declare window: number;
50
+ /** Where along its line each shown stretch is: one turn of it runs every line from end to end. */
51
+ @property({ default: 0, step: 0.01 }) declare phase: number;
52
+
53
+ @strokeProperty({ default: { weight: 2, fill: PLOT_COLOR, align: "center", cap: "round", join: "round" } })
54
+ declare stroke: Stroke;
55
+
56
+ constructor(props?: NodeConfig<StreamLines, StreamLinesProps>) {
57
+ super(props ?? {});
58
+ }
59
+
60
+ /** Send the shown stretches along their lines: a whole number runs each line that many times over. */
61
+ @command({ args: ["phase"] })
62
+ flowTo(args: CommandArgs<{ phase: number }> & { duration: number }): Command<StreamLinesProps> {
63
+ return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
64
+ }
65
+
66
+ protected own(): string {
67
+ return `${this.spacing}|${this.length}|${this.window}|${this.phase}`;
68
+ }
69
+
70
+ protected strokes(map: CoordinateMap, field: FieldSampler): FieldStroke[] {
71
+ const [x0, x1] = map.xRange;
72
+ const [y0, y1] = map.yRange;
73
+ let spacing = Math.max(this.spacing, 1e-6);
74
+ const count = () => (Math.floor((x1 - x0) / spacing) + 1) * (Math.floor((y1 - y0) / spacing) + 1);
75
+ while (count() > MAX_LINES) spacing *= 2;
76
+ const step = spacing / STEPS_PER_SPACING;
77
+ const steps = Math.min(MAX_STEPS, Math.round(this.length / step));
78
+ if (steps < 1) return [];
79
+ // A line is followed a little past what is shown, so it leaves the box rather than stopping at its edge.
80
+ const inside = (x: number, y: number) => x >= x0 - spacing && x <= x1 + spacing && y >= y0 - spacing && y <= y1 + spacing;
81
+ const window = clamp(this.window, 0, 1);
82
+
83
+ const drawn: FieldStroke[] = [];
84
+ let index = 0;
85
+ for (let i = Math.ceil(x0 / spacing - 1e-9); i * spacing <= x1 + 1e-9; i++) {
86
+ for (let j = Math.ceil(y0 / spacing - 1e-9); j * spacing <= y1 + 1e-9; j++) {
87
+ const line = follow(field, i * spacing, j * spacing, step, steps, inside);
88
+ index++;
89
+ if (line.points.length < 2) continue;
90
+ const shown = window >= 1 ? line.points : stretch(line.points, window, this.phase + stagger(index));
91
+ if (shown.length < 2) continue;
92
+ drawn.push({ runs: [shown.map((p) => map.toPoint(p.x, p.y))], magnitude: line.magnitude });
93
+ }
94
+ }
95
+ return drawn;
96
+ }
97
+ }
98
+
99
+ /** The graph points a field carries a point through from `(x, y)`, at an even pace, and how strong it was on the way. */
100
+ function follow(
101
+ field: FieldSampler,
102
+ x: number,
103
+ y: number,
104
+ step: number,
105
+ steps: number,
106
+ inside: (x: number, y: number) => boolean,
107
+ ): { points: Vector2[]; magnitude: number } {
108
+ const points: Vector2[] = [{ x, y }];
109
+ const v: Vector2 = { x: 0, y: 0 };
110
+ let strength = 0;
111
+ let read = 0;
112
+ // The field's direction at a point, as a unit step; `false` where it has none or is at rest.
113
+ const heading = (px: number, py: number, out: Vector2): boolean => {
114
+ if (!field(px, py, v)) return false;
115
+ const magnitude = Math.hypot(v.x, v.y);
116
+ if (!(magnitude > 1e-9)) return false;
117
+ out.x = v.x / magnitude;
118
+ out.y = v.y / magnitude;
119
+ strength += magnitude;
120
+ read++;
121
+ return true;
122
+ };
123
+ const a: Vector2 = { x: 0, y: 0 };
124
+ const b: Vector2 = { x: 0, y: 0 };
125
+ for (let i = 0; i < steps; i++) {
126
+ // Midpoint rule: the direction half a step on is the one the whole step takes.
127
+ if (!heading(x, y, a) || !heading(x + (a.x * step) / 2, y + (a.y * step) / 2, b)) break;
128
+ x += b.x * step;
129
+ y += b.y * step;
130
+ if (!inside(x, y)) break;
131
+ points.push({ x, y });
132
+ }
133
+ return { points, magnitude: read > 0 ? strength / read : 0 };
134
+ }
135
+
136
+ /** The part of `points` a window `share` long shows at `phase`: it enters at one end and leaves by the other. */
137
+ function stretch(points: Vector2[], share: number, phase: number): Vector2[] {
138
+ const turn = phase - Math.floor(phase);
139
+ const end = turn * (1 + share);
140
+ const from = Math.max(0, end - share);
141
+ const to = Math.min(1, end);
142
+ if (!(to > from)) return [];
143
+ const last = points.length - 1;
144
+ return points.slice(Math.floor(from * last), Math.ceil(to * last) + 1);
145
+ }
146
+
147
+ /** A share of a turn that differs from line to line and never from frame to frame, so the stretches do not march in step. */
148
+ function stagger(index: number): number {
149
+ const mixed = Math.sin(index * 12.9898) * 43758.5453;
150
+ return mixed - Math.floor(mixed);
151
+ }
@@ -0,0 +1,52 @@
1
+ import { command, node, property, withdraw, type Command, type CommandArgs, type NodeConfig } from "@motionscript/sdk";
2
+ import type { Interval } from "../kit/coords";
3
+ import { stated } from "../kit/geometry";
4
+ import { SYSTEMS } from "../kit/placement";
5
+ import { FunctionPlot, type FunctionPlotProps } from "./function-plot";
6
+ import { OUTLINE_TRIM } from "../kit/withdrawn";
7
+
8
+ export interface TracedPathProps extends FunctionPlotProps {
9
+ from?: number;
10
+ value?: number;
11
+ tail?: number;
12
+ }
13
+
14
+ /**
15
+ * The trail a point leaves as a value changes: the curve of its equation from
16
+ * where the value began to where it is now. Bind `value` to a variable
17
+ * (`"=[t]"`) and the trail grows as the variable is driven, or drive it with
18
+ * `traceTo`; a `tail` keeps only its most recent stretch.
19
+ */
20
+ @node({
21
+ key: "tracedPath",
22
+ within: SYSTEMS,
23
+ parentKey: "functionPlot",
24
+ forkable: true,
25
+ layout: { children: "freeform", defaultWidthMode: "hug", defaultHeightMode: "hug", acceptsChildren: false },
26
+ seed: { expression: "(2cos(t), sin(2t))", value: 6.28 },
27
+ })
28
+ @withdraw(...OUTLINE_TRIM)
29
+ export class TracedPath extends FunctionPlot<TracedPathProps> {
30
+ /** The value the trail starts at. */
31
+ @property({ default: 0, step: 0.1 }) declare from: number;
32
+ /** The value the point is at now: where the trail ends. */
33
+ @property({ default: 0, step: 0.1 }) declare value: number;
34
+ /** How much of the trail is kept, in the value's own units; zero keeps all of it. */
35
+ @property({ default: 0, min: 0, step: 0.1 }) declare tail: number;
36
+
37
+ constructor(props?: NodeConfig<TracedPath, TracedPathProps>) {
38
+ super((props ?? {}) as NodeConfig<FunctionPlot<TracedPathProps>, TracedPathProps>);
39
+ }
40
+
41
+ /** Carry the point on to another value, the trail following it. */
42
+ @command({ args: ["value"] })
43
+ traceTo(args: CommandArgs<{ value: number }> & { duration: number }): Command<TracedPathProps> {
44
+ return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
45
+ }
46
+
47
+ protected override span(): Interval | null {
48
+ const { from, value, tail } = this;
49
+ const kept = tail > 0 ? Math.min(Math.abs(value - from), tail) : Math.abs(value - from);
50
+ return value >= from ? [value - kept, value] : [value, value + kept];
51
+ }
52
+ }
@@ -0,0 +1,88 @@
1
+ import { node, property, strokeProperty, type NodeConfig, type Stroke, type Vector2 } from "@motionscript/sdk";
2
+ import type { CoordinateMap } from "../kit/coords";
3
+ import { FieldMark, type FieldProps, type FieldSampler, type FieldStroke } from "../kit/field-mark";
4
+ import { PLOT_COLOR } from "../kit/furniture";
5
+ import { SYSTEMS } from "../kit/placement";
6
+
7
+ export interface VectorFieldProps extends FieldProps {
8
+ spacing?: number;
9
+ arrowScale?: number;
10
+ }
11
+
12
+ /** Past this many arrows the spacing is widened, so a field over a huge range is still a field and not a wash. */
13
+ const MAX_ARROWS = 2400;
14
+ /** An arrow's head, as a share of its length and in pixels at most, and the half-angle it opens to. */
15
+ const HEAD_SHARE = 0.38;
16
+ const HEAD_MAX = 11;
17
+ const HEAD_ANGLE = (26 * Math.PI) / 180;
18
+
19
+ /**
20
+ * A grid of arrows showing a vector field, two parts in x and y: `(-y, x)`.
21
+ * Each arrow is centred on its point and is as long as the field is strong
22
+ * there, up to the spacing.
23
+ */
24
+ @node({
25
+ key: "vectorField",
26
+ within: SYSTEMS,
27
+ parentKey: "shape",
28
+ forkable: true,
29
+ layout: { children: "freeform", defaultWidthMode: "hug", defaultHeightMode: "hug", acceptsChildren: false },
30
+ seed: { expression: "(-y, x)" },
31
+ })
32
+ export class VectorField extends FieldMark<VectorFieldProps> {
33
+ /** How far apart the arrows stand, in graph units. */
34
+ @property({ default: 1, min: 0.01, step: 0.1 }) declare spacing: number;
35
+ /** How long an arrow is drawn for a vector of length one, in graph units. */
36
+ @property({ default: 0.4, min: 0, step: 0.05 }) declare arrowScale: number;
37
+
38
+ @strokeProperty({ default: { weight: 2.5, fill: PLOT_COLOR, align: "center", cap: "round", join: "round" } })
39
+ declare stroke: Stroke;
40
+
41
+ constructor(props?: NodeConfig<VectorField, VectorFieldProps>) {
42
+ super(props ?? {});
43
+ }
44
+
45
+ protected own(): string {
46
+ return `${this.spacing}|${this.arrowScale}`;
47
+ }
48
+
49
+ protected strokes(map: CoordinateMap, field: FieldSampler): FieldStroke[] {
50
+ const [x0, x1] = map.xRange;
51
+ const [y0, y1] = map.yRange;
52
+ let spacing = Math.max(this.spacing, 1e-6);
53
+ const count = () => (Math.floor((x1 - x0) / spacing) + 1) * (Math.floor((y1 - y0) / spacing) + 1);
54
+ while (count() > MAX_ARROWS) spacing *= 2;
55
+
56
+ const drawn: FieldStroke[] = [];
57
+ const v: Vector2 = { x: 0, y: 0 };
58
+ const longest = spacing * 0.9;
59
+ for (let i = Math.ceil(x0 / spacing - 1e-9); i * spacing <= x1 + 1e-9; i++) {
60
+ for (let j = Math.ceil(y0 / spacing - 1e-9); j * spacing <= y1 + 1e-9; j++) {
61
+ const gx = i * spacing;
62
+ const gy = j * spacing;
63
+ if (!field(gx, gy, v)) continue;
64
+ const magnitude = Math.hypot(v.x, v.y);
65
+ const length = Math.min(magnitude * this.arrowScale, longest);
66
+ if (!(length > 0)) continue;
67
+ const hx = ((v.x / magnitude) * length) / 2;
68
+ const hy = ((v.y / magnitude) * length) / 2;
69
+ const tail = map.toPoint(gx - hx, gy - hy);
70
+ const head = map.toPoint(gx + hx, gy + hy);
71
+ drawn.push({ runs: arrow(tail, head), magnitude });
72
+ }
73
+ }
74
+ return drawn;
75
+ }
76
+ }
77
+
78
+ /** A shaft and the two barbs of its head, in pixels. */
79
+ function arrow(tail: Vector2, head: Vector2): Vector2[][] {
80
+ const dx = head.x - tail.x;
81
+ const dy = head.y - tail.y;
82
+ const length = Math.hypot(dx, dy);
83
+ if (!(length > 0)) return [];
84
+ const barb = Math.min(length * HEAD_SHARE, HEAD_MAX);
85
+ const back = Math.atan2(dy, dx) + Math.PI;
86
+ const at = (angle: number): Vector2 => ({ x: head.x + Math.cos(angle) * barb, y: head.y + Math.sin(angle) * barb });
87
+ return [[tail, head], [at(back - HEAD_ANGLE), head, at(back + HEAD_ANGLE)]];
88
+ }
@@ -0,0 +1,145 @@
1
+ import type { Vector2 } from "@motionscript/sdk";
2
+
3
+ /** Where a graph's vertices are placed, inside a square from -1 to 1 each way, y up. */
4
+
5
+ export type GraphLayout = "circular" | "spring" | "tree" | "grid";
6
+
7
+ export const GRAPH_LAYOUTS: readonly GraphLayout[] = ["circular", "spring", "tree", "grid"];
8
+
9
+ export interface GraphEdge {
10
+ from: string;
11
+ to: string;
12
+ }
13
+
14
+ /** Each vertex's place under `layout`, by name. */
15
+ export function layOut(layout: GraphLayout, vertices: readonly string[], edges: readonly GraphEdge[], root: string): Map<string, Vector2> {
16
+ const n = vertices.length;
17
+ if (n === 0) return new Map();
18
+ if (n === 1) return new Map([[vertices[0], { x: 0, y: 0 }]]);
19
+ switch (layout) {
20
+ case "grid":
21
+ return grid(vertices);
22
+ case "tree":
23
+ return fill(tree(vertices, edges, root));
24
+ case "spring":
25
+ return fill(spring(vertices, edges));
26
+ default:
27
+ return circular(vertices);
28
+ }
29
+ }
30
+
31
+ function circular(vertices: readonly string[]): Map<string, Vector2> {
32
+ const n = vertices.length;
33
+ // The first at the top, the rest clockwise, as a clock is read.
34
+ return new Map(vertices.map((name, i) => {
35
+ const angle = Math.PI / 2 - (2 * Math.PI * i) / n;
36
+ return [name, { x: Math.cos(angle), y: Math.sin(angle) }];
37
+ }));
38
+ }
39
+
40
+ function grid(vertices: readonly string[]): Map<string, Vector2> {
41
+ const columns = Math.ceil(Math.sqrt(vertices.length));
42
+ const rows = Math.ceil(vertices.length / columns);
43
+ const at = (i: number, count: number) => (count === 1 ? 0 : -1 + (2 * i) / (count - 1));
44
+ return new Map(vertices.map((name, i) => [name, { x: at(i % columns, columns), y: -at(Math.floor(i / columns), rows) }]));
45
+ }
46
+
47
+ function neighbours(vertices: readonly string[], edges: readonly GraphEdge[]): Map<string, string[]> {
48
+ const near = new Map(vertices.map((name) => [name, [] as string[]]));
49
+ for (const { from, to } of edges) {
50
+ if (from === to || !near.has(from) || !near.has(to)) continue;
51
+ near.get(from)!.push(to);
52
+ near.get(to)!.push(from);
53
+ }
54
+ return near;
55
+ }
56
+
57
+ /** Levels down from `root`, and across by the leaves under each, every component its own tree beside the last. */
58
+ function tree(vertices: readonly string[], edges: readonly GraphEdge[], root: string): Map<string, Vector2> {
59
+ const near = neighbours(vertices, edges);
60
+ const placed = new Map<string, Vector2>();
61
+ let column = 0;
62
+ const visit = (name: string, depth: number, seen: Set<string>): number => {
63
+ seen.add(name);
64
+ const children = near.get(name)!.filter((next) => !seen.has(next));
65
+ for (const child of children) seen.add(child);
66
+ if (children.length === 0) {
67
+ placed.set(name, { x: column++, y: -depth });
68
+ return placed.get(name)!.x;
69
+ }
70
+ const xs = children.map((child) => visit(child, depth + 1, seen));
71
+ const x = (xs[0] + xs[xs.length - 1]) / 2;
72
+ placed.set(name, { x, y: -depth });
73
+ return x;
74
+ };
75
+ const seen = new Set<string>();
76
+ const first = near.has(root) ? root : vertices[0];
77
+ for (const start of [first, ...vertices]) if (!seen.has(start)) visit(start, 0, seen);
78
+ return placed;
79
+ }
80
+
81
+ /** Edges pull and every pair pushes, from the circle, for a fixed number of cooling steps: the same answer every time. */
82
+ function spring(vertices: readonly string[], edges: readonly GraphEdge[]): Map<string, Vector2> {
83
+ const n = vertices.length;
84
+ const index = new Map(vertices.map((name, i) => [name, i]));
85
+ const start = circular(vertices);
86
+ const x = vertices.map((name) => start.get(name)!.x);
87
+ const y = vertices.map((name) => start.get(name)!.y);
88
+ const links = edges.flatMap(({ from, to }) => {
89
+ const a = index.get(from);
90
+ const b = index.get(to);
91
+ return a === undefined || b === undefined || a === b ? [] : [[a, b] as const];
92
+ });
93
+ const k = Math.sqrt(4 / n);
94
+ const steps = 160;
95
+ for (let step = 0; step < steps; step++) {
96
+ const dx = new Float64Array(n);
97
+ const dy = new Float64Array(n);
98
+ for (let i = 0; i < n; i++) {
99
+ for (let j = i + 1; j < n; j++) {
100
+ let ex = x[i] - x[j];
101
+ let ey = y[i] - y[j];
102
+ let d = Math.hypot(ex, ey);
103
+ // Two on one spot are parted along a fixed, if arbitrary, line.
104
+ if (d < 1e-6) [ex, ey, d] = [1e-3 * (i - j), 1e-3, 1e-3];
105
+ const push = (k * k) / d;
106
+ dx[i] += (ex / d) * push;
107
+ dy[i] += (ey / d) * push;
108
+ dx[j] -= (ex / d) * push;
109
+ dy[j] -= (ey / d) * push;
110
+ }
111
+ }
112
+ for (const [a, b] of links) {
113
+ const ex = x[a] - x[b];
114
+ const ey = y[a] - y[b];
115
+ const d = Math.max(Math.hypot(ex, ey), 1e-6);
116
+ const pull = (d * d) / k;
117
+ dx[a] -= (ex / d) * pull;
118
+ dy[a] -= (ey / d) * pull;
119
+ dx[b] += (ex / d) * pull;
120
+ dy[b] += (ey / d) * pull;
121
+ }
122
+ const limit = 0.12 * (1 - step / steps);
123
+ for (let i = 0; i < n; i++) {
124
+ const d = Math.hypot(dx[i], dy[i]);
125
+ if (d > 0) {
126
+ x[i] += (dx[i] / d) * Math.min(d, limit);
127
+ y[i] += (dy[i] / d) * Math.min(d, limit);
128
+ }
129
+ }
130
+ }
131
+ return new Map(vertices.map((name, i) => [name, { x: x[i], y: y[i] }]));
132
+ }
133
+
134
+ /** `placed` stretched to reach -1 and 1 on each axis it spreads along, and centred on the one it does not. */
135
+ function fill(placed: Map<string, Vector2>): Map<string, Vector2> {
136
+ let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
137
+ for (const p of placed.values()) {
138
+ minX = Math.min(minX, p.x);
139
+ maxX = Math.max(maxX, p.x);
140
+ minY = Math.min(minY, p.y);
141
+ maxY = Math.max(maxY, p.y);
142
+ }
143
+ const scale = (v: number, lo: number, hi: number) => (hi - lo < 1e-9 ? 0 : -1 + (2 * (v - lo)) / (hi - lo));
144
+ return new Map([...placed].map(([name, p]) => [name, { x: scale(p.x, minX, maxX), y: scale(p.y, minY, maxY) }]));
145
+ }