@inglorious/utils 3.6.3 → 3.7.1
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/README.md +1 -1
- package/package.json +5 -4
- package/src/data-structures/object.js +96 -3
- package/src/algorithms/decision-tree.test.js +0 -154
- package/src/algorithms/path-finding.test.js +0 -151
- package/src/data-structures/array.test.js +0 -206
- package/src/data-structures/board.test.js +0 -242
- package/src/data-structures/heap.test.js +0 -103
- package/src/data-structures/object.test.js +0 -335
- package/src/data-structures/objects.test.js +0 -196
- package/src/data-structures/set.test.js +0 -15
- package/src/data-structures/tree.test.js +0 -33
- package/src/functions/function.test.js +0 -15
- package/src/functions/functions.test.js +0 -51
- package/src/math/circle.test.js +0 -98
- package/src/math/hitmask.test.js +0 -156
- package/src/math/line.test.js +0 -50
- package/src/math/numbers.test.js +0 -198
- package/src/math/point.test.js +0 -94
- package/src/math/quaternion.test.js +0 -25
- package/src/math/quaternions.test.js +0 -29
- package/src/math/rectangle.test.js +0 -43
- package/src/math/rng.test.js +0 -55
- package/src/math/segment.test.js +0 -184
- package/src/math/statistics.test.js +0 -47
- package/src/math/triangle.test.js +0 -11
- package/src/math/trigonometry.test.js +0 -52
- package/src/math/vector.test.js +0 -345
- package/src/math/vectors.test.js +0 -77
- package/src/physics/friction.test.js +0 -43
- package/src/physics/gravity.test.js +0 -78
- package/src/v.test.js +0 -48
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import { pi, toDegrees, toRadians, toRange } from "./trigonometry.js"
|
|
4
|
-
|
|
5
|
-
test("it should convert radians to degrees", () => {
|
|
6
|
-
const radians = pi() / 4
|
|
7
|
-
const expectedResult = 45
|
|
8
|
-
|
|
9
|
-
expect(toDegrees(radians)).toBeCloseTo(expectedResult)
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
test("it should convert radians to degrees", () => {
|
|
13
|
-
const degrees = 45
|
|
14
|
-
const expectedResult = pi() / 4
|
|
15
|
-
|
|
16
|
-
expect(toRadians(degrees)).toBeCloseTo(expectedResult)
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
test("it should convert an angle greater than pi to a range between -pi and pi", () => {
|
|
20
|
-
const angle = (5 / 4) * pi()
|
|
21
|
-
const expectedResult = (-3 / 4) * pi()
|
|
22
|
-
|
|
23
|
-
expect(toRange(angle)).toBeCloseTo(expectedResult)
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
test("it should convert an angle greater than 2pi to a range between -pi and pi", () => {
|
|
27
|
-
const angle = (13 / 4) * pi()
|
|
28
|
-
const expectedResult = (-3 / 4) * pi()
|
|
29
|
-
|
|
30
|
-
expect(toRange(angle)).toBeCloseTo(expectedResult)
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
test("it should convert an angle less than pi to a range between -pi and pi", () => {
|
|
34
|
-
const angle = (-5 / 4) * pi()
|
|
35
|
-
const expectedResult = (3 / 4) * pi()
|
|
36
|
-
|
|
37
|
-
expect(toRange(angle)).toBeCloseTo(expectedResult)
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
test("it should convert an angle less than 2pi to a range between -pi and pi", () => {
|
|
41
|
-
const angle = (-13 / 4) * pi()
|
|
42
|
-
const expectedResult = (3 / 4) * pi()
|
|
43
|
-
|
|
44
|
-
expect(toRange(angle)).toBeCloseTo(expectedResult)
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
test("it should not convert an angle already in the range [-pi, pi]", () => {
|
|
48
|
-
const angle = (3 / 4) * pi()
|
|
49
|
-
const expectedResult = (3 / 4) * pi()
|
|
50
|
-
|
|
51
|
-
expect(toRange(angle)).toBe(expectedResult)
|
|
52
|
-
})
|
package/src/math/vector.test.js
DELETED
|
@@ -1,345 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import { v } from "../v.js"
|
|
4
|
-
import { sqrt } from "./numbers.js"
|
|
5
|
-
import { cos, pi, sin } from "./trigonometry.js"
|
|
6
|
-
import {
|
|
7
|
-
abs,
|
|
8
|
-
angle,
|
|
9
|
-
clamp,
|
|
10
|
-
conjugate,
|
|
11
|
-
createVector,
|
|
12
|
-
divide,
|
|
13
|
-
divideBy,
|
|
14
|
-
from2D,
|
|
15
|
-
fromAngle,
|
|
16
|
-
isVector,
|
|
17
|
-
magnitude,
|
|
18
|
-
mod,
|
|
19
|
-
modOf,
|
|
20
|
-
multiply,
|
|
21
|
-
normalize,
|
|
22
|
-
power,
|
|
23
|
-
powerOf,
|
|
24
|
-
rotate,
|
|
25
|
-
setAngle,
|
|
26
|
-
setMagnitude,
|
|
27
|
-
shift,
|
|
28
|
-
snap,
|
|
29
|
-
to2D,
|
|
30
|
-
toCartesian,
|
|
31
|
-
toCylindrical,
|
|
32
|
-
toPolar,
|
|
33
|
-
toSpherical,
|
|
34
|
-
toString,
|
|
35
|
-
unit,
|
|
36
|
-
zero,
|
|
37
|
-
} from "./vector.js"
|
|
38
|
-
|
|
39
|
-
test("it should compute the absolute value of a vector's coordinates", () => {
|
|
40
|
-
const vector = v(-2, 0, 3)
|
|
41
|
-
const expectedResult = v(2, 0, 3)
|
|
42
|
-
|
|
43
|
-
expect(abs(vector)).toStrictEqual(expectedResult)
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
test("it should compute the angle of a 2D vector", () => {
|
|
47
|
-
const vector = v(1, 1)
|
|
48
|
-
const expectedResult = pi() / 4
|
|
49
|
-
|
|
50
|
-
expect(angle(vector)).toBe(expectedResult)
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
test("it should compute the angle of a 3D vector", () => {
|
|
54
|
-
const vector = v(1, 0, 1)
|
|
55
|
-
const expectedResult = pi() / 4
|
|
56
|
-
|
|
57
|
-
expect(angle(vector)).toBe(expectedResult)
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
test("it should clamp the magnitude of a vector to a certain length if too long", () => {
|
|
61
|
-
const vector = v(6, 8)
|
|
62
|
-
const min = 0
|
|
63
|
-
const max = 5
|
|
64
|
-
const expectedResult = v(3, 4)
|
|
65
|
-
|
|
66
|
-
expect(clamp(vector, min, max)).toStrictEqual(expectedResult)
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
test("it should not clamp the magnitude of a vector to a certain length if not too long", () => {
|
|
70
|
-
const vector = v(3, 4)
|
|
71
|
-
const min = 0
|
|
72
|
-
const max = 6
|
|
73
|
-
const expectedResult = v(3, 4)
|
|
74
|
-
|
|
75
|
-
expect(clamp(vector, min, max)).toStrictEqual(expectedResult)
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
test("it should clamp the magnitude of a vector to the values of other vector bounds", () => {
|
|
79
|
-
const vector = v(6, 8)
|
|
80
|
-
const lowerBound = [0, 0]
|
|
81
|
-
const upperBound = [3, 4]
|
|
82
|
-
const expectedResult = v(3, 4)
|
|
83
|
-
|
|
84
|
-
expect(clamp(vector, lowerBound, upperBound)).toStrictEqual(expectedResult)
|
|
85
|
-
})
|
|
86
|
-
|
|
87
|
-
test("it should conjugate a vector", () => {
|
|
88
|
-
const vector = v(1, 2, 3)
|
|
89
|
-
const expectedResult = v(1, -2, -3)
|
|
90
|
-
|
|
91
|
-
expect(conjugate(vector)).toStrictEqual(expectedResult)
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
test("it should create a 3D vector given its magnitude and angle", () => {
|
|
95
|
-
const magnitude = sqrt(2)
|
|
96
|
-
const angle = pi() / 4
|
|
97
|
-
const expectedResult = v(1, -0, 1.0000000000000002) // close to v(1, 0, 1)
|
|
98
|
-
|
|
99
|
-
expect(createVector(magnitude, angle)).toStrictEqual(expectedResult)
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
test("it should divide a vector by a scalar", () => {
|
|
103
|
-
const vector = v(4, 8, 12)
|
|
104
|
-
const scalar = 4
|
|
105
|
-
const expectedResult = v(1, 2, 3)
|
|
106
|
-
|
|
107
|
-
expect(divide(vector, scalar)).toStrictEqual(expectedResult)
|
|
108
|
-
})
|
|
109
|
-
|
|
110
|
-
test("it should divide a scalar by a vector", () => {
|
|
111
|
-
const scalar = 12
|
|
112
|
-
const vector = v(4, 8, 12)
|
|
113
|
-
const expectedResult = v(3, 1.5, 1)
|
|
114
|
-
|
|
115
|
-
expect(divideBy(scalar, vector)).toStrictEqual(expectedResult)
|
|
116
|
-
})
|
|
117
|
-
|
|
118
|
-
test("it should build a 3D vector from a 2D one", () => {
|
|
119
|
-
const vector = v(3, 4)
|
|
120
|
-
const expectedResult = v(3, 0, 4)
|
|
121
|
-
|
|
122
|
-
expect(from2D(vector)).toStrictEqual(expectedResult)
|
|
123
|
-
})
|
|
124
|
-
|
|
125
|
-
test("it should create a 3D unit vector from an angle", () => {
|
|
126
|
-
const angle = pi() / 4
|
|
127
|
-
const expectedResult = v(0.7071067811865475, -0, 0.7071067811865476) // close to [cos(angle), 0, sin(angle)]
|
|
128
|
-
|
|
129
|
-
expect(fromAngle(angle)).toStrictEqual(expectedResult)
|
|
130
|
-
})
|
|
131
|
-
|
|
132
|
-
test("it should compute the magnitude of a vector (aka length)", () => {
|
|
133
|
-
const vector = v(3, 4)
|
|
134
|
-
const expectedResult = 5
|
|
135
|
-
|
|
136
|
-
expect(magnitude(vector)).toBe(expectedResult)
|
|
137
|
-
})
|
|
138
|
-
|
|
139
|
-
test("it should check if a value is a vector", () => {
|
|
140
|
-
expect(isVector([1, 2, 3])).toBe(false)
|
|
141
|
-
expect(isVector(v(1, 2, 3))).toBe(true)
|
|
142
|
-
})
|
|
143
|
-
|
|
144
|
-
test("it should check if a value is not a vector", () => {
|
|
145
|
-
expect(isVector([1, "a", 3])).toBe(false)
|
|
146
|
-
expect(isVector({ x: 1, y: 2 })).toBe(false)
|
|
147
|
-
expect(isVector(null)).toBe(false)
|
|
148
|
-
expect(isVector("vector")).toBe(false)
|
|
149
|
-
expect(isVector(123)).toBe(false)
|
|
150
|
-
})
|
|
151
|
-
|
|
152
|
-
test("it should apply the mod operator (aka remainder) on a vector", () => {
|
|
153
|
-
const vector = v(10, 12, -18)
|
|
154
|
-
const divisor = 12
|
|
155
|
-
const expectedResult = v(10, 0, 6)
|
|
156
|
-
|
|
157
|
-
expect(mod(vector, divisor)).toStrictEqual(expectedResult)
|
|
158
|
-
})
|
|
159
|
-
|
|
160
|
-
test("it should apply the mod operator of a scalar with a vector", () => {
|
|
161
|
-
const scalar = 18
|
|
162
|
-
const vector = v(10, 12, -18)
|
|
163
|
-
const expectedResult = v(8, 6, -0)
|
|
164
|
-
|
|
165
|
-
expect(modOf(scalar, vector)).toStrictEqual(expectedResult)
|
|
166
|
-
})
|
|
167
|
-
|
|
168
|
-
test("it should multiply a vector with a scalar (aka times)", () => {
|
|
169
|
-
const vector = v(1, 2, 3)
|
|
170
|
-
const scalar = 4
|
|
171
|
-
const expectedResult = v(4, 8, 12)
|
|
172
|
-
|
|
173
|
-
expect(multiply(vector, scalar)).toStrictEqual(expectedResult)
|
|
174
|
-
})
|
|
175
|
-
|
|
176
|
-
test("it should normalize a vector so it has unit length", () => {
|
|
177
|
-
const vector = v(3, 4)
|
|
178
|
-
const expectedResult = v(0.6, 0.8)
|
|
179
|
-
|
|
180
|
-
expect(normalize(vector)).toStrictEqual(expectedResult)
|
|
181
|
-
})
|
|
182
|
-
|
|
183
|
-
test("it should normalize a negative vector", () => {
|
|
184
|
-
const vector = v(-3, -4)
|
|
185
|
-
const expectedResult = v(-0.6, -0.8)
|
|
186
|
-
|
|
187
|
-
expect(normalize(vector)).toStrictEqual(expectedResult)
|
|
188
|
-
})
|
|
189
|
-
|
|
190
|
-
test("it should raise each component of a vector to the given exponent (aka pow)", () => {
|
|
191
|
-
const vector = v(1, 2, 3)
|
|
192
|
-
const exponent = 2
|
|
193
|
-
const expectedResult = v(1, 4, 9)
|
|
194
|
-
|
|
195
|
-
expect(power(vector, exponent)).toStrictEqual(expectedResult)
|
|
196
|
-
})
|
|
197
|
-
|
|
198
|
-
test("it should raise a scalar to the power of each component of a vector", () => {
|
|
199
|
-
const scalar = 2
|
|
200
|
-
const vector = v(1, 2, 3)
|
|
201
|
-
const expectedResult = v(2, 4, 8)
|
|
202
|
-
|
|
203
|
-
expect(powerOf(scalar, vector)).toStrictEqual(expectedResult)
|
|
204
|
-
})
|
|
205
|
-
|
|
206
|
-
test("it should rotate a 2D vector by a certain angle", () => {
|
|
207
|
-
const vector = v(1, 0)
|
|
208
|
-
const angle = pi() / 4
|
|
209
|
-
const expectedResult = v(0.7071067811865475, 0.7071067811865476) // close to [cos(pi/4), sin(pi/4)]
|
|
210
|
-
|
|
211
|
-
expect(rotate(vector, angle)).toStrictEqual(expectedResult)
|
|
212
|
-
})
|
|
213
|
-
|
|
214
|
-
test("it should not rotate a vector when the angle is zero", () => {
|
|
215
|
-
const vector = v(1, 0, 0)
|
|
216
|
-
const angle = 0
|
|
217
|
-
const expectedResult = v(1, 0, 0)
|
|
218
|
-
|
|
219
|
-
expect(rotate(vector, angle)).toStrictEqual(expectedResult)
|
|
220
|
-
})
|
|
221
|
-
|
|
222
|
-
test("it should rotate a vector by a certain angle", () => {
|
|
223
|
-
const vector = v(1, 0, 0)
|
|
224
|
-
const angle = pi() / 4
|
|
225
|
-
const expectedResult = v(0.7071067811865475, -0, 0.7071067811865476) // close to [cos(pi/4), 0, sin(pi/4)]
|
|
226
|
-
|
|
227
|
-
expect(rotate(vector, angle)).toStrictEqual(expectedResult)
|
|
228
|
-
})
|
|
229
|
-
|
|
230
|
-
test("it should rotate a vector that faces left by a certain angle", () => {
|
|
231
|
-
const vector = v(-1, 0, 0)
|
|
232
|
-
const angle = pi() / 4
|
|
233
|
-
const expectedResult = v(-0.7071067811865475, -0, -0.7071067811865476) // close to [cos(-3/4pi), 0, sin(-3/4pi)]
|
|
234
|
-
|
|
235
|
-
expect(rotate(vector, angle)).toStrictEqual(expectedResult)
|
|
236
|
-
})
|
|
237
|
-
|
|
238
|
-
test("it should change the angle of a vector", () => {
|
|
239
|
-
const vector = v(cos(pi() / 4), 0, sin(pi() / 4))
|
|
240
|
-
const expectedResult = v(6.123233995736766e-17, 0, 1) // close to v(0, 0, 1)
|
|
241
|
-
|
|
242
|
-
expect(setAngle(vector, 1.5707963267948966)).toStrictEqual(expectedResult)
|
|
243
|
-
})
|
|
244
|
-
|
|
245
|
-
test("it should change magnitude of a vector (aka setLength)", () => {
|
|
246
|
-
const vector = v(3, 4)
|
|
247
|
-
const magnitude = 10
|
|
248
|
-
const expectedResult = v(6, 8)
|
|
249
|
-
|
|
250
|
-
expect(setMagnitude(vector, magnitude)).toStrictEqual(expectedResult)
|
|
251
|
-
})
|
|
252
|
-
|
|
253
|
-
test("it should change magnitude of a negative vector", () => {
|
|
254
|
-
const vector = v(-3, -4)
|
|
255
|
-
const magnitude = 10
|
|
256
|
-
const expectedResult = v(-6, -8)
|
|
257
|
-
|
|
258
|
-
expect(setMagnitude(vector, magnitude)).toStrictEqual(expectedResult)
|
|
259
|
-
})
|
|
260
|
-
|
|
261
|
-
test("it should shift a vector at a certain index", () => {
|
|
262
|
-
const vector = v(1, 2, 3, 4, 5)
|
|
263
|
-
const index = 2
|
|
264
|
-
const expectedResult = v(3, 4, 5, 1, 2)
|
|
265
|
-
|
|
266
|
-
expect(shift(vector, index)).toStrictEqual(expectedResult)
|
|
267
|
-
})
|
|
268
|
-
|
|
269
|
-
test("it should snap a floating vector to a certain precision", () => {
|
|
270
|
-
const vector = v(1, 1.5, 1.7)
|
|
271
|
-
const precision = 1
|
|
272
|
-
const expectedResult = v(1, 1, 2)
|
|
273
|
-
|
|
274
|
-
expect(snap(vector, precision)).toStrictEqual(expectedResult)
|
|
275
|
-
})
|
|
276
|
-
|
|
277
|
-
test("it should build a 2D vector from a 3D one", () => {
|
|
278
|
-
const vector = v(3, 0, 4)
|
|
279
|
-
const expectedResult = v(3, 4)
|
|
280
|
-
|
|
281
|
-
expect(to2D(vector)).toStrictEqual(expectedResult)
|
|
282
|
-
})
|
|
283
|
-
|
|
284
|
-
test("it should convert a 2D polar vector to cartesian coordinates", () => {
|
|
285
|
-
const vector = v(sqrt(2), pi() / 4)
|
|
286
|
-
const expectedResult = v(1.0000000000000002, 1)
|
|
287
|
-
|
|
288
|
-
expect(toCartesian(vector)).toStrictEqual(expectedResult)
|
|
289
|
-
})
|
|
290
|
-
|
|
291
|
-
test("it should convert a 3D cartesian vector to cylindrical coordinates", () => {
|
|
292
|
-
const vector = v(1, 1, 1)
|
|
293
|
-
const expectedResult = v(1.2247448713915892, 1.224744871391589, 1)
|
|
294
|
-
|
|
295
|
-
expect(toCylindrical(vector)).toStrictEqual(expectedResult)
|
|
296
|
-
})
|
|
297
|
-
|
|
298
|
-
test("it should convert a 2D cartesian vector to polar coordinates", () => {
|
|
299
|
-
const vector = v(1, 1)
|
|
300
|
-
const expectedResult = v(sqrt(2), pi() / 4)
|
|
301
|
-
|
|
302
|
-
expect(toPolar(vector)).toStrictEqual(expectedResult)
|
|
303
|
-
})
|
|
304
|
-
|
|
305
|
-
test("it should convert a 3D cartesian vector to spherical coordinates", () => {
|
|
306
|
-
const vector = v(1, 1, 1)
|
|
307
|
-
const expectedResult = v(sqrt(3), Math.acos(1 / sqrt(3)), pi() / 4)
|
|
308
|
-
|
|
309
|
-
expect(toSpherical(vector)).toStrictEqual(expectedResult)
|
|
310
|
-
})
|
|
311
|
-
|
|
312
|
-
test("it should create a string representation of an integer vector", () => {
|
|
313
|
-
const vector = v(3, 4)
|
|
314
|
-
const expectedResult = "[3, 4]"
|
|
315
|
-
|
|
316
|
-
expect(toString(vector)).toBe(expectedResult)
|
|
317
|
-
})
|
|
318
|
-
|
|
319
|
-
test("it should create a string representation of a float vector", () => {
|
|
320
|
-
const vector = v(pi() / 4, pi() / 4)
|
|
321
|
-
const decimals = 2
|
|
322
|
-
const expectedResult = "[0.79, 0.79]"
|
|
323
|
-
|
|
324
|
-
expect(toString(vector, decimals)).toBe(expectedResult)
|
|
325
|
-
})
|
|
326
|
-
|
|
327
|
-
test("it should create a unit vector oriented on the X-axis", () => {
|
|
328
|
-
expect(unit()).toStrictEqual(v(1, 0, 0))
|
|
329
|
-
})
|
|
330
|
-
|
|
331
|
-
test("it should create a unit vector oriented on the Z-axis", () => {
|
|
332
|
-
expect(unit(pi() / 2)).toStrictEqual(v(6.123233995736766e-17, 0, 1)) // close to v(0, 0, 1)
|
|
333
|
-
})
|
|
334
|
-
|
|
335
|
-
test("it should create a vector from a list of coordinates", () => {
|
|
336
|
-
expect(v(1, 2, 3)).toStrictEqual([1, 2, 3])
|
|
337
|
-
expect(v()).toStrictEqual([])
|
|
338
|
-
expect(v(0, -5.5)).toStrictEqual([0, -5.5])
|
|
339
|
-
})
|
|
340
|
-
|
|
341
|
-
test("it should create a 3D zero vector", () => {
|
|
342
|
-
const expectedResult = v(0, 0, 0)
|
|
343
|
-
|
|
344
|
-
expect(zero()).toStrictEqual(expectedResult)
|
|
345
|
-
})
|
package/src/math/vectors.test.js
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import { v } from "../v.js"
|
|
4
|
-
import {
|
|
5
|
-
cross,
|
|
6
|
-
distance,
|
|
7
|
-
divide,
|
|
8
|
-
dot,
|
|
9
|
-
multiply,
|
|
10
|
-
power,
|
|
11
|
-
subtract,
|
|
12
|
-
sum,
|
|
13
|
-
} from "./vectors.js"
|
|
14
|
-
|
|
15
|
-
test("it should compute the cross product between two vectors (aka vectorProduct)", () => {
|
|
16
|
-
const vector1 = v(1, 2, 3)
|
|
17
|
-
const vector2 = v(4, 5, 6)
|
|
18
|
-
const expectedResult = v(-3, 6, -3)
|
|
19
|
-
|
|
20
|
-
expect(cross(vector1, vector2)).toStrictEqual(expectedResult)
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
test("it should compute the cross product of multiple vectors (aka vectorProduct)", () => {
|
|
24
|
-
const vectors = [v(1, 2, 3), v(4, 5, 6), v(7, 8, 9)]
|
|
25
|
-
const expectedResult = v(78, 6, -66)
|
|
26
|
-
|
|
27
|
-
expect(cross(...vectors)).toStrictEqual(expectedResult)
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
test("it should compute the distance between multiple vectors", () => {
|
|
31
|
-
const vectors = [v(0, 0, 0), v(4, 0, 3)]
|
|
32
|
-
const expectedResult = 5
|
|
33
|
-
|
|
34
|
-
expect(distance(...vectors)).toStrictEqual(expectedResult)
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
test("it should divide multiple vectors component-wise", () => {
|
|
38
|
-
const vectors = [v(80, 40, 20), v(2, 4, 5), v(2, 2, 2)]
|
|
39
|
-
const expectedResult = v(20, 5, 2)
|
|
40
|
-
|
|
41
|
-
expect(divide(...vectors)).toStrictEqual(expectedResult)
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
test("it should compute the dot product of multiple vectors (aka scalarProduct)", () => {
|
|
45
|
-
const vectors = [v(1, 2, 3), v(4, 5, 6), v(7, 8, 9)]
|
|
46
|
-
const expectedResult = 270
|
|
47
|
-
|
|
48
|
-
expect(dot(...vectors)).toStrictEqual(expectedResult)
|
|
49
|
-
})
|
|
50
|
-
|
|
51
|
-
test("it should multiply multiple vectors component-wise (Hadamard product)", () => {
|
|
52
|
-
const vectors = [v(1, 2, 3), v(4, 5, 6), v(2, 2, 2)]
|
|
53
|
-
const expectedResult = v(8, 20, 36)
|
|
54
|
-
|
|
55
|
-
expect(multiply(...vectors)).toStrictEqual(expectedResult)
|
|
56
|
-
})
|
|
57
|
-
|
|
58
|
-
test("it should raise a vector to the power of another vector", () => {
|
|
59
|
-
const vectors = [v(2, 3, 4), v(4, 3, 2)]
|
|
60
|
-
const expectedResult = v(16, 27, 16)
|
|
61
|
-
|
|
62
|
-
expect(power(...vectors)).toStrictEqual(expectedResult)
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
test("it should subtract multiple vectors", () => {
|
|
66
|
-
const vectors = [v(1, 2, 3), v(4, 5, 6), v(7, 8, 9)]
|
|
67
|
-
const expectedResult = v(-10, -11, -12)
|
|
68
|
-
|
|
69
|
-
expect(subtract(...vectors)).toStrictEqual(expectedResult)
|
|
70
|
-
})
|
|
71
|
-
|
|
72
|
-
test("it should sum multiple vectors (aka add)", () => {
|
|
73
|
-
const vectors = [v(1, 2, 3), v(4, 5, 6), v(7, 8, 9)]
|
|
74
|
-
const expectedResult = v(12, 15, 18)
|
|
75
|
-
|
|
76
|
-
expect(sum(...vectors)).toStrictEqual(expectedResult)
|
|
77
|
-
})
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import { zero } from "../math/vector.js"
|
|
4
|
-
import { v } from "../v.js"
|
|
5
|
-
import { applyFriction } from "./friction.js"
|
|
6
|
-
|
|
7
|
-
test("it should apply friction to some velocity", () => {
|
|
8
|
-
const velocity = v(8, 0, 6)
|
|
9
|
-
const friction = 5
|
|
10
|
-
const expectedResult = v(4, 0, 3)
|
|
11
|
-
|
|
12
|
-
expect(applyFriction({ velocity, friction })).toStrictEqual(expectedResult)
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
test("it should stop movement when friction is equal to velocity", () => {
|
|
16
|
-
const velocity = v(8, 0, 6)
|
|
17
|
-
const friction = 5
|
|
18
|
-
const dt = 2
|
|
19
|
-
|
|
20
|
-
expect(applyFriction({ velocity, friction }, dt)).toStrictEqual(zero())
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
test("it should apply no friction on no velocity", () => {
|
|
24
|
-
const velocity = zero()
|
|
25
|
-
const friction = 5
|
|
26
|
-
|
|
27
|
-
expect(applyFriction({ velocity, friction })).toBe(velocity)
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
test("it should apply no friction if no friction is passed", () => {
|
|
31
|
-
const velocity = v(8, 0, 6)
|
|
32
|
-
const friction = 0
|
|
33
|
-
|
|
34
|
-
expect(applyFriction({ velocity, friction })).toStrictEqual(velocity)
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
test("it should apply no friction if no time passed", () => {
|
|
38
|
-
const velocity = v(8, 0, 6)
|
|
39
|
-
const friction = 5
|
|
40
|
-
const dt = 0
|
|
41
|
-
|
|
42
|
-
expect(applyFriction({ velocity, friction }, dt)).toStrictEqual(velocity)
|
|
43
|
-
})
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import { v } from "../v.js"
|
|
4
|
-
import { applyGravity } from "./gravity.js"
|
|
5
|
-
|
|
6
|
-
test("it should apply gravity based on input parameters", () => {
|
|
7
|
-
const params = {
|
|
8
|
-
maxJump: 10,
|
|
9
|
-
maxLeap: 10,
|
|
10
|
-
maxSpeed: 10,
|
|
11
|
-
vy: 10,
|
|
12
|
-
position: v(0, 10, 0),
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
expect(applyGravity(params)).toStrictEqual({
|
|
16
|
-
ay: -20,
|
|
17
|
-
vy: -10,
|
|
18
|
-
position: v(0, -10, 0),
|
|
19
|
-
})
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
test("it should compute no gravity when max jump height is not set", () => {
|
|
23
|
-
const params = {
|
|
24
|
-
maxLeap: 10,
|
|
25
|
-
maxSpeed: 10,
|
|
26
|
-
vy: 10,
|
|
27
|
-
position: v(0, 10, 0),
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
expect(applyGravity(params)).toStrictEqual({
|
|
31
|
-
ay: -0,
|
|
32
|
-
vy: 10,
|
|
33
|
-
position: v(0, 20, 0),
|
|
34
|
-
})
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
test("it should compute no gravity when max movement speed is not set", () => {
|
|
38
|
-
const params = {
|
|
39
|
-
maxJump: 10,
|
|
40
|
-
maxLeap: 10,
|
|
41
|
-
vy: 10,
|
|
42
|
-
position: v(0, 10, 0),
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
expect(applyGravity(params)).toStrictEqual({
|
|
46
|
-
ay: -0,
|
|
47
|
-
vy: 10,
|
|
48
|
-
position: v(0, 20, 0),
|
|
49
|
-
})
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
test("it should apply no gravity when no time has passed", () => {
|
|
53
|
-
const params = {
|
|
54
|
-
maxJump: 10,
|
|
55
|
-
maxLeap: 10,
|
|
56
|
-
maxSpeed: 10,
|
|
57
|
-
vy: 10,
|
|
58
|
-
position: v(0, 10, 0),
|
|
59
|
-
}
|
|
60
|
-
const dt = 0
|
|
61
|
-
|
|
62
|
-
expect(applyGravity(params, dt)).toStrictEqual({
|
|
63
|
-
ay: -20,
|
|
64
|
-
vy: 10,
|
|
65
|
-
position: v(0, 10, 0),
|
|
66
|
-
})
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
test("it should throw an error when max leap distance is not set", () => {
|
|
70
|
-
const params = {
|
|
71
|
-
maxJump: 10,
|
|
72
|
-
maxSpeed: 10,
|
|
73
|
-
vy: 10,
|
|
74
|
-
position: v(0, 10, 0),
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
expect(() => applyGravity(params)).toThrow()
|
|
78
|
-
})
|
package/src/v.test.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import { ensureV, v } from "./v"
|
|
4
|
-
|
|
5
|
-
test("ensureV() should convert a numeric array into a vector", () => {
|
|
6
|
-
const array = [1, 2, 3]
|
|
7
|
-
const result = ensureV(array)
|
|
8
|
-
expect(result).toStrictEqual([1, 2, 3])
|
|
9
|
-
expect(result.__isVector__).toBe(true)
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
test("ensureV() should return an existing vector unchanged", () => {
|
|
13
|
-
const vector = v(4, 5, 6)
|
|
14
|
-
const result = ensureV(vector)
|
|
15
|
-
expect(result).toBe(vector)
|
|
16
|
-
expect(result.__isVector__).toBe(true)
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
test("ensureV() should not convert an array with non-finite numbers", () => {
|
|
20
|
-
const array = [1, Infinity, 3]
|
|
21
|
-
const result = ensureV(array)
|
|
22
|
-
expect(result).toBe(array)
|
|
23
|
-
expect(result.__isVector__).toBeUndefined()
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
test("ensureV() should not convert an array with non-numeric values", () => {
|
|
27
|
-
const array = [1, "a", 3]
|
|
28
|
-
const result = ensureV(array)
|
|
29
|
-
expect(result).toBe(array)
|
|
30
|
-
expect(result.__isVector__).toBeUndefined()
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
test("ensureV() should return non-array values unchanged", () => {
|
|
34
|
-
expect(ensureV(123)).toBe(123)
|
|
35
|
-
expect(ensureV("hello")).toBe("hello")
|
|
36
|
-
const obj = { a: 1 }
|
|
37
|
-
expect(ensureV(obj)).toBe(obj)
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
test("a vector created with the v() function is just a number array", () => {
|
|
41
|
-
const vector = v(1, 2, 3)
|
|
42
|
-
expect(vector).toStrictEqual([1, 2, 3])
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
test("a vector created with the v() function has a __isVector__ property", () => {
|
|
46
|
-
const vector = v(1, 2, 3)
|
|
47
|
-
expect(vector.__isVector__).toBe(true)
|
|
48
|
-
})
|