@inglorious/utils 2.3.0 → 3.1.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.
Files changed (45) hide show
  1. package/README.md +1 -1
  2. package/package.json +1 -1
  3. package/src/algorithms/decision-tree.test.js +7 -6
  4. package/src/algorithms/index.js +4 -0
  5. package/src/algorithms/path-finding.js +2 -2
  6. package/src/data-structures/index.js +10 -0
  7. package/src/functions/index.js +4 -0
  8. package/src/index.js +8 -0
  9. package/src/math/{geometry/circle.js → circle.js} +6 -6
  10. package/src/math/{geometry/circle.test.js → circle.test.js} +23 -22
  11. package/src/math/{geometry/hitmask.test.js → hitmask.test.js} +16 -15
  12. package/src/math/index.js +35 -0
  13. package/src/math/{geometry/line.js → line.js} +4 -6
  14. package/src/math/{geometry/line.test.js → line.test.js} +11 -10
  15. package/src/math/linear-interpolation.js +0 -1
  16. package/src/math/{geometry/point.js → point.js} +5 -5
  17. package/src/math/{geometry/point.test.js → point.test.js} +23 -22
  18. package/src/math/{linear-algebra/quaternion.js → quaternion.js} +4 -4
  19. package/src/math/{linear-algebra/quaternion.test.js → quaternion.test.js} +1 -1
  20. package/src/math/{linear-algebra/quaternions.js → quaternions.js} +7 -4
  21. package/src/math/{linear-algebra/quaternions.test.js → quaternions.test.js} +1 -1
  22. package/src/math/{geometry/rectangle.js → rectangle.js} +3 -5
  23. package/src/math/{geometry/rectangle.test.js → rectangle.test.js} +13 -12
  24. package/src/math/rng.js +0 -2
  25. package/src/math/{geometry/segment.js → segment.js} +5 -9
  26. package/src/math/{geometry/segment.test.js → segment.test.js} +48 -47
  27. package/src/math/{geometry/triangle.js → triangle.js} +1 -1
  28. package/src/math/{geometry/types.d.ts → types.d.ts} +10 -0
  29. package/src/math/{linear-algebra/vector.js → vector.js} +73 -80
  30. package/src/math/{linear-algebra/vector.test.js → vector.test.js} +73 -63
  31. package/src/math/{linear-algebra/vectors.js → vectors.js} +18 -13
  32. package/src/math/{linear-algebra/vectors.test.js → vectors.test.js} +12 -30
  33. package/src/physics/acceleration.js +5 -5
  34. package/src/physics/friction.js +2 -3
  35. package/src/physics/friction.test.js +7 -6
  36. package/src/physics/gravity.js +2 -2
  37. package/src/physics/gravity.test.js +10 -9
  38. package/src/physics/index.js +7 -0
  39. package/src/physics/jump.js +1 -2
  40. package/src/physics/velocity.js +3 -4
  41. package/src/v.js +17 -0
  42. package/src/v.test.js +13 -0
  43. package/src/math/linear-algebra/types.d.ts +0 -5
  44. /package/src/math/{geometry/hitmask.js → hitmask.js} +0 -0
  45. /package/src/math/{geometry/triangle.test.js → triangle.test.js} +0 -0
@@ -1,7 +1,8 @@
1
1
  import { expect, test } from "vitest"
2
2
 
3
- import { sqrt } from "../numbers.js"
4
- import { cos, pi, sin } from "../trigonometry.js"
3
+ import { v } from "../v.js"
4
+ import { sqrt } from "./numbers.js"
5
+ import { cos, pi, sin } from "./trigonometry.js"
5
6
  import {
6
7
  abs,
7
8
  angle,
@@ -16,6 +17,7 @@ import {
16
17
  mod,
17
18
  multiply,
18
19
  normalize,
20
+ power,
19
21
  rotate,
20
22
  setAngle,
21
23
  setMagnitude,
@@ -28,61 +30,60 @@ import {
28
30
  toSpherical,
29
31
  toString,
30
32
  unit,
31
- v,
32
33
  zero,
33
34
  } from "./vector.js"
34
35
 
35
36
  test("it should compute the absolute value of a vector's coordinates", () => {
36
- const vector = [-2, 0, 3]
37
- const expectedResult = [2, 0, 3]
37
+ const vector = v(-2, 0, 3)
38
+ const expectedResult = v(2, 0, 3)
38
39
 
39
40
  expect(abs(vector)).toStrictEqual(expectedResult)
40
41
  })
41
42
 
42
43
  test("it should compute the angle of a 2D vector", () => {
43
- const vector = [1, 1]
44
+ const vector = v(1, 1)
44
45
  const expectedResult = pi() / 4
45
46
 
46
47
  expect(angle(vector)).toBe(expectedResult)
47
48
  })
48
49
 
49
50
  test("it should compute the angle of a 3D vector", () => {
50
- const vector = [1, 0, 1]
51
+ const vector = v(1, 0, 1)
51
52
  const expectedResult = pi() / 4
52
53
 
53
54
  expect(angle(vector)).toBe(expectedResult)
54
55
  })
55
56
 
56
57
  test("it should clamp the magnitude of a vector to a certain length if too long", () => {
57
- const vector = [6, 8]
58
+ const vector = v(6, 8)
58
59
  const min = 0
59
60
  const max = 5
60
- const expectedResult = [3, 4]
61
+ const expectedResult = v(3, 4)
61
62
 
62
63
  expect(clamp(vector, min, max)).toStrictEqual(expectedResult)
63
64
  })
64
65
 
65
66
  test("it should not clamp the magnitude of a vector to a certain length if not too long", () => {
66
- const vector = [3, 4]
67
+ const vector = v(3, 4)
67
68
  const min = 0
68
69
  const max = 6
69
- const expectedResult = [3, 4]
70
+ const expectedResult = v(3, 4)
70
71
 
71
72
  expect(clamp(vector, min, max)).toStrictEqual(expectedResult)
72
73
  })
73
74
 
74
75
  test("it should clamp the magnitude of a vector to the values of other vector bounds", () => {
75
- const vector = [6, 8]
76
+ const vector = v(6, 8)
76
77
  const lowerBound = [0, 0]
77
78
  const upperBound = [3, 4]
78
- const expectedResult = [3, 4]
79
+ const expectedResult = v(3, 4)
79
80
 
80
81
  expect(clamp(vector, lowerBound, upperBound)).toStrictEqual(expectedResult)
81
82
  })
82
83
 
83
84
  test("it should conjugate a vector", () => {
84
- const vector = [1, 2, 3]
85
- const expectedResult = [1, -2, -3]
85
+ const vector = v(1, 2, 3)
86
+ const expectedResult = v(1, -2, -3)
86
87
 
87
88
  expect(conjugate(vector)).toStrictEqual(expectedResult)
88
89
  })
@@ -90,35 +91,35 @@ test("it should conjugate a vector", () => {
90
91
  test("it should create a 3D vector given its magnitude and angle", () => {
91
92
  const magnitude = sqrt(2)
92
93
  const angle = pi() / 4
93
- const expectedResult = [1, -0, 1.0000000000000002] // close to [1, 0, 1]
94
+ const expectedResult = v(1, -0, 1.0000000000000002) // close to v(1, 0, 1)
94
95
 
95
96
  expect(createVector(magnitude, angle)).toStrictEqual(expectedResult)
96
97
  })
97
98
 
98
99
  test("it should divide a vector by a scalar", () => {
99
- const vector = [4, 8, 12]
100
+ const vector = v(4, 8, 12)
100
101
  const scalar = 4
101
- const expectedResult = [1, 2, 3]
102
+ const expectedResult = v(1, 2, 3)
102
103
 
103
104
  expect(divide(vector, scalar)).toStrictEqual(expectedResult)
104
105
  })
105
106
 
106
107
  test("it should build a 3D vector from a 2D one", () => {
107
- const vector = [3, 4]
108
- const expectedResult = [3, 0, 4]
108
+ const vector = v(3, 4)
109
+ const expectedResult = v(3, 0, 4)
109
110
 
110
111
  expect(from2D(vector)).toStrictEqual(expectedResult)
111
112
  })
112
113
 
113
114
  test("it should create a 3D unit vector from an angle", () => {
114
115
  const angle = pi() / 4
115
- const expectedResult = [0.7071067811865475, -0, 0.7071067811865476] // close to [cos(angle), 0, sin(angle)]
116
+ const expectedResult = v(0.7071067811865475, -0, 0.7071067811865476) // close to [cos(angle), 0, sin(angle)]
116
117
 
117
118
  expect(fromAngle(angle)).toStrictEqual(expectedResult)
118
119
  })
119
120
 
120
121
  test("it should compute the magnitude of a vector (aka length)", () => {
121
- const vector = [3, 4]
122
+ const vector = v(3, 4)
122
123
  const expectedResult = 5
123
124
 
124
125
  expect(magnitude(vector)).toBe(expectedResult)
@@ -127,6 +128,7 @@ test("it should compute the magnitude of a vector (aka length)", () => {
127
128
  test("it should check if a value is a vector", () => {
128
129
  expect(isVector([1, 2, 3])).toBe(true)
129
130
  expect(isVector([])).toBe(true)
131
+ expect(isVector(v(1, 2, 3))).toBe(true)
130
132
  })
131
133
 
132
134
  test("it should check if a value is not a vector", () => {
@@ -138,150 +140,158 @@ test("it should check if a value is not a vector", () => {
138
140
  })
139
141
 
140
142
  test("it should apply the mod operator (aka remainder) on a vector", () => {
141
- const vector = [10, 12, -18]
143
+ const vector = v(10, 12, -18)
142
144
  const divisor = 12
143
- const expectedResult = [10, 0, 6]
145
+ const expectedResult = v(10, 0, 6)
144
146
 
145
147
  expect(mod(vector, divisor)).toStrictEqual(expectedResult)
146
148
  })
147
149
 
148
150
  test("it should multiply a vector with a scalar (aka times)", () => {
149
- const vector = [1, 2, 3]
151
+ const vector = v(1, 2, 3)
150
152
  const scalar = 4
151
- const expectedResult = [4, 8, 12]
153
+ const expectedResult = v(4, 8, 12)
152
154
 
153
155
  expect(multiply(vector, scalar)).toStrictEqual(expectedResult)
154
156
  })
155
157
 
156
158
  test("it should normalize a vector so it has unit length", () => {
157
- const vector = [3, 4]
158
- const expectedResult = [0.6, 0.8]
159
+ const vector = v(3, 4)
160
+ const expectedResult = v(0.6, 0.8)
159
161
 
160
162
  expect(normalize(vector)).toStrictEqual(expectedResult)
161
163
  })
162
164
 
163
165
  test("it should normalize a negative vector", () => {
164
- const vector = [-3, -4]
165
- const expectedResult = [-0.6, -0.8]
166
+ const vector = v(-3, -4)
167
+ const expectedResult = v(-0.6, -0.8)
166
168
 
167
169
  expect(normalize(vector)).toStrictEqual(expectedResult)
168
170
  })
169
171
 
172
+ test("it should raise each component of a vector to the given exponent (aka pow)", () => {
173
+ const vector = v(1, 2, 3)
174
+ const exponent = 2
175
+ const expectedResult = v(1, 4, 9)
176
+
177
+ expect(power(vector, exponent)).toStrictEqual(expectedResult)
178
+ })
179
+
170
180
  test("it should rotate a 2D vector by a certain angle", () => {
171
- const vector = [1, 0]
181
+ const vector = v(1, 0)
172
182
  const angle = pi() / 4
173
- const expectedResult = [0.7071067811865475, 0.7071067811865476] // close to [cos(pi/4), sin(pi/4)]
183
+ const expectedResult = v(0.7071067811865475, 0.7071067811865476) // close to [cos(pi/4), sin(pi/4)]
174
184
 
175
185
  expect(rotate(vector, angle)).toStrictEqual(expectedResult)
176
186
  })
177
187
 
178
188
  test("it should not rotate a vector when the angle is zero", () => {
179
- const vector = [1, 0, 0]
189
+ const vector = v(1, 0, 0)
180
190
  const angle = 0
181
- const expectedResult = [1, 0, 0]
191
+ const expectedResult = v(1, 0, 0)
182
192
 
183
193
  expect(rotate(vector, angle)).toStrictEqual(expectedResult)
184
194
  })
185
195
 
186
196
  test("it should rotate a vector by a certain angle", () => {
187
- const vector = [1, 0, 0]
197
+ const vector = v(1, 0, 0)
188
198
  const angle = pi() / 4
189
- const expectedResult = [0.7071067811865475, -0, 0.7071067811865476] // close to [cos(pi/4), 0, sin(pi/4)]
199
+ const expectedResult = v(0.7071067811865475, -0, 0.7071067811865476) // close to [cos(pi/4), 0, sin(pi/4)]
190
200
 
191
201
  expect(rotate(vector, angle)).toStrictEqual(expectedResult)
192
202
  })
193
203
 
194
204
  test("it should rotate a vector that faces left by a certain angle", () => {
195
- const vector = [-1, 0, 0]
205
+ const vector = v(-1, 0, 0)
196
206
  const angle = pi() / 4
197
- const expectedResult = [-0.7071067811865475, -0, -0.7071067811865476] // close to [cos(-3/4pi), 0, sin(-3/4pi)]
207
+ const expectedResult = v(-0.7071067811865475, -0, -0.7071067811865476) // close to [cos(-3/4pi), 0, sin(-3/4pi)]
198
208
 
199
209
  expect(rotate(vector, angle)).toStrictEqual(expectedResult)
200
210
  })
201
211
 
202
212
  test("it should change the angle of a vector", () => {
203
- const vector = [cos(pi() / 4), 0, sin(pi() / 4)]
204
- const expectedResult = [6.123233995736766e-17, 0, 1] // close to [0, 0, 1]
213
+ const vector = v(cos(pi() / 4), 0, sin(pi() / 4))
214
+ const expectedResult = v(6.123233995736766e-17, 0, 1) // close to v(0, 0, 1)
205
215
 
206
216
  expect(setAngle(vector, 1.5707963267948966)).toStrictEqual(expectedResult)
207
217
  })
208
218
 
209
219
  test("it should change magnitude of a vector (aka setLength)", () => {
210
- const vector = [3, 4]
220
+ const vector = v(3, 4)
211
221
  const magnitude = 10
212
- const expectedResult = [6, 8]
222
+ const expectedResult = v(6, 8)
213
223
 
214
224
  expect(setMagnitude(vector, magnitude)).toStrictEqual(expectedResult)
215
225
  })
216
226
 
217
227
  test("it should change magnitude of a negative vector", () => {
218
- const vector = [-3, -4]
228
+ const vector = v(-3, -4)
219
229
  const magnitude = 10
220
- const expectedResult = [-6, -8]
230
+ const expectedResult = v(-6, -8)
221
231
 
222
232
  expect(setMagnitude(vector, magnitude)).toStrictEqual(expectedResult)
223
233
  })
224
234
 
225
235
  test("it should shift a vector at a certain index", () => {
226
- const vector = [1, 2, 3, 4, 5]
236
+ const vector = v(1, 2, 3, 4, 5)
227
237
  const index = 2
228
- const expectedResult = [3, 4, 5, 1, 2]
238
+ const expectedResult = v(3, 4, 5, 1, 2)
229
239
 
230
240
  expect(shift(vector, index)).toStrictEqual(expectedResult)
231
241
  })
232
242
 
233
243
  test("it should snap a floating vector to a certain precision", () => {
234
- const vector = [1, 1.5, 1.7]
244
+ const vector = v(1, 1.5, 1.7)
235
245
  const precision = 1
236
- const expectedResult = [1, 1, 2]
246
+ const expectedResult = v(1, 1, 2)
237
247
 
238
248
  expect(snap(vector, precision)).toStrictEqual(expectedResult)
239
249
  })
240
250
 
241
251
  test("it should build a 2D vector from a 3D one", () => {
242
- const vector = [3, 0, 4]
243
- const expectedResult = [3, 4]
252
+ const vector = v(3, 0, 4)
253
+ const expectedResult = v(3, 4)
244
254
 
245
255
  expect(to2D(vector)).toStrictEqual(expectedResult)
246
256
  })
247
257
 
248
258
  test("it should convert a 2D polar vector to cartesian coordinates", () => {
249
- const vector = [sqrt(2), pi() / 4]
250
- const expectedResult = [1.0000000000000002, 1]
259
+ const vector = v(sqrt(2), pi() / 4)
260
+ const expectedResult = v(1.0000000000000002, 1)
251
261
 
252
262
  expect(toCartesian(vector)).toStrictEqual(expectedResult)
253
263
  })
254
264
 
255
265
  test("it should convert a 3D cartesian vector to cylindrical coordinates", () => {
256
- const vector = [1, 1, 1]
257
- const expectedResult = [1.2247448713915892, 1.224744871391589, 1]
266
+ const vector = v(1, 1, 1)
267
+ const expectedResult = v(1.2247448713915892, 1.224744871391589, 1)
258
268
 
259
269
  expect(toCylindrical(vector)).toStrictEqual(expectedResult)
260
270
  })
261
271
 
262
272
  test("it should convert a 2D cartesian vector to polar coordinates", () => {
263
- const vector = [1, 1]
264
- const expectedResult = [sqrt(2), pi() / 4]
273
+ const vector = v(1, 1)
274
+ const expectedResult = v(sqrt(2), pi() / 4)
265
275
 
266
276
  expect(toPolar(vector)).toStrictEqual(expectedResult)
267
277
  })
268
278
 
269
279
  test("it should convert a 3D cartesian vector to spherical coordinates", () => {
270
- const vector = [1, 1, 1]
271
- const expectedResult = [sqrt(3), Math.acos(1 / sqrt(3)), pi() / 4]
280
+ const vector = v(1, 1, 1)
281
+ const expectedResult = v(sqrt(3), Math.acos(1 / sqrt(3)), pi() / 4)
272
282
 
273
283
  expect(toSpherical(vector)).toStrictEqual(expectedResult)
274
284
  })
275
285
 
276
286
  test("it should create a string representation of an integer vector", () => {
277
- const vector = [3, 4]
287
+ const vector = v(3, 4)
278
288
  const expectedResult = "[3, 4]"
279
289
 
280
290
  expect(toString(vector)).toBe(expectedResult)
281
291
  })
282
292
 
283
293
  test("it should create a string representation of a float vector", () => {
284
- const vector = [pi() / 4, pi() / 4]
294
+ const vector = v(pi() / 4, pi() / 4)
285
295
  const decimals = 2
286
296
  const expectedResult = "[0.79, 0.79]"
287
297
 
@@ -289,11 +299,11 @@ test("it should create a string representation of a float vector", () => {
289
299
  })
290
300
 
291
301
  test("it should create a unit vector oriented on the X-axis", () => {
292
- expect(unit()).toStrictEqual([1, 0, 0])
302
+ expect(unit()).toStrictEqual(v(1, 0, 0))
293
303
  })
294
304
 
295
305
  test("it should create a unit vector oriented on the Z-axis", () => {
296
- expect(unit(pi() / 2)).toStrictEqual([6.123233995736766e-17, 0, 1]) // close to [0, 0, 1]
306
+ expect(unit(pi() / 2)).toStrictEqual(v(6.123233995736766e-17, 0, 1)) // close to v(0, 0, 1)
297
307
  })
298
308
 
299
309
  test("it should create a vector from a list of coordinates", () => {
@@ -303,7 +313,7 @@ test("it should create a vector from a list of coordinates", () => {
303
313
  })
304
314
 
305
315
  test("it should create a 3D zero vector", () => {
306
- const expectedResult = [0, 0, 0]
316
+ const expectedResult = v(0, 0, 0)
307
317
 
308
318
  expect(zero()).toStrictEqual(expectedResult)
309
319
  })
@@ -4,6 +4,7 @@
4
4
  * @typedef {import('./types').Vector7} Vector7
5
5
  */
6
6
 
7
+ import { v } from "../v.js"
7
8
  import { magnitude, shift } from "./vector.js"
8
9
 
9
10
  /**
@@ -18,7 +19,7 @@ export const add = sum
18
19
  * @returns {Vector3 | Vector7} The resulting vector after the cross product.
19
20
  */
20
21
  export function cross(...vectors) {
21
- return vectors.reduce(crossMultiplyCoordinates)
22
+ return v(...vectors.reduce(crossMultiplyCoordinates))
22
23
  }
23
24
 
24
25
  /**
@@ -53,7 +54,7 @@ export const scalarProduct = dot
53
54
  * @returns {Vector2 | Vector3} The resulting vector after subtraction.
54
55
  */
55
56
  export function subtract(...vectors) {
56
- return vectors.reduce(subtractCoordinates)
57
+ return v(...vectors.reduce(subtractCoordinates))
57
58
  }
58
59
 
59
60
  /**
@@ -62,7 +63,7 @@ export function subtract(...vectors) {
62
63
  * @returns {Vector2 | Vector3} The resulting vector after summation.
63
64
  */
64
65
  export function sum(...vectors) {
65
- return vectors.reduce(sumCoordinates)
66
+ return v(...vectors.reduce(sumCoordinates))
66
67
  }
67
68
 
68
69
  /**
@@ -78,7 +79,7 @@ export const vectorProduct = cross
78
79
  * @returns {Vector2 | Vector3} The resulting vector after addition.
79
80
  */
80
81
  function sumCoordinates(vector1, vector2) {
81
- return vector1.map((coordinate, index) => coordinate + vector2[index])
82
+ return v(...vector1.map((coordinate, index) => coordinate + vector2[index]))
82
83
  }
83
84
 
84
85
  /**
@@ -88,7 +89,7 @@ function sumCoordinates(vector1, vector2) {
88
89
  * @returns {Vector2 | Vector3} The resulting vector after subtraction.
89
90
  */
90
91
  function subtractCoordinates(vector1, vector2) {
91
- return vector1.map((coordinate, index) => coordinate - vector2[index])
92
+ return v(...vector1.map((coordinate, index) => coordinate - vector2[index]))
92
93
  }
93
94
 
94
95
  /**
@@ -102,13 +103,17 @@ function crossMultiplyCoordinates(vector1, vector2) {
102
103
  .fill(null)
103
104
  .map((_, index) => index)
104
105
 
105
- return indexes.map((_, index) => {
106
- const [index1, index2] = shift(
107
- indexes.filter((_, i) => i !== index),
108
- index,
109
- )
110
- return vector1[index1] * vector2[index2] - vector1[index2] * vector2[index1]
111
- })
106
+ return v(
107
+ ...indexes.map((_, index) => {
108
+ const [index1, index2] = shift(
109
+ indexes.filter((_, i) => i !== index),
110
+ index,
111
+ )
112
+ return (
113
+ vector1[index1] * vector2[index2] - vector1[index2] * vector2[index1]
114
+ )
115
+ }),
116
+ )
112
117
  }
113
118
 
114
119
  /**
@@ -118,5 +123,5 @@ function crossMultiplyCoordinates(vector1, vector2) {
118
123
  * @returns {Vector2 | Vector3} The resulting vector containing multiplied coordinates.
119
124
  */
120
125
  function dotMultiplyCoordinates(vector1, vector2) {
121
- return vector1.map((coordinate, index) => coordinate * vector2[index])
126
+ return v(...vector1.map((coordinate, index) => coordinate * vector2[index]))
122
127
  }
@@ -1,65 +1,47 @@
1
1
  import { expect, test } from "vitest"
2
2
 
3
+ import { v } from "../v.js"
3
4
  import { cross, distance, dot, subtract, sum } from "./vectors.js"
4
5
 
5
6
  test("it should compute the cross product between two vectors (aka vectorProduct)", () => {
6
- const vector1 = [1, 2, 3]
7
- const vector2 = [4, 5, 6]
8
- const expectedResult = [-3, 6, -3]
7
+ const vector1 = v(1, 2, 3)
8
+ const vector2 = v(4, 5, 6)
9
+ const expectedResult = v(-3, 6, -3)
9
10
 
10
11
  expect(cross(vector1, vector2)).toStrictEqual(expectedResult)
11
12
  })
12
13
 
13
14
  test("it should compute the cross product of multiple vectors (aka vectorProduct)", () => {
14
- const vectors = [
15
- [1, 2, 3],
16
- [4, 5, 6],
17
- [7, 8, 9],
18
- ]
19
- const expectedResult = [78, 6, -66]
15
+ const vectors = [v(1, 2, 3), v(4, 5, 6), v(7, 8, 9)]
16
+ const expectedResult = v(78, 6, -66)
20
17
 
21
18
  expect(cross(...vectors)).toStrictEqual(expectedResult)
22
19
  })
23
20
 
24
21
  test("it should compute the distance between multiple vectors", () => {
25
- const vectors = [
26
- [0, 0, 0],
27
- [4, 0, 3],
28
- ]
22
+ const vectors = [v(0, 0, 0), v(4, 0, 3)]
29
23
  const expectedResult = 5
30
24
 
31
25
  expect(distance(...vectors)).toStrictEqual(expectedResult)
32
26
  })
33
27
 
34
28
  test("it should compute the dot product of multiple vectors (aka scalarProduct)", () => {
35
- const vectors = [
36
- [1, 2, 3],
37
- [4, 5, 6],
38
- [7, 8, 9],
39
- ]
29
+ const vectors = [v(1, 2, 3), v(4, 5, 6), v(7, 8, 9)]
40
30
  const expectedResult = 270
41
31
 
42
32
  expect(dot(...vectors)).toStrictEqual(expectedResult)
43
33
  })
44
34
 
45
35
  test("it should subtract multiple vectors", () => {
46
- const vectors = [
47
- [1, 2, 3],
48
- [4, 5, 6],
49
- [7, 8, 9],
50
- ]
51
- const expectedResult = [-10, -11, -12]
36
+ const vectors = [v(1, 2, 3), v(4, 5, 6), v(7, 8, 9)]
37
+ const expectedResult = v(-10, -11, -12)
52
38
 
53
39
  expect(subtract(...vectors)).toStrictEqual(expectedResult)
54
40
  })
55
41
 
56
42
  test("it should sum multiple vectors (aka add)", () => {
57
- const vectors = [
58
- [1, 2, 3],
59
- [4, 5, 6],
60
- [7, 8, 9],
61
- ]
62
- const expectedResult = [12, 15, 18]
43
+ const vectors = [v(1, 2, 3), v(4, 5, 6), v(7, 8, 9)]
44
+ const expectedResult = v(12, 15, 18)
63
45
 
64
46
  expect(sum(...vectors)).toStrictEqual(expectedResult)
65
47
  })
@@ -1,9 +1,9 @@
1
1
  /**
2
- * @typedef {import('../math/linear-algebra/types').Vector3} Vector3
2
+ * @typedef {import('../math/types.js').Vector3} Vector3
3
3
  */
4
4
 
5
- import { clamp, multiply, zero } from "../math/linear-algebra/vector.js"
6
- import { sum } from "../math/linear-algebra/vectors.js"
5
+ import { clamp, multiply, zero } from "../math/vector.js"
6
+ import { sum } from "../math/vectors.js"
7
7
  import { applyFriction } from "./friction.js"
8
8
 
9
9
  const DEFAULT_DT = 1 // Default time delta for the applyAcceleration function.
@@ -14,8 +14,8 @@ const HALF_ACCELERATION = 0.5 // Half of the acceleration factor used in positio
14
14
  * Applies acceleration to an object using Euler's integration method.
15
15
  *
16
16
  * Euler's Integration:
17
- * v += a * dt
18
- * p += v * dt + 1/2 * a * dt * dt
17
+ * 1. v += a * dt
18
+ * 2. p += v * dt + 1/2 * a * dt * dt
19
19
  *
20
20
  * @param {Object} params - The parameters for the function.
21
21
  * @param {number} params.maxAcceleration - The maximum allowed acceleration.
@@ -1,15 +1,14 @@
1
1
  /**
2
- * @typedef {import('../math/linear-algebra/types').Vector3} Vector3
2
+ * @typedef {import('../math/types.js').Vector3} Vector3
3
3
  */
4
4
 
5
- import { magnitude, setMagnitude, zero } from "../math/linear-algebra/vector.js"
5
+ import { magnitude, setMagnitude, zero } from "../math/vector.js"
6
6
 
7
7
  const DEFAULT_DT = 1 // Default time delta for the applyFriction function.
8
8
  const NO_FRICTION = 0 // No friction constant.
9
9
 
10
10
  /**
11
11
  * Applies friction to a given velocity vector.
12
- *
13
12
  * @param {Object} params - The parameters for the function.
14
13
  * @param {Vector3} params.velocity - The velocity vector. Defaults to a zero vector.
15
14
  * @param {number} params.friction - The friction coefficient. Defaults to 0.
@@ -1,18 +1,19 @@
1
1
  import { expect, test } from "vitest"
2
2
 
3
- import { zero } from "../math/linear-algebra/vector.js"
3
+ import { zero } from "../math/vector.js"
4
+ import { v } from "../v.js"
4
5
  import { applyFriction } from "./friction.js"
5
6
 
6
7
  test("it should apply friction to some velocity", () => {
7
- const velocity = [8, 0, 6]
8
+ const velocity = v(8, 0, 6)
8
9
  const friction = 5
9
- const expectedResult = [4, 0, 3]
10
+ const expectedResult = v(4, 0, 3)
10
11
 
11
12
  expect(applyFriction({ velocity, friction })).toStrictEqual(expectedResult)
12
13
  })
13
14
 
14
15
  test("it should stop movement when friction is equal to velocity", () => {
15
- const velocity = [8, 0, 6]
16
+ const velocity = v(8, 0, 6)
16
17
  const friction = 5
17
18
  const dt = 2
18
19
 
@@ -27,14 +28,14 @@ test("it should apply no friction on no velocity", () => {
27
28
  })
28
29
 
29
30
  test("it should apply no friction if no friction is passed", () => {
30
- const velocity = [8, 0, 6]
31
+ const velocity = v(8, 0, 6)
31
32
  const friction = 0
32
33
 
33
34
  expect(applyFriction({ velocity, friction })).toStrictEqual(velocity)
34
35
  })
35
36
 
36
37
  test("it should apply no friction if no time passed", () => {
37
- const velocity = [8, 0, 6]
38
+ const velocity = v(8, 0, 6)
38
39
  const friction = 5
39
40
  const dt = 0
40
41
 
@@ -1,10 +1,10 @@
1
1
  /**
2
- * @typedef {import('../math/linear-algebra/types').Vector3} Vector3
2
+ * @typedef {import('../math/types.js').Vector3} Vector3
3
3
  */
4
4
 
5
5
  /* eslint-disable no-magic-numbers */
6
6
 
7
- import { zero } from "../math/linear-algebra/vector.js"
7
+ import { zero } from "../math/vector.js"
8
8
 
9
9
  const DEFAULT_DT = 1 // Default delta time for the applyGravity function.
10
10
  const DEFAULT_JUMP = 0 // Default maximum jump height.