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

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 +92 -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,69 @@
1
+ import type { Extents, Vector2 } from "@motionscript/sdk";
2
+ /** A closed span of graph values, low to high. */
3
+ export type Interval = readonly [number, number];
4
+ export type AxisScaling = "linear" | "log";
5
+ /**
6
+ * How a coordinate system's graph values land on its pixels. Pixels are y-up
7
+ * from the centre of the system's own box, which is the frame a `Graphics2D` op
8
+ * is written in.
9
+ */
10
+ export interface CoordinateMap {
11
+ /** Graph to pixels. Writes into `out` when given, so a sampler allocates nothing. */
12
+ toPoint(x: number, y: number, out?: Vector2): Vector2;
13
+ toCoords(x: number, y: number, out?: Vector2): Vector2;
14
+ /** The plot area. */
15
+ readonly area: Extents;
16
+ readonly xRange: Interval;
17
+ readonly yRange: Interval;
18
+ /** Equal for two maps that place every point alike: what a geometry cache keys on. */
19
+ readonly revision: string;
20
+ /** Set where a straight run of graph values is not a straight run of pixels, so a line through the map is sampled. */
21
+ readonly curved?: boolean;
22
+ /** Set where what is ruled over the ranges can leave the plot area, so the system keeps it in. */
23
+ readonly spills?: boolean;
24
+ }
25
+ /** One axis: a graph value to a pixel and back. */
26
+ export interface AxisScale {
27
+ to(value: number): number;
28
+ from(pixel: number): number;
29
+ }
30
+ /** `range`, made usable: finite, ordered, non-empty, and positive on a log axis. */
31
+ export declare function resolveInterval(range: Interval | undefined, scaling?: AxisScaling): Interval;
32
+ /** `range` spread over the pixels from `lo` to `hi`. */
33
+ export declare function axisScale(range: Interval, lo: number, hi: number, scaling?: AxisScaling): AxisScale;
34
+ export interface CartesianOptions {
35
+ xRange: Interval;
36
+ yRange: Interval;
37
+ area: Extents;
38
+ xScaling?: AxisScaling;
39
+ yScaling?: AxisScaling;
40
+ }
41
+ /** Two independent axes at right angles: what a number line, axes and a number plane share. */
42
+ export declare function cartesianMap({ xRange, yRange, area, xScaling, yScaling }: CartesianOptions): CoordinateMap;
43
+ /** One graph unit is one pixel: what a mark outside any system is drawn through. */
44
+ export declare const PIXEL_MAP: CoordinateMap;
45
+ /** A 2 × 2 matrix, row by row: `[a, b, c, d]` sends `(x, y)` to `(ax + by, cx + dy)`. */
46
+ export type Matrix2 = readonly [number, number, number, number];
47
+ export declare const IDENTITY_MATRIX: Matrix2;
48
+ /** A matrix from its rows, or flat; anything else is the identity. */
49
+ export declare function readMatrix(value: unknown, previous?: Matrix2): Matrix2;
50
+ export declare function lerpMatrix(from: Matrix2, to: Matrix2, t: number): Matrix2;
51
+ export declare function isIdentityMatrix([a, b, c, d]: Matrix2): boolean;
52
+ /**
53
+ * `base` with every graph point sent through `matrix` first: a plane sheared,
54
+ * turned or squeezed about its origin. Its ranges are what the plot area then
55
+ * shows, up to {@link REACH}, so a grid ruled over them fills the box under a
56
+ * turn, a shear or a fourfold shrink, and folds into a band as it goes flat.
57
+ */
58
+ export declare function transformedMap(base: CoordinateMap, matrix: Matrix2): CoordinateMap;
59
+ /** Where a warp sends a graph point, written into `out`; `false` where it has no value. */
60
+ export type Warp = (x: number, y: number, out: Vector2) => boolean;
61
+ /**
62
+ * `base` with every graph point sent to where `warp` sends it: a plane bent by
63
+ * a function of its own points. `key` names the warp for the revision, since a
64
+ * function has no text of its own.
65
+ */
66
+ export declare function warpedMap(base: CoordinateMap, warp: Warp, key: string): CoordinateMap;
67
+ /** The pixel points that draw the straight run of graph values from one point to another. */
68
+ export declare function lineThrough(map: CoordinateMap, x0: number, y0: number, x1: number, y1: number): Vector2[];
69
+ //# sourceMappingURL=coords.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"coords.d.ts","sourceRoot":"","sources":["../../src/kit/coords.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAE1D,kDAAkD;AAClD,MAAM,MAAM,QAAQ,GAAG,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAEjD,MAAM,MAAM,WAAW,GAAG,QAAQ,GAAG,KAAK,CAAC;AAE3C;;;;GAIG;AACH,MAAM,WAAW,aAAa;IAC1B,qFAAqF;IACrF,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC;IACtD,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC;IACvD,qBAAqB;IACrB,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IACv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M,eAAe,EAAE,OAAsB,CAAC;AAErD,sEAAsE;AACtE,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,EAAE,QAAQ,GAAE,OAAyB,GAAG,OAAO,CAIvF;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE,EAAE,OAAO,EAAE,CAAC,EAAE,MAAM,GAAG,OAAO,CAEzE;AAED,wBAAgB,gBAAgB,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAE/D;AAgBD;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,aAAa,EAAE,MAAM,EAAE,OAAO,GAAG,aAAa,CA8ClF;AAED,2FAA2F;AAC3F,MAAM,MAAM,IAAI,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,KAAK,OAAO,CAAC;AAEnE;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,aAAa,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,GAAG,aAAa,CAmDrF;AAaD,6FAA6F;AAC7F,wBAAgB,WAAW,CAAC,GAAG,EAAE,aAAa,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,EAAE,CAQzG"}
@@ -0,0 +1,224 @@
1
+ /** `range`, made usable: finite, ordered, non-empty, and positive on a log axis. */
2
+ export function resolveInterval(range, scaling = "linear") {
3
+ let [min, max] = range ?? [0, 1];
4
+ if (!Number.isFinite(min) || !Number.isFinite(max))
5
+ [min, max] = [0, 1];
6
+ if (min > max)
7
+ [min, max] = [max, min];
8
+ if (scaling === "log") {
9
+ if (!(max > 0))
10
+ max = 10;
11
+ if (!(min > 0))
12
+ min = max / 1000;
13
+ }
14
+ return max > min ? [min, max] : [min, min + 1];
15
+ }
16
+ /** `range` spread over the pixels from `lo` to `hi`. */
17
+ export function axisScale(range, lo, hi, scaling = "linear") {
18
+ const [min, max] = range;
19
+ if (scaling === "log") {
20
+ const from = Math.log10(min);
21
+ const k = (hi - lo) / (Math.log10(max) - from);
22
+ return { to: (v) => lo + (Math.log10(v) - from) * k, from: (p) => 10 ** (from + (p - lo) / k) };
23
+ }
24
+ const k = (hi - lo) / (max - min);
25
+ return { to: (v) => lo + (v - min) * k, from: (p) => min + (p - lo) / k };
26
+ }
27
+ /** Two independent axes at right angles: what a number line, axes and a number plane share. */
28
+ export function cartesianMap({ xRange, yRange, area, xScaling = "linear", yScaling = "linear" }) {
29
+ const x = axisScale(xRange, area.minX, area.maxX, xScaling);
30
+ const y = axisScale(yRange, area.minY, area.maxY, yScaling);
31
+ return {
32
+ toPoint: (gx, gy, out = { x: 0, y: 0 }) => {
33
+ out.x = x.to(gx);
34
+ out.y = y.to(gy);
35
+ return out;
36
+ },
37
+ toCoords: (px, py, out = { x: 0, y: 0 }) => {
38
+ out.x = x.from(px);
39
+ out.y = y.from(py);
40
+ return out;
41
+ },
42
+ area,
43
+ xRange,
44
+ yRange,
45
+ revision: [xRange, yRange, area.minX, area.minY, area.maxX, area.maxY, xScaling, yScaling].join("|"),
46
+ };
47
+ }
48
+ const UNBOUNDED = { minX: -Infinity, minY: -Infinity, maxX: Infinity, maxY: Infinity };
49
+ /** One graph unit is one pixel: what a mark outside any system is drawn through. */
50
+ export const PIXEL_MAP = {
51
+ toPoint: (x, y, out = { x: 0, y: 0 }) => {
52
+ out.x = x;
53
+ out.y = y;
54
+ return out;
55
+ },
56
+ toCoords: (x, y, out = { x: 0, y: 0 }) => {
57
+ out.x = x;
58
+ out.y = y;
59
+ return out;
60
+ },
61
+ area: UNBOUNDED,
62
+ xRange: [-Infinity, Infinity],
63
+ yRange: [-Infinity, Infinity],
64
+ revision: "pixels",
65
+ };
66
+ export const IDENTITY_MATRIX = [1, 0, 0, 1];
67
+ /** A matrix from its rows, or flat; anything else is the identity. */
68
+ export function readMatrix(value, previous = IDENTITY_MATRIX) {
69
+ const flat = Array.isArray(value) ? value.flat() : [];
70
+ if (flat.length !== 4 || flat.some((entry) => typeof entry !== "number" || !Number.isFinite(entry)))
71
+ return previous;
72
+ return [flat[0], flat[1], flat[2], flat[3]];
73
+ }
74
+ export function lerpMatrix(from, to, t) {
75
+ return [0, 1, 2, 3].map((i) => from[i] + (to[i] - from[i]) * t);
76
+ }
77
+ export function isIdentityMatrix([a, b, c, d]) {
78
+ return a === 1 && b === 0 && c === 0 && d === 1;
79
+ }
80
+ /**
81
+ * How far a transformed plane reaches, in half-widths of its own ranges about
82
+ * their middle. Finite on purpose: a plane squeezed towards a line shows the
83
+ * pre-image of its box growing as 1/det, and ruling all of it packs the box
84
+ * with lines, a solid wash just before the plane goes flat.
85
+ */
86
+ const REACH = 4;
87
+ function reach([min, max]) {
88
+ const middle = (min + max) / 2;
89
+ const half = ((max - min) / 2) * REACH;
90
+ return [middle - half, middle + half];
91
+ }
92
+ /**
93
+ * `base` with every graph point sent through `matrix` first: a plane sheared,
94
+ * turned or squeezed about its origin. Its ranges are what the plot area then
95
+ * shows, up to {@link REACH}, so a grid ruled over them fills the box under a
96
+ * turn, a shear or a fourfold shrink, and folds into a band as it goes flat.
97
+ */
98
+ export function transformedMap(base, matrix) {
99
+ const [a, b, c, d] = matrix;
100
+ const det = a * d - b * c;
101
+ // A plane squeezed onto a line has no way back: a point is read as the untransformed plane has it.
102
+ const flat = Math.abs(det) < 1e-9;
103
+ const back = (u, v, out) => {
104
+ if (flat) {
105
+ out.x = u;
106
+ out.y = v;
107
+ }
108
+ else {
109
+ out.x = (d * u - b * v) / det;
110
+ out.y = (a * v - c * u) / det;
111
+ }
112
+ return out;
113
+ };
114
+ const xReach = reach(base.xRange);
115
+ const yReach = reach(base.yRange);
116
+ let xRange = xReach;
117
+ let yRange = yReach;
118
+ if (!flat) {
119
+ const corner = { x: 0, y: 0 };
120
+ let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
121
+ for (const u of base.xRange) {
122
+ for (const v of base.yRange) {
123
+ back(u, v, corner);
124
+ minX = Math.min(minX, corner.x);
125
+ maxX = Math.max(maxX, corner.x);
126
+ minY = Math.min(minY, corner.y);
127
+ maxY = Math.max(maxY, corner.y);
128
+ }
129
+ }
130
+ xRange = [Math.max(minX, xReach[0]), Math.min(maxX, xReach[1])];
131
+ yRange = [Math.max(minY, yReach[0]), Math.min(maxY, yReach[1])];
132
+ }
133
+ return {
134
+ toPoint: (x, y, out = { x: 0, y: 0 }) => base.toPoint(a * x + b * y, c * x + d * y, out),
135
+ toCoords: (px, py, out = { x: 0, y: 0 }) => {
136
+ base.toCoords(px, py, out);
137
+ return back(out.x, out.y, out);
138
+ },
139
+ area: base.area,
140
+ xRange,
141
+ yRange,
142
+ revision: `${base.revision}|m${matrix.join(",")}`,
143
+ spills: true,
144
+ };
145
+ }
146
+ /**
147
+ * `base` with every graph point sent to where `warp` sends it: a plane bent by
148
+ * a function of its own points. `key` names the warp for the revision, since a
149
+ * function has no text of its own.
150
+ */
151
+ export function warpedMap(base, warp, key) {
152
+ const bend = (x, y, out) => {
153
+ // A point the warp has no value at stays where it is, so a pole does not tear the grid.
154
+ if (!warp(x, y, out)) {
155
+ out.x = x;
156
+ out.y = y;
157
+ }
158
+ return out;
159
+ };
160
+ const at = { x: 0, y: 0 };
161
+ const dx = { x: 0, y: 0 };
162
+ const dy = { x: 0, y: 0 };
163
+ const H = 1e-4;
164
+ return {
165
+ toPoint: (x, y, out = { x: 0, y: 0 }) => {
166
+ bend(x, y, out);
167
+ return base.toPoint(out.x, out.y, out);
168
+ },
169
+ // No closed form: a few Newton steps from where the unbent plane has the point.
170
+ toCoords: (px, py, out = { x: 0, y: 0 }) => {
171
+ const target = base.toCoords(px, py);
172
+ let gx = target.x;
173
+ let gy = target.y;
174
+ for (let i = 0; i < 8; i++) {
175
+ bend(gx, gy, at);
176
+ const ex = at.x - target.x;
177
+ const ey = at.y - target.y;
178
+ if (Math.abs(ex) + Math.abs(ey) < 1e-9)
179
+ break;
180
+ bend(gx + H, gy, dx);
181
+ bend(gx, gy + H, dy);
182
+ const j00 = (dx.x - at.x) / H;
183
+ const j10 = (dx.y - at.y) / H;
184
+ const j01 = (dy.x - at.x) / H;
185
+ const j11 = (dy.y - at.y) / H;
186
+ const det = j00 * j11 - j01 * j10;
187
+ if (!(Math.abs(det) > 1e-12))
188
+ break;
189
+ gx -= (j11 * ex - j01 * ey) / det;
190
+ gy -= (j00 * ey - j10 * ex) / det;
191
+ }
192
+ out.x = Number.isFinite(gx) ? gx : target.x;
193
+ out.y = Number.isFinite(gy) ? gy : target.y;
194
+ return out;
195
+ },
196
+ area: base.area,
197
+ // A bent plane pulls its edges in somewhere: it is ruled a little past them so the box stays full.
198
+ xRange: widened(base.xRange),
199
+ yRange: widened(base.yRange),
200
+ revision: `${base.revision}|w${key}`,
201
+ curved: true,
202
+ spills: true,
203
+ };
204
+ }
205
+ /** How much further than its range, as a share of it, a warped plane is ruled each way. */
206
+ const WARP_MARGIN = 0.3;
207
+ function widened([min, max]) {
208
+ const margin = (max - min) * WARP_MARGIN;
209
+ return Number.isFinite(margin) ? [min - margin, max + margin] : [min, max];
210
+ }
211
+ /** How many pieces a line through a curved map is drawn in. */
212
+ const CURVED_STEPS = 48;
213
+ /** The pixel points that draw the straight run of graph values from one point to another. */
214
+ export function lineThrough(map, x0, y0, x1, y1) {
215
+ if (!map.curved)
216
+ return [map.toPoint(x0, y0), map.toPoint(x1, y1)];
217
+ const run = [];
218
+ for (let i = 0; i <= CURVED_STEPS; i++) {
219
+ const t = i / CURVED_STEPS;
220
+ run.push(map.toPoint(x0 + (x1 - x0) * t, y0 + (y1 - y0) * t));
221
+ }
222
+ return run;
223
+ }
224
+ //# sourceMappingURL=coords.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"coords.js","sourceRoot":"","sources":["../../src/kit/coords.ts"],"names":[],"mappings":"AAkCA,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E,MAAM,GAAG,GAAc,EAAE,CAAC;IAC1B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,YAAY,EAAE,CAAC,EAAE,EAAE,CAAC;QACrC,MAAM,CAAC,GAAG,CAAC,GAAG,YAAY,CAAC;QAC3B,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,GAAG,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,EAAE,EAAE,GAAG,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IAClE,CAAC;IACD,OAAO,GAAG,CAAC;AACf,CAAC","sourcesContent":["import type { Extents, Vector2 } from \"@motionscript/sdk\";\r\n\r\n/** A closed span of graph values, low to high. */\r\nexport type Interval = readonly [number, number];\r\n\r\nexport type AxisScaling = \"linear\" | \"log\";\r\n\r\n/**\r\n * How a coordinate system's graph values land on its pixels. Pixels are y-up\r\n * from the centre of the system's own box, which is the frame a `Graphics2D` op\r\n * is written in.\r\n */\r\nexport interface CoordinateMap {\r\n /** Graph to pixels. Writes into `out` when given, so a sampler allocates nothing. */\r\n toPoint(x: number, y: number, out?: Vector2): Vector2;\r\n toCoords(x: number, y: number, out?: Vector2): Vector2;\r\n /** The plot area. */\r\n readonly area: Extents;\r\n readonly xRange: Interval;\r\n readonly yRange: Interval;\r\n /** Equal for two maps that place every point alike: what a geometry cache keys on. */\r\n readonly revision: string;\r\n /** Set where a straight run of graph values is not a straight run of pixels, so a line through the map is sampled. */\r\n readonly curved?: boolean;\r\n /** Set where what is ruled over the ranges can leave the plot area, so the system keeps it in. */\r\n readonly spills?: boolean;\r\n}\r\n\r\n/** One axis: a graph value to a pixel and back. */\r\nexport interface AxisScale {\r\n to(value: number): number;\r\n from(pixel: number): number;\r\n}\r\n\r\n/** `range`, made usable: finite, ordered, non-empty, and positive on a log axis. */\r\nexport function resolveInterval(range: Interval | undefined, scaling: AxisScaling = \"linear\"): Interval {\r\n let [min, max] = range ?? [0, 1];\r\n if (!Number.isFinite(min) || !Number.isFinite(max)) [min, max] = [0, 1];\r\n if (min > max) [min, max] = [max, min];\r\n if (scaling === \"log\") {\r\n if (!(max > 0)) max = 10;\r\n if (!(min > 0)) min = max / 1000;\r\n }\r\n return max > min ? [min, max] : [min, min + 1];\r\n}\r\n\r\n/** `range` spread over the pixels from `lo` to `hi`. */\r\nexport function axisScale(range: Interval, lo: number, hi: number, scaling: AxisScaling = \"linear\"): AxisScale {\r\n const [min, max] = range;\r\n if (scaling === \"log\") {\r\n const from = Math.log10(min);\r\n const k = (hi - lo) / (Math.log10(max) - from);\r\n return { to: (v) => lo + (Math.log10(v) - from) * k, from: (p) => 10 ** (from + (p - lo) / k) };\r\n }\r\n const k = (hi - lo) / (max - min);\r\n return { to: (v) => lo + (v - min) * k, from: (p) => min + (p - lo) / k };\r\n}\r\n\r\nexport interface CartesianOptions {\r\n xRange: Interval;\r\n yRange: Interval;\r\n area: Extents;\r\n xScaling?: AxisScaling;\r\n yScaling?: AxisScaling;\r\n}\r\n\r\n/** Two independent axes at right angles: what a number line, axes and a number plane share. */\r\nexport function cartesianMap({ xRange, yRange, area, xScaling = \"linear\", yScaling = \"linear\" }: CartesianOptions): CoordinateMap {\r\n const x = axisScale(xRange, area.minX, area.maxX, xScaling);\r\n const y = axisScale(yRange, area.minY, area.maxY, yScaling);\r\n return {\r\n toPoint: (gx, gy, out = { x: 0, y: 0 }) => {\r\n out.x = x.to(gx);\r\n out.y = y.to(gy);\r\n return out;\r\n },\r\n toCoords: (px, py, out = { x: 0, y: 0 }) => {\r\n out.x = x.from(px);\r\n out.y = y.from(py);\r\n return out;\r\n },\r\n area,\r\n xRange,\r\n yRange,\r\n revision: [xRange, yRange, area.minX, area.minY, area.maxX, area.maxY, xScaling, yScaling].join(\"|\"),\r\n };\r\n}\r\n\r\nconst UNBOUNDED: Extents = { minX: -Infinity, minY: -Infinity, maxX: Infinity, maxY: Infinity };\r\n\r\n/** One graph unit is one pixel: what a mark outside any system is drawn through. */\r\nexport const PIXEL_MAP: CoordinateMap = {\r\n toPoint: (x, y, out = { x: 0, y: 0 }) => {\r\n out.x = x;\r\n out.y = y;\r\n return out;\r\n },\r\n toCoords: (x, y, out = { x: 0, y: 0 }) => {\r\n out.x = x;\r\n out.y = y;\r\n return out;\r\n },\r\n area: UNBOUNDED,\r\n xRange: [-Infinity, Infinity],\r\n yRange: [-Infinity, Infinity],\r\n revision: \"pixels\",\r\n};\r\n\r\n/** A 2 × 2 matrix, row by row: `[a, b, c, d]` sends `(x, y)` to `(ax + by, cx + dy)`. */\r\nexport type Matrix2 = readonly [number, number, number, number];\r\n\r\nexport const IDENTITY_MATRIX: Matrix2 = [1, 0, 0, 1];\r\n\r\n/** A matrix from its rows, or flat; anything else is the identity. */\r\nexport function readMatrix(value: unknown, previous: Matrix2 = IDENTITY_MATRIX): Matrix2 {\r\n const flat = Array.isArray(value) ? value.flat() : [];\r\n if (flat.length !== 4 || flat.some((entry) => typeof entry !== \"number\" || !Number.isFinite(entry))) return previous;\r\n return [flat[0], flat[1], flat[2], flat[3]];\r\n}\r\n\r\nexport function lerpMatrix(from: Matrix2, to: Matrix2, t: number): Matrix2 {\r\n return [0, 1, 2, 3].map((i) => from[i] + (to[i] - from[i]) * t) as unknown as Matrix2;\r\n}\r\n\r\nexport function isIdentityMatrix([a, b, c, d]: Matrix2): boolean {\r\n return a === 1 && b === 0 && c === 0 && d === 1;\r\n}\r\n\r\n/**\r\n * How far a transformed plane reaches, in half-widths of its own ranges about\r\n * their middle. Finite on purpose: a plane squeezed towards a line shows the\r\n * pre-image of its box growing as 1/det, and ruling all of it packs the box\r\n * with lines, a solid wash just before the plane goes flat.\r\n */\r\nconst REACH = 4;\r\n\r\nfunction reach([min, max]: Interval): Interval {\r\n const middle = (min + max) / 2;\r\n const half = ((max - min) / 2) * REACH;\r\n return [middle - half, middle + half];\r\n}\r\n\r\n/**\r\n * `base` with every graph point sent through `matrix` first: a plane sheared,\r\n * turned or squeezed about its origin. Its ranges are what the plot area then\r\n * shows, up to {@link REACH}, so a grid ruled over them fills the box under a\r\n * turn, a shear or a fourfold shrink, and folds into a band as it goes flat.\r\n */\r\nexport function transformedMap(base: CoordinateMap, matrix: Matrix2): CoordinateMap {\r\n const [a, b, c, d] = matrix;\r\n const det = a * d - b * c;\r\n // A plane squeezed onto a line has no way back: a point is read as the untransformed plane has it.\r\n const flat = Math.abs(det) < 1e-9;\r\n const back = (u: number, v: number, out: Vector2): Vector2 => {\r\n if (flat) {\r\n out.x = u;\r\n out.y = v;\r\n } else {\r\n out.x = (d * u - b * v) / det;\r\n out.y = (a * v - c * u) / det;\r\n }\r\n return out;\r\n };\r\n const xReach = reach(base.xRange);\r\n const yReach = reach(base.yRange);\r\n let xRange = xReach;\r\n let yRange = yReach;\r\n if (!flat) {\r\n const corner: Vector2 = { x: 0, y: 0 };\r\n let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;\r\n for (const u of base.xRange) {\r\n for (const v of base.yRange) {\r\n back(u, v, corner);\r\n minX = Math.min(minX, corner.x);\r\n maxX = Math.max(maxX, corner.x);\r\n minY = Math.min(minY, corner.y);\r\n maxY = Math.max(maxY, corner.y);\r\n }\r\n }\r\n xRange = [Math.max(minX, xReach[0]), Math.min(maxX, xReach[1])];\r\n yRange = [Math.max(minY, yReach[0]), Math.min(maxY, yReach[1])];\r\n }\r\n return {\r\n toPoint: (x, y, out = { x: 0, y: 0 }) => base.toPoint(a * x + b * y, c * x + d * y, out),\r\n toCoords: (px, py, out = { x: 0, y: 0 }) => {\r\n base.toCoords(px, py, out);\r\n return back(out.x, out.y, out);\r\n },\r\n area: base.area,\r\n xRange,\r\n yRange,\r\n revision: `${base.revision}|m${matrix.join(\",\")}`,\r\n spills: true,\r\n };\r\n}\r\n\r\n/** Where a warp sends a graph point, written into `out`; `false` where it has no value. */\r\nexport type Warp = (x: number, y: number, out: Vector2) => boolean;\r\n\r\n/**\r\n * `base` with every graph point sent to where `warp` sends it: a plane bent by\r\n * a function of its own points. `key` names the warp for the revision, since a\r\n * function has no text of its own.\r\n */\r\nexport function warpedMap(base: CoordinateMap, warp: Warp, key: string): CoordinateMap {\r\n const bend = (x: number, y: number, out: Vector2): Vector2 => {\r\n // A point the warp has no value at stays where it is, so a pole does not tear the grid.\r\n if (!warp(x, y, out)) {\r\n out.x = x;\r\n out.y = y;\r\n }\r\n return out;\r\n };\r\n const at: Vector2 = { x: 0, y: 0 };\r\n const dx: Vector2 = { x: 0, y: 0 };\r\n const dy: Vector2 = { x: 0, y: 0 };\r\n const H = 1e-4;\r\n return {\r\n toPoint: (x, y, out = { x: 0, y: 0 }) => {\r\n bend(x, y, out);\r\n return base.toPoint(out.x, out.y, out);\r\n },\r\n // No closed form: a few Newton steps from where the unbent plane has the point.\r\n toCoords: (px, py, out = { x: 0, y: 0 }) => {\r\n const target = base.toCoords(px, py);\r\n let gx = target.x;\r\n let gy = target.y;\r\n for (let i = 0; i < 8; i++) {\r\n bend(gx, gy, at);\r\n const ex = at.x - target.x;\r\n const ey = at.y - target.y;\r\n if (Math.abs(ex) + Math.abs(ey) < 1e-9) break;\r\n bend(gx + H, gy, dx);\r\n bend(gx, gy + H, dy);\r\n const j00 = (dx.x - at.x) / H;\r\n const j10 = (dx.y - at.y) / H;\r\n const j01 = (dy.x - at.x) / H;\r\n const j11 = (dy.y - at.y) / H;\r\n const det = j00 * j11 - j01 * j10;\r\n if (!(Math.abs(det) > 1e-12)) break;\r\n gx -= (j11 * ex - j01 * ey) / det;\r\n gy -= (j00 * ey - j10 * ex) / det;\r\n }\r\n out.x = Number.isFinite(gx) ? gx : target.x;\r\n out.y = Number.isFinite(gy) ? gy : target.y;\r\n return out;\r\n },\r\n area: base.area,\r\n // A bent plane pulls its edges in somewhere: it is ruled a little past them so the box stays full.\r\n xRange: widened(base.xRange),\r\n yRange: widened(base.yRange),\r\n revision: `${base.revision}|w${key}`,\r\n curved: true,\r\n spills: true,\r\n };\r\n}\r\n\r\n/** How much further than its range, as a share of it, a warped plane is ruled each way. */\r\nconst WARP_MARGIN = 0.3;\r\n\r\nfunction widened([min, max]: Interval): Interval {\r\n const margin = (max - min) * WARP_MARGIN;\r\n return Number.isFinite(margin) ? [min - margin, max + margin] : [min, max];\r\n}\r\n\r\n/** How many pieces a line through a curved map is drawn in. */\r\nconst CURVED_STEPS = 48;\r\n\r\n/** The pixel points that draw the straight run of graph values from one point to another. */\r\nexport function lineThrough(map: CoordinateMap, x0: number, y0: number, x1: number, y1: number): Vector2[] {\r\n if (!map.curved) return [map.toPoint(x0, y0), map.toPoint(x1, y1)];\r\n const run: Vector2[] = [];\r\n for (let i = 0; i <= CURVED_STEPS; i++) {\r\n const t = i / CURVED_STEPS;\r\n run.push(map.toPoint(x0 + (x1 - x0) * t, y0 + (y1 - y0) * t));\r\n }\r\n return run;\r\n}\r\n"]}
@@ -0,0 +1,17 @@
1
+ import { type Node2D } from "@motionscript/sdk";
2
+ /**
3
+ * A node with a height at each `x`: what a point, a tangent or an area rides.
4
+ * A protocol rather than a class, so a copied plot is ridden like the kit's own.
5
+ */
6
+ export interface Curve {
7
+ /** The curve's height at graph `x`, or `NaN` where it has none. */
8
+ valueAt(x: number): number;
9
+ }
10
+ /** The curve `id` names in `from`'s tree, or `null`. */
11
+ export declare function curveNamed(from: Node2D, id: string): Curve | null;
12
+ /**
13
+ * The curve a mark reads: the one `on` names, or, naming none, the nearest
14
+ * curve the mark sits inside. Putting an area in a plot is how it is told which.
15
+ */
16
+ export declare function curveOf(mark: Node2D, on: string): Curve | null;
17
+ //# sourceMappingURL=curve.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"curve.d.ts","sourceRoot":"","sources":["../../src/kit/curve.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,KAAK,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAE5D;;;GAGG;AACH,MAAM,WAAW,KAAK;IAClB,mEAAmE;IACnE,OAAO,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;CAC9B;AAED,wDAAwD;AACxD,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,KAAK,GAAG,IAAI,CAIjE;AAED;;;GAGG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,KAAK,GAAG,IAAI,CAM9D"}
@@ -0,0 +1,22 @@
1
+ import { findNode2D } from "@motionscript/sdk";
2
+ /** The curve `id` names in `from`'s tree, or `null`. */
3
+ export function curveNamed(from, id) {
4
+ if (id === "")
5
+ return null;
6
+ const found = findNode2D(from, id);
7
+ return found && typeof found.valueAt === "function" ? found : null;
8
+ }
9
+ /**
10
+ * The curve a mark reads: the one `on` names, or, naming none, the nearest
11
+ * curve the mark sits inside. Putting an area in a plot is how it is told which.
12
+ */
13
+ export function curveOf(mark, on) {
14
+ if (on !== "")
15
+ return curveNamed(mark, on);
16
+ for (let at = mark.parent; at; at = at.parent) {
17
+ if (typeof at.valueAt === "function")
18
+ return at;
19
+ }
20
+ return null;
21
+ }
22
+ //# sourceMappingURL=curve.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"curve.js","sourceRoot":"","sources":["../../src/kit/curve.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAe,MAAM,mBAAmB,CAAC;AAW5D,wDAAwD;AACxD,MAAM,UAAU,UAAU,CAAC,IAAY,EAAE,EAAU;IAC/C,IAAI,EAAE,KAAK,EAAE;QAAE,OAAO,IAAI,CAAC;IAC3B,MAAM,KAAK,GAAG,UAAU,CAAC,IAAI,EAAE,EAAE,CAAqC,CAAC;IACvE,OAAO,KAAK,IAAI,OAAO,KAAK,CAAC,OAAO,KAAK,UAAU,CAAC,CAAC,CAAE,KAAwB,CAAC,CAAC,CAAC,IAAI,CAAC;AAC3F,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,OAAO,CAAC,IAAY,EAAE,EAAU;IAC5C,IAAI,EAAE,KAAK,EAAE;QAAE,OAAO,UAAU,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;IAC3C,KAAK,IAAI,EAAE,GAAkB,IAAI,CAAC,MAAM,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,CAAC,MAAM,EAAE,CAAC;QAC3D,IAAI,OAAQ,EAAqB,CAAC,OAAO,KAAK,UAAU;YAAE,OAAO,EAAoB,CAAC;IAC1F,CAAC;IACD,OAAO,IAAI,CAAC;AAChB,CAAC","sourcesContent":["import { findNode2D, type Node2D } from \"@motionscript/sdk\";\r\n\r\n/**\r\n * A node with a height at each `x`: what a point, a tangent or an area rides.\r\n * A protocol rather than a class, so a copied plot is ridden like the kit's own.\r\n */\r\nexport interface Curve {\r\n /** The curve's height at graph `x`, or `NaN` where it has none. */\r\n valueAt(x: number): number;\r\n}\r\n\r\n/** The curve `id` names in `from`'s tree, or `null`. */\r\nexport function curveNamed(from: Node2D, id: string): Curve | null {\r\n if (id === \"\") return null;\r\n const found = findNode2D(from, id) as (Node2D & Partial<Curve>) | null;\r\n return found && typeof found.valueAt === \"function\" ? (found as Node2D & Curve) : null;\r\n}\r\n\r\n/**\r\n * The curve a mark reads: the one `on` names, or, naming none, the nearest\r\n * curve the mark sits inside. Putting an area in a plot is how it is told which.\r\n */\r\nexport function curveOf(mark: Node2D, on: string): Curve | null {\r\n if (on !== \"\") return curveNamed(mark, on);\r\n for (let at: Node2D | null = mark.parent; at; at = at.parent) {\r\n if (typeof (at as Partial<Curve>).valueAt === \"function\") return at as Node2D & Curve;\r\n }\r\n return null;\r\n}\r\n"]}
@@ -0,0 +1,61 @@
1
+ import { type CompiledExpression } from "./expression.js";
2
+ /**
3
+ * A plotted equation, read the way it is written rather than by a `kind` prop:
4
+ *
5
+ * | Written as | Is |
6
+ * |---|---|
7
+ * | `sin(x)`, `y = sin(x)` | a function of `x` |
8
+ * | `x = y^2` | a function of `y` |
9
+ * | `r = 1 + cos(theta)` | polar, in `theta` |
10
+ * | `(cos(t), sin(t))` | parametric, in `t` |
11
+ * | `x^2 + y^2 = 4` | implicit, in `x` and `y` |
12
+ */
13
+ export type Equation = {
14
+ kind: "function";
15
+ of: "x" | "y";
16
+ f: CompiledExpression;
17
+ } | {
18
+ kind: "polar";
19
+ f: CompiledExpression;
20
+ } | {
21
+ kind: "parametric";
22
+ x: CompiledExpression;
23
+ y: CompiledExpression;
24
+ }
25
+ /** Drawn where `f(x, y)` is zero. */
26
+ | {
27
+ kind: "implicit";
28
+ f: CompiledExpression;
29
+ };
30
+ /** The free variable each kind is sampled over, for a host's help text. */
31
+ export declare const EQUATION_VARIABLES: {
32
+ readonly function: readonly ["x"];
33
+ readonly polar: readonly ["theta"];
34
+ readonly parametric: readonly ["t"];
35
+ readonly implicit: readonly ["x", "y"];
36
+ };
37
+ /**
38
+ * `source` as an {@link Equation}, or the reason it is not one. Memoised, so an
39
+ * unchanged equation is the identical object and a geometry cache can key on it.
40
+ */
41
+ export declare function parseEquation(source: string, params?: readonly string[]): Equation | Error;
42
+ /**
43
+ * Why `source` will not plot, or `null` when it will. An empty one is not an
44
+ * error (nobody has written it yet), and neither is a name it does not know:
45
+ * that is a named value, see {@link equationParams}.
46
+ */
47
+ export declare function equationError(source: string): string | null;
48
+ /**
49
+ * Every name `source` reads, in the order it first names them, variables
50
+ * included. Empty for text that does not parse.
51
+ */
52
+ export declare function equationNames(source: string): readonly string[];
53
+ /**
54
+ * The named values `source` reads: every name that is not a variable, a
55
+ * function or a constant.
56
+ */
57
+ export declare function equationParams(source: string): readonly string[];
58
+ /** `(a, b, …)` as its parts, or `null` when `source` is not one parenthesised list of two or more. */
59
+ export declare function splitTuple(source: string): string[] | null;
60
+ export declare function splitTopLevel(source: string, separator: string): string[];
61
+ //# sourceMappingURL=equation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"equation.d.ts","sourceRoot":"","sources":["../../src/kit/equation.ts"],"names":[],"mappings":"AAEA,OAAO,EAAyC,KAAK,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAE9F;;;;;;;;;;GAUG;AACH,MAAM,MAAM,QAAQ,GACd;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,EAAE,EAAE,GAAG,GAAG,GAAG,CAAC;IAAC,CAAC,EAAE,kBAAkB,CAAA;CAAE,GAC1D;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,CAAC,EAAE,kBAAkB,CAAA;CAAE,GACxC;IAAE,IAAI,EAAE,YAAY,CAAC;IAAC,CAAC,EAAE,kBAAkB,CAAC;IAAC,CAAC,EAAE,kBAAkB,CAAA;CAAE;AACtE,qCAAqC;GACnC;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,CAAC,EAAE,kBAAkB,CAAA;CAAE,CAAC;AAElD,2EAA2E;AAC3E,eAAO,MAAM,kBAAkB;;;;;CAKiC,CAAC;AAIjE;;;GAGG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,GAAE,SAAS,MAAM,EAAO,GAAG,QAAQ,GAAG,KAAK,CAY9F;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAI3D;AAOD;;;GAGG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,MAAM,EAAE,CAe/D;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,MAAM,EAAE,CAEhE;AA4DD,sGAAsG;AACtG,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,IAAI,CAS1D;AAED,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,EAAE,CAezE"}
@@ -0,0 +1,168 @@
1
+ import { expressionOps } from "@motionscript/sdk";
2
+ import { createBoundedCache } from "./cache.js";
3
+ import { compileExpressionCached, MATH_DIALECT } from "./expression.js";
4
+ /** The free variable each kind is sampled over, for a host's help text. */
5
+ export const EQUATION_VARIABLES = {
6
+ function: ["x"],
7
+ polar: ["theta"],
8
+ parametric: ["t"],
9
+ implicit: ["x", "y"],
10
+ };
11
+ const parsed = createBoundedCache(256);
12
+ /**
13
+ * `source` as an {@link Equation}, or the reason it is not one. Memoised, so an
14
+ * unchanged equation is the identical object and a geometry cache can key on it.
15
+ */
16
+ export function parseEquation(source, params = []) {
17
+ const key = `${params.join(",")}|${source}`;
18
+ const hit = parsed.get(key);
19
+ if (hit !== undefined)
20
+ return hit;
21
+ let result;
22
+ try {
23
+ result = read(source.trim(), params);
24
+ }
25
+ catch (error) {
26
+ result = error instanceof Error ? error : new Error(String(error));
27
+ }
28
+ parsed.set(key, result);
29
+ return result;
30
+ }
31
+ /**
32
+ * Why `source` will not plot, or `null` when it will. An empty one is not an
33
+ * error (nobody has written it yet), and neither is a name it does not know:
34
+ * that is a named value, see {@link equationParams}.
35
+ */
36
+ export function equationError(source) {
37
+ if (source.trim() === "")
38
+ return null;
39
+ const equation = parseEquation(source, equationParams(source));
40
+ return equation instanceof Error ? equation.message : null;
41
+ }
42
+ /** A name that is always a variable of some kind of equation, so never a named value. */
43
+ const VARIABLE_NAMES = new Set(["r", ...Object.values(EQUATION_VARIABLES).flat()]);
44
+ const named = createBoundedCache(256);
45
+ /**
46
+ * Every name `source` reads, in the order it first names them, variables
47
+ * included. Empty for text that does not parse.
48
+ */
49
+ export function equationNames(source) {
50
+ const hit = named.get(source);
51
+ if (hit !== undefined)
52
+ return hit;
53
+ const names = new Set();
54
+ const text = source.trim();
55
+ try {
56
+ for (const part of splitTuple(text) ?? splitTopLevel(text, "=")) {
57
+ collectNames(expressionOps.parse(part.trim(), MATH_DIALECT).root, names);
58
+ }
59
+ }
60
+ catch {
61
+ names.clear();
62
+ }
63
+ const result = [...names];
64
+ named.set(source, result);
65
+ return result;
66
+ }
67
+ /**
68
+ * The named values `source` reads: every name that is not a variable, a
69
+ * function or a constant.
70
+ */
71
+ export function equationParams(source) {
72
+ return equationNames(source).filter((name) => !VARIABLE_NAMES.has(name));
73
+ }
74
+ function collectNames(node, into) {
75
+ switch (node.kind) {
76
+ case "ref":
77
+ into.add(node.name);
78
+ return;
79
+ case "unary":
80
+ collectNames(node.operand, into);
81
+ return;
82
+ case "binary":
83
+ collectNames(node.left, into);
84
+ collectNames(node.right, into);
85
+ return;
86
+ case "call":
87
+ for (const arg of node.args)
88
+ collectNames(arg, into);
89
+ return;
90
+ default:
91
+ }
92
+ }
93
+ function read(source, params) {
94
+ if (source === "")
95
+ throw new Error("Empty equation");
96
+ const compile = (text, variables) => {
97
+ const result = compileExpressionCached(text.trim(), variables, params);
98
+ if (result instanceof Error)
99
+ throw result;
100
+ return result;
101
+ };
102
+ const pair = splitTuple(source);
103
+ if (pair?.length === 2)
104
+ return { kind: "parametric", x: compile(pair[0], ["t"]), y: compile(pair[1], ["t"]) };
105
+ const sides = splitTopLevel(source, "=");
106
+ if (sides.length > 2)
107
+ throw new Error("An equation has one '='");
108
+ if (sides.length === 1)
109
+ return { kind: "function", of: "x", f: compile(source, ["x"]) };
110
+ const [left, right] = sides;
111
+ const subject = left.trim();
112
+ const implicit = () => {
113
+ const l = compile(left, ["x", "y"]);
114
+ const r = compile(right, ["x", "y"]);
115
+ return { kind: "implicit", f: (x, y, scope) => l(x, y, scope) - r(x, y, scope) };
116
+ };
117
+ if (subject === "r")
118
+ return { kind: "polar", f: compile(right, ["theta"]) };
119
+ if (subject === "y" || subject === "x") {
120
+ const of = subject === "y" ? "x" : "y";
121
+ // `y = x + y` names its own subject on the right, so it is not a function of one variable.
122
+ try {
123
+ return { kind: "function", of, f: compile(right, [of]) };
124
+ }
125
+ catch (error) {
126
+ try {
127
+ return implicit();
128
+ }
129
+ catch {
130
+ throw error;
131
+ }
132
+ }
133
+ }
134
+ return implicit();
135
+ }
136
+ /** `(a, b, …)` as its parts, or `null` when `source` is not one parenthesised list of two or more. */
137
+ export function splitTuple(source) {
138
+ if (!source.startsWith("(") || !source.endsWith(")"))
139
+ return null;
140
+ let depth = 0;
141
+ for (let i = 0; i < source.length - 1; i++) {
142
+ if (source[i] === "(")
143
+ depth++;
144
+ else if (source[i] === ")" && --depth === 0)
145
+ return null;
146
+ }
147
+ const parts = splitTopLevel(source.slice(1, -1), ",");
148
+ return parts.length >= 2 ? parts : null;
149
+ }
150
+ export function splitTopLevel(source, separator) {
151
+ const parts = [];
152
+ let depth = 0;
153
+ let from = 0;
154
+ for (let i = 0; i < source.length; i++) {
155
+ const c = source[i];
156
+ if (c === "(")
157
+ depth++;
158
+ else if (c === ")")
159
+ depth--;
160
+ else if (c === separator && depth === 0) {
161
+ parts.push(source.slice(from, i));
162
+ from = i + 1;
163
+ }
164
+ }
165
+ parts.push(source.slice(from));
166
+ return parts;
167
+ }
168
+ //# sourceMappingURL=equation.js.map