@mark1russell7/vex-domains 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/build/index.d.ts +283 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +815 -0
- package/build/index.js.map +1 -0
- package/package.json +45 -3
- package/src/angle.ts +73 -0
- package/src/color.ts +83 -0
- package/src/index.ts +10 -0
- package/src/ndvector.ts +199 -0
- package/src/scalars.ts +86 -0
- package/src/vec2.ts +231 -0
- package/src/wrappers.ts +68 -0
- package/README.md +0 -3
package/build/index.js
ADDED
|
@@ -0,0 +1,815 @@
|
|
|
1
|
+
import { defineDomain } from "@mark1russell7/vex";
|
|
2
|
+
//#region src/vec2.ts
|
|
3
|
+
/**
|
|
4
|
+
* An immutable 2D vector. The methods live on the prototype, so a vector costs two numbers. Division is real
|
|
5
|
+
* division: a division by zero gives an infinite component, and the domain check turns it into `#NUM!`.
|
|
6
|
+
*/
|
|
7
|
+
var Vec2 = class Vec2 {
|
|
8
|
+
x;
|
|
9
|
+
y;
|
|
10
|
+
constructor(x, y) {
|
|
11
|
+
this.x = x;
|
|
12
|
+
this.y = y;
|
|
13
|
+
}
|
|
14
|
+
/** This function makes a vector. */
|
|
15
|
+
static of(x, y) {
|
|
16
|
+
return new Vec2(x, y);
|
|
17
|
+
}
|
|
18
|
+
/** This function makes a vector with the same value in both components. */
|
|
19
|
+
static scalar(n) {
|
|
20
|
+
return new Vec2(n, n);
|
|
21
|
+
}
|
|
22
|
+
/** The zero vector. */
|
|
23
|
+
static zero = new Vec2(0, 0);
|
|
24
|
+
/** The vector of ones. */
|
|
25
|
+
static one = new Vec2(1, 1);
|
|
26
|
+
add(o) {
|
|
27
|
+
return new Vec2(this.x + o.x, this.y + o.y);
|
|
28
|
+
}
|
|
29
|
+
subtract(o) {
|
|
30
|
+
return new Vec2(this.x - o.x, this.y - o.y);
|
|
31
|
+
}
|
|
32
|
+
/** The component-wise product. */
|
|
33
|
+
multiply(o) {
|
|
34
|
+
return new Vec2(this.x * o.x, this.y * o.y);
|
|
35
|
+
}
|
|
36
|
+
/** The component-wise quotient. A zero component of `o` gives an infinite component. */
|
|
37
|
+
divide(o) {
|
|
38
|
+
return new Vec2(this.x / o.x, this.y / o.y);
|
|
39
|
+
}
|
|
40
|
+
/** The component-wise minimum. */
|
|
41
|
+
min(o) {
|
|
42
|
+
return new Vec2(Math.min(this.x, o.x), Math.min(this.y, o.y));
|
|
43
|
+
}
|
|
44
|
+
/** The component-wise maximum. */
|
|
45
|
+
max(o) {
|
|
46
|
+
return new Vec2(Math.max(this.x, o.x), Math.max(this.y, o.y));
|
|
47
|
+
}
|
|
48
|
+
scale(k) {
|
|
49
|
+
return new Vec2(this.x * k, this.y * k);
|
|
50
|
+
}
|
|
51
|
+
negate() {
|
|
52
|
+
return new Vec2(-this.x, -this.y);
|
|
53
|
+
}
|
|
54
|
+
halve() {
|
|
55
|
+
return this.scale(.5);
|
|
56
|
+
}
|
|
57
|
+
abs() {
|
|
58
|
+
return new Vec2(Math.abs(this.x), Math.abs(this.y));
|
|
59
|
+
}
|
|
60
|
+
round() {
|
|
61
|
+
return new Vec2(Math.round(this.x), Math.round(this.y));
|
|
62
|
+
}
|
|
63
|
+
floor() {
|
|
64
|
+
return new Vec2(Math.floor(this.x), Math.floor(this.y));
|
|
65
|
+
}
|
|
66
|
+
ceil() {
|
|
67
|
+
return new Vec2(Math.ceil(this.x), Math.ceil(this.y));
|
|
68
|
+
}
|
|
69
|
+
clamp(lo, hi) {
|
|
70
|
+
return new Vec2(Math.min(hi, Math.max(lo, this.x)), Math.min(hi, Math.max(lo, this.y)));
|
|
71
|
+
}
|
|
72
|
+
swap() {
|
|
73
|
+
return new Vec2(this.y, this.x);
|
|
74
|
+
}
|
|
75
|
+
/** The vector turned by `radians`, counterclockwise. */
|
|
76
|
+
rotate(radians) {
|
|
77
|
+
const c = Math.cos(radians);
|
|
78
|
+
const s = Math.sin(radians);
|
|
79
|
+
return new Vec2(c * this.x - s * this.y, s * this.x + c * this.y);
|
|
80
|
+
}
|
|
81
|
+
/** The vector with length 1. The zero vector stays zero. */
|
|
82
|
+
normalize() {
|
|
83
|
+
const len = this.length();
|
|
84
|
+
return len === 0 ? this : this.scale(1 / len);
|
|
85
|
+
}
|
|
86
|
+
/** The linear interpolation from this vector to `o`. */
|
|
87
|
+
lerp(o, t) {
|
|
88
|
+
return new Vec2(this.x + (o.x - this.x) * t, this.y + (o.y - this.y) * t);
|
|
89
|
+
}
|
|
90
|
+
length() {
|
|
91
|
+
return Math.hypot(this.x, this.y);
|
|
92
|
+
}
|
|
93
|
+
lengthSq() {
|
|
94
|
+
return this.x * this.x + this.y * this.y;
|
|
95
|
+
}
|
|
96
|
+
distance(o) {
|
|
97
|
+
return Math.hypot(this.x - o.x, this.y - o.y);
|
|
98
|
+
}
|
|
99
|
+
dot(o) {
|
|
100
|
+
return this.x * o.x + this.y * o.y;
|
|
101
|
+
}
|
|
102
|
+
/** The z component of the 3D cross product. */
|
|
103
|
+
cross(o) {
|
|
104
|
+
return this.x * o.y - this.y * o.x;
|
|
105
|
+
}
|
|
106
|
+
/** The angle of the vector, in radians, from the x axis. */
|
|
107
|
+
angle() {
|
|
108
|
+
return Math.atan2(this.y, this.x);
|
|
109
|
+
}
|
|
110
|
+
/** The product of the components: the area of a size vector. */
|
|
111
|
+
area() {
|
|
112
|
+
return this.x * this.y;
|
|
113
|
+
}
|
|
114
|
+
/** The sum of the components. */
|
|
115
|
+
sum() {
|
|
116
|
+
return this.x + this.y;
|
|
117
|
+
}
|
|
118
|
+
minComponent() {
|
|
119
|
+
return Math.min(this.x, this.y);
|
|
120
|
+
}
|
|
121
|
+
maxComponent() {
|
|
122
|
+
return Math.max(this.x, this.y);
|
|
123
|
+
}
|
|
124
|
+
equals(o) {
|
|
125
|
+
return this.x === o.x && this.y === o.y;
|
|
126
|
+
}
|
|
127
|
+
anyNonPositive() {
|
|
128
|
+
return this.x <= 0 || this.y <= 0;
|
|
129
|
+
}
|
|
130
|
+
allPositive() {
|
|
131
|
+
return this.x > 0 && this.y > 0;
|
|
132
|
+
}
|
|
133
|
+
anyNegative() {
|
|
134
|
+
return this.x < 0 || this.y < 0;
|
|
135
|
+
}
|
|
136
|
+
allNonNegative() {
|
|
137
|
+
return this.x >= 0 && this.y >= 0;
|
|
138
|
+
}
|
|
139
|
+
anyZero() {
|
|
140
|
+
return this.x === 0 || this.y === 0;
|
|
141
|
+
}
|
|
142
|
+
allZero() {
|
|
143
|
+
return this.x === 0 && this.y === 0;
|
|
144
|
+
}
|
|
145
|
+
anyLessThan(n) {
|
|
146
|
+
return this.x < n || this.y < n;
|
|
147
|
+
}
|
|
148
|
+
allLessThan(n) {
|
|
149
|
+
return this.x < n && this.y < n;
|
|
150
|
+
}
|
|
151
|
+
anyGreaterThan(n) {
|
|
152
|
+
return this.x > n || this.y > n;
|
|
153
|
+
}
|
|
154
|
+
allGreaterThan(n) {
|
|
155
|
+
return this.x > n && this.y > n;
|
|
156
|
+
}
|
|
157
|
+
toJSON() {
|
|
158
|
+
return {
|
|
159
|
+
x: this.x,
|
|
160
|
+
y: this.y
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
toString() {
|
|
164
|
+
return `(${this.x}, ${this.y})`;
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
/** The domain of 2D vectors. A number argument of a component-wise op lifts to a vector with that value twice. */
|
|
168
|
+
const Vec2Domain = defineDomain({
|
|
169
|
+
name: "Vec2",
|
|
170
|
+
is: (u) => u instanceof Vec2,
|
|
171
|
+
fromScalar: (n) => Vec2.scalar(n),
|
|
172
|
+
valid: (v) => Number.isFinite(v.x) && Number.isFinite(v.y),
|
|
173
|
+
show: (v) => v.toString(),
|
|
174
|
+
encode: (v) => [v.x, v.y],
|
|
175
|
+
decode: (u) => {
|
|
176
|
+
const [x, y] = Array.isArray(u) ? u : [];
|
|
177
|
+
return new Vec2(typeof x === "number" ? x : NaN, typeof y === "number" ? y : NaN);
|
|
178
|
+
},
|
|
179
|
+
ops: {
|
|
180
|
+
add: {
|
|
181
|
+
laws: ["commutative", "associative"],
|
|
182
|
+
identity: () => Vec2.zero,
|
|
183
|
+
liftScalar: true,
|
|
184
|
+
params: ["domain"]
|
|
185
|
+
},
|
|
186
|
+
subtract: {
|
|
187
|
+
liftScalar: true,
|
|
188
|
+
params: ["domain"]
|
|
189
|
+
},
|
|
190
|
+
multiply: {
|
|
191
|
+
laws: ["commutative", "associative"],
|
|
192
|
+
identity: () => Vec2.one,
|
|
193
|
+
liftScalar: true,
|
|
194
|
+
params: ["domain"]
|
|
195
|
+
},
|
|
196
|
+
divide: {
|
|
197
|
+
liftScalar: true,
|
|
198
|
+
params: ["domain"]
|
|
199
|
+
},
|
|
200
|
+
min: {
|
|
201
|
+
laws: [
|
|
202
|
+
"commutative",
|
|
203
|
+
"associative",
|
|
204
|
+
"idempotent"
|
|
205
|
+
],
|
|
206
|
+
liftScalar: true,
|
|
207
|
+
params: ["domain"]
|
|
208
|
+
},
|
|
209
|
+
max: {
|
|
210
|
+
laws: [
|
|
211
|
+
"commutative",
|
|
212
|
+
"associative",
|
|
213
|
+
"idempotent"
|
|
214
|
+
],
|
|
215
|
+
liftScalar: true,
|
|
216
|
+
params: ["domain"]
|
|
217
|
+
},
|
|
218
|
+
scale: { params: ["number"] },
|
|
219
|
+
negate: {},
|
|
220
|
+
halve: {},
|
|
221
|
+
abs: {},
|
|
222
|
+
round: {},
|
|
223
|
+
floor: {},
|
|
224
|
+
ceil: {},
|
|
225
|
+
clamp: { params: ["number", "number"] },
|
|
226
|
+
swap: {},
|
|
227
|
+
rotate: { params: ["number"] },
|
|
228
|
+
normalize: {},
|
|
229
|
+
lerp: { params: ["domain", "number"] },
|
|
230
|
+
length: {},
|
|
231
|
+
lengthSq: {},
|
|
232
|
+
distance: { params: ["domain"] },
|
|
233
|
+
dot: {
|
|
234
|
+
laws: ["commutative"],
|
|
235
|
+
params: ["domain"]
|
|
236
|
+
},
|
|
237
|
+
cross: { params: ["domain"] },
|
|
238
|
+
angle: {},
|
|
239
|
+
area: {},
|
|
240
|
+
sum: {},
|
|
241
|
+
minComponent: {},
|
|
242
|
+
maxComponent: {},
|
|
243
|
+
equals: {
|
|
244
|
+
laws: ["commutative"],
|
|
245
|
+
params: ["domain"]
|
|
246
|
+
},
|
|
247
|
+
anyNonPositive: {},
|
|
248
|
+
allPositive: {},
|
|
249
|
+
anyNegative: {},
|
|
250
|
+
allNonNegative: {},
|
|
251
|
+
anyZero: {},
|
|
252
|
+
allZero: {},
|
|
253
|
+
anyLessThan: { params: ["number"] },
|
|
254
|
+
allLessThan: { params: ["number"] },
|
|
255
|
+
anyGreaterThan: { params: ["number"] },
|
|
256
|
+
allGreaterThan: { params: ["number"] }
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
//#endregion
|
|
260
|
+
//#region src/ndvector.ts
|
|
261
|
+
/**
|
|
262
|
+
* An immutable vector with named components. The type parameter `K` is the set of component names. Ops that
|
|
263
|
+
* combine two vectors use the union of their names, and a missing component counts as 0. The constructor
|
|
264
|
+
* keeps each number, also a number that is not finite: the domain check reports such a value as `#NUM!`.
|
|
265
|
+
*/
|
|
266
|
+
var NDVector = class NDVector {
|
|
267
|
+
/** The components: name to number. */
|
|
268
|
+
c;
|
|
269
|
+
constructor(c) {
|
|
270
|
+
this.c = Object.freeze({ ...c });
|
|
271
|
+
}
|
|
272
|
+
/** This function makes a vector from name and value pairs. */
|
|
273
|
+
static of(entries) {
|
|
274
|
+
const c = {};
|
|
275
|
+
for (const [k, v] of entries) c[k] = v;
|
|
276
|
+
return new NDVector(c);
|
|
277
|
+
}
|
|
278
|
+
/** This function makes the zero vector over the names `keys`. */
|
|
279
|
+
static zero(keys = []) {
|
|
280
|
+
return NDVector.of(keys.map((k) => [k, 0]));
|
|
281
|
+
}
|
|
282
|
+
/** The names of the components. */
|
|
283
|
+
keys() {
|
|
284
|
+
return Object.keys(this.c);
|
|
285
|
+
}
|
|
286
|
+
/** The value of a component, or 0 if the vector does not have it. */
|
|
287
|
+
get(k) {
|
|
288
|
+
return Object.hasOwn(this.c, k) ? this.c[k] ?? 0 : 0;
|
|
289
|
+
}
|
|
290
|
+
set(k, v) {
|
|
291
|
+
return new NDVector({
|
|
292
|
+
...this.c,
|
|
293
|
+
[k]: v
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
map(f) {
|
|
297
|
+
const c = {};
|
|
298
|
+
for (const k of this.keys()) c[k] = f(k, this.get(k));
|
|
299
|
+
return new NDVector(c);
|
|
300
|
+
}
|
|
301
|
+
/** The vector with only the names in `keys`. A zero component is dropped, unless `keepZeros` is true. */
|
|
302
|
+
pick(keys, keepZeros = false) {
|
|
303
|
+
const c = {};
|
|
304
|
+
for (const k of keys) {
|
|
305
|
+
const v = this.get(k);
|
|
306
|
+
if (keepZeros || v !== 0) c[k] = v;
|
|
307
|
+
}
|
|
308
|
+
return new NDVector(c);
|
|
309
|
+
}
|
|
310
|
+
/** The vector with the names in `keys` added. A new component is 0. */
|
|
311
|
+
withKeys(keys) {
|
|
312
|
+
const c = { ...this.c };
|
|
313
|
+
for (const k of keys) if (!Object.hasOwn(c, k)) c[k] = 0;
|
|
314
|
+
return new NDVector(c);
|
|
315
|
+
}
|
|
316
|
+
#combine(o, f) {
|
|
317
|
+
const c = {};
|
|
318
|
+
for (const k of /* @__PURE__ */ new Set([...this.keys(), ...o.keys()])) c[k] = f(this.get(k), o.get(k));
|
|
319
|
+
return new NDVector(c);
|
|
320
|
+
}
|
|
321
|
+
add(o) {
|
|
322
|
+
return this.#combine(o, (a, b) => a + b);
|
|
323
|
+
}
|
|
324
|
+
subtract(o) {
|
|
325
|
+
return this.#combine(o, (a, b) => a - b);
|
|
326
|
+
}
|
|
327
|
+
/** The component-wise product. */
|
|
328
|
+
multiply(o) {
|
|
329
|
+
return this.#combine(o, (a, b) => a * b);
|
|
330
|
+
}
|
|
331
|
+
/** Of each component, the value of this vector if it is not 0, else the value of `o`. */
|
|
332
|
+
mergePreferNonZero(o) {
|
|
333
|
+
return this.#combine(o, (a, b) => a !== 0 ? a : b);
|
|
334
|
+
}
|
|
335
|
+
scale(k) {
|
|
336
|
+
return this.map((_n, v) => v * k);
|
|
337
|
+
}
|
|
338
|
+
clamp(lo, hi) {
|
|
339
|
+
return this.map((_n, v) => Math.min(hi, Math.max(lo, v)));
|
|
340
|
+
}
|
|
341
|
+
dot(o) {
|
|
342
|
+
let s = 0;
|
|
343
|
+
for (const k of /* @__PURE__ */ new Set([...this.keys(), ...o.keys()])) s += this.get(k) * o.get(k);
|
|
344
|
+
return s;
|
|
345
|
+
}
|
|
346
|
+
/** The L2 length. */
|
|
347
|
+
length() {
|
|
348
|
+
return Math.sqrt(this.keys().reduce((s, k) => s + this.get(k) ** 2, 0));
|
|
349
|
+
}
|
|
350
|
+
/** The L1 norm. */
|
|
351
|
+
norm1() {
|
|
352
|
+
return this.keys().reduce((s, k) => s + Math.abs(this.get(k)), 0);
|
|
353
|
+
}
|
|
354
|
+
/** The largest absolute component. */
|
|
355
|
+
normInf() {
|
|
356
|
+
return this.keys().reduce((m, k) => Math.max(m, Math.abs(this.get(k))), 0);
|
|
357
|
+
}
|
|
358
|
+
equals(o) {
|
|
359
|
+
const keys = /* @__PURE__ */ new Set([...this.keys(), ...o.keys()]);
|
|
360
|
+
for (const k of keys) if (this.get(k) !== o.get(k)) return false;
|
|
361
|
+
return true;
|
|
362
|
+
}
|
|
363
|
+
/** True if each component is a finite number. */
|
|
364
|
+
isFinite() {
|
|
365
|
+
return this.keys().every((k) => Number.isFinite(this.get(k)));
|
|
366
|
+
}
|
|
367
|
+
toJSON() {
|
|
368
|
+
return { ...this.c };
|
|
369
|
+
}
|
|
370
|
+
toString() {
|
|
371
|
+
return `NDVector(${JSON.stringify(this.c)})`;
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
const ndOps = {
|
|
375
|
+
add: {
|
|
376
|
+
laws: ["commutative", "associative"],
|
|
377
|
+
identity: () => NDVector.zero(),
|
|
378
|
+
params: ["domain"]
|
|
379
|
+
},
|
|
380
|
+
subtract: { params: ["domain"] },
|
|
381
|
+
multiply: {
|
|
382
|
+
laws: ["commutative", "associative"],
|
|
383
|
+
params: ["domain"]
|
|
384
|
+
},
|
|
385
|
+
mergePreferNonZero: {
|
|
386
|
+
laws: ["associative", "idempotent"],
|
|
387
|
+
params: ["domain"]
|
|
388
|
+
},
|
|
389
|
+
scale: { params: ["number"] },
|
|
390
|
+
clamp: { params: ["number", "number"] },
|
|
391
|
+
dot: {
|
|
392
|
+
laws: ["commutative"],
|
|
393
|
+
params: ["domain"]
|
|
394
|
+
},
|
|
395
|
+
length: {},
|
|
396
|
+
norm1: {},
|
|
397
|
+
normInf: {},
|
|
398
|
+
equals: {
|
|
399
|
+
laws: ["commutative"],
|
|
400
|
+
params: ["domain"]
|
|
401
|
+
},
|
|
402
|
+
pick: {},
|
|
403
|
+
withKeys: {},
|
|
404
|
+
get: { params: ["string"] },
|
|
405
|
+
set: { params: ["string", "number"] }
|
|
406
|
+
};
|
|
407
|
+
const isNDVector = (u) => u instanceof NDVector;
|
|
408
|
+
/** The domain of named-component vectors. A number argument is an error here: use `LiftedNDVectorDomain` to lift. */
|
|
409
|
+
const NDVectorDomain = defineDomain({
|
|
410
|
+
name: "NDVector",
|
|
411
|
+
is: isNDVector,
|
|
412
|
+
valid: (v) => v.isFinite(),
|
|
413
|
+
show: (v) => v.toString(),
|
|
414
|
+
encode: (v) => v.toJSON(),
|
|
415
|
+
decode: (u) => new NDVector(u),
|
|
416
|
+
ops: ndOps
|
|
417
|
+
});
|
|
418
|
+
/**
|
|
419
|
+
* The domain of named-component vectors with scalar lifting. A number argument of `add`, `subtract`,
|
|
420
|
+
* `multiply` or `dot` becomes the vector `{ scalar: n }`. This domain is independent of `NDVectorDomain`:
|
|
421
|
+
* the two can be in one program, and neither changes the other.
|
|
422
|
+
*/
|
|
423
|
+
const LiftedNDVectorDomain = defineDomain({
|
|
424
|
+
name: "NDVector(lifted)",
|
|
425
|
+
is: isNDVector,
|
|
426
|
+
fromScalar: (n) => new NDVector({ scalar: n }),
|
|
427
|
+
valid: (v) => v.isFinite(),
|
|
428
|
+
show: (v) => v.toString(),
|
|
429
|
+
ops: {
|
|
430
|
+
...ndOps,
|
|
431
|
+
add: {
|
|
432
|
+
...ndOps.add,
|
|
433
|
+
liftScalar: true
|
|
434
|
+
},
|
|
435
|
+
subtract: {
|
|
436
|
+
...ndOps.subtract,
|
|
437
|
+
liftScalar: true
|
|
438
|
+
},
|
|
439
|
+
multiply: {
|
|
440
|
+
...ndOps.multiply,
|
|
441
|
+
liftScalar: true
|
|
442
|
+
},
|
|
443
|
+
dot: {
|
|
444
|
+
...ndOps.dot,
|
|
445
|
+
liftScalar: true
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
});
|
|
449
|
+
const isNumRecord = (u) => {
|
|
450
|
+
if (typeof u !== "object" || u === null || Array.isArray(u)) return false;
|
|
451
|
+
const proto = Object.getPrototypeOf(u);
|
|
452
|
+
return (proto === Object.prototype || proto === null) && Object.values(u).every((v) => typeof v === "number" && Number.isFinite(v));
|
|
453
|
+
};
|
|
454
|
+
/** The domain of plain number records. Its op `toNDVector` builds a vector from fields, for example from `rec()`. */
|
|
455
|
+
const NumRecordDomain = defineDomain({
|
|
456
|
+
name: "NumRecord",
|
|
457
|
+
is: isNumRecord,
|
|
458
|
+
ops: { toNDVector: { fn: (r) => new NDVector(r) } }
|
|
459
|
+
});
|
|
460
|
+
//#endregion
|
|
461
|
+
//#region src/scalars.ts
|
|
462
|
+
/** The domain of numbers. A result that is not a finite number is the error `#NUM!`. */
|
|
463
|
+
const NumDomain = defineDomain({
|
|
464
|
+
name: "Num",
|
|
465
|
+
is: (u) => typeof u === "number",
|
|
466
|
+
valid: (n) => Number.isFinite(n),
|
|
467
|
+
show: (n) => String(n),
|
|
468
|
+
ops: {
|
|
469
|
+
add: {
|
|
470
|
+
fn: (a, b) => a + b,
|
|
471
|
+
laws: ["commutative", "associative"],
|
|
472
|
+
identity: () => 0,
|
|
473
|
+
params: ["number"]
|
|
474
|
+
},
|
|
475
|
+
subtract: {
|
|
476
|
+
fn: (a, b) => a - b,
|
|
477
|
+
params: ["number"]
|
|
478
|
+
},
|
|
479
|
+
multiply: {
|
|
480
|
+
fn: (a, b) => a * b,
|
|
481
|
+
laws: ["commutative", "associative"],
|
|
482
|
+
identity: () => 1,
|
|
483
|
+
params: ["number"]
|
|
484
|
+
},
|
|
485
|
+
divide: {
|
|
486
|
+
fn: (a, b) => a / b,
|
|
487
|
+
params: ["number"]
|
|
488
|
+
},
|
|
489
|
+
mod: {
|
|
490
|
+
fn: (a, b) => a % b,
|
|
491
|
+
params: ["number"]
|
|
492
|
+
},
|
|
493
|
+
pow: {
|
|
494
|
+
fn: (a, b) => a ** b,
|
|
495
|
+
params: ["number"]
|
|
496
|
+
},
|
|
497
|
+
min: {
|
|
498
|
+
fn: (a, b) => Math.min(a, b),
|
|
499
|
+
laws: [
|
|
500
|
+
"commutative",
|
|
501
|
+
"associative",
|
|
502
|
+
"idempotent"
|
|
503
|
+
],
|
|
504
|
+
params: ["number"]
|
|
505
|
+
},
|
|
506
|
+
max: {
|
|
507
|
+
fn: (a, b) => Math.max(a, b),
|
|
508
|
+
laws: [
|
|
509
|
+
"commutative",
|
|
510
|
+
"associative",
|
|
511
|
+
"idempotent"
|
|
512
|
+
],
|
|
513
|
+
params: ["number"]
|
|
514
|
+
},
|
|
515
|
+
clamp: {
|
|
516
|
+
fn: (a, lo, hi) => Math.min(hi, Math.max(lo, a)),
|
|
517
|
+
params: ["number", "number"]
|
|
518
|
+
},
|
|
519
|
+
abs: { fn: (a) => Math.abs(a) },
|
|
520
|
+
negate: { fn: (a) => -a },
|
|
521
|
+
sqrt: { fn: (a) => Math.sqrt(a) },
|
|
522
|
+
floor: { fn: (a) => Math.floor(a) },
|
|
523
|
+
ceil: { fn: (a) => Math.ceil(a) },
|
|
524
|
+
round: { fn: (a) => Math.round(a) },
|
|
525
|
+
gt: {
|
|
526
|
+
fn: (a, b) => a > b,
|
|
527
|
+
params: ["number"]
|
|
528
|
+
},
|
|
529
|
+
gte: {
|
|
530
|
+
fn: (a, b) => a >= b,
|
|
531
|
+
params: ["number"]
|
|
532
|
+
},
|
|
533
|
+
lt: {
|
|
534
|
+
fn: (a, b) => a < b,
|
|
535
|
+
params: ["number"]
|
|
536
|
+
},
|
|
537
|
+
lte: {
|
|
538
|
+
fn: (a, b) => a <= b,
|
|
539
|
+
params: ["number"]
|
|
540
|
+
},
|
|
541
|
+
eq: {
|
|
542
|
+
fn: (a, b) => a === b,
|
|
543
|
+
laws: ["commutative"],
|
|
544
|
+
params: ["number"]
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
});
|
|
548
|
+
/** The domain of booleans. For lazy logic, use the special forms `and`, `or` and `if` of the IR. */
|
|
549
|
+
const BoolDomain = defineDomain({
|
|
550
|
+
name: "Bool",
|
|
551
|
+
is: (u) => typeof u === "boolean",
|
|
552
|
+
show: (b) => String(b),
|
|
553
|
+
ops: {
|
|
554
|
+
and: {
|
|
555
|
+
fn: (a, b) => a && b,
|
|
556
|
+
laws: [
|
|
557
|
+
"commutative",
|
|
558
|
+
"associative",
|
|
559
|
+
"idempotent"
|
|
560
|
+
],
|
|
561
|
+
identity: () => true,
|
|
562
|
+
params: ["boolean"]
|
|
563
|
+
},
|
|
564
|
+
or: {
|
|
565
|
+
fn: (a, b) => a || b,
|
|
566
|
+
laws: [
|
|
567
|
+
"commutative",
|
|
568
|
+
"associative",
|
|
569
|
+
"idempotent"
|
|
570
|
+
],
|
|
571
|
+
identity: () => false,
|
|
572
|
+
params: ["boolean"]
|
|
573
|
+
},
|
|
574
|
+
xor: {
|
|
575
|
+
fn: (a, b) => a !== b,
|
|
576
|
+
laws: ["commutative", "associative"],
|
|
577
|
+
identity: () => false,
|
|
578
|
+
params: ["boolean"]
|
|
579
|
+
},
|
|
580
|
+
not: { fn: (a) => !a },
|
|
581
|
+
eq: {
|
|
582
|
+
fn: (a, b) => a === b,
|
|
583
|
+
laws: ["commutative"],
|
|
584
|
+
params: ["boolean"]
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
});
|
|
588
|
+
//#endregion
|
|
589
|
+
//#region src/color.ts
|
|
590
|
+
/**
|
|
591
|
+
* An immutable RGBA color. The channels have no fixed range: use `clamp` to limit them. `add` adds the color
|
|
592
|
+
* channels and keeps the larger alpha, so `add` is commutative and associative.
|
|
593
|
+
*/
|
|
594
|
+
var Color = class Color {
|
|
595
|
+
r;
|
|
596
|
+
g;
|
|
597
|
+
b;
|
|
598
|
+
a;
|
|
599
|
+
constructor(r, g, b, a = 1) {
|
|
600
|
+
this.r = r;
|
|
601
|
+
this.g = g;
|
|
602
|
+
this.b = b;
|
|
603
|
+
this.a = a;
|
|
604
|
+
}
|
|
605
|
+
/** This function makes a gray color with the value `n` in each channel. */
|
|
606
|
+
static gray(n) {
|
|
607
|
+
return new Color(n, n, n, 1);
|
|
608
|
+
}
|
|
609
|
+
/** The black color with alpha 0: the identity of `add`. */
|
|
610
|
+
static none = new Color(0, 0, 0, 0);
|
|
611
|
+
/** The channel sum. The alpha is the larger alpha. */
|
|
612
|
+
add(o) {
|
|
613
|
+
return new Color(this.r + o.r, this.g + o.g, this.b + o.b, Math.max(this.a, o.a));
|
|
614
|
+
}
|
|
615
|
+
/** Each color channel times `factor`. The alpha does not change. */
|
|
616
|
+
multiply(factor) {
|
|
617
|
+
return new Color(this.r * factor, this.g * factor, this.b * factor, this.a);
|
|
618
|
+
}
|
|
619
|
+
/** Each color channel in the range from `lo` to `hi`. */
|
|
620
|
+
clamp(lo = 0, hi = 255) {
|
|
621
|
+
const c = (x) => Math.min(hi, Math.max(lo, x));
|
|
622
|
+
return new Color(c(this.r), c(this.g), c(this.b), this.a);
|
|
623
|
+
}
|
|
624
|
+
/** The linear mix from this color to `o`. */
|
|
625
|
+
mix(o, t) {
|
|
626
|
+
const m = (x, y) => x + (y - x) * t;
|
|
627
|
+
return new Color(m(this.r, o.r), m(this.g, o.g), m(this.b, o.b), m(this.a, o.a));
|
|
628
|
+
}
|
|
629
|
+
/** The relative luminance (Rec. 709 weights) of the color channels. */
|
|
630
|
+
luminance() {
|
|
631
|
+
return .2126 * this.r + .7152 * this.g + .0722 * this.b;
|
|
632
|
+
}
|
|
633
|
+
equals(o) {
|
|
634
|
+
return this.r === o.r && this.g === o.g && this.b === o.b && this.a === o.a;
|
|
635
|
+
}
|
|
636
|
+
toArray() {
|
|
637
|
+
return [
|
|
638
|
+
this.r,
|
|
639
|
+
this.g,
|
|
640
|
+
this.b,
|
|
641
|
+
this.a
|
|
642
|
+
];
|
|
643
|
+
}
|
|
644
|
+
toString() {
|
|
645
|
+
return `rgba(${this.r}, ${this.g}, ${this.b}, ${this.a})`;
|
|
646
|
+
}
|
|
647
|
+
};
|
|
648
|
+
/** The domain of colors. */
|
|
649
|
+
const ColorDomain = defineDomain({
|
|
650
|
+
name: "Color",
|
|
651
|
+
is: (u) => u instanceof Color,
|
|
652
|
+
fromScalar: (n) => Color.gray(n),
|
|
653
|
+
valid: (c) => c.toArray().every((x) => Number.isFinite(x)),
|
|
654
|
+
show: (c) => c.toString(),
|
|
655
|
+
encode: (c) => c.toArray(),
|
|
656
|
+
decode: (u) => {
|
|
657
|
+
const [r, g, b, a] = Array.isArray(u) ? u : [];
|
|
658
|
+
return new Color(r ?? NaN, g ?? NaN, b ?? NaN, a ?? 1);
|
|
659
|
+
},
|
|
660
|
+
ops: {
|
|
661
|
+
add: {
|
|
662
|
+
laws: ["commutative", "associative"],
|
|
663
|
+
identity: () => Color.none,
|
|
664
|
+
params: ["domain"]
|
|
665
|
+
},
|
|
666
|
+
multiply: { params: ["number"] },
|
|
667
|
+
clamp: { params: ["number", "number"] },
|
|
668
|
+
mix: { params: ["domain", "number"] },
|
|
669
|
+
luminance: {},
|
|
670
|
+
equals: {
|
|
671
|
+
laws: ["commutative"],
|
|
672
|
+
params: ["domain"]
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
});
|
|
676
|
+
//#endregion
|
|
677
|
+
//#region src/angle.ts
|
|
678
|
+
/** An immutable angle in radians. */
|
|
679
|
+
var Angle = class Angle {
|
|
680
|
+
radians;
|
|
681
|
+
constructor(radians) {
|
|
682
|
+
this.radians = radians;
|
|
683
|
+
}
|
|
684
|
+
/** This function makes an angle from degrees. */
|
|
685
|
+
static fromDegrees(deg) {
|
|
686
|
+
return new Angle(deg * Math.PI / 180);
|
|
687
|
+
}
|
|
688
|
+
/** The zero angle: the identity of `add`. */
|
|
689
|
+
static zero = new Angle(0);
|
|
690
|
+
add(o) {
|
|
691
|
+
return new Angle(this.radians + o.radians);
|
|
692
|
+
}
|
|
693
|
+
subtract(o) {
|
|
694
|
+
return new Angle(this.radians - o.radians);
|
|
695
|
+
}
|
|
696
|
+
scale(k) {
|
|
697
|
+
return new Angle(this.radians * k);
|
|
698
|
+
}
|
|
699
|
+
/** The same angle in the range from -π (included) to π (not included). */
|
|
700
|
+
normalize() {
|
|
701
|
+
const twoPi = Math.PI * 2;
|
|
702
|
+
return new Angle(((this.radians + Math.PI) % twoPi + twoPi) % twoPi - Math.PI);
|
|
703
|
+
}
|
|
704
|
+
/** The vector with this angle and the length `length`. The result is a `Vec2`, so a chain continues in that domain. */
|
|
705
|
+
toVec2(length = 1) {
|
|
706
|
+
return new Vec2(Math.cos(this.radians) * length, Math.sin(this.radians) * length);
|
|
707
|
+
}
|
|
708
|
+
sin() {
|
|
709
|
+
return Math.sin(this.radians);
|
|
710
|
+
}
|
|
711
|
+
cos() {
|
|
712
|
+
return Math.cos(this.radians);
|
|
713
|
+
}
|
|
714
|
+
degrees() {
|
|
715
|
+
return this.radians * 180 / Math.PI;
|
|
716
|
+
}
|
|
717
|
+
toString() {
|
|
718
|
+
return `${this.radians} rad`;
|
|
719
|
+
}
|
|
720
|
+
};
|
|
721
|
+
/** The domain of angles. A number argument of `add` or `subtract` lifts to an angle in radians. */
|
|
722
|
+
const AngleDomain = defineDomain({
|
|
723
|
+
name: "Angle",
|
|
724
|
+
is: (u) => u instanceof Angle,
|
|
725
|
+
fromScalar: (n) => new Angle(n),
|
|
726
|
+
valid: (a) => Number.isFinite(a.radians),
|
|
727
|
+
show: (a) => a.toString(),
|
|
728
|
+
encode: (a) => a.radians,
|
|
729
|
+
decode: (u) => new Angle(typeof u === "number" ? u : NaN),
|
|
730
|
+
ops: {
|
|
731
|
+
add: {
|
|
732
|
+
laws: ["commutative", "associative"],
|
|
733
|
+
identity: () => Angle.zero,
|
|
734
|
+
liftScalar: true,
|
|
735
|
+
params: ["domain"]
|
|
736
|
+
},
|
|
737
|
+
subtract: {
|
|
738
|
+
liftScalar: true,
|
|
739
|
+
params: ["domain"]
|
|
740
|
+
},
|
|
741
|
+
scale: { params: ["number"] },
|
|
742
|
+
normalize: {},
|
|
743
|
+
toVec2: { params: ["number"] },
|
|
744
|
+
sin: {},
|
|
745
|
+
cos: {},
|
|
746
|
+
degrees: {}
|
|
747
|
+
}
|
|
748
|
+
});
|
|
749
|
+
//#endregion
|
|
750
|
+
//#region src/wrappers.ts
|
|
751
|
+
/** A wrapper around a function. Its op `invoke` calls the function. It shows how a domain can hold behavior. */
|
|
752
|
+
var Fn = class {
|
|
753
|
+
fn;
|
|
754
|
+
constructor(fn) {
|
|
755
|
+
this.fn = fn;
|
|
756
|
+
}
|
|
757
|
+
invoke(...args) {
|
|
758
|
+
return this.fn(...args);
|
|
759
|
+
}
|
|
760
|
+
};
|
|
761
|
+
/** The domain of function wrappers. */
|
|
762
|
+
const FnDomain = defineDomain({
|
|
763
|
+
name: "Fn",
|
|
764
|
+
is: (u) => u instanceof Fn,
|
|
765
|
+
ops: { invoke: {} }
|
|
766
|
+
});
|
|
767
|
+
/**
|
|
768
|
+
* A value that is there (`top`) or not there (`bottom`). It shows a domain whose ops take functions. A Vex
|
|
769
|
+
* program can also hold this state in its own errors, so most programs do not need this domain.
|
|
770
|
+
*/
|
|
771
|
+
var Maybe = class Maybe {
|
|
772
|
+
#has;
|
|
773
|
+
#value;
|
|
774
|
+
constructor(has, value) {
|
|
775
|
+
this.#has = has;
|
|
776
|
+
this.#value = value;
|
|
777
|
+
}
|
|
778
|
+
static top(value) {
|
|
779
|
+
return new Maybe(true, value);
|
|
780
|
+
}
|
|
781
|
+
static bottom() {
|
|
782
|
+
return new Maybe(false, void 0);
|
|
783
|
+
}
|
|
784
|
+
isTop() {
|
|
785
|
+
return this.#has;
|
|
786
|
+
}
|
|
787
|
+
isBottom() {
|
|
788
|
+
return !this.#has;
|
|
789
|
+
}
|
|
790
|
+
map(f) {
|
|
791
|
+
return this.#has ? Maybe.top(f(this.#value)) : Maybe.bottom();
|
|
792
|
+
}
|
|
793
|
+
chain(f) {
|
|
794
|
+
return this.#has ? f(this.#value) : Maybe.bottom();
|
|
795
|
+
}
|
|
796
|
+
valueOr(fallback) {
|
|
797
|
+
return this.#has ? this.#value : fallback;
|
|
798
|
+
}
|
|
799
|
+
};
|
|
800
|
+
/** The domain of `Maybe` values. */
|
|
801
|
+
const MaybeDomain = defineDomain({
|
|
802
|
+
name: "Maybe",
|
|
803
|
+
is: (u) => u instanceof Maybe,
|
|
804
|
+
ops: {
|
|
805
|
+
isTop: {},
|
|
806
|
+
isBottom: {},
|
|
807
|
+
map: {},
|
|
808
|
+
chain: {},
|
|
809
|
+
valueOr: {}
|
|
810
|
+
}
|
|
811
|
+
});
|
|
812
|
+
//#endregion
|
|
813
|
+
export { Angle, AngleDomain, BoolDomain, Color, ColorDomain, Fn, FnDomain, LiftedNDVectorDomain, Maybe, MaybeDomain, NDVector, NDVectorDomain, NumDomain, NumRecordDomain, Vec2, Vec2Domain };
|
|
814
|
+
|
|
815
|
+
//# sourceMappingURL=index.js.map
|