@inglorious/utils 1.0.0 → 1.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.
Files changed (67) hide show
  1. package/README.md +10 -11
  2. package/package.json +4 -4
  3. package/src/algorithms/decision-tree.js +24 -0
  4. package/src/algorithms/decision-tree.test.js +153 -0
  5. package/src/algorithms/path-finding.js +150 -0
  6. package/src/algorithms/path-finding.test.js +151 -0
  7. package/src/algorithms/types.d.ts +28 -0
  8. package/src/data-structures/array.js +93 -0
  9. package/src/data-structures/array.test.js +185 -0
  10. package/src/data-structures/board.js +159 -0
  11. package/src/data-structures/board.test.js +242 -0
  12. package/src/data-structures/boolean.js +9 -0
  13. package/src/data-structures/heap.js +164 -0
  14. package/src/data-structures/heap.test.js +103 -0
  15. package/src/data-structures/object.js +138 -0
  16. package/src/data-structures/object.test.js +218 -0
  17. package/src/data-structures/objects.js +66 -0
  18. package/src/data-structures/objects.test.js +99 -0
  19. package/src/data-structures/set.js +9 -0
  20. package/src/data-structures/set.test.js +15 -0
  21. package/src/data-structures/tree.js +36 -0
  22. package/src/data-structures/tree.test.js +33 -0
  23. package/src/data-structures/types.d.ts +4 -0
  24. package/src/functions/functions.js +19 -0
  25. package/src/functions/functions.test.js +23 -0
  26. package/src/math/geometry/circle.js +69 -0
  27. package/src/math/geometry/circle.test.js +97 -0
  28. package/src/math/geometry/hitmask.js +70 -0
  29. package/src/math/geometry/hitmask.test.js +155 -0
  30. package/src/math/geometry/line.js +34 -0
  31. package/src/math/geometry/line.test.js +49 -0
  32. package/src/math/geometry/point.js +78 -0
  33. package/src/math/geometry/point.test.js +81 -0
  34. package/src/math/geometry/rectangle.js +76 -0
  35. package/src/math/geometry/rectangle.test.js +42 -0
  36. package/src/math/geometry/segment.js +72 -0
  37. package/src/math/geometry/segment.test.js +183 -0
  38. package/src/math/geometry/triangle.js +15 -0
  39. package/src/math/geometry/triangle.test.js +11 -0
  40. package/src/math/geometry/types.d.ts +23 -0
  41. package/src/math/linear-algebra/2d.js +28 -0
  42. package/src/math/linear-algebra/2d.test.js +17 -0
  43. package/src/math/linear-algebra/quaternion.js +22 -0
  44. package/src/math/linear-algebra/quaternion.test.js +25 -0
  45. package/src/math/linear-algebra/quaternions.js +20 -0
  46. package/src/math/linear-algebra/quaternions.test.js +29 -0
  47. package/src/math/linear-algebra/types.d.ts +4 -0
  48. package/src/math/linear-algebra/vector.js +337 -0
  49. package/src/math/linear-algebra/vector.test.js +265 -0
  50. package/src/math/linear-algebra/vectors.js +122 -0
  51. package/src/math/linear-algebra/vectors.test.js +65 -0
  52. package/src/math/linear-interpolation.js +9 -0
  53. package/src/math/numbers.js +90 -0
  54. package/src/math/numbers.test.js +137 -0
  55. package/src/math/rng.js +44 -0
  56. package/src/math/rng.test.js +39 -0
  57. package/src/math/statistics.js +43 -0
  58. package/src/math/statistics.test.js +47 -0
  59. package/src/math/trigonometry.js +89 -0
  60. package/src/math/trigonometry.test.js +52 -0
  61. package/src/physics/acceleration.js +56 -0
  62. package/src/physics/friction.js +24 -0
  63. package/src/physics/friction.test.js +42 -0
  64. package/src/physics/gravity.js +69 -0
  65. package/src/physics/gravity.test.js +77 -0
  66. package/src/physics/jump.js +31 -0
  67. package/src/physics/velocity.js +32 -0
@@ -0,0 +1,22 @@
1
+ /**
2
+ * @typedef {import('../../../types').Vector3} Vector3
3
+ * @typedef {import('../../../types').Quaternion} Quaternion
4
+ */
5
+
6
+ import { cos, sin } from "../trigonometry.js"
7
+
8
+ const DEFAULT_ANGLE = 0
9
+ const HALF = 2 // Constant representing the divisor for halving an angle.
10
+ // eslint-disable-next-line no-magic-numbers
11
+ const Y_AXIS = [0, 1, 0] // Default axis of rotation (Y-axis).
12
+
13
+ /**
14
+ * Computes a quaternion representing a rotation around a given axis.
15
+ *
16
+ * @param {number} [angle=0] - The angle of rotation in radians. Defaults to 0.
17
+ * @param {Vector3} [axis=Y_AXIS] - The axis of rotation as a 3D vector Defaults to the Y-axis.
18
+ * @returns {Quaternion} The quaternion as an array [w, x, y, z].
19
+ */
20
+ export function quaternion(angle = DEFAULT_ANGLE, axis = Y_AXIS) {
21
+ return [cos(angle / HALF), ...axis.map((coord) => coord * sin(angle / HALF))]
22
+ }
@@ -0,0 +1,25 @@
1
+ import { expect, test } from "vitest"
2
+
3
+ import { cos, pi, sin } from "../trigonometry.js"
4
+ import { quaternion } from "./quaternion.js"
5
+
6
+ test("it should return the quaternion for no rotation", () => {
7
+ const angle = 0
8
+ const expectedResult = [1, 0, 0, 0]
9
+
10
+ expect(quaternion(angle)).toStrictEqual(expectedResult)
11
+ })
12
+
13
+ test("it should return the quaternion for a rotation of pi/2", () => {
14
+ const angle = pi() / 2
15
+ const expectedResult = [cos(pi() / 4), 0, sin(pi() / 4), 0]
16
+
17
+ expect(quaternion(angle)).toStrictEqual(expectedResult)
18
+ })
19
+
20
+ test("it should return the quaternion for a rotation of negative pi/2", () => {
21
+ const angle = -pi() / 2
22
+ const expectedResult = [cos(-pi() / 4), -0, sin(-pi() / 4), -0]
23
+
24
+ expect(quaternion(angle)).toStrictEqual(expectedResult)
25
+ })
@@ -0,0 +1,20 @@
1
+ /**
2
+ * @typedef {import('./types').Quaternion} Quaternion
3
+ */
4
+
5
+ import { times } from "./vector.js"
6
+ import { cross, dot, sum } from "./vectors.js"
7
+
8
+ /**
9
+ * Combines two quaternions using the Hamilton product.
10
+ *
11
+ * @param {Quaternion} q1 - The first quaternion.
12
+ * @param {Quaternion} q2 - The second quaternion.
13
+ * @returns {Quaternion} - The resulting quaternion after combining q1 and q2.
14
+ */
15
+ export function combine(q1, q2) {
16
+ const [s, ...v] = q1
17
+ const [t, ...w] = q2
18
+
19
+ return [s * t - dot(v, w), ...sum(times(w, s), times(v, t), cross(v, w))]
20
+ }
@@ -0,0 +1,29 @@
1
+ import { expect, test } from "vitest"
2
+
3
+ import { cos, pi, sin } from "../trigonometry.js"
4
+ import { quaternion } from "./quaternion.js"
5
+ import { combine } from "./quaternions.js"
6
+
7
+ test("it should combine two zero-angle quaternions by giving the same 0-angle quaternion", () => {
8
+ const q1 = quaternion(0)
9
+ const q2 = quaternion(0)
10
+ const expectedResult = [1, 0, 0, 0]
11
+
12
+ expect(combine(q1, q2)).toStrictEqual(expectedResult)
13
+ })
14
+
15
+ test("it should combine a zero-angle quaternion with a nonzero-angle quaternion", () => {
16
+ const q1 = quaternion(0)
17
+ const q2 = quaternion(pi() / 2)
18
+ const expectedResult = [cos(pi() / 4), 0, sin(pi() / 4), 0]
19
+
20
+ expect(combine(q1, q2)).toStrictEqual(expectedResult)
21
+ })
22
+
23
+ test("it should combine two nonzero-angle quaternions", () => {
24
+ const q1 = quaternion(pi() / 2)
25
+ const q2 = quaternion(pi() / 2)
26
+ const expectedResult = [2.220446049250313e-16, 0, 1, 0] // close to [0, 0, 1, 0]
27
+
28
+ expect(combine(q1, q2)).toStrictEqual(expectedResult)
29
+ })
@@ -0,0 +1,4 @@
1
+ export type Vector2 = [number, number]
2
+ export type Vector3 = [number, number, number]
3
+ export type Quaternion = [number, number, number, number]
4
+ export type Vector7 = [number, number, number, number, number, number, number]
@@ -0,0 +1,337 @@
1
+ /**
2
+ * @typedef {import('./types').Vector2} Vector2
3
+ * @typedef {import('./types').Vector3} Vector3
4
+ */
5
+
6
+ import { hypothenuse } from "../geometry/triangle.js"
7
+ import {
8
+ abs as nAbs,
9
+ clamp as nClamp,
10
+ mod as nMod,
11
+ snap as nSnap,
12
+ } from "../numbers.js"
13
+ import { atan2, cos, sin } from "../trigonometry.js"
14
+ import { from2D, to2D } from "./2d.js"
15
+ import { quaternion } from "./quaternion.js"
16
+ import { cross, sum } from "./vectors.js"
17
+
18
+ const ZERO_VECTOR = [0, 0, 0] // eslint-disable-line no-magic-numbers
19
+ const UNIT_VECTOR = [1, 0, 0] // eslint-disable-line no-magic-numbers
20
+
21
+ const X = 0
22
+ const Y = 1
23
+ const Z = 2
24
+ const LAST_COORDINATE = 1
25
+ const TWO_COORDINATES = 2
26
+ const NO_Y = 0
27
+ const DEFAULT_PRECISION = 1
28
+ const DEFAULT_DECIMALS = 0
29
+
30
+ /**
31
+ * Returns the absolute value of each component in the vector.
32
+ * @param {Vector3} vector - The input vector.
33
+ * @returns {Vector3} The vector with absolute values.
34
+ */
35
+ export function abs(vector) {
36
+ return vector.map(nAbs)
37
+ }
38
+
39
+ /**
40
+ * Calculates the angle of the vector in radians.
41
+ * @param {Vector3} vector - The input vector.
42
+ * @returns {number} The angle in radians.
43
+ */
44
+ export function angle(vector) {
45
+ return atan2(vector[vector.length - LAST_COORDINATE], vector[X])
46
+ }
47
+
48
+ /**
49
+ * Clamps the magnitude of the vector between the given min and max values.
50
+ * @param {Vector3} vector - The input vector.
51
+ * @param {number} min - The minimum magnitude.
52
+ * @param {number} max - The maximum magnitude.
53
+ * @returns {Vector3} The clamped vector.
54
+ */
55
+ export function clamp(vector, min, max) {
56
+ const length = magnitude(vector)
57
+
58
+ if (typeof min === "number" && length < min) {
59
+ return setMagnitude(vector, min)
60
+ }
61
+
62
+ if (typeof max === "number" && length > max) {
63
+ return setMagnitude(vector, max)
64
+ }
65
+
66
+ if (typeof min !== "number" && typeof max !== "number") {
67
+ return vector.map((coordinate, index) =>
68
+ nClamp(coordinate, min[index], max[index]),
69
+ )
70
+ }
71
+
72
+ return vector
73
+ }
74
+
75
+ /**
76
+ * Returns the conjugate of the vector.
77
+ * @param {Vector3} vector - The input vector.
78
+ * @returns {Vector3} The conjugated vector.
79
+ */
80
+ export function conjugate(vector) {
81
+ return vector.map((coordinate, index) => (index ? -coordinate : coordinate))
82
+ }
83
+
84
+ /**
85
+ * Creates a vector with the given magnitude and angle.
86
+ * @param {number} magnitude - The magnitude of the vector.
87
+ * @param {number} angle - The angle of the vector in radians.
88
+ * @returns {Vector3} The created vector.
89
+ */
90
+ export function createVector(magnitude, angle) {
91
+ return multiply(fromAngle(angle), magnitude)
92
+ }
93
+
94
+ /**
95
+ * Divides each component of the vector by the given scalar.
96
+ * @param {Vector3} vector - The input vector.
97
+ * @param {number} scalar - The scalar value.
98
+ * @returns {Vector3} The resulting vector.
99
+ */
100
+ export function divide(vector, scalar) {
101
+ return vector.map((coordinate) => coordinate / scalar)
102
+ }
103
+
104
+ /**
105
+ * Creates a unit vector from the given angle.
106
+ * @param {number} angle - The angle in radians.
107
+ * @returns {Vector3} The unit vector.
108
+ */
109
+ export function fromAngle(angle) {
110
+ return rotate(UNIT_VECTOR, angle)
111
+ }
112
+
113
+ export const length = magnitude
114
+
115
+ /**
116
+ * Calculates the magnitude of the vector.
117
+ * @param {Vector3} vector - The input vector.
118
+ * @returns {number} The magnitude of the vector.
119
+ */
120
+ export function magnitude(vector) {
121
+ return hypothenuse(...vector)
122
+ }
123
+
124
+ /**
125
+ * Calculates the modulus of each component in the vector with the given divisor.
126
+ * @param {Vector3} vector - The input vector.
127
+ * @param {number} divisor - The divisor value.
128
+ * @returns {Vector3} The resulting vector.
129
+ */
130
+ export function mod(vector, divisor) {
131
+ return vector.map((coordinate) => nMod(coordinate, divisor))
132
+ }
133
+
134
+ /**
135
+ * Multiplies each component of the vector by the given scalar.
136
+ * @param {Vector3} vector - The input vector.
137
+ * @param {number} scalar - The scalar value.
138
+ * @returns {Vector3} The resulting vector.
139
+ */
140
+ export function multiply(vector, scalar) {
141
+ return vector.map((coordinate) => coordinate * scalar)
142
+ }
143
+
144
+ /**
145
+ * Normalizes the vector to have a magnitude of 1.
146
+ * @param {Vector3} vector - The input vector.
147
+ * @returns {Vector3} The normalized vector.
148
+ */
149
+ export function normalize(vector) {
150
+ const length = magnitude(vector)
151
+ return vector.map((coordinate) => coordinate / length)
152
+ }
153
+
154
+ export const remainder = mod
155
+
156
+ /**
157
+ * Rotates the vector by the given angle.
158
+ * @param {Vector3} vector - The input vector.
159
+ * @param {number} angle - The angle in radians.
160
+ * @returns {Vector3} The rotated vector.
161
+ */
162
+ export function rotate(vector, angle) {
163
+ const is2D = vector.length === TWO_COORDINATES
164
+
165
+ let v = is2D ? from2D(vector) : vector
166
+
167
+ let result = rotateWithQuaternion(v, angle)
168
+
169
+ return is2D ? to2D(result) : result
170
+ }
171
+
172
+ /**
173
+ * Scales the vector by the given scalar. Alias of `multiply`.
174
+ * @param {Vector3} vector - The input vector.
175
+ * @param {number} scalar - The scalar value.
176
+ * @returns {Vector3} The resulting vector.
177
+ */
178
+ export const scale = multiply
179
+
180
+ /**
181
+ * Sets the angle of the vector while maintaining its magnitude.
182
+ * @param {Vector3} vector - The input vector.
183
+ * @param {number} angle - The new angle in radians.
184
+ * @returns {Vector3} The vector with the updated angle.
185
+ */
186
+ export function setAngle(vector, angle) {
187
+ const length = magnitude(vector)
188
+ const [x, z] = toCartesian([length, angle])
189
+ return [x, NO_Y, z]
190
+ }
191
+
192
+ /**
193
+ * Sets the length of the vector while maintaining its direction. Alias of `setMagnitude`.
194
+ */
195
+ export const setLength = setMagnitude
196
+
197
+ /**
198
+ * Sets the magnitude of the vector while maintaining its direction.
199
+ * @param {Vector3} vector - The input vector.
200
+ * @param {number} length - The new magnitude.
201
+ * @returns {Vector3} The vector with the updated magnitude.
202
+ */
203
+ export function setMagnitude(vector, length) {
204
+ const normalized = normalize(vector)
205
+ return multiply(normalized, length)
206
+ }
207
+
208
+ /**
209
+ * Shifts the components of the vector by the given index.
210
+ * @param {Vector3} vector - The input vector.
211
+ * @param {number} index - The index to shift by.
212
+ * @returns {Vector3} The shifted vector.
213
+ */
214
+ export function shift(vector, index) {
215
+ return [...vector.slice(index), ...vector.slice(X, index)]
216
+ }
217
+
218
+ /**
219
+ * Snaps each component of the vector to the nearest multiple of the given precision.
220
+ * @param {Vector3} vector - The input vector.
221
+ * @param {number} [precision=DEFAULT_PRECISION] - The precision value.
222
+ * @returns {Vector3} The snapped vector.
223
+ */
224
+ export function snap(vector, precision = DEFAULT_PRECISION) {
225
+ return vector.map((coordinate) => nSnap(coordinate, -precision, precision))
226
+ }
227
+
228
+ export const times = multiply
229
+
230
+ /**
231
+ * Converts polar coordinates to Cartesian coordinates.
232
+ * @param {Vector2} vector - The polar coordinates [magnitude, angle].
233
+ * @returns {Vector2} The Cartesian coordinates [x, y].
234
+ */
235
+ export function toCartesian([magnitude, angle]) {
236
+ return [magnitude * cos(angle), magnitude * sin(angle)]
237
+ }
238
+
239
+ /**
240
+ * Converts a vector to cylindrical coordinates.
241
+ * @param {Vector2} vector - The input vector.
242
+ * @returns {Vector2} The cylindrical coordinates [radius, theta, z].
243
+ */
244
+ export function toCylindrical(vector) {
245
+ const radius = magnitude(vector)
246
+ const theta = angle(vector)
247
+ return [radius * cos(theta), radius * sin(theta), vector[Z]]
248
+ }
249
+
250
+ /**
251
+ * Converts a vector to polar coordinates.
252
+ * @param {Vector2} vector - The input vector.
253
+ * @returns {Vector2} The polar coordinates [magnitude, angle].
254
+ */
255
+ export function toPolar(vector) {
256
+ return [magnitude(vector), angle(vector)]
257
+ }
258
+
259
+ /**
260
+ * Converts a vector to a string representation.
261
+ * @param {Vector3} vector - The input vector.
262
+ * @param {number} [decimals=DEFAULT_DECIMALS] - The number of decimal places.
263
+ * @returns {string} The string representation of the vector.
264
+ */
265
+ export function toString(vector, decimals = DEFAULT_DECIMALS) {
266
+ return `[${vector
267
+ .map((coordinate) => coordinate.toFixed(decimals))
268
+ .join(", ")}]`
269
+ }
270
+
271
+ // TODO: add toSpherical(vector),
272
+
273
+ /**
274
+ * Converts a 3D cartesian vector to spherical coordinates [radius, inclination, azimuth].
275
+ * - radius (r): distance from the origin.
276
+ * - inclination (θ): angle from the Y-axis (0 to PI).
277
+ * - azimuth (φ): angle from the X-axis in the XZ-plane (-PI to PI).
278
+ *
279
+ * @param {Vector3} vector The cartesian vector [x, y, z].
280
+ * @returns {Vector3} The spherical coordinates [r, θ, φ].
281
+ */
282
+ export function toSpherical(vector) {
283
+ const r = magnitude(vector)
284
+
285
+ if (!r) {
286
+ return zero()
287
+ }
288
+
289
+ // In a Y-up system, inclination is the angle with the Y axis.
290
+ const inclination = Math.acos(vector[Y] / r)
291
+ // Azimuth is the angle in the XZ plane, which `angle()` calculates.
292
+ const azimuth = angle(vector)
293
+
294
+ return [r, inclination, azimuth]
295
+ }
296
+
297
+ /**
298
+ * Creates a unit vector with the given angle.
299
+ * @param {number} angle - The angle in radians.
300
+ * @returns {Vector3} The unit vector.
301
+ */
302
+ export function unit(angle) {
303
+ if (!angle) {
304
+ return [...UNIT_VECTOR]
305
+ }
306
+
307
+ return setAngle(UNIT_VECTOR, angle)
308
+ }
309
+
310
+ /**
311
+ * Creates a zero vector.
312
+ * @returns {Vector3} The zero vector.
313
+ */
314
+ export function zero() {
315
+ return [...ZERO_VECTOR]
316
+ }
317
+
318
+ /**
319
+ * Rotates the vector using a quaternion.
320
+ * @param {Vector3} vector - The input vector.
321
+ * @param {number} angle - The angle in radians.
322
+ * @returns {Vector3} The rotated vector.
323
+ */
324
+ function rotateWithQuaternion(vector, angle) {
325
+ if (!angle) {
326
+ return vector
327
+ }
328
+
329
+ // @see https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation#Performance_comparisons
330
+ const [w, ...r] = quaternion(angle)
331
+ const result = sum(
332
+ vector,
333
+ cross(multiply(r, 2), sum(cross(r, vector), multiply(vector, w))), // eslint-disable-line no-magic-numbers
334
+ )
335
+
336
+ return conjugate(result) // HACK: not really sure why I should invert the result, it just works this way
337
+ }
@@ -0,0 +1,265 @@
1
+ import { expect, test } from "vitest"
2
+
3
+ import { sqrt } from "../numbers.js"
4
+ import { cos, pi, sin } from "../trigonometry.js"
5
+ import {
6
+ abs,
7
+ angle,
8
+ clamp,
9
+ conjugate,
10
+ createVector,
11
+ divide,
12
+ fromAngle,
13
+ magnitude,
14
+ mod,
15
+ multiply,
16
+ normalize,
17
+ rotate,
18
+ setAngle,
19
+ setMagnitude,
20
+ shift,
21
+ snap,
22
+ toCartesian,
23
+ toCylindrical,
24
+ toPolar,
25
+ toSpherical,
26
+ toString,
27
+ unit,
28
+ } from "./vector.js"
29
+
30
+ test("it should compute the absolute value of a vector's coordinates", () => {
31
+ const vector = [-2, 0, 3]
32
+ const expectedResult = [2, 0, 3]
33
+
34
+ expect(abs(vector)).toStrictEqual(expectedResult)
35
+ })
36
+
37
+ test("it should compute the angle of a 2D vector", () => {
38
+ const vector = [1, 1]
39
+ const expectedResult = pi() / 4
40
+
41
+ expect(angle(vector)).toBe(expectedResult)
42
+ })
43
+
44
+ test("it should compute the angle of a 3D vector", () => {
45
+ const vector = [1, 0, 1]
46
+ const expectedResult = pi() / 4
47
+
48
+ expect(angle(vector)).toBe(expectedResult)
49
+ })
50
+
51
+ test("it should clamp the magnitude of a vector to a certain length if too long", () => {
52
+ const vector = [6, 8]
53
+ const min = 0
54
+ const max = 5
55
+ const expectedResult = [3, 4]
56
+
57
+ expect(clamp(vector, min, max)).toStrictEqual(expectedResult)
58
+ })
59
+
60
+ test("it should not clamp the magnitude of a vector to a certain length if not too long", () => {
61
+ const vector = [3, 4]
62
+ const min = 0
63
+ const max = 6
64
+ const expectedResult = [3, 4]
65
+
66
+ expect(clamp(vector, min, max)).toStrictEqual(expectedResult)
67
+ })
68
+
69
+ test("it should clamp the magnitude of a vector to the values of other vector bounds", () => {
70
+ const vector = [6, 8]
71
+ const lowerBound = [0, 0]
72
+ const upperBound = [3, 4]
73
+ const expectedResult = [3, 4]
74
+
75
+ expect(clamp(vector, lowerBound, upperBound)).toStrictEqual(expectedResult)
76
+ })
77
+
78
+ test("it should conjugate a vector", () => {
79
+ const vector = [1, 2, 3]
80
+ const expectedResult = [1, -2, -3]
81
+
82
+ expect(conjugate(vector)).toStrictEqual(expectedResult)
83
+ })
84
+
85
+ test("it should create a 3D vector given its magnitude and angle", () => {
86
+ const magnitude = sqrt(2)
87
+ const angle = pi() / 4
88
+ const expectedResult = [1, -0, 1.0000000000000002] // close to [1, 0, 1]
89
+
90
+ expect(createVector(magnitude, angle)).toStrictEqual(expectedResult)
91
+ })
92
+
93
+ test("it should divide a vector by a scalar", () => {
94
+ const vector = [4, 8, 12]
95
+ const scalar = 4
96
+ const expectedResult = [1, 2, 3]
97
+
98
+ expect(divide(vector, scalar)).toStrictEqual(expectedResult)
99
+ })
100
+
101
+ test("it should create a 3D unit vector from an angle", () => {
102
+ const angle = pi() / 4
103
+ const expectedResult = [0.7071067811865475, -0, 0.7071067811865476] // close to [cos(angle), 0, sin(angle)]
104
+
105
+ expect(fromAngle(angle)).toStrictEqual(expectedResult)
106
+ })
107
+
108
+ test("it should compute the magnitude of a vector (aka length)", () => {
109
+ const vector = [3, 4]
110
+ const expectedResult = 5
111
+
112
+ expect(magnitude(vector)).toBe(expectedResult)
113
+ })
114
+
115
+ test("it should apply the mod operator (aka remainder) on a vector", () => {
116
+ const vector = [10, 12, -18]
117
+ const divisor = 12
118
+ const expectedResult = [10, 0, 6]
119
+
120
+ expect(mod(vector, divisor)).toStrictEqual(expectedResult)
121
+ })
122
+
123
+ test("it should multiply a vector with a scalar (aka times)", () => {
124
+ const vector = [1, 2, 3]
125
+ const scalar = 4
126
+ const expectedResult = [4, 8, 12]
127
+
128
+ expect(multiply(vector, scalar)).toStrictEqual(expectedResult)
129
+ })
130
+
131
+ test("it should normalize a vector so it has unit length", () => {
132
+ const vector = [3, 4]
133
+ const expectedResult = [0.6, 0.8]
134
+
135
+ expect(normalize(vector)).toStrictEqual(expectedResult)
136
+ })
137
+
138
+ test("it should normalize a negative vector", () => {
139
+ const vector = [-3, -4]
140
+ const expectedResult = [-0.6, -0.8]
141
+
142
+ expect(normalize(vector)).toStrictEqual(expectedResult)
143
+ })
144
+
145
+ test("it should rotate a 2D vector by a certain angle", () => {
146
+ const vector = [1, 0]
147
+ const angle = pi() / 4
148
+ const expectedResult = [0.7071067811865475, 0.7071067811865476] // close to [cos(pi/4), sin(pi/4)]
149
+
150
+ expect(rotate(vector, angle)).toStrictEqual(expectedResult)
151
+ })
152
+
153
+ test("it should not rotate a vector when the angle is zero", () => {
154
+ const vector = [1, 0, 0]
155
+ const angle = 0
156
+ const expectedResult = [1, 0, 0]
157
+
158
+ expect(rotate(vector, angle)).toStrictEqual(expectedResult)
159
+ })
160
+
161
+ test("it should rotate a vector by a certain angle", () => {
162
+ const vector = [1, 0, 0]
163
+ const angle = pi() / 4
164
+ const expectedResult = [0.7071067811865475, -0, 0.7071067811865476] // close to [cos(pi/4), 0, sin(pi/4)]
165
+
166
+ expect(rotate(vector, angle)).toStrictEqual(expectedResult)
167
+ })
168
+
169
+ test("it should rotate a vector that faces left by a certain angle", () => {
170
+ const vector = [-1, 0, 0]
171
+ const angle = pi() / 4
172
+ const expectedResult = [-0.7071067811865475, -0, -0.7071067811865476] // close to [cos(-3/4pi), 0, sin(-3/4pi)]
173
+
174
+ expect(rotate(vector, angle)).toStrictEqual(expectedResult)
175
+ })
176
+
177
+ test("it should change the angle of a vector", () => {
178
+ const vector = [cos(pi() / 4), 0, sin(pi() / 4)]
179
+ const expectedResult = [6.123233995736766e-17, 0, 1] // close to [0, 0, 1]
180
+
181
+ expect(setAngle(vector, 1.5707963267948966)).toStrictEqual(expectedResult)
182
+ })
183
+
184
+ test("it should change magnitude of a vector (aka setLength)", () => {
185
+ const vector = [3, 4]
186
+ const magnitude = 10
187
+ const expectedResult = [6, 8]
188
+
189
+ expect(setMagnitude(vector, magnitude)).toStrictEqual(expectedResult)
190
+ })
191
+
192
+ test("it should change magnitude of a negative vector", () => {
193
+ const vector = [-3, -4]
194
+ const magnitude = 10
195
+ const expectedResult = [-6, -8]
196
+
197
+ expect(setMagnitude(vector, magnitude)).toStrictEqual(expectedResult)
198
+ })
199
+
200
+ test("it should shift a vector at a certain index", () => {
201
+ const vector = [1, 2, 3, 4, 5]
202
+ const index = 2
203
+ const expectedResult = [3, 4, 5, 1, 2]
204
+
205
+ expect(shift(vector, index)).toStrictEqual(expectedResult)
206
+ })
207
+
208
+ test("it should snap a floating vector to a certain precision", () => {
209
+ const vector = [1, 1.5, 1.7]
210
+ const precision = 1
211
+ const expectedResult = [1, 1, 2]
212
+
213
+ expect(snap(vector, precision)).toStrictEqual(expectedResult)
214
+ })
215
+
216
+ test("it should convert a 2D polar vector to cartesian coordinates", () => {
217
+ const vector = [sqrt(2), pi() / 4]
218
+ const expectedResult = [1.0000000000000002, 1]
219
+
220
+ expect(toCartesian(vector)).toStrictEqual(expectedResult)
221
+ })
222
+
223
+ test("it should convert a 3D cartesian vector to cylindrical coordinates", () => {
224
+ const vector = [1, 1, 1]
225
+ const expectedResult = [1.2247448713915892, 1.224744871391589, 1]
226
+
227
+ expect(toCylindrical(vector)).toStrictEqual(expectedResult)
228
+ })
229
+
230
+ test("it should convert a 2D cartesian vector to polar coordinates", () => {
231
+ const vector = [1, 1]
232
+ const expectedResult = [sqrt(2), pi() / 4]
233
+
234
+ expect(toPolar(vector)).toStrictEqual(expectedResult)
235
+ })
236
+
237
+ test("it should convert a 3D cartesian vector to spherical coordinates", () => {
238
+ const vector = [1, 1, 1]
239
+ const expectedResult = [sqrt(3), Math.acos(1 / sqrt(3)), pi() / 4]
240
+
241
+ expect(toSpherical(vector)).toStrictEqual(expectedResult)
242
+ })
243
+
244
+ test("it should create a string representation of an integer vector", () => {
245
+ const vector = [3, 4]
246
+ const expectedResult = "[3, 4]"
247
+
248
+ expect(toString(vector)).toBe(expectedResult)
249
+ })
250
+
251
+ test("it should create a string representation of a float vector", () => {
252
+ const vector = [pi() / 4, pi() / 4]
253
+ const decimals = 2
254
+ const expectedResult = "[0.79, 0.79]"
255
+
256
+ expect(toString(vector, decimals)).toBe(expectedResult)
257
+ })
258
+
259
+ test("it should create a unit vector oriented on the X-axis", () => {
260
+ expect(unit()).toStrictEqual([1, 0, 0])
261
+ })
262
+
263
+ test("it should create a unit vector oriented on the Z-axis", () => {
264
+ expect(unit(pi() / 2)).toStrictEqual([6.123233995736766e-17, 0, 1]) // close to [0, 0, 1]
265
+ })