@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,159 @@
1
+ import { expressionOps } from "@motionscript/sdk";
2
+ import type { ExpressionDialect, ExpressionNode } from "@motionscript/sdk";
3
+ import { createBoundedCache } from "./cache";
4
+
5
+ /**
6
+ * The language a plot's equation is written in: the engine's own expression
7
+ * grammar (`expressionOps.parse`) under a dialect of its own, where trig is in
8
+ * radians, names are bare identifiers, and the logs and hyperbolics a plot
9
+ * wants are present. Never `new Function`: an equation is text stored in a
10
+ * document, and the shared parser can only produce arithmetic.
11
+ */
12
+
13
+ /** Named values an expression reads at call time, beside its two variables. */
14
+ export type ExpressionScope = Readonly<Record<string, number>>;
15
+
16
+ /**
17
+ * A compiled expression. The two variables are fixed slots because this is the
18
+ * hot path of every sampled curve; a one-variable expression never reads `b`.
19
+ */
20
+ export type CompiledExpression = (a: number, b: number, scope?: ExpressionScope) => number;
21
+
22
+ const UNARY: Record<string, (v: number) => number> = {
23
+ sin: Math.sin, cos: Math.cos, tan: Math.tan,
24
+ asin: Math.asin, acos: Math.acos, atan: Math.atan,
25
+ sinh: Math.sinh, cosh: Math.cosh, tanh: Math.tanh,
26
+ sqrt: Math.sqrt, cbrt: Math.cbrt, abs: Math.abs, exp: Math.exp,
27
+ log: Math.log, ln: Math.log, log2: Math.log2, log10: Math.log10,
28
+ floor: Math.floor, ceil: Math.ceil, round: Math.round, sign: Math.sign, trunc: Math.trunc,
29
+ };
30
+
31
+ const NARY: Record<string, { fn: (...args: number[]) => number; arity: [number, number] }> = {
32
+ atan2: { fn: Math.atan2, arity: [2, 2] },
33
+ pow: { fn: Math.pow, arity: [2, 2] },
34
+ mod: { fn: (a, b) => a % b, arity: [2, 2] },
35
+ hypot: { fn: Math.hypot, arity: [1, Infinity] },
36
+ min: { fn: Math.min, arity: [1, Infinity] },
37
+ max: { fn: Math.max, arity: [1, Infinity] },
38
+ };
39
+
40
+ const CONSTANTS: Record<string, number> = {
41
+ pi: Math.PI, PI: Math.PI, e: Math.E, E: Math.E, tau: Math.PI * 2, phi: (1 + Math.sqrt(5)) / 2,
42
+ };
43
+
44
+ export const MATH_DIALECT: ExpressionDialect = {
45
+ functions: {
46
+ ...Object.fromEntries(Object.keys(UNARY).map((name) => [name, { arity: [1, 1] as [number, number] }])),
47
+ ...Object.fromEntries(Object.entries(NARY).map(([name, { arity }]) => [name, { arity }])),
48
+ },
49
+ constants: CONSTANTS,
50
+ refs: "bare",
51
+ unknownName: (name) => `Unknown name '${name}'`,
52
+ };
53
+
54
+ /** What an expression may name, for a host's "what can I write here" help. */
55
+ export interface ExpressionVocabulary {
56
+ variables: readonly string[];
57
+ functions: readonly string[];
58
+ constants: readonly string[];
59
+ }
60
+
61
+ export function expressionVocabulary(variables: readonly string[]): ExpressionVocabulary {
62
+ return {
63
+ variables,
64
+ functions: [...Object.keys(UNARY), ...Object.keys(NARY)].sort(),
65
+ constants: ["pi", "e", "tau", "phi"],
66
+ };
67
+ }
68
+
69
+ /**
70
+ * Compile `source` over up to two `variables`, in slot order, and any `params`
71
+ * it may read from the scope. Throws on a syntax error or an unknown name.
72
+ */
73
+ export function compileExpression(
74
+ source: string,
75
+ variables: readonly string[],
76
+ params: readonly string[] = [],
77
+ ): CompiledExpression {
78
+ // The parser folds a constant before it yields a reference, so a name that
79
+ // is both would silently stop being the variable it was passed as.
80
+ for (const name of [...variables, ...params]) {
81
+ if (Object.hasOwn(CONSTANTS, name)) throw new Error(`'${name}' is a constant, so it cannot be a variable`);
82
+ }
83
+ return emit(expressionOps.parse(source, MATH_DIALECT).root, variables, params);
84
+ }
85
+
86
+ function emit(node: ExpressionNode, variables: readonly string[], params: readonly string[]): CompiledExpression {
87
+ switch (node.kind) {
88
+ case "const": {
89
+ const { value } = node;
90
+ return () => value;
91
+ }
92
+ case "ref": {
93
+ const { name } = node;
94
+ const slot = variables.indexOf(name);
95
+ if (slot === 0) return (a) => a;
96
+ if (slot === 1) return (_a, b) => b;
97
+ if (params.includes(name)) return (_a, _b, scope) => scope?.[name] ?? NaN;
98
+ throw new Error(MATH_DIALECT.unknownName(name));
99
+ }
100
+ case "unary": {
101
+ const operand = emit(node.operand, variables, params);
102
+ return (a, b, s) => -operand(a, b, s);
103
+ }
104
+ case "binary": {
105
+ const l = emit(node.left, variables, params);
106
+ const r = emit(node.right, variables, params);
107
+ switch (node.op) {
108
+ case "+": return (a, b, s) => l(a, b, s) + r(a, b, s);
109
+ case "-": return (a, b, s) => l(a, b, s) - r(a, b, s);
110
+ case "*": return (a, b, s) => l(a, b, s) * r(a, b, s);
111
+ case "/": return (a, b, s) => l(a, b, s) / r(a, b, s);
112
+ case "%": return (a, b, s) => l(a, b, s) % r(a, b, s);
113
+ case "^": return (a, b, s) => Math.pow(l(a, b, s), r(a, b, s));
114
+ }
115
+ break;
116
+ }
117
+ case "call": {
118
+ const args = node.args.map((arg) => emit(arg, variables, params));
119
+ const unary = UNARY[node.name];
120
+ if (unary) {
121
+ const [x] = args;
122
+ return (a, b, s) => unary(x(a, b, s));
123
+ }
124
+ const { fn } = NARY[node.name];
125
+ // The common arity, so the hot path allocates no array per sample.
126
+ if (args.length === 2) {
127
+ const [x, y] = args;
128
+ return (a, b, s) => fn(x(a, b, s), y(a, b, s));
129
+ }
130
+ return (a, b, s) => fn(...args.map((arg) => arg(a, b, s)));
131
+ }
132
+ }
133
+ throw new Error("Unsupported expression");
134
+ }
135
+
136
+ const compiled = createBoundedCache<CompiledExpression | Error>(256);
137
+
138
+ /**
139
+ * {@link compileExpression}, memoised: equal source under equal names is the
140
+ * identical closure, so "did this expression change" is an `===`, and a failure
141
+ * is remembered rather than re-parsed every frame.
142
+ */
143
+ export function compileExpressionCached(
144
+ source: string,
145
+ variables: readonly string[],
146
+ params: readonly string[] = [],
147
+ ): CompiledExpression | Error {
148
+ const key = `${variables.join(",")}|${params.join(",")}|${source}`;
149
+ const hit = compiled.get(key);
150
+ if (hit !== undefined) return hit;
151
+ let result: CompiledExpression | Error;
152
+ try {
153
+ result = compileExpression(source, variables, params);
154
+ } catch (error) {
155
+ result = error instanceof Error ? error : new Error(String(error));
156
+ }
157
+ compiled.set(key, result);
158
+ return result;
159
+ }
@@ -0,0 +1,171 @@
1
+ import {
2
+ Graphics2D,
3
+ Types,
4
+ type Fill,
5
+ type FillResolved,
6
+ type PaintProps,
7
+ type RenderContext,
8
+ type StrokeResolved,
9
+ type Vector2,
10
+ // Named so the emitted declaration reaches these through the sdk, not core.
11
+ type Shadow as _Shadow,
12
+ type ShadowResolved as _ShadowResolved,
13
+ type Stroke as _Stroke,
14
+ } from "@motionscript/sdk";
15
+ import { createBoundedCache } from "./cache";
16
+ import { omit, PAINT, PAINT_COMMANDS } from "./component";
17
+ import type { CoordinateMap } from "./coords";
18
+ import type { ExpressionScope } from "./expression";
19
+ import { fieldParams, parseField } from "./field";
20
+ import { runsToPath } from "./geometry";
21
+ import { PLANE_ENTRY } from "./graph";
22
+ import { drawMark, type MarkPaint, type MarkReading } from "./mark";
23
+ import { fillAt, lerpFillScale, readFillScale } from "./scale";
24
+ import { lerpParams, lerpSource, readSource, scopeFor, type PlotSource } from "./source";
25
+
26
+ /**
27
+ * What a mark drawn from a vector field, `(P(x, y), Q(x, y))`, shares: arrows,
28
+ * or the lines that follow it, all in one stroke. Without strength fills it is
29
+ * one path in the stroke's paint; with them, what it draws is sorted by the
30
+ * field's strength into a few shades.
31
+ */
32
+
33
+ export interface FieldProps extends PaintProps {
34
+ expression?: string | PlotSource;
35
+ params?: ExpressionScope;
36
+ strengthFills?: Fill[];
37
+ maxMagnitude?: number;
38
+ }
39
+
40
+ /** A field's vector at a graph point, written into `out`; `false` where it has none. */
41
+ export type FieldSampler = (x: number, y: number, out: Vector2) => boolean;
42
+
43
+ /** One thing a field draws, and how strong the field is there: what it is coloured by. */
44
+ export interface FieldStroke {
45
+ runs: Vector2[][];
46
+ magnitude: number;
47
+ }
48
+
49
+ /** How many shades the strength fills are drawn in: each is one path. */
50
+ const SHADES = 12;
51
+
52
+ /** A field mark's entries. It is only strokes, revealed its own way: it has no fill and trims no outline. */
53
+ export const FIELD_STATE = {
54
+ ...omit(PAINT, "fill", "start", "end"),
55
+ expression: Types.custom<string | PlotSource, PlotSource>("", { resolve: readSource, lerp: lerpSource, kind: "fieldEquation" }),
56
+ /** The equation's named values: any name in it that is not `x`, `y`, a function or a constant. */
57
+ params: Types.custom<ExpressionScope>({}, { lerp: lerpParams }),
58
+ /** Paints by the field's strength, weakest first. Empty draws everything in the stroke's own paint. */
59
+ strengthFills: Types.custom<Fill[], FillResolved[][]>([], { resolve: readFillScale, lerp: lerpFillScale, kind: "vector" }),
60
+ /** The strength the last paint stands for; zero takes the strongest the field is where it is drawn. */
61
+ maxMagnitude: Types.number(0, { min: 0, step: 0.1 }),
62
+ graph: PLANE_ENTRY,
63
+ };
64
+
65
+ export const FIELD_COMMANDS = {
66
+ ...omit(PAINT_COMMANDS, "fillTo", "progressTo", "flash"),
67
+ /** Change the field, each vector turning from the one into the other. */
68
+ morphTo: { args: ["expression"] as const },
69
+ /** Animate the equation's named values. */
70
+ paramsTo: { args: ["params"] as const },
71
+ };
72
+
73
+ /** What a field mark is drawn from. */
74
+ export interface FieldValues {
75
+ readonly expression: PlotSource;
76
+ readonly params: ExpressionScope;
77
+ }
78
+
79
+ /** What one kind of field mark draws of `field` through `map`, and the entries of its own that change it. */
80
+ export interface FieldDrawing<S> {
81
+ strokes(s: S, map: CoordinateMap, field: FieldSampler): FieldStroke[];
82
+ /** The entries of its own that change what `strokes` returns, as a key. */
83
+ own(s: S): string;
84
+ }
85
+
86
+ interface Traced {
87
+ readonly drawn: FieldStroke[];
88
+ readonly runs: Vector2[][];
89
+ }
90
+
91
+ const traces = createBoundedCache<Traced>(64);
92
+ const EMPTY: Traced = { drawn: [], runs: [] };
93
+
94
+ /** What a field mark of `kind` draws through `read`: each stroke with its strength, and all their runs. */
95
+ export function traceField<S extends FieldValues>(kind: string, drawing: FieldDrawing<S>, s: S, { map, shift }: MarkReading): Traced {
96
+ const { area } = map;
97
+ if (!Number.isFinite(area.maxX - area.minX)) return EMPTY;
98
+ const { source, blend } = s.expression;
99
+ const key = [kind, source, blend?.from, blend?.t, map.revision, JSON.stringify(s.params), shift.x, shift.y, drawing.own(s)].join("|");
100
+ const known = traces.get(key);
101
+ if (known) return known;
102
+ const field = sampler(s, shift);
103
+ const drawn = field ? drawing.strokes(s, map, field) : [];
104
+ const traced = { drawn, runs: drawn.flatMap((stroke) => stroke.runs) };
105
+ traces.set(key, traced);
106
+ return traced;
107
+ }
108
+
109
+ /** A field mark's paint: one stroke, or a shade per strength when it has strength fills. */
110
+ export function drawField(
111
+ ctx: RenderContext,
112
+ s: MarkPaint & { readonly strengthFills: FillResolved[][]; readonly maxMagnitude: number },
113
+ { drawn, runs }: Traced,
114
+ ): void {
115
+ const scale = s.strengthFills;
116
+ const stroke = s.stroke as StrokeResolved[];
117
+ const shaded = scale.length > 0 && stroke.length > 0;
118
+ drawMark(ctx, s, runs, {
119
+ ink: shaded
120
+ ? (ctx, ink) => {
121
+ const top = s.maxMagnitude > 0 ? s.maxMagnitude : drawn.reduce((most, d) => Math.max(most, d.magnitude), 0);
122
+ const shades: Vector2[][][] = Array.from({ length: SHADES }, () => []);
123
+ for (const d of drawn) {
124
+ const share = top > 0 ? Math.min(1, d.magnitude / top) : 0;
125
+ shades[Math.min(SHADES - 1, Math.floor(share * SHADES))].push(...d.runs);
126
+ }
127
+ shades.forEach((shade, index) => {
128
+ if (shade.length === 0) return;
129
+ const fill = fillAt(scale, (index + 0.5) / SHADES);
130
+ ctx.draw(
131
+ new Graphics2D()
132
+ .path({ data: runsToPath(shade), centerBounds: [ink.minX, ink.minY, ink.maxX, ink.maxY], start: s.start ?? 0, end: s.end ?? 1 })
133
+ .shadow(s.shadow)
134
+ .stroke(stroke.map((layer) => ({ ...layer, fill }))),
135
+ );
136
+ });
137
+ }
138
+ : undefined,
139
+ });
140
+ }
141
+
142
+ /** The field at a graph point, measured from the mark's own shift, mid-morph a blend of two. */
143
+ function sampler(s: FieldValues, shift: Vector2): FieldSampler | null {
144
+ const { source, blend } = s.expression;
145
+ const to = fieldOf(source, s.params, shift);
146
+ if (!to || !blend) return to;
147
+ const from = fieldOf(blend.from, s.params, shift);
148
+ if (!from) return to;
149
+ const start: Vector2 = { x: 0, y: 0 };
150
+ return (x, y, out) => {
151
+ if (!from(x, y, start) || !to(x, y, out)) return false;
152
+ out.x = start.x + (out.x - start.x) * blend.t;
153
+ out.y = start.y + (out.y - start.y) * blend.t;
154
+ return true;
155
+ };
156
+ }
157
+
158
+ function fieldOf(source: string, params: ExpressionScope, shift: Vector2): FieldSampler | null {
159
+ const names = fieldParams(source);
160
+ const field = parseField(source, names);
161
+ if (field instanceof Error) return null;
162
+ const scope = scopeFor(names, params);
163
+ const { x, y } = field;
164
+ return (gx, gy, out) => {
165
+ const vx = x(gx - shift.x, gy - shift.y, scope);
166
+ const vy = y(gx - shift.x, gy - shift.y, scope);
167
+ out.x = vx;
168
+ out.y = vy;
169
+ return Number.isFinite(vx) && Number.isFinite(vy);
170
+ };
171
+ }
@@ -0,0 +1,50 @@
1
+ import { createBoundedCache } from "./cache";
2
+ import { equationNames, splitTuple } from "./equation";
3
+ import { compileExpressionCached, type CompiledExpression } from "./expression";
4
+
5
+ /** A vector at every point of the plane, two parts in `x` and `y`: `(-y, x)`. A warp is written the same way. */
6
+ export interface FieldEquation {
7
+ x: CompiledExpression;
8
+ y: CompiledExpression;
9
+ }
10
+
11
+ export const FIELD_VARIABLES = ["x", "y"] as const;
12
+
13
+ const NAMES: ReadonlySet<string> = new Set(FIELD_VARIABLES);
14
+ const fields = createBoundedCache<FieldEquation | Error>(128);
15
+
16
+ /** The named values a field's equation reads: every name that is not `x` or `y`. */
17
+ export function fieldParams(source: string): readonly string[] {
18
+ return equationNames(source).filter((name) => !NAMES.has(name));
19
+ }
20
+
21
+ /** `source` as a field, or the reason it is not one. Memoised, so an unchanged equation is the identical object. */
22
+ export function parseField(source: string, params: readonly string[] = []): FieldEquation | Error {
23
+ const key = `${params.join(",")}|${source}`;
24
+ const hit = fields.get(key);
25
+ if (hit !== undefined) return hit;
26
+ let result: FieldEquation | Error;
27
+ try {
28
+ const text = source.trim();
29
+ if (text === "") throw new Error("Empty equation");
30
+ const parts = splitTuple(text);
31
+ if (parts?.length !== 2) throw new Error("A field has two parts: (x, y)");
32
+ const [x, y] = parts.map((part) => {
33
+ const compiled = compileExpressionCached(part.trim(), FIELD_VARIABLES, params);
34
+ if (compiled instanceof Error) throw compiled;
35
+ return compiled;
36
+ });
37
+ result = { x, y };
38
+ } catch (error) {
39
+ result = error instanceof Error ? error : new Error(String(error));
40
+ }
41
+ fields.set(key, result);
42
+ return result;
43
+ }
44
+
45
+ /** Why `source` is not a field, or `null`; empty text is not an error. */
46
+ export function fieldError(source: string): string | null {
47
+ if (source.trim() === "") return null;
48
+ const field = parseField(source, fieldParams(source));
49
+ return field instanceof Error ? field.message : null;
50
+ }
@@ -0,0 +1,229 @@
1
+ import {
2
+ commandsFor,
3
+ pathOps,
4
+ Types,
5
+ type BoxBounds,
6
+ type NodeView,
7
+ type PaintProps,
8
+ type PathCommand,
9
+ type RenderContext,
10
+ type Vector2,
11
+ // Named so the emitted declaration reaches these through the sdk, not core.
12
+ type Shadow as _Shadow,
13
+ type ShadowResolved as _ShadowResolved,
14
+ type Stroke as _Stroke,
15
+ type StrokeResolved as _StrokeResolved,
16
+ } from "@motionscript/sdk";
17
+ import { omit, PAINT, PAINT_COMMANDS } from "./component";
18
+ import { LatexMatrix } from "../tex/matrix";
19
+ import { Latex } from "../tex/node";
20
+ import { isFormula, latexPartBounds } from "../tex/typeset";
21
+ import { refId } from "./graph";
22
+
23
+ /** A node with parts that can be pointed at by name: a formula's terms. */
24
+ export interface Parted {
25
+ /** The box of `part`, y-up from the node's centre, or `null` when it has no such part. */
26
+ partBounds(part: string, occurrence?: number): BoxBounds | null;
27
+ }
28
+
29
+ function isParted(node: object): boolean {
30
+ return typeof (node as Partial<Parted>).partBounds === "function" || isFormula(node);
31
+ }
32
+
33
+ /** The box of `part` in `node`: its own `partBounds` answer, or a formula component's by its source. */
34
+ function partBoundsOf(node: object, part: string, occurrence: number): BoxBounds | null {
35
+ if (typeof (node as Partial<Parted>).partBounds === "function") return (node as Parted).partBounds(part, occurrence);
36
+ return latexPartBounds(node, part, occurrence) ?? null;
37
+ }
38
+
39
+ /** What a mark is drawn on: a node by id, and one part of it where it has parts. */
40
+ export interface Target {
41
+ on: string;
42
+ part?: string;
43
+ occurrence?: number;
44
+ }
45
+
46
+ /** A target on its way to another: `t` of the way from `from`, or from the mark's resting target. */
47
+ export interface Retarget extends Target {
48
+ from?: Target | null;
49
+ t: number;
50
+ }
51
+
52
+ export interface FollowProps extends PaintProps {
53
+ on?: string;
54
+ part?: string;
55
+ occurrence?: number;
56
+ retarget?: Retarget | null;
57
+ }
58
+
59
+ /** A target's four corners in its mark's own frame, y-up: top left, top right, bottom right, bottom left. */
60
+ export type Quad = [Vector2, Vector2, Vector2, Vector2];
61
+
62
+ const restOf = ({ on, part, occurrence }: Target): Target => ({ on, part, occurrence });
63
+
64
+ function lerpRetarget(from: Retarget | null, to: Retarget | null, t: number): Retarget | null {
65
+ if (t <= 0 || !to) return t < 1 ? from : to;
66
+ return { ...restOf(to), from: from ? restOf(from) : null, t };
67
+ }
68
+
69
+ const REF = Types.custom<string, NodeView | null>("", { kind: "nodeRef", animatable: false });
70
+ const HELD_REF = Types.custom<string, NodeView | null, { readonly private: true }>("", { kind: "nodeRef", private: true });
71
+
72
+ /**
73
+ * A mark drawn on another node, or on one part of a formula: a brace, a box,
74
+ * an underline. It has no size of its own and takes no place in a layout; it
75
+ * is drawn where its target is, wherever the two are in the tree, and its
76
+ * selection box is its ink. Its entries, and what it draws on.
77
+ */
78
+ export const FOLLOW_STATE = {
79
+ // It is only an outline.
80
+ ...omit(PAINT, "fill"),
81
+ /** The node this mark is drawn on, by id. Left empty, it is the formula the mark sits inside. */
82
+ on: REF,
83
+ /** One part of that node, by its source, where it has parts: `x^2` of a formula. */
84
+ part: Types.text("", { animatable: false }),
85
+ /** Which of the places the part is written, counted from 1. */
86
+ occurrence: Types.number(1, { min: 1, step: 1, animatable: false }),
87
+ /** Where a `retargetTo` has taken the mark, and how far along it is. Not an author's to state. */
88
+ retarget: Types.custom<Retarget | null>(null, { lerp: lerpRetarget }),
89
+ /** The nodes the retarget names, there and where it set out from, read as nodes. */
90
+ retargetOn: HELD_REF,
91
+ retargetFrom: HELD_REF,
92
+ };
93
+
94
+ export const FOLLOW_COMMANDS = {
95
+ ...omit(PAINT_COMMANDS, "fillTo"),
96
+ ...commandsFor(FOLLOW_STATE, {
97
+ /** Move the mark onto another node, or another part. */
98
+ retargetTo: {
99
+ // No defaults for the node and the part: one left out keeps the mark's own.
100
+ args: {
101
+ on: Types.custom<string | undefined>(undefined, { kind: "nodeRef" }),
102
+ part: Types.custom<string | undefined>(undefined, { kind: "text" }),
103
+ occurrence: Types.number(1, { min: 1, step: 1 }),
104
+ },
105
+ run: (timeline, s) => {
106
+ const next = timeline.args;
107
+ const held = s.retarget ?? restingOf(s);
108
+ const to: Retarget = {
109
+ on: next.on ?? held.on,
110
+ part: next.part ?? (next.on ? "" : held.part),
111
+ occurrence: next.occurrence ?? 1,
112
+ t: 1,
113
+ };
114
+ const from = s.retarget;
115
+ return timeline.animate((t) => {
116
+ const retarget = lerpRetarget(from, to, t);
117
+ return { retarget, retargetOn: retarget?.on ?? "", retargetFrom: retarget?.from?.on ?? "" };
118
+ });
119
+ },
120
+ },
121
+ }),
122
+ };
123
+
124
+ /** What a follow mark reads of its state. */
125
+ export interface FollowValues {
126
+ readonly on: NodeView | null;
127
+ readonly part: string;
128
+ readonly occurrence: number;
129
+ readonly retarget: Retarget | null;
130
+ readonly retargetOn: NodeView | null;
131
+ readonly retargetFrom: NodeView | null;
132
+ }
133
+
134
+ function restingOf(s: FollowValues): Target {
135
+ return { on: refId(s.on), part: s.part, occurrence: s.occurrence };
136
+ }
137
+
138
+ const containers = new WeakMap<object, NodeView | null>();
139
+
140
+ /**
141
+ * The formula a follow mark sits in, as its render last found it: what a mark
142
+ * naming no node is drawn on. Its bounds and handles read it from there, where
143
+ * there is no context to ask.
144
+ */
145
+ export function container(ctx: RenderContext | null, s: object): NodeView | null {
146
+ if (!ctx) return containers.get(s) ?? null;
147
+ const found = ctx.ancestor(Latex) ?? ctx.ancestor(LatexMatrix);
148
+ const parted = found && isParted(found.state) ? found : null;
149
+ containers.set(s, parted);
150
+ return parted;
151
+ }
152
+
153
+ /** The node the mark rests on: the one `on` names, or the formula it sits in. */
154
+ export function resting(s: FollowValues, parted: NodeView | null): NodeView | null {
155
+ return s.on ?? parted;
156
+ }
157
+
158
+ /** The target's corners now: its resting one, or part-way to where a `retargetTo` is taking it. */
159
+ export function followedQuad(s: FollowValues, parted: NodeView | null): Quad | null {
160
+ const state = s.retarget;
161
+ const rest = (): Quad | null => quadOf(resting(s, parted), s);
162
+ if (!state) return rest();
163
+ const to = quadOf(state.on === "" ? parted : s.retargetOn, state);
164
+ const from = state.from ? quadOf(state.from.on === "" ? parted : s.retargetFrom, state.from) : rest();
165
+ if (!from || !to || state.t >= 1) return to ?? from;
166
+ return lerpQuad(from, to, state.t);
167
+ }
168
+
169
+ /** Where `target` is, as the mark sees it; `null` when it names no node or part. */
170
+ function quadOf(node: NodeView | null, target: Omit<Target, "on">): Quad | null {
171
+ if (!node) return null;
172
+ const box: BoxBounds | null =
173
+ target.part && isParted(node.state)
174
+ ? partBoundsOf(node.state, target.part, target.occurrence || 1)
175
+ : { x: 0, y: 0, width: node.size.width, height: node.size.height };
176
+ if (!box) return null;
177
+ const frame = node.frame;
178
+ const turn = (frame.rotation * Math.PI) / 180;
179
+ const cos = Math.cos(turn);
180
+ const sin = Math.sin(turn);
181
+ // The engine's turn is clockwise, in a y-up frame.
182
+ const place = (x: number, y: number): Vector2 => ({
183
+ x: frame.x + (cos * x + sin * y) * frame.scale,
184
+ y: frame.y + (-sin * x + cos * y) * frame.scale,
185
+ });
186
+ const w = box.width / 2;
187
+ const h = box.height / 2;
188
+ return [place(box.x - w, box.y + h), place(box.x + w, box.y + h), place(box.x + w, box.y - h), place(box.x - w, box.y - h)];
189
+ }
190
+
191
+ function lerpQuad(a: Quad, b: Quad, t: number): Quad {
192
+ return a.map((from, i) => ({ x: from.x + (b[i].x - from.x) * t, y: from.y + (b[i].y - from.y) * t })) as Quad;
193
+ }
194
+
195
+ /** The box of an outline, as a follow mark's bounds: it has no size of its own, so its ink is what is selected. */
196
+ export function outlineBounds(outline: readonly PathCommand[]): BoxBounds {
197
+ const ink = pathOps.bounds(outline as PathCommand[]);
198
+ if (!ink) return { x: 0, y: 0, width: 0, height: 0 };
199
+ return { x: (ink.minX + ink.maxX) / 2, y: (ink.minY + ink.maxY) / 2, width: ink.maxX - ink.minX, height: ink.maxY - ink.minY };
200
+ }
201
+
202
+ /** Every drawn point's centre frame: the mark's own centre, so its outline is where it says. */
203
+ export const OWN_FRAME: [number, number, number, number] = [-1, -1, 1, 1];
204
+
205
+ // --- Geometry the marks share ---
206
+
207
+ export const add = (a: Vector2, b: Vector2, k = 1): Vector2 => ({ x: a.x + b.x * k, y: a.y + b.y * k });
208
+ export const between = (a: Vector2, b: Vector2, t = 0.5): Vector2 => ({ x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t });
209
+
210
+ /** The unit vector from `a` to `b`, and its length; a zero one points right. */
211
+ export function heading(a: Vector2, b: Vector2): { unit: Vector2; length: number } {
212
+ const dx = b.x - a.x;
213
+ const dy = b.y - a.y;
214
+ const length = Math.hypot(dx, dy);
215
+ return { unit: length === 0 ? { x: 1, y: 0 } : { x: dx / length, y: dy / length }, length };
216
+ }
217
+
218
+ /** A quad grown by `by` pixels on every side, along its own edges. */
219
+ export function grown(quad: Quad, by: number): Quad {
220
+ const [tl, tr, br, bl] = quad;
221
+ const across = heading(tl, tr).unit;
222
+ const up = heading(bl, tl).unit;
223
+ return [
224
+ add(add(tl, across, -by), up, by),
225
+ add(add(tr, across, by), up, by),
226
+ add(add(br, across, by), up, -by),
227
+ add(add(bl, across, -by), up, -by),
228
+ ];
229
+ }