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

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 (335) hide show
  1. package/CHANGELOG.md +144 -0
  2. package/LICENSE +201 -0
  3. package/NOTICE +49 -0
  4. package/README.md +389 -2
  5. package/dist/annotate/angle-mark.d.ts +96 -0
  6. package/dist/annotate/angle-mark.d.ts.map +1 -0
  7. package/dist/annotate/angle-mark.js +97 -0
  8. package/dist/annotate/angle-mark.js.map +1 -0
  9. package/dist/annotate/curly-brace.d.ts +115 -0
  10. package/dist/annotate/curly-brace.d.ts.map +1 -0
  11. package/dist/annotate/curly-brace.js +99 -0
  12. package/dist/annotate/curly-brace.js.map +1 -0
  13. package/dist/annotate/emphasis-mark.d.ts +80 -0
  14. package/dist/annotate/emphasis-mark.d.ts.map +1 -0
  15. package/dist/annotate/emphasis-mark.js +80 -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-IRZCG6PM.js +2 -0
  19. package/dist/browser/chunks/chunk-IRZCG6PM.js.map +7 -0
  20. package/dist/browser/chunks/chunk-WMVW6ZIV.js +2 -0
  21. package/dist/browser/chunks/chunk-WMVW6ZIV.js.map +7 -0
  22. package/dist/browser/chunks/mathjax-WV55AC2P.js +61 -0
  23. package/dist/browser/chunks/mathjax-WV55AC2P.js.map +7 -0
  24. package/dist/browser/index.js +2 -0
  25. package/dist/browser/index.js.map +7 -0
  26. package/dist/browser/kit.js +2 -0
  27. package/dist/browser/kit.js.map +7 -0
  28. package/dist/browser/manifest.json +30 -0
  29. package/dist/browser/tex.js +2 -0
  30. package/dist/browser/tex.js.map +7 -0
  31. package/dist/engine.d.ts +71 -0
  32. package/dist/engine.d.ts.map +1 -0
  33. package/dist/engine.js +45 -0
  34. package/dist/engine.js.map +1 -0
  35. package/dist/index.d.ts +30 -0
  36. package/dist/index.d.ts.map +1 -0
  37. package/dist/index.js +30 -0
  38. package/dist/index.js.map +1 -0
  39. package/dist/kit/brace.d.ts +8 -0
  40. package/dist/kit/brace.d.ts.map +1 -0
  41. package/dist/kit/brace.js +36 -0
  42. package/dist/kit/brace.js.map +1 -0
  43. package/dist/kit/cache.d.ts +30 -0
  44. package/dist/kit/cache.d.ts.map +1 -0
  45. package/dist/kit/cache.js +53 -0
  46. package/dist/kit/cache.js.map +1 -0
  47. package/dist/kit/component.d.ts +51 -0
  48. package/dist/kit/component.d.ts.map +1 -0
  49. package/dist/kit/component.js +30 -0
  50. package/dist/kit/component.js.map +1 -0
  51. package/dist/kit/contour.d.ts +8 -0
  52. package/dist/kit/contour.d.ts.map +1 -0
  53. package/dist/kit/contour.js +93 -0
  54. package/dist/kit/contour.js.map +1 -0
  55. package/dist/kit/coords.d.ts +69 -0
  56. package/dist/kit/coords.d.ts.map +1 -0
  57. package/dist/kit/coords.js +224 -0
  58. package/dist/kit/coords.js.map +1 -0
  59. package/dist/kit/curve.d.ts +28 -0
  60. package/dist/kit/curve.d.ts.map +1 -0
  61. package/dist/kit/curve.js +29 -0
  62. package/dist/kit/curve.js.map +1 -0
  63. package/dist/kit/equation.d.ts +61 -0
  64. package/dist/kit/equation.d.ts.map +1 -0
  65. package/dist/kit/equation.js +168 -0
  66. package/dist/kit/equation.js.map +1 -0
  67. package/dist/kit/equation3d.d.ts +50 -0
  68. package/dist/kit/equation3d.d.ts.map +1 -0
  69. package/dist/kit/equation3d.js +103 -0
  70. package/dist/kit/equation3d.js.map +1 -0
  71. package/dist/kit/expression.d.ts +35 -0
  72. package/dist/kit/expression.d.ts.map +1 -0
  73. package/dist/kit/expression.js +124 -0
  74. package/dist/kit/expression.js.map +1 -0
  75. package/dist/kit/field-mark.d.ts +79 -0
  76. package/dist/kit/field-mark.d.ts.map +1 -0
  77. package/dist/kit/field-mark.js +109 -0
  78. package/dist/kit/field-mark.js.map +1 -0
  79. package/dist/kit/field.d.ts +14 -0
  80. package/dist/kit/field.d.ts.map +1 -0
  81. package/dist/kit/field.js +46 -0
  82. package/dist/kit/field.js.map +1 -0
  83. package/dist/kit/follow.d.ts +109 -0
  84. package/dist/kit/follow.d.ts.map +1 -0
  85. package/dist/kit/follow.js +163 -0
  86. package/dist/kit/follow.js.map +1 -0
  87. package/dist/kit/furniture.d.ts +32 -0
  88. package/dist/kit/furniture.d.ts.map +1 -0
  89. package/dist/kit/furniture.js +92 -0
  90. package/dist/kit/furniture.js.map +1 -0
  91. package/dist/kit/geometry.d.ts +13 -0
  92. package/dist/kit/geometry.d.ts.map +1 -0
  93. package/dist/kit/geometry.js +54 -0
  94. package/dist/kit/geometry.js.map +1 -0
  95. package/dist/kit/graph.d.ts +46 -0
  96. package/dist/kit/graph.d.ts.map +1 -0
  97. package/dist/kit/graph.js +90 -0
  98. package/dist/kit/graph.js.map +1 -0
  99. package/dist/kit/mark.d.ts +72 -0
  100. package/dist/kit/mark.d.ts.map +1 -0
  101. package/dist/kit/mark.js +87 -0
  102. package/dist/kit/mark.js.map +1 -0
  103. package/dist/kit/part-fills.d.ts +23 -0
  104. package/dist/kit/part-fills.d.ts.map +1 -0
  105. package/dist/kit/part-fills.js +46 -0
  106. package/dist/kit/part-fills.js.map +1 -0
  107. package/dist/kit/place.d.ts +20 -0
  108. package/dist/kit/place.d.ts.map +1 -0
  109. package/dist/kit/place.js +66 -0
  110. package/dist/kit/place.js.map +1 -0
  111. package/dist/kit/sampler.d.ts +17 -0
  112. package/dist/kit/sampler.d.ts.map +1 -0
  113. package/dist/kit/sampler.js +250 -0
  114. package/dist/kit/sampler.js.map +1 -0
  115. package/dist/kit/scale.d.ts +12 -0
  116. package/dist/kit/scale.d.ts.map +1 -0
  117. package/dist/kit/scale.js +37 -0
  118. package/dist/kit/scale.js.map +1 -0
  119. package/dist/kit/source.d.ts +22 -0
  120. package/dist/kit/source.d.ts.map +1 -0
  121. package/dist/kit/source.js +45 -0
  122. package/dist/kit/source.js.map +1 -0
  123. package/dist/kit/system.d.ts +31 -0
  124. package/dist/kit/system.d.ts.map +1 -0
  125. package/dist/kit/system.js +71 -0
  126. package/dist/kit/system.js.map +1 -0
  127. package/dist/kit/ticks.d.ts +19 -0
  128. package/dist/kit/ticks.d.ts.map +1 -0
  129. package/dist/kit/ticks.js +78 -0
  130. package/dist/kit/ticks.js.map +1 -0
  131. package/dist/kit/withdrawn.d.ts +9 -0
  132. package/dist/kit/withdrawn.d.ts.map +1 -0
  133. package/dist/kit/withdrawn.js +9 -0
  134. package/dist/kit/withdrawn.js.map +1 -0
  135. package/dist/marks/curve-area.d.ts +51 -0
  136. package/dist/marks/curve-area.d.ts.map +1 -0
  137. package/dist/marks/curve-area.js +59 -0
  138. package/dist/marks/curve-area.js.map +1 -0
  139. package/dist/marks/drop-lines.d.ts +57 -0
  140. package/dist/marks/drop-lines.d.ts.map +1 -0
  141. package/dist/marks/drop-lines.js +43 -0
  142. package/dist/marks/drop-lines.js.map +1 -0
  143. package/dist/marks/plot-point.d.ts +71 -0
  144. package/dist/marks/plot-point.d.ts.map +1 -0
  145. package/dist/marks/plot-point.js +71 -0
  146. package/dist/marks/plot-point.js.map +1 -0
  147. package/dist/marks/riemann-sum.d.ts +58 -0
  148. package/dist/marks/riemann-sum.d.ts.map +1 -0
  149. package/dist/marks/riemann-sum.js +63 -0
  150. package/dist/marks/riemann-sum.js.map +1 -0
  151. package/dist/marks/tangent-line.d.ts +71 -0
  152. package/dist/marks/tangent-line.d.ts.map +1 -0
  153. package/dist/marks/tangent-line.js +89 -0
  154. package/dist/marks/tangent-line.js.map +1 -0
  155. package/dist/marks/vector-arrow.d.ts +60 -0
  156. package/dist/marks/vector-arrow.d.ts.map +1 -0
  157. package/dist/marks/vector-arrow.js +67 -0
  158. package/dist/marks/vector-arrow.js.map +1 -0
  159. package/dist/nodes.d.ts +43 -0
  160. package/dist/nodes.d.ts.map +1 -0
  161. package/dist/nodes.js +57 -0
  162. package/dist/nodes.js.map +1 -0
  163. package/dist/plots/function-plot.d.ts +159 -0
  164. package/dist/plots/function-plot.d.ts.map +1 -0
  165. package/dist/plots/function-plot.js +184 -0
  166. package/dist/plots/function-plot.js.map +1 -0
  167. package/dist/plots/points-plot.d.ts +64 -0
  168. package/dist/plots/points-plot.d.ts.map +1 -0
  169. package/dist/plots/points-plot.js +72 -0
  170. package/dist/plots/points-plot.js.map +1 -0
  171. package/dist/plots/stream-lines.d.ts +76 -0
  172. package/dist/plots/stream-lines.d.ts.map +1 -0
  173. package/dist/plots/stream-lines.js +122 -0
  174. package/dist/plots/stream-lines.js.map +1 -0
  175. package/dist/plots/traced-path.d.ts +70 -0
  176. package/dist/plots/traced-path.d.ts.map +1 -0
  177. package/dist/plots/traced-path.js +41 -0
  178. package/dist/plots/traced-path.js.map +1 -0
  179. package/dist/plots/vector-field.d.ts +60 -0
  180. package/dist/plots/vector-field.d.ts.map +1 -0
  181. package/dist/plots/vector-field.js +77 -0
  182. package/dist/plots/vector-field.js.map +1 -0
  183. package/dist/structures/graph-layout.d.ts +11 -0
  184. package/dist/structures/graph-layout.d.ts.map +1 -0
  185. package/dist/structures/graph-layout.js +135 -0
  186. package/dist/structures/graph-layout.js.map +1 -0
  187. package/dist/structures/network-graph.d.ts +174 -0
  188. package/dist/structures/network-graph.d.ts.map +1 -0
  189. package/dist/structures/network-graph.js +240 -0
  190. package/dist/structures/network-graph.js.map +1 -0
  191. package/dist/structures/sample-space.d.ts +114 -0
  192. package/dist/structures/sample-space.d.ts.map +1 -0
  193. package/dist/structures/sample-space.js +161 -0
  194. package/dist/structures/sample-space.js.map +1 -0
  195. package/dist/systems/axes2d.d.ts +197 -0
  196. package/dist/systems/axes2d.d.ts.map +1 -0
  197. package/dist/systems/axes2d.js +157 -0
  198. package/dist/systems/axes2d.js.map +1 -0
  199. package/dist/systems/number-line.d.ts +101 -0
  200. package/dist/systems/number-line.d.ts.map +1 -0
  201. package/dist/systems/number-line.js +91 -0
  202. package/dist/systems/number-line.js.map +1 -0
  203. package/dist/systems/number-plane.d.ts +168 -0
  204. package/dist/systems/number-plane.d.ts.map +1 -0
  205. package/dist/systems/number-plane.js +186 -0
  206. package/dist/systems/number-plane.js.map +1 -0
  207. package/dist/systems/polar-plane.d.ts +98 -0
  208. package/dist/systems/polar-plane.d.ts.map +1 -0
  209. package/dist/systems/polar-plane.js +139 -0
  210. package/dist/systems/polar-plane.js.map +1 -0
  211. package/dist/tex/geometry.d.ts +85 -0
  212. package/dist/tex/geometry.d.ts.map +1 -0
  213. package/dist/tex/geometry.js +632 -0
  214. package/dist/tex/geometry.js.map +1 -0
  215. package/dist/tex/index.d.ts +11 -0
  216. package/dist/tex/index.d.ts.map +1 -0
  217. package/dist/tex/index.js +10 -0
  218. package/dist/tex/index.js.map +1 -0
  219. package/dist/tex/mathjax.d.ts +33 -0
  220. package/dist/tex/mathjax.d.ts.map +1 -0
  221. package/dist/tex/mathjax.js +74 -0
  222. package/dist/tex/mathjax.js.map +1 -0
  223. package/dist/tex/matrix.d.ts +101 -0
  224. package/dist/tex/matrix.d.ts.map +1 -0
  225. package/dist/tex/matrix.js +56 -0
  226. package/dist/tex/matrix.js.map +1 -0
  227. package/dist/tex/node.d.ts +98 -0
  228. package/dist/tex/node.d.ts.map +1 -0
  229. package/dist/tex/node.js +25 -0
  230. package/dist/tex/node.js.map +1 -0
  231. package/dist/tex/tween.d.ts +50 -0
  232. package/dist/tex/tween.d.ts.map +1 -0
  233. package/dist/tex/tween.js +253 -0
  234. package/dist/tex/tween.js.map +1 -0
  235. package/dist/tex/typeset.d.ts +133 -0
  236. package/dist/tex/typeset.d.ts.map +1 -0
  237. package/dist/tex/typeset.js +403 -0
  238. package/dist/tex/typeset.js.map +1 -0
  239. package/dist/three/axes3d.d.ts +247 -0
  240. package/dist/three/axes3d.d.ts.map +1 -0
  241. package/dist/three/axes3d.js +187 -0
  242. package/dist/three/axes3d.js.map +1 -0
  243. package/dist/three/curve-plot3d.d.ts +126 -0
  244. package/dist/three/curve-plot3d.d.ts.map +1 -0
  245. package/dist/three/curve-plot3d.js +109 -0
  246. package/dist/three/curve-plot3d.js.map +1 -0
  247. package/dist/three/entries.d.ts +17 -0
  248. package/dist/three/entries.d.ts.map +1 -0
  249. package/dist/three/entries.js +15 -0
  250. package/dist/three/entries.js.map +1 -0
  251. package/dist/three/label3d.d.ts +11 -0
  252. package/dist/three/label3d.d.ts.map +1 -0
  253. package/dist/three/label3d.js +42 -0
  254. package/dist/three/label3d.js.map +1 -0
  255. package/dist/three/plot-point3d.d.ts +99 -0
  256. package/dist/three/plot-point3d.d.ts.map +1 -0
  257. package/dist/three/plot-point3d.js +34 -0
  258. package/dist/three/plot-point3d.js.map +1 -0
  259. package/dist/three/space.d.ts +43 -0
  260. package/dist/three/space.d.ts.map +1 -0
  261. package/dist/three/space.js +58 -0
  262. package/dist/three/space.js.map +1 -0
  263. package/dist/three/surface-plot3d.d.ts +207 -0
  264. package/dist/three/surface-plot3d.d.ts.map +1 -0
  265. package/dist/three/surface-plot3d.js +261 -0
  266. package/dist/three/surface-plot3d.js.map +1 -0
  267. package/dist/three/vector-arrow3d.d.ts +96 -0
  268. package/dist/three/vector-arrow3d.d.ts.map +1 -0
  269. package/dist/three/vector-arrow3d.js +53 -0
  270. package/dist/three/vector-arrow3d.js.map +1 -0
  271. package/package.json +80 -3
  272. package/registry.json +251 -0
  273. package/src/annotate/angle-mark.ts +137 -0
  274. package/src/annotate/curly-brace.ts +148 -0
  275. package/src/annotate/emphasis-mark.ts +118 -0
  276. package/src/engine.ts +86 -0
  277. package/src/index.ts +29 -0
  278. package/src/kit/brace.ts +37 -0
  279. package/src/kit/cache.ts +61 -0
  280. package/src/kit/component.ts +57 -0
  281. package/src/kit/contour.ts +94 -0
  282. package/src/kit/coords.ts +278 -0
  283. package/src/kit/curve.ts +47 -0
  284. package/src/kit/equation.ts +181 -0
  285. package/src/kit/equation3d.ts +137 -0
  286. package/src/kit/expression.ts +159 -0
  287. package/src/kit/field-mark.ts +171 -0
  288. package/src/kit/field.ts +50 -0
  289. package/src/kit/follow.ts +229 -0
  290. package/src/kit/furniture.ts +113 -0
  291. package/src/kit/geometry.ts +49 -0
  292. package/src/kit/graph.ts +116 -0
  293. package/src/kit/mark.ts +152 -0
  294. package/src/kit/part-fills.ts +61 -0
  295. package/src/kit/place.ts +77 -0
  296. package/src/kit/sampler.ts +281 -0
  297. package/src/kit/scale.ts +39 -0
  298. package/src/kit/source.ts +57 -0
  299. package/src/kit/system.ts +105 -0
  300. package/src/kit/ticks.ts +85 -0
  301. package/src/kit/withdrawn.ts +8 -0
  302. package/src/marks/curve-area.ts +89 -0
  303. package/src/marks/drop-lines.ts +71 -0
  304. package/src/marks/plot-point.ts +102 -0
  305. package/src/marks/riemann-sum.ts +99 -0
  306. package/src/marks/tangent-line.ts +119 -0
  307. package/src/marks/vector-arrow.ts +98 -0
  308. package/src/nodes.ts +99 -0
  309. package/src/plots/function-plot.ts +220 -0
  310. package/src/plots/points-plot.ts +103 -0
  311. package/src/plots/stream-lines.ts +160 -0
  312. package/src/plots/traced-path.ts +70 -0
  313. package/src/plots/vector-field.ts +107 -0
  314. package/src/structures/graph-layout.ts +145 -0
  315. package/src/structures/network-graph.ts +291 -0
  316. package/src/structures/sample-space.ts +210 -0
  317. package/src/systems/axes2d.ts +226 -0
  318. package/src/systems/number-line.ts +130 -0
  319. package/src/systems/number-plane.ts +232 -0
  320. package/src/systems/polar-plane.ts +177 -0
  321. package/src/tex/geometry.ts +703 -0
  322. package/src/tex/index.ts +15 -0
  323. package/src/tex/mathjax.ts +82 -0
  324. package/src/tex/matrix.ts +97 -0
  325. package/src/tex/node.ts +74 -0
  326. package/src/tex/tween.ts +308 -0
  327. package/src/tex/typeset.ts +461 -0
  328. package/src/three/axes3d.ts +267 -0
  329. package/src/three/curve-plot3d.ts +146 -0
  330. package/src/three/entries.ts +42 -0
  331. package/src/three/label3d.ts +52 -0
  332. package/src/three/plot-point3d.ts +63 -0
  333. package/src/three/space.ts +92 -0
  334. package/src/three/surface-plot3d.ts +318 -0
  335. package/src/three/vector-arrow3d.ts +88 -0
@@ -0,0 +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
+ }
@@ -0,0 +1,103 @@
1
+ import {
2
+ component2d,
3
+ Types,
4
+ type NodeOf,
5
+ type PaintProps,
6
+ type Vector2,
7
+ // Named so the emitted declaration reaches these through the sdk, not core.
8
+ type Fill as _Fill,
9
+ type FillResolved as _FillResolved,
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 { PAINT, PAINT_COMMANDS } from "../kit/component";
18
+ import { PLOT_COLOR } from "../kit/furniture";
19
+ import { PLANE_ENTRY } from "../kit/graph";
20
+ import { drawMark, inkSize, reading, traced, type MarkReading } from "../kit/mark";
21
+
22
+ export interface PointsPlotProps extends PaintProps {
23
+ points?: Vector2[];
24
+ closed?: boolean;
25
+ smooth?: boolean;
26
+ }
27
+
28
+ // A shorter list is padded with its own last point, so a line grows new points out of its end.
29
+ function lerpPoints(from: Vector2[], to: Vector2[], t: number): Vector2[] {
30
+ const count = Math.max(from.length, to.length);
31
+ if (from.length === 0 || to.length === 0) return t < 1 ? from : to;
32
+ const out = new Array<Vector2>(count);
33
+ for (let i = 0; i < count; i++) {
34
+ const a = from[Math.min(i, from.length - 1)];
35
+ const b = to[Math.min(i, to.length - 1)];
36
+ out[i] = { x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t };
37
+ }
38
+ return out;
39
+ }
40
+
41
+ /** Points on the curve between each pair of a smooth line's points. */
42
+ const SMOOTH_STEPS = 16;
43
+
44
+ const state = {
45
+ ...PAINT,
46
+ points: Types.custom<Vector2[]>([], { lerp: lerpPoints }),
47
+ /** Join the last point to the first. */
48
+ closed: Types.boolean(false, { animatable: false }),
49
+ /** Curve through the points instead of turning a corner at each. */
50
+ smooth: Types.boolean(false, { animatable: false }),
51
+ graph: PLANE_ENTRY,
52
+ };
53
+
54
+ interface PointsValues {
55
+ readonly points: Vector2[];
56
+ readonly closed: boolean;
57
+ readonly smooth: boolean;
58
+ }
59
+
60
+ /** A line through listed graph points: data, or a shape drawn in graph coordinates when closed, which is when it is filled. */
61
+ export const PointsPlot = traced(component2d({
62
+ key: "pointsPlot",
63
+ state,
64
+ defaults: { stroke: { weight: 3, fill: PLOT_COLOR, align: "center", cap: "round", join: "round" }, width: "hug", height: "hug" },
65
+ commands: {
66
+ ...PAINT_COMMANDS,
67
+ /** Move the points to new places; extra ones grow from the line's end. */
68
+ pointsTo: { args: ["points"] },
69
+ },
70
+ measure: (_measurer, s) => inkSize(tracePoints(s, reading(s))),
71
+ // An open line has no inside, so its fill waits until it is closed.
72
+ render: (ctx, s) => drawMark(ctx, s, tracePoints(s, reading(s)), { closed: s.closed, fills: s.closed }),
73
+ }), tracePoints);
74
+
75
+ export type PointsPlot = NodeOf<typeof PointsPlot>;
76
+
77
+ function tracePoints(s: PointsValues, { map, shift }: MarkReading): Vector2[][] {
78
+ const pixels = s.points.map((p) => map.toPoint(p.x + shift.x, p.y + shift.y));
79
+ if (pixels.length < 2) return [];
80
+ return [s.smooth && pixels.length > 2 ? spline(pixels, s.closed) : pixels];
81
+ }
82
+
83
+ // Catmull–Rom, so the curve passes through every point it was given.
84
+ function spline(points: readonly Vector2[], closed: boolean): Vector2[] {
85
+ const n = points.length;
86
+ const at = (i: number): Vector2 => (closed ? points[((i % n) + n) % n] : points[Math.max(0, Math.min(n - 1, i))]);
87
+ const out: Vector2[] = [];
88
+ const spans = closed ? n : n - 1;
89
+ for (let i = 0; i < spans; i++) {
90
+ const p0 = at(i - 1), p1 = at(i), p2 = at(i + 1), p3 = at(i + 2);
91
+ for (let s = 0; s < SMOOTH_STEPS; s++) {
92
+ const t = s / SMOOTH_STEPS;
93
+ const t2 = t * t;
94
+ const t3 = t2 * t;
95
+ out.push({
96
+ 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),
97
+ 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),
98
+ });
99
+ }
100
+ }
101
+ if (!closed) out.push(points[n - 1]);
102
+ return out;
103
+ }
@@ -0,0 +1,160 @@
1
+ import {
2
+ component2d,
3
+ Types,
4
+ type NodeOf,
5
+ type Vector2,
6
+ // Named so the emitted declaration reaches these through the sdk, not core.
7
+ type Fill as _Fill,
8
+ type FillResolved as _FillResolved,
9
+ type Node2D as _Node2D,
10
+ type Node2DProps as _Node2DProps,
11
+ type Shadow as _Shadow,
12
+ type ShadowResolved as _ShadowResolved,
13
+ type Stroke as _Stroke,
14
+ type StrokeResolved as _StrokeResolved,
15
+ } from "@motionscript/sdk";
16
+ import type { CoordinateMap } from "../kit/coords";
17
+ import { drawField, FIELD_COMMANDS, FIELD_STATE, traceField, type FieldDrawing, type FieldProps, type FieldSampler, type FieldStroke, type FieldValues } from "../kit/field-mark";
18
+ import { PLOT_COLOR } from "../kit/furniture";
19
+ import { clamp } from "../kit/geometry";
20
+ import { inkSize, reading, traced, type MarkReading } from "../kit/mark";
21
+
22
+ export interface StreamLinesProps extends FieldProps {
23
+ spacing?: number;
24
+ length?: number;
25
+ window?: number;
26
+ phase?: number;
27
+ }
28
+
29
+ const MAX_LINES = 1600;
30
+ /** Steps a line takes across one spacing: what keeps a tight swirl round. */
31
+ const STEPS_PER_SPACING = 8;
32
+ const MAX_STEPS = 600;
33
+
34
+ const state = {
35
+ ...FIELD_STATE,
36
+ /** How far apart the lines start, in graph units. */
37
+ spacing: Types.number(1, { min: 0.01, step: 0.1 }),
38
+ /** How far each line runs from where it starts, in graph units along itself. */
39
+ length: Types.number(3, { min: 0, step: 0.1 }),
40
+ /** The share of each line that is shown at once; one shows all of it. */
41
+ window: Types.number(1, { min: 0.01, max: 1, step: 0.01 }),
42
+ /** Where along its line each shown stretch is: one turn of it runs every line from end to end. */
43
+ phase: Types.number(0, { step: 0.01 }),
44
+ };
45
+
46
+ interface LinesValues extends FieldValues {
47
+ readonly spacing: number;
48
+ readonly length: number;
49
+ readonly window: number;
50
+ readonly phase: number;
51
+ }
52
+
53
+ const LINES: FieldDrawing<LinesValues> = {
54
+ own: (s) => `${s.spacing}|${s.length}|${s.window}|${s.phase}`,
55
+ strokes: (s, map, field) => lines(s, map, field),
56
+ };
57
+
58
+ const traceLines = (s: LinesValues, read: MarkReading) => traceField("streamLines", LINES, s, read);
59
+
60
+ /**
61
+ * The lines a vector field carries a point along, one from each point of a
62
+ * grid: `(-y, x)` draws circles. With a `window` below one, each line shows
63
+ * only a stretch of itself, and `flowTo` sends those stretches along.
64
+ */
65
+ export const StreamLines = traced(component2d({
66
+ key: "streamLines",
67
+ state,
68
+ defaults: { stroke: { weight: 2, fill: PLOT_COLOR, align: "center", cap: "round", join: "round" }, width: "hug", height: "hug" },
69
+ commands: {
70
+ ...FIELD_COMMANDS,
71
+ /** Send the shown stretches along their lines: a whole number runs each line that many times over. */
72
+ flowTo: { args: ["phase"] },
73
+ },
74
+ measure: (_measurer, s) => inkSize(traceLines(s, reading(s)).runs),
75
+ render: (ctx, s) => drawField(ctx, s, traceLines(s, reading(s))),
76
+ }), (s: LinesValues, read: MarkReading) => traceLines(s, read).runs);
77
+
78
+ export type StreamLines = NodeOf<typeof StreamLines>;
79
+
80
+ function lines(s: LinesValues, map: CoordinateMap, field: FieldSampler): FieldStroke[] {
81
+ const [x0, x1] = map.xRange;
82
+ const [y0, y1] = map.yRange;
83
+ let spacing = Math.max(s.spacing, 1e-6);
84
+ const count = () => (Math.floor((x1 - x0) / spacing) + 1) * (Math.floor((y1 - y0) / spacing) + 1);
85
+ while (count() > MAX_LINES) spacing *= 2;
86
+ const step = spacing / STEPS_PER_SPACING;
87
+ const steps = Math.min(MAX_STEPS, Math.round(s.length / step));
88
+ if (steps < 1) return [];
89
+ // A line is followed a little past what is shown, so it leaves the box rather than stopping at its edge.
90
+ const inside = (x: number, y: number) => x >= x0 - spacing && x <= x1 + spacing && y >= y0 - spacing && y <= y1 + spacing;
91
+ const window = clamp(s.window, 0, 1);
92
+
93
+ const drawn: FieldStroke[] = [];
94
+ let index = 0;
95
+ for (let i = Math.ceil(x0 / spacing - 1e-9); i * spacing <= x1 + 1e-9; i++) {
96
+ for (let j = Math.ceil(y0 / spacing - 1e-9); j * spacing <= y1 + 1e-9; j++) {
97
+ const line = follow(field, i * spacing, j * spacing, step, steps, inside);
98
+ index++;
99
+ if (line.points.length < 2) continue;
100
+ const shown = window >= 1 ? line.points : stretch(line.points, window, s.phase + stagger(index));
101
+ if (shown.length < 2) continue;
102
+ drawn.push({ runs: [shown.map((p) => map.toPoint(p.x, p.y))], magnitude: line.magnitude });
103
+ }
104
+ }
105
+ return drawn;
106
+ }
107
+
108
+ /** The graph points a field carries a point through from `(x, y)`, at an even pace, and how strong it was on the way. */
109
+ function follow(
110
+ field: FieldSampler,
111
+ x: number,
112
+ y: number,
113
+ step: number,
114
+ steps: number,
115
+ inside: (x: number, y: number) => boolean,
116
+ ): { points: Vector2[]; magnitude: number } {
117
+ const points: Vector2[] = [{ x, y }];
118
+ const v: Vector2 = { x: 0, y: 0 };
119
+ let strength = 0;
120
+ let read = 0;
121
+ // The field's direction at a point, as a unit step; `false` where it has none or is at rest.
122
+ const heading = (px: number, py: number, out: Vector2): boolean => {
123
+ if (!field(px, py, v)) return false;
124
+ const magnitude = Math.hypot(v.x, v.y);
125
+ if (!(magnitude > 1e-9)) return false;
126
+ out.x = v.x / magnitude;
127
+ out.y = v.y / magnitude;
128
+ strength += magnitude;
129
+ read++;
130
+ return true;
131
+ };
132
+ const a: Vector2 = { x: 0, y: 0 };
133
+ const b: Vector2 = { x: 0, y: 0 };
134
+ for (let i = 0; i < steps; i++) {
135
+ // Midpoint rule: the direction half a step on is the one the whole step takes.
136
+ if (!heading(x, y, a) || !heading(x + (a.x * step) / 2, y + (a.y * step) / 2, b)) break;
137
+ x += b.x * step;
138
+ y += b.y * step;
139
+ if (!inside(x, y)) break;
140
+ points.push({ x, y });
141
+ }
142
+ return { points, magnitude: read > 0 ? strength / read : 0 };
143
+ }
144
+
145
+ /** The part of `points` a window `share` long shows at `phase`: it enters at one end and leaves by the other. */
146
+ function stretch(points: Vector2[], share: number, phase: number): Vector2[] {
147
+ const turn = phase - Math.floor(phase);
148
+ const end = turn * (1 + share);
149
+ const from = Math.max(0, end - share);
150
+ const to = Math.min(1, end);
151
+ if (!(to > from)) return [];
152
+ const last = points.length - 1;
153
+ return points.slice(Math.floor(from * last), Math.ceil(to * last) + 1);
154
+ }
155
+
156
+ /** 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. */
157
+ function stagger(index: number): number {
158
+ const mixed = Math.sin(index * 12.9898) * 43758.5453;
159
+ return mixed - Math.floor(mixed);
160
+ }
@@ -0,0 +1,70 @@
1
+ import {
2
+ component2d,
3
+ Types,
4
+ type NodeOf,
5
+ // Named so the emitted declaration reaches these through the sdk, not core.
6
+ type Node2D as _Node2D,
7
+ type Node2DProps as _Node2DProps,
8
+ type Shadow as _Shadow,
9
+ type ShadowResolved as _ShadowResolved,
10
+ type Stroke as _Stroke,
11
+ type StrokeResolved as _StrokeResolved,
12
+ } from "@motionscript/sdk";
13
+ import { omit } from "../kit/component";
14
+ import type { Interval } from "../kit/coords";
15
+ import { drawMark, inkSize, reading, traced, type MarkReading } from "../kit/mark";
16
+ import { PLOT_COMMANDS, PLOT_STATE, PLOT_STROKE, tracePlot, type FunctionPlotProps, type PlotValues } from "./function-plot";
17
+
18
+ export interface TracedPathProps extends FunctionPlotProps {
19
+ from?: number;
20
+ value?: number;
21
+ tail?: number;
22
+ }
23
+
24
+ const state = {
25
+ // It reveals itself by its value, so it trims no outline of its own.
26
+ ...omit(PLOT_STATE, "start", "end"),
27
+ /** The value the trail starts at. */
28
+ from: Types.number(0, { step: 0.1 }),
29
+ /** The value the point is at now: where the trail ends. */
30
+ value: Types.number(0, { step: 0.1 }),
31
+ /** How much of the trail is kept, in the value's own units; zero keeps all of it. */
32
+ tail: Types.number(0, { min: 0, step: 0.1 }),
33
+ };
34
+
35
+ interface TrailValues extends PlotValues {
36
+ readonly from: number;
37
+ readonly value: number;
38
+ readonly tail: number;
39
+ }
40
+
41
+ /**
42
+ * The trail a point leaves as a value changes: the curve of its equation from
43
+ * where the value began to where it is now. Bind `value` to a variable
44
+ * (`"=[t]"`) and the trail grows as the variable is driven, or drive it with
45
+ * `traceTo`; a `tail` keeps only its most recent stretch.
46
+ */
47
+ export const TracedPath = traced(component2d({
48
+ key: "tracedPath",
49
+ state,
50
+ defaults: { stroke: PLOT_STROKE, width: "hug", height: "hug" },
51
+ commands: {
52
+ ...omit(PLOT_COMMANDS, "progressTo", "flash"),
53
+ /** Carry the point on to another value, the trail following it. */
54
+ traceTo: { args: ["value"] },
55
+ },
56
+ measure: (_measurer, s) => inkSize(traceTrail(s, reading(s))),
57
+ render: (ctx, s) => drawMark(ctx, s, traceTrail(s, reading(s))),
58
+ }), traceTrail);
59
+
60
+ export type TracedPath = NodeOf<typeof TracedPath>;
61
+
62
+ function traceTrail(s: TrailValues, read: MarkReading) {
63
+ return tracePlot(s, read, span(s));
64
+ }
65
+
66
+ /** The span of the free variable the trail covers: from where the value began, or its tail, to where it is. */
67
+ function span({ from, value, tail }: TrailValues): Interval {
68
+ const kept = tail > 0 ? Math.min(Math.abs(value - from), tail) : Math.abs(value - from);
69
+ return value >= from ? [value - kept, value] : [value, value + kept];
70
+ }
@@ -0,0 +1,107 @@
1
+ import {
2
+ component2d,
3
+ Types,
4
+ type NodeOf,
5
+ type Vector2,
6
+ // Named so the emitted declaration reaches these through the sdk, not core.
7
+ type Fill as _Fill,
8
+ type FillResolved as _FillResolved,
9
+ type Node2D as _Node2D,
10
+ type Node2DProps as _Node2DProps,
11
+ type Shadow as _Shadow,
12
+ type ShadowResolved as _ShadowResolved,
13
+ type Stroke as _Stroke,
14
+ type StrokeResolved as _StrokeResolved,
15
+ } from "@motionscript/sdk";
16
+ import type { CoordinateMap } from "../kit/coords";
17
+ import { drawField, FIELD_COMMANDS, FIELD_STATE, traceField, type FieldDrawing, type FieldProps, type FieldValues, type FieldSampler, type FieldStroke } from "../kit/field-mark";
18
+ import { PLOT_COLOR } from "../kit/furniture";
19
+ import { inkSize, reading, traced, type MarkReading } from "../kit/mark";
20
+
21
+ export interface VectorFieldProps extends FieldProps {
22
+ spacing?: number;
23
+ arrowScale?: number;
24
+ }
25
+
26
+ /** Past this many arrows the spacing is widened, so a field over a huge range is still a field and not a wash. */
27
+ const MAX_ARROWS = 2400;
28
+ /** An arrow's head, as a share of its length and in pixels at most, and the half-angle it opens to. */
29
+ const HEAD_SHARE = 0.38;
30
+ const HEAD_MAX = 11;
31
+ const HEAD_ANGLE = (26 * Math.PI) / 180;
32
+
33
+ const state = {
34
+ ...FIELD_STATE,
35
+ /** How far apart the arrows stand, in graph units. */
36
+ spacing: Types.number(1, { min: 0.01, step: 0.1 }),
37
+ /** How long an arrow is drawn for a vector of length one, in graph units. */
38
+ arrowScale: Types.number(0.4, { min: 0, step: 0.05 }),
39
+ };
40
+
41
+ interface ArrowsValues extends FieldValues {
42
+ readonly spacing: number;
43
+ readonly arrowScale: number;
44
+ }
45
+
46
+ const ARROWS: FieldDrawing<ArrowsValues> = {
47
+ own: (s) => `${s.spacing}|${s.arrowScale}`,
48
+ strokes: (s, map, field) => arrows(s, map, field),
49
+ };
50
+
51
+ const traceArrows = (s: ArrowsValues, read: MarkReading) => traceField("vectorField", ARROWS, s, read);
52
+
53
+ /**
54
+ * A grid of arrows showing a vector field, two parts in x and y: `(-y, x)`.
55
+ * Each arrow is centred on its point and is as long as the field is strong
56
+ * there, up to the spacing.
57
+ */
58
+ export const VectorField = traced(component2d({
59
+ key: "vectorField",
60
+ state,
61
+ defaults: { stroke: { weight: 2.5, fill: PLOT_COLOR, align: "center", cap: "round", join: "round" }, width: "hug", height: "hug" },
62
+ commands: FIELD_COMMANDS,
63
+ measure: (_measurer, s) => inkSize(traceArrows(s, reading(s)).runs),
64
+ render: (ctx, s) => drawField(ctx, s, traceArrows(s, reading(s))),
65
+ }), (s: ArrowsValues, read: MarkReading) => traceArrows(s, read).runs);
66
+
67
+ export type VectorField = NodeOf<typeof VectorField>;
68
+
69
+ function arrows(s: ArrowsValues, map: CoordinateMap, field: FieldSampler): FieldStroke[] {
70
+ const [x0, x1] = map.xRange;
71
+ const [y0, y1] = map.yRange;
72
+ let spacing = Math.max(s.spacing, 1e-6);
73
+ const count = () => (Math.floor((x1 - x0) / spacing) + 1) * (Math.floor((y1 - y0) / spacing) + 1);
74
+ while (count() > MAX_ARROWS) spacing *= 2;
75
+
76
+ const drawn: FieldStroke[] = [];
77
+ const v: Vector2 = { x: 0, y: 0 };
78
+ const longest = spacing * 0.9;
79
+ for (let i = Math.ceil(x0 / spacing - 1e-9); i * spacing <= x1 + 1e-9; i++) {
80
+ for (let j = Math.ceil(y0 / spacing - 1e-9); j * spacing <= y1 + 1e-9; j++) {
81
+ const gx = i * spacing;
82
+ const gy = j * spacing;
83
+ if (!field(gx, gy, v)) continue;
84
+ const magnitude = Math.hypot(v.x, v.y);
85
+ const length = Math.min(magnitude * s.arrowScale, longest);
86
+ if (!(length > 0)) continue;
87
+ const hx = ((v.x / magnitude) * length) / 2;
88
+ const hy = ((v.y / magnitude) * length) / 2;
89
+ const tail = map.toPoint(gx - hx, gy - hy);
90
+ const head = map.toPoint(gx + hx, gy + hy);
91
+ drawn.push({ runs: arrow(tail, head), magnitude });
92
+ }
93
+ }
94
+ return drawn;
95
+ }
96
+
97
+ /** A shaft and the two barbs of its head, in pixels. */
98
+ function arrow(tail: Vector2, head: Vector2): Vector2[][] {
99
+ const dx = head.x - tail.x;
100
+ const dy = head.y - tail.y;
101
+ const length = Math.hypot(dx, dy);
102
+ if (!(length > 0)) return [];
103
+ const barb = Math.min(length * HEAD_SHARE, HEAD_MAX);
104
+ const back = Math.atan2(dy, dx) + Math.PI;
105
+ const at = (angle: number): Vector2 => ({ x: head.x + Math.cos(angle) * barb, y: head.y + Math.sin(angle) * barb });
106
+ return [[tail, head], [at(back - HEAD_ANGLE), head, at(back + HEAD_ANGLE)]];
107
+ }