@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,281 @@
1
+ import type { Extents, Vector2 } from "@motionscript/sdk";
2
+
3
+ /**
4
+ * Turns a curve into the polylines that draw it: adaptively, so a straight
5
+ * stretch costs two points and a tight bend as many as it needs, and broken
6
+ * wherever the curve is, at an asymptote or the edge of its domain.
7
+ */
8
+
9
+ /** A curve's parameter to its pixel, written into `out`. `false` where the curve is undefined. */
10
+ export type CurveProjection = (t: number, out: Vector2) => boolean;
11
+
12
+ export interface SampleOptions {
13
+ /** The equal steps the parameter range starts as, before any is split. */
14
+ seeds?: number;
15
+ /** A parameter step that is not split again, however bent. */
16
+ minStep?: number;
17
+ }
18
+
19
+ /** Path coordinates are 32-bit floats in the renderer; past this a clamped point still clips to the same edge. */
20
+ const COORD_LIMIT = 16_000;
21
+ /** How far, in pixels, the drawn chord may stray from the curve. */
22
+ const TOLERANCE_PX = 0.25;
23
+ /** A turn, in radians, a chord is allowed to hide: what keeps a symmetric bump from reading as flat. */
24
+ const ANGLE_TOLERANCE = 0.05;
25
+ const MAX_DEPTH = 12;
26
+ const MAX_EVALUATIONS = 40_000;
27
+ const MAX_POINTS = 24_000;
28
+ /** Bisections spent finding where a domain ends. */
29
+ const BOUNDARY_STEPS = 60;
30
+ const DEFAULT_SEEDS = 256;
31
+
32
+ interface Sample {
33
+ t: number;
34
+ px: number;
35
+ py: number;
36
+ /** The pixel before clamping: what tells an asymptote from a steep line. */
37
+ rx: number;
38
+ ry: number;
39
+ ok: boolean;
40
+ }
41
+
42
+ // Midpoints are pooled: a refine is a depth-first walk, so a slot is free again
43
+ // as soon as the seed interval it was taken for is done.
44
+ const arena: Sample[] = [];
45
+ let arenaMark = 0;
46
+
47
+ function takeSample(): Sample {
48
+ let slot = arena[arenaMark];
49
+ if (slot === undefined) {
50
+ slot = blank();
51
+ arena.push(slot);
52
+ }
53
+ arenaMark++;
54
+ return slot;
55
+ }
56
+
57
+ function blank(): Sample {
58
+ return { t: 0, px: 0, py: 0, rx: 0, ry: 0, ok: false };
59
+ }
60
+
61
+ const scratch: Vector2 = { x: 0, y: 0 };
62
+
63
+ /** The runs of points that draw `project` over `[t0, t1]`, clipped to `clip`. */
64
+ export function sampleCurve(
65
+ project: CurveProjection,
66
+ t0: number,
67
+ t1: number,
68
+ clip: Extents,
69
+ { seeds = DEFAULT_SEEDS, minStep = 0 }: SampleOptions = {},
70
+ ): Vector2[][] {
71
+ if (!(t1 > t0) || !Number.isFinite(t0) || !Number.isFinite(t1)) return [];
72
+ const pen = new CurvePen(clip);
73
+ let spent = 0;
74
+
75
+ const write = (slot: Sample, t: number): Sample => {
76
+ const defined = project(t, scratch);
77
+ spent++;
78
+ const ok = defined && Number.isFinite(scratch.x) && Number.isFinite(scratch.y);
79
+ slot.t = t;
80
+ slot.rx = ok ? scratch.x : 0;
81
+ slot.ry = ok ? scratch.y : 0;
82
+ slot.px = clampCoord(slot.rx);
83
+ slot.py = clampCoord(slot.ry);
84
+ slot.ok = ok;
85
+ return slot;
86
+ };
87
+ const at = (t: number): Sample => write(takeSample(), t);
88
+
89
+ // The last defined point before `to`, which is not: where `sqrt(x)` meets its axis.
90
+ const edgeOf = (from: Sample, to: Sample): Sample => {
91
+ let inside = from;
92
+ let outsideT = to.t;
93
+ for (let i = 0; i < BOUNDARY_STEPS; i++) {
94
+ if (spent >= MAX_EVALUATIONS || offFrame(inside, clip)) break;
95
+ const midT = (inside.t + outsideT) / 2;
96
+ if (midT === inside.t || midT === outsideT) break;
97
+ const mid = at(midT);
98
+ if (!mid.ok) {
99
+ outsideT = midT;
100
+ continue;
101
+ }
102
+ const settled = Math.abs(mid.px - inside.px) < TOLERANCE_PX && Math.abs(mid.py - inside.py) < TOLERANCE_PX;
103
+ inside = mid;
104
+ if (settled) break;
105
+ }
106
+ return inside;
107
+ };
108
+
109
+ const emit = (a: Sample, m: Sample | null, b: Sample, floored: boolean): void => {
110
+ if (a.ok && b.ok) {
111
+ if (floored && diverges(a, m, b, clip)) pen.break();
112
+ else pen.draw(a, b);
113
+ return;
114
+ }
115
+ if (a.ok) {
116
+ const edge = edgeOf(a, b);
117
+ if (edge.t !== a.t) pen.draw(a, edge);
118
+ pen.break();
119
+ return;
120
+ }
121
+ pen.break();
122
+ if (b.ok) {
123
+ const edge = edgeOf(b, a);
124
+ if (edge.t !== b.t) pen.draw(edge, b);
125
+ else pen.start(b);
126
+ }
127
+ };
128
+
129
+ const refine = (a: Sample, b: Sample, depth: number): void => {
130
+ if (spent >= MAX_EVALUATIONS || pen.points >= MAX_POINTS) {
131
+ emit(a, null, b, false);
132
+ return;
133
+ }
134
+ const m = at((a.t + b.t) / 2);
135
+ const floored = depth >= MAX_DEPTH || b.t - a.t <= minStep;
136
+ if (floored || flat(a, m, b)) {
137
+ emit(a, m, b, floored);
138
+ return;
139
+ }
140
+ refine(a, m, depth + 1);
141
+ refine(m, b, depth + 1);
142
+ };
143
+
144
+ const count = Math.max(1, Math.round(seeds));
145
+ const step = (t1 - t0) / count;
146
+ const ends: [Sample, Sample] = [blank(), blank()];
147
+ let previous = write(ends[0], t0);
148
+ for (let i = 1; i <= count; i++) {
149
+ const next = write(ends[i & 1], i === count ? t1 : t0 + i * step);
150
+ arenaMark = 0;
151
+ refine(previous, next, 0);
152
+ previous = next;
153
+ }
154
+ arenaMark = 0;
155
+ return pen.finish();
156
+ }
157
+
158
+ function flat(a: Sample, m: Sample, b: Sample): boolean {
159
+ if (a.ok !== m.ok || m.ok !== b.ok) return false;
160
+ if (!a.ok) return true;
161
+ const dx = b.px - a.px;
162
+ const dy = b.py - a.py;
163
+ const length = Math.hypot(dx, dy) || 1;
164
+ if (Math.abs((m.px - a.px) * dy - (m.py - a.py) * dx) / length > TOLERANCE_PX) return false;
165
+ let turn = Math.abs(Math.atan2(m.py - a.py, m.px - a.px) - Math.atan2(b.py - m.py, b.px - m.px));
166
+ if (turn > Math.PI) turn = 2 * Math.PI - turn;
167
+ return turn < ANGLE_TOLERANCE;
168
+ }
169
+
170
+ // Two neighbours on opposite sides of the frame, with nothing between them that
171
+ // is between them: an asymptote, not a steep line.
172
+ function diverges(a: Sample, m: Sample | null, b: Sample, clip: Extents): boolean {
173
+ const straddles =
174
+ (a.py > clip.maxY && b.py < clip.minY) || (a.py < clip.minY && b.py > clip.maxY) ||
175
+ (a.px > clip.maxX && b.px < clip.minX) || (a.px < clip.minX && b.px > clip.maxX);
176
+ if (!straddles || m === null) return false;
177
+ if (!m.ok) return true;
178
+ return (
179
+ m.ry < Math.min(a.ry, b.ry) || m.ry > Math.max(a.ry, b.ry) ||
180
+ m.rx < Math.min(a.rx, b.rx) || m.rx > Math.max(a.rx, b.rx)
181
+ );
182
+ }
183
+
184
+ function offFrame(sample: Sample, clip: Extents): boolean {
185
+ return sample.py > clip.maxY || sample.py < clip.minY || sample.px > clip.maxX || sample.px < clip.minX;
186
+ }
187
+
188
+ function clampCoord(value: number): number {
189
+ if (value > COORD_LIMIT) return COORD_LIMIT;
190
+ if (value < -COORD_LIMIT) return -COORD_LIMIT;
191
+ return value;
192
+ }
193
+
194
+ class CurvePen {
195
+ points = 0;
196
+ private readonly runs: Vector2[][] = [];
197
+ private open: Vector2[] = [];
198
+ private cursor: Vector2 | null = null;
199
+
200
+ constructor(private readonly clip: Extents) {}
201
+
202
+ draw(a: Sample, b: Sample): void {
203
+ const segment = clipSegment(a.px, a.py, b.px, b.py, this.clip);
204
+ if (!segment) {
205
+ this.break();
206
+ return;
207
+ }
208
+ const [start, end] = segment;
209
+ if (this.cursor && samePoint(this.cursor, start)) {
210
+ this.push(end);
211
+ } else {
212
+ this.break();
213
+ this.push(start);
214
+ this.push(end);
215
+ }
216
+ this.cursor = end;
217
+ }
218
+
219
+ start(b: Sample): void {
220
+ this.break();
221
+ if (!inside(b.px, b.py, this.clip)) return;
222
+ this.cursor = { x: b.px, y: b.py };
223
+ this.push(this.cursor);
224
+ }
225
+
226
+ break(): void {
227
+ if (this.open.length >= 2) this.runs.push(this.open);
228
+ this.open = [];
229
+ this.cursor = null;
230
+ }
231
+
232
+ finish(): Vector2[][] {
233
+ this.break();
234
+ return this.runs;
235
+ }
236
+
237
+ private push(point: Vector2): void {
238
+ this.open.push(point);
239
+ this.points++;
240
+ }
241
+ }
242
+
243
+ function samePoint(a: Vector2, b: Vector2): boolean {
244
+ return Math.abs(a.x - b.x) < 0.1 && Math.abs(a.y - b.y) < 0.1;
245
+ }
246
+
247
+ function inside(x: number, y: number, clip: Extents): boolean {
248
+ return x >= clip.minX && x <= clip.maxX && y >= clip.minY && y <= clip.maxY;
249
+ }
250
+
251
+ // Liang–Barsky: the part of a segment inside the clip, or null when none is.
252
+ function clipSegment(ax: number, ay: number, bx: number, by: number, clip: Extents): [Vector2, Vector2] | null {
253
+ const dx = bx - ax;
254
+ const dy = by - ay;
255
+ let t0 = 0;
256
+ let t1 = 1;
257
+ const edges: [number, number][] = [
258
+ [-dx, ax - clip.minX],
259
+ [dx, clip.maxX - ax],
260
+ [-dy, ay - clip.minY],
261
+ [dy, clip.maxY - ay],
262
+ ];
263
+ for (const [p, q] of edges) {
264
+ if (p === 0) {
265
+ if (q < 0) return null;
266
+ continue;
267
+ }
268
+ const r = q / p;
269
+ if (p < 0) {
270
+ if (r > t1) return null;
271
+ if (r > t0) t0 = r;
272
+ } else {
273
+ if (r < t0) return null;
274
+ if (r < t1) t1 = r;
275
+ }
276
+ }
277
+ return [
278
+ { x: ax + t0 * dx, y: ay + t0 * dy },
279
+ { x: ax + t1 * dx, y: ay + t1 * dy },
280
+ ];
281
+ }
@@ -0,0 +1,39 @@
1
+ import { colorOps, fillOps, type Color, type Fill, type FillResolved, type NormalizedColor } from "@motionscript/sdk";
2
+ import { clamp } from "./geometry";
3
+
4
+ /** A run of colours a value is read off, lowest first: a surface's height, a field's strength. */
5
+
6
+ export function readScale(value: readonly Color[] | undefined): NormalizedColor[] {
7
+ return (value ?? []).map((color) => colorOps.resolve(color));
8
+ }
9
+
10
+ export function lerpScale(from: NormalizedColor[], to: NormalizedColor[], t: number): NormalizedColor[] {
11
+ if (from.length !== to.length) return t < 1 ? from : to;
12
+ return to.map((color, index) => colorOps.lerp(from[index], color, t));
13
+ }
14
+
15
+ /** The colour `share` of the way along `scale`, 0 to 1. */
16
+ export function colorAt(scale: readonly NormalizedColor[], share: number): NormalizedColor {
17
+ if (scale.length === 1) return scale[0];
18
+ const at = clamp(share, 0, 1) * (scale.length - 1);
19
+ const index = Math.min(scale.length - 2, Math.floor(at));
20
+ return colorOps.lerp(scale[index], scale[index + 1], at - index);
21
+ }
22
+
23
+ /** A run of paints a value is read off, lowest first: a field's strength. Every entry is kept as written. */
24
+ export function readFillScale(value: readonly Fill[] | undefined): FillResolved[][] {
25
+ return (Array.isArray(value) ? value : []).map((fill) => fillOps.resolve(fill ?? []));
26
+ }
27
+
28
+ export function lerpFillScale(from: FillResolved[][], to: FillResolved[][], t: number): FillResolved[][] {
29
+ if (from.length !== to.length) return t < 1 ? from : to;
30
+ return to.map((fill, index) => fillOps.lerp(from[index], fill, t));
31
+ }
32
+
33
+ /** The paint `share` of the way along `scale`, 0 to 1. */
34
+ export function fillAt(scale: readonly FillResolved[][], share: number): FillResolved[] {
35
+ if (scale.length === 1) return scale[0];
36
+ const at = clamp(share, 0, 1) * (scale.length - 1);
37
+ const index = Math.min(scale.length - 2, Math.floor(at));
38
+ return fillOps.lerp(scale[index], scale[index + 1], at - index);
39
+ }
@@ -0,0 +1,57 @@
1
+ import type { Interval } from "./coords";
2
+ import type { ExpressionScope } from "./expression";
3
+
4
+ /** What a plot's equation, domain and named values are held as, and how each tweens: shared by every plot, 2D and 3D. */
5
+
6
+ /** An equation on its way to another: both are drawn, `t` of the way across. */
7
+ export interface PlotSource {
8
+ source: string;
9
+ blend?: { from: string; t: number };
10
+ }
11
+
12
+ export function readSource(value: string | PlotSource | undefined): PlotSource {
13
+ if (typeof value === "string") return { source: value };
14
+ return value ?? { source: "" };
15
+ }
16
+
17
+ // Mid-tween the prop holds both equations, so a seek restores the blend with it.
18
+ export function lerpSource(from: PlotSource, to: PlotSource, t: number): PlotSource {
19
+ if (t <= 0 || from.source === to.source) return t <= 0 ? from : to;
20
+ return t >= 1 ? to : { source: to.source, blend: { from: from.source, t } };
21
+ }
22
+
23
+ export function lerpDomain(from: Interval | null, to: Interval | null, t: number): Interval | null {
24
+ if (!from || !to) return t < 1 ? from : to;
25
+ return [from[0] + (to[0] - from[0]) * t, from[1] + (to[1] - from[1]) * t];
26
+ }
27
+
28
+ /** What a named value is until it is given one. */
29
+ export const UNSET_PARAM = 1;
30
+
31
+ export function lerpParams(from: ExpressionScope, to: ExpressionScope, t: number): ExpressionScope {
32
+ const out: Record<string, number> = { ...from };
33
+ for (const [name, value] of Object.entries(to)) {
34
+ const start = from[name] ?? UNSET_PARAM;
35
+ out[name] = start + (value - start) * t;
36
+ }
37
+ return out;
38
+ }
39
+
40
+ /** `params` with every name of `names` given a value, so one nobody has set yet still draws. */
41
+ export function scopeFor(names: readonly string[], params: ExpressionScope): ExpressionScope {
42
+ let filled: Record<string, number> | null = null;
43
+ for (const name of names) {
44
+ if (typeof params[name] !== "number") (filled ??= { ...params })[name] = UNSET_PARAM;
45
+ }
46
+ return filled ?? params;
47
+ }
48
+
49
+ /** A string as a number that changes when it does: what a geometry's `revision` wants. */
50
+ export function revisionOf(key: string): number {
51
+ let hash = 0x811c9dc5;
52
+ for (let i = 0; i < key.length; i++) {
53
+ hash ^= key.charCodeAt(i);
54
+ hash = Math.imul(hash, 0x01000193);
55
+ }
56
+ return hash >>> 0;
57
+ }
@@ -0,0 +1,105 @@
1
+ import {
2
+ Graphics2D,
3
+ insetsOps,
4
+ Paint,
5
+ provides,
6
+ type ChildPlane,
7
+ type ChildView,
8
+ type Extents,
9
+ type InsetsResolved,
10
+ type Placement2D,
11
+ type Provided,
12
+ type RenderContext,
13
+ type Shadow,
14
+ type Fill,
15
+ type Size2D,
16
+ type Vector2,
17
+ } from "@motionscript/sdk";
18
+ import type { CoordinateMap } from "./coords";
19
+ import { settle, SYSTEM, systemPlane } from "./graph";
20
+ import { openPlane, placeOnMap } from "./place";
21
+
22
+ /**
23
+ * What a coordinate system shares: a node whose children sit in its graph
24
+ * coordinates. A child's `x`/`y` are in the system's units and the child keeps
25
+ * its own size, so anything can be put on a graph: a dot, a label, an image. A
26
+ * mark is drawn through the system's map instead and laid out on its ink.
27
+ *
28
+ * The system's `stroke` is its axis line (so `endCap` is the tip) and its `fill`
29
+ * the backdrop. Ticks, grid and labels are drawing ops, never child nodes: a
30
+ * document's children would otherwise share their indices with them.
31
+ */
32
+
33
+ const CENTRE: Vector2 = { x: 0, y: 0 };
34
+
35
+ /** What a system's helpers read of its state. */
36
+ export interface SystemState {
37
+ readonly padding: InsetsResolved;
38
+ readonly fill: Fill;
39
+ readonly shadow: Shadow;
40
+ readonly clip: boolean;
41
+ }
42
+
43
+ /** How a system's graph values land on its plot area. */
44
+ export type MapFor<S> = (area: Extents, state: S) => CoordinateMap;
45
+
46
+ /** The plot area: the box less its padding, y-up from the box's centre. */
47
+ export function plotArea(size: Size2D, padding: InsetsResolved): Extents {
48
+ const inner = insetsOps.insetBounds(size.width, size.height, padding);
49
+ return {
50
+ minX: inner.x - inner.width / 2,
51
+ maxX: inner.x + inner.width / 2,
52
+ minY: -inner.y - inner.height / 2,
53
+ maxY: -inner.y + inner.height / 2,
54
+ };
55
+ }
56
+
57
+ /** A system's map at a laid-out `size`. */
58
+ export function systemMap<S extends SystemState>(size: Size2D, state: S, mapFor: MapFor<S>): CoordinateMap {
59
+ return mapFor(plotArea(size, state.padding), state);
60
+ }
61
+
62
+ /** A system is the size it is given: its children are placed in it, not measured into it. */
63
+ export function systemSize(state: SystemState): Size2D {
64
+ const { left, right, top, bottom } = state.padding;
65
+ return { width: left + right, height: top + bottom };
66
+ }
67
+
68
+ /** What a system hands its subtree: its plane, which its layout keeps current. */
69
+ export function systemProvides(state: object): readonly Provided[] {
70
+ return [provides(SYSTEM, systemPlane(state))];
71
+ }
72
+
73
+ /** Lay a system's children out on its map. */
74
+ export function systemLayout<S extends SystemState>(size: Size2D, state: S, children: readonly ChildView[], mapFor: MapFor<S>): Placement2D[] {
75
+ const map = systemMap(size, state, mapFor);
76
+ const plane = systemPlane(state);
77
+ settle(plane, map, children);
78
+ const inner = insetsOps.insetBounds(size.width, size.height, state.padding);
79
+ return placeOnMap(children, map, CENTRE, inner, plane);
80
+ }
81
+
82
+ /** Where a child dropped on a system would sit. */
83
+ export function systemOpenPlane<S extends SystemState>(size: Size2D, state: S, mapFor: MapFor<S>): ChildPlane {
84
+ return openPlane(systemMap(size, state, mapFor), CENTRE);
85
+ }
86
+
87
+ /**
88
+ * A system's paint: the backdrop, then the furniture under its children, kept
89
+ * in the plot area where the map spills from it, then the children, confined
90
+ * to the plot area when `clip` is set. The stroke is the axis line, drawn with
91
+ * the furniture: painted after the children it would land over them.
92
+ */
93
+ export function drawSystem(ctx: RenderContext, state: SystemState, map: CoordinateMap, furniture: (map: CoordinateMap) => Graphics2D[]): void {
94
+ const { width, height } = ctx.size;
95
+ const { minX, minY, maxX, maxY } = map.area;
96
+ const area = new Graphics2D().rect({ x: (minX + maxX) / 2, y: (minY + maxY) / 2, width: maxX - minX, height: maxY - minY });
97
+ ctx.inFront(() => {
98
+ ctx.draw(new Graphics2D().rect({ x: 0, y: 0, width, height }).shadow(state.shadow).fill(state.fill));
99
+ const parts = furniture(map);
100
+ if (map.spills) ctx.clip(area, () => ctx.draw(parts));
101
+ else ctx.draw(parts);
102
+ });
103
+ // No shape of its own to paint here; the plot area is what clips the children and what picking tests.
104
+ Paint.draw(ctx, state as unknown as Paint.PaintState, null, { clipShape: area });
105
+ }
@@ -0,0 +1,85 @@
1
+ import { niceStep } from "@motionscript/sdk/component";
2
+ import type { AxisScaling, Interval } from "./coords";
3
+
4
+ /** Where an axis is marked, and the spacing its labels are rounded to. */
5
+ export interface Ticks {
6
+ values: number[];
7
+ step: number;
8
+ }
9
+
10
+ /** Past this an axis is not marked at all, rather than drawn as a solid bar of ticks. */
11
+ const MAX_TICKS = 200;
12
+
13
+ /** How far apart, in pixels, marks sit when the axis picks its own step. */
14
+ const AUTO_SPACING = 64;
15
+
16
+ /**
17
+ * The marks along `range`, drawn over `length` pixels. A `step` of zero lets the
18
+ * axis choose a tidy one (1, 2 or 5 times a power of ten) for its size; a log
19
+ * axis is marked at the powers of ten whatever the step.
20
+ */
21
+ export function ticksFor(range: Interval, step: number, length: number, scaling: AxisScaling = "linear"): Ticks {
22
+ const [min, max] = range;
23
+ if (scaling === "log") return decades(min, max);
24
+ const span = max - min;
25
+ if (!(span > 0)) return { values: [], step: 1 };
26
+ const chosen = step > 0 ? step : niceStep((AUTO_SPACING / Math.max(1, length)) * span);
27
+ return { values: multiplesWithin(min, max, chosen), step: chosen };
28
+ }
29
+
30
+ function decades(min: number, max: number): Ticks {
31
+ const values: number[] = [];
32
+ for (let k = Math.ceil(Math.log10(min) - 1e-9); k <= Math.floor(Math.log10(max) + 1e-9) && values.length < MAX_TICKS; k++) {
33
+ values.push(10 ** k);
34
+ }
35
+ return { values, step: 0 };
36
+ }
37
+
38
+ function multiplesWithin(min: number, max: number, step: number): number[] {
39
+ const first = Math.ceil(min / step - 1e-9);
40
+ const last = Math.floor(max / step + 1e-9);
41
+ const count = last - first + 1;
42
+ if (!Number.isFinite(count) || count <= 0 || count > MAX_TICKS) return [];
43
+ const out = new Array<number>(count);
44
+ // Multiplied rather than accumulated, so the tenth tick of 0.1 is 1 and not 0.9999999999999999.
45
+ for (let i = 0; i < count; i++) out[i] = (first + i) * step;
46
+ return out;
47
+ }
48
+
49
+ function decadeOf(value: number): number {
50
+ return Math.floor(Math.log10(value) + 1e-12);
51
+ }
52
+
53
+ /**
54
+ * `value` as a tick label, with as many decimals as `step` needs and no more:
55
+ * 0.30000000000000004 on a 0.1 ladder is `0.3`. A `step` of zero (a log axis)
56
+ * prints the value as it is.
57
+ */
58
+ export function formatTick(value: number, step: number): string {
59
+ if (value === 0) return "0";
60
+ if (!Number.isFinite(value)) return "";
61
+ if (!(step > 0)) {
62
+ const decade = decadeOf(Math.abs(value));
63
+ return decade >= 6 || decade <= -5 ? trimExponent(value.toExponential(0)) : String(Number(value.toPrecision(12)));
64
+ }
65
+ const decade = decadeOf(Math.abs(value));
66
+ const digits = Math.max(1, decade - decadeOf(step) + 1);
67
+ if ((decade >= 6 || decade <= -5) && digits <= 6) return trimExponent(value.toExponential(digits - 1));
68
+ const decimals = decimalsOf(step);
69
+ const text = value.toFixed(decimals);
70
+ return decimals > 0 ? text.replace(/\.?0+$/, "") : text;
71
+ }
72
+
73
+ /** The decimal places it takes to write `step` out: 1 for 0.5, 2 for 0.25. */
74
+ function decimalsOf(step: number): number {
75
+ for (let d = 0; d < 12; d++) {
76
+ const scaled = step * 10 ** d;
77
+ if (Math.abs(scaled - Math.round(scaled)) < 1e-9 * Math.max(1, scaled)) return d;
78
+ }
79
+ return 12;
80
+ }
81
+
82
+ function trimExponent(text: string): string {
83
+ const [mantissa, exponent] = text.split("e");
84
+ return `${mantissa.replace(/\.?0+$/, "")}e${Number(exponent)}`;
85
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * The paint entries and commands a math node may have no use for: a node that
3
+ * is only a line has no fill, an area no outline, and one that reveals itself
4
+ * in its own way no outline trim.
5
+ */
6
+ export const FILL_PAINT = ["fill", "fillTo"] as const;
7
+ export const STROKE_PAINT = ["stroke", "strokeTo"] as const;
8
+ export const OUTLINE_TRIM = ["start", "end", "progressTo", "flash"] as const;
@@ -0,0 +1,89 @@
1
+ import {
2
+ component2d,
3
+ Types,
4
+ type NodeOf,
5
+ type NodeView,
6
+ type PaintProps,
7
+ type Vector2,
8
+ // Named so the emitted declaration reaches these through the sdk, not core.
9
+ type Fill as _Fill,
10
+ type FillResolved as _FillResolved,
11
+ type Node2D as _Node2D,
12
+ type Node2DProps as _Node2DProps,
13
+ type Shadow as _Shadow,
14
+ type ShadowResolved as _ShadowResolved,
15
+ } from "@motionscript/sdk";
16
+ import { omit, PAINT, PAINT_COMMANDS } from "../kit/component";
17
+ import type { Interval } from "../kit/coords";
18
+ import { curveValue } from "../kit/curve";
19
+ import { lerpInterval, PLOT_COLOR, readInterval } from "../kit/furniture";
20
+ import { clamp } from "../kit/geometry";
21
+ import { curveFor, PLANE_ENTRY, PLOT_REF } from "../kit/graph";
22
+ import { drawMark, inkSize, reading, traced, type MarkReading } from "../kit/mark";
23
+
24
+ export interface CurveAreaProps extends PaintProps {
25
+ on?: string;
26
+ under?: string;
27
+ domain?: Interval;
28
+ }
29
+
30
+ const state = {
31
+ // An area is only its fill, and has no outline to trim.
32
+ ...omit(PAINT, "stroke", "start", "end"),
33
+ /** The plot that bounds the area, by id. */
34
+ on: PLOT_REF,
35
+ /** A second plot to fill down to, in place of the x axis. */
36
+ under: PLOT_REF,
37
+ domain: Types.custom<Interval>([0, 1], { resolve: readInterval, lerp: lerpInterval }),
38
+ graph: PLANE_ENTRY,
39
+ };
40
+
41
+ interface AreaValues {
42
+ readonly on: NodeView | null;
43
+ readonly under: NodeView | null;
44
+ readonly domain: Interval;
45
+ }
46
+
47
+ /**
48
+ * The region between a plot and the x axis, or between two plots, over a span
49
+ * of x. It sits beside the plots it reads rather than inside one: it may read
50
+ * two, and it has a life of its own on the timeline, swept open or kept while
51
+ * a curve changes.
52
+ */
53
+ export const CurveArea = traced(component2d({
54
+ key: "curveArea",
55
+ state,
56
+ defaults: { fill: { type: "solid", color: PLOT_COLOR, opacity: 0.35 }, width: "hug", height: "hug" },
57
+ commands: {
58
+ ...omit(PAINT_COMMANDS, "strokeTo", "progressTo", "flash"),
59
+ /** Animate the span of x that is filled: sweep an area open, or slide it along. */
60
+ domainTo: { args: ["domain"] },
61
+ },
62
+ measure: (_measurer, s) => inkSize(traceArea(s, reading(s))),
63
+ render: (ctx, s) => drawMark(ctx, s, traceArea(s, reading(s)), { closed: true }),
64
+ }), traceArea);
65
+
66
+ export type CurveArea = NodeOf<typeof CurveArea>;
67
+
68
+ function traceArea(s: AreaValues, { map, plane }: MarkReading): Vector2[][] {
69
+ const top = curveFor(plane, s.on);
70
+ if (!top) return [];
71
+ const floor = curveFor(plane, s.under, false);
72
+ const [yMin, yMax] = map.yRange;
73
+ const a = Math.max(s.domain[0], map.xRange[0]);
74
+ const b = Math.min(s.domain[1], map.xRange[1]);
75
+ if (!(b > a)) return [];
76
+
77
+ const width = Math.abs(map.toPoint(b, 0).x - map.toPoint(a, 0).x);
78
+ const steps = Math.max(8, Math.min(600, Math.round(width / 2)));
79
+ // Held inside the system's range, and at the axis where a curve has no value.
80
+ const height = (value: number) => (Number.isFinite(value) ? clamp(value, yMin, yMax) : clamp(0, yMin, yMax));
81
+ const upper: Vector2[] = [];
82
+ const lower: Vector2[] = [];
83
+ for (let i = 0; i <= steps; i++) {
84
+ const x = a + ((b - a) * i) / steps;
85
+ upper.push(map.toPoint(x, height(curveValue(top, x))));
86
+ lower.push(map.toPoint(x, height(floor ? curveValue(floor, x) : 0)));
87
+ }
88
+ return [[...upper, ...lower.reverse()]];
89
+ }