@mark1russell7/vex-domains 0.6.0 → 0.7.0

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.
package/src/vec2.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { defineDomain, type Domain, type OpTable } from "@mark1russell7/vex";
1
+ import { defineDomain, type Domain, type FnOp, type OpTable } from "@mark1russell7/vex";
2
2
 
3
3
  /**
4
4
  * An immutable 2D vector. The methods live on the prototype, so a vector costs two numbers. Division is real
@@ -172,8 +172,11 @@ type Vec2OpNames =
172
172
  | "anyNegative" | "allNonNegative" | "anyZero" | "allZero" | "anyLessThan" | "allLessThan" | "anyGreaterThan"
173
173
  | "allGreaterThan";
174
174
 
175
- /** The ops of `Vec2Domain`. */
176
- export type Vec2Ops = OpTable<Vec2, Vec2OpNames, "add" | "subtract" | "multiply" | "divide" | "min" | "max">;
175
+ /** The ops of `Vec2Domain`. The ops `x` and `y` give a component. */
176
+ export type Vec2Ops = OpTable<Vec2, Vec2OpNames, "add" | "subtract" | "multiply" | "divide" | "min" | "max"> & {
177
+ readonly x: FnOp<Vec2, (v: Vec2) => number>;
178
+ readonly y: FnOp<Vec2, (v: Vec2) => number>;
179
+ };
177
180
 
178
181
  /** The domain of 2D vectors. A number argument of a component-wise op lifts to a vector with that value twice. */
179
182
  export const Vec2Domain: Domain<"Vec2", Vec2, Vec2Ops> = defineDomain<"Vec2", Vec2, Vec2Ops>({
@@ -189,44 +192,47 @@ export const Vec2Domain: Domain<"Vec2", Vec2, Vec2Ops> = defineDomain<"Vec2", Ve
189
192
  return new Vec2(typeof x === "number" ? x : Number.NaN, typeof y === "number" ? y : Number.NaN);
190
193
  },
191
194
  ops: {
192
- add: { laws: ["commutative", "associative"], identity: () => Vec2.zero, liftScalar: true, params: ["domain"] },
193
- subtract: { liftScalar: true, params: ["domain"] },
194
- multiply: { laws: ["commutative", "associative"], identity: () => Vec2.one, liftScalar: true, params: ["domain"] },
195
- divide: { liftScalar: true, params: ["domain"] },
196
- min: { laws: ["commutative", "associative", "idempotent"], liftScalar: true, params: ["domain"] },
197
- max: { laws: ["commutative", "associative", "idempotent"], liftScalar: true, params: ["domain"] },
198
- scale: { params: ["number"] },
199
- negate: {},
200
- halve: {},
201
- abs: {},
202
- round: {},
203
- floor: {},
204
- ceil: {},
205
- clamp: { params: ["number", "number"] },
206
- swap: {},
207
- rotate: { params: ["number"] },
208
- normalize: {},
209
- lerp: { params: ["domain", "number"] },
210
- length: {},
211
- lengthSq: {},
212
- distance: { params: ["domain"] },
213
- dot: { laws: ["commutative"], params: ["domain"] },
214
- cross: { params: ["domain"] },
215
- angle: {},
216
- area: {},
217
- sum: {},
218
- minComponent: {},
219
- maxComponent: {},
220
- equals: { laws: ["commutative"], params: ["domain"] },
221
- anyNonPositive: {},
222
- allPositive: {},
223
- anyNegative: {},
224
- allNonNegative: {},
225
- anyZero: {},
226
- allZero: {},
227
- anyLessThan: { params: ["number"] },
228
- allLessThan: { params: ["number"] },
229
- anyGreaterThan: { params: ["number"] },
230
- allGreaterThan: { params: ["number"] },
195
+ // The components are fields of a vector, not methods, so these two ops are functions.
196
+ x: { fn: (v: Vec2): number => v.x, params: [], returns: "number" },
197
+ y: { fn: (v: Vec2): number => v.y, params: [], returns: "number" },
198
+ add: { laws: ["commutative", "associative"], identity: () => Vec2.zero, liftScalar: true, params: ["domain"], returns: "domain" },
199
+ subtract: { liftScalar: true, params: ["domain"], returns: "domain" },
200
+ multiply: { laws: ["commutative", "associative"], identity: () => Vec2.one, liftScalar: true, params: ["domain"], returns: "domain" },
201
+ divide: { liftScalar: true, params: ["domain"], returns: "domain" },
202
+ min: { laws: ["commutative", "associative", "idempotent"], liftScalar: true, params: ["domain"], returns: "domain" },
203
+ max: { laws: ["commutative", "associative", "idempotent"], liftScalar: true, params: ["domain"], returns: "domain" },
204
+ scale: { params: ["number"], returns: "domain" },
205
+ negate: { params: [], returns: "domain" },
206
+ halve: { params: [], returns: "domain" },
207
+ abs: { params: [], returns: "domain" },
208
+ round: { params: [], returns: "domain" },
209
+ floor: { params: [], returns: "domain" },
210
+ ceil: { params: [], returns: "domain" },
211
+ clamp: { params: ["number", "number"], returns: "domain" },
212
+ swap: { params: [], returns: "domain" },
213
+ rotate: { params: ["number"], returns: "domain" },
214
+ normalize: { params: [], returns: "domain" },
215
+ lerp: { params: ["domain", "number"], returns: "domain" },
216
+ length: { params: [], returns: "number" },
217
+ lengthSq: { params: [], returns: "number" },
218
+ distance: { params: ["domain"], returns: "number" },
219
+ dot: { laws: ["commutative"], params: ["domain"], returns: "number" },
220
+ cross: { params: ["domain"], returns: "number" },
221
+ angle: { params: [], returns: "number" },
222
+ area: { params: [], returns: "number" },
223
+ sum: { params: [], returns: "number" },
224
+ minComponent: { params: [], returns: "number" },
225
+ maxComponent: { params: [], returns: "number" },
226
+ equals: { laws: ["commutative"], params: ["domain"], returns: "boolean" },
227
+ anyNonPositive: { params: [], returns: "boolean" },
228
+ allPositive: { params: [], returns: "boolean" },
229
+ anyNegative: { params: [], returns: "boolean" },
230
+ allNonNegative: { params: [], returns: "boolean" },
231
+ anyZero: { params: [], returns: "boolean" },
232
+ allZero: { params: [], returns: "boolean" },
233
+ anyLessThan: { params: ["number"], returns: "boolean" },
234
+ allLessThan: { params: ["number"], returns: "boolean" },
235
+ anyGreaterThan: { params: ["number"], returns: "boolean" },
236
+ allGreaterThan: { params: ["number"], returns: "boolean" },
231
237
  },
232
238
  });
package/src/xy.ts ADDED
@@ -0,0 +1,74 @@
1
+ import { defineDomain, type Domain, type FnOp } from "@mark1russell7/vex";
2
+
3
+ /**
4
+ * A 2D vector as plain data. `XYDomain` has the component-wise ops of `Vec2Domain`, but each op is a function of plain
5
+ * data. Thus a standalone module of `@mark1russell7/vex-compiler` can copy the domain, and a 2D program can be a module.
6
+ */
7
+ export interface XY {
8
+ readonly x: number;
9
+ readonly y: number;
10
+ }
11
+
12
+ type XYFn2 = (a: XY, b: XY) => XY;
13
+ type XYFn1 = (a: XY) => XY;
14
+ type XYNum1 = (a: XY) => number;
15
+
16
+ /** The ops of `XYDomain`. */
17
+ export type XYOps = {
18
+ readonly x: FnOp<XY, XYNum1>;
19
+ readonly y: FnOp<XY, XYNum1>;
20
+ readonly add: FnOp<XY, XYFn2>;
21
+ readonly subtract: FnOp<XY, XYFn2>;
22
+ readonly multiply: FnOp<XY, XYFn2>;
23
+ readonly divide: FnOp<XY, XYFn2>;
24
+ readonly min: FnOp<XY, XYFn2>;
25
+ readonly max: FnOp<XY, XYFn2>;
26
+ readonly scale: FnOp<XY, (a: XY, k: number) => XY>;
27
+ readonly negate: FnOp<XY, XYFn1>;
28
+ readonly abs: FnOp<XY, XYFn1>;
29
+ readonly floor: FnOp<XY, XYFn1>;
30
+ readonly ceil: FnOp<XY, XYFn1>;
31
+ readonly round: FnOp<XY, XYFn1>;
32
+ readonly dot: FnOp<XY, (a: XY, b: XY) => number>;
33
+ readonly length: FnOp<XY, XYNum1>;
34
+ readonly distance: FnOp<XY, (a: XY, b: XY) => number>;
35
+ readonly area: FnOp<XY, XYNum1>;
36
+ readonly sum: FnOp<XY, XYNum1>;
37
+ readonly equals: FnOp<XY, (a: XY, b: XY) => boolean>;
38
+ };
39
+
40
+ /**
41
+ * The domain of 2D vectors as plain data: an object with the two number fields `x` and `y` and no other field. The op
42
+ * `xy` of `NumDomain` makes one. No op lifts a number, so each op has a static site in a module. Put `XYDomain` before
43
+ * `NumRecordDomain`, because a record of numbers accepts the same objects.
44
+ */
45
+ export const XYDomain: Domain<"XY", XY, XYOps> = defineDomain<"XY", XY, XYOps>({
46
+ name: "XY",
47
+ // A plain object: its prototype is Object.prototype or null, so an instance of a class such as Vec2 is not an XY.
48
+ is: (u: unknown): u is XY => typeof u === "object" && u !== null && [Object.prototype, null].includes(Object.getPrototypeOf(u) as object | null) && Object.keys(u).length === 2 && typeof (u as XY).x === "number" && typeof (u as XY).y === "number",
49
+ valid: (v: XY): boolean => Number.isFinite(v.x) && Number.isFinite(v.y),
50
+ show: (v: XY): string => `(${v.x}, ${v.y})`,
51
+ samples: (): readonly XY[] => [{ x: 0, y: 0 }, { x: 1.5, y: -2 }, { x: 3, y: 4 }],
52
+ ops: {
53
+ x: { fn: (a: XY): number => a.x, params: [], returns: "number" },
54
+ y: { fn: (a: XY): number => a.y, params: [], returns: "number" },
55
+ add: { fn: (a: XY, b: XY): XY => ({ x: a.x + b.x, y: a.y + b.y }), laws: ["commutative", "associative"], identity: (): XY => ({ x: 0, y: 0 }), params: ["domain"], returns: "domain" },
56
+ subtract: { fn: (a: XY, b: XY): XY => ({ x: a.x - b.x, y: a.y - b.y }), params: ["domain"], returns: "domain" },
57
+ multiply: { fn: (a: XY, b: XY): XY => ({ x: a.x * b.x, y: a.y * b.y }), laws: ["commutative", "associative"], identity: (): XY => ({ x: 1, y: 1 }), params: ["domain"], returns: "domain" },
58
+ divide: { fn: (a: XY, b: XY): XY => ({ x: a.x / b.x, y: a.y / b.y }), params: ["domain"], returns: "domain" },
59
+ min: { fn: (a: XY, b: XY): XY => ({ x: Math.min(a.x, b.x), y: Math.min(a.y, b.y) }), laws: ["commutative", "associative", "idempotent"], params: ["domain"], returns: "domain" },
60
+ max: { fn: (a: XY, b: XY): XY => ({ x: Math.max(a.x, b.x), y: Math.max(a.y, b.y) }), laws: ["commutative", "associative", "idempotent"], params: ["domain"], returns: "domain" },
61
+ scale: { fn: (a: XY, k: number): XY => ({ x: a.x * k, y: a.y * k }), params: ["number"], returns: "domain" },
62
+ negate: { fn: (a: XY): XY => ({ x: -a.x, y: -a.y }), params: [], returns: "domain" },
63
+ abs: { fn: (a: XY): XY => ({ x: Math.abs(a.x), y: Math.abs(a.y) }), params: [], returns: "domain" },
64
+ floor: { fn: (a: XY): XY => ({ x: Math.floor(a.x), y: Math.floor(a.y) }), params: [], returns: "domain" },
65
+ ceil: { fn: (a: XY): XY => ({ x: Math.ceil(a.x), y: Math.ceil(a.y) }), params: [], returns: "domain" },
66
+ round: { fn: (a: XY): XY => ({ x: Math.round(a.x), y: Math.round(a.y) }), params: [], returns: "domain" },
67
+ dot: { fn: (a: XY, b: XY): number => a.x * b.x + a.y * b.y, laws: ["commutative"], params: ["domain"], returns: "number" },
68
+ length: { fn: (a: XY): number => Math.hypot(a.x, a.y), params: [], returns: "number" },
69
+ distance: { fn: (a: XY, b: XY): number => Math.hypot(a.x - b.x, a.y - b.y), laws: ["commutative"], params: ["domain"], returns: "number" },
70
+ area: { fn: (a: XY): number => a.x * a.y, params: [], returns: "number" },
71
+ sum: { fn: (a: XY): number => a.x + a.y, params: [], returns: "number" },
72
+ equals: { fn: (a: XY, b: XY): boolean => a.x === b.x && a.y === b.y, laws: ["commutative"], params: ["domain"], returns: "boolean" },
73
+ },
74
+ });