@inglorious/utils 2.2.0 → 3.0.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 (44) 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 -4
  14. package/src/math/{geometry/line.test.js → line.test.js} +11 -10
  15. package/src/math/{geometry/point.js → point.js} +5 -5
  16. package/src/math/{geometry/point.test.js → point.test.js} +23 -22
  17. package/src/math/{linear-algebra/quaternion.js → quaternion.js} +3 -2
  18. package/src/math/{linear-algebra/quaternion.test.js → quaternion.test.js} +1 -1
  19. package/src/math/{linear-algebra/quaternions.test.js → quaternions.test.js} +1 -1
  20. package/src/math/{geometry/rectangle.js → rectangle.js} +3 -3
  21. package/src/math/{geometry/rectangle.test.js → rectangle.test.js} +13 -12
  22. package/src/math/{geometry/segment.js → segment.js} +5 -5
  23. package/src/math/{geometry/segment.test.js → segment.test.js} +48 -47
  24. package/src/math/{geometry/types.d.ts → types.d.ts} +10 -0
  25. package/src/math/{linear-algebra/vector.js → vector.js} +113 -65
  26. package/src/math/{linear-algebra/vector.test.js → vector.test.js} +80 -29
  27. package/src/math/{linear-algebra/vectors.test.js → vectors.test.js} +12 -30
  28. package/src/physics/acceleration.js +3 -3
  29. package/src/physics/friction.js +2 -2
  30. package/src/physics/friction.test.js +7 -6
  31. package/src/physics/gravity.js +2 -2
  32. package/src/physics/gravity.test.js +10 -9
  33. package/src/physics/index.js +7 -0
  34. package/src/physics/jump.js +1 -1
  35. package/src/physics/velocity.js +3 -3
  36. package/src/v.js +18 -0
  37. package/src/math/linear-algebra/2d.js +0 -28
  38. package/src/math/linear-algebra/2d.test.js +0 -17
  39. package/src/math/linear-algebra/types.d.ts +0 -4
  40. /package/src/math/{geometry/hitmask.js → hitmask.js} +0 -0
  41. /package/src/math/{linear-algebra/quaternions.js → quaternions.js} +0 -0
  42. /package/src/math/{geometry/triangle.js → triangle.js} +0 -0
  43. /package/src/math/{geometry/triangle.test.js → triangle.test.js} +0 -0
  44. /package/src/math/{linear-algebra/vectors.js → vectors.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,
@@ -9,7 +10,9 @@ import {
9
10
  conjugate,
10
11
  createVector,
11
12
  divide,
13
+ from2D,
12
14
  fromAngle,
15
+ isVector,
13
16
  magnitude,
14
17
  mod,
15
18
  multiply,
@@ -19,17 +22,19 @@ import {
19
22
  setMagnitude,
20
23
  shift,
21
24
  snap,
25
+ to2D,
22
26
  toCartesian,
23
27
  toCylindrical,
24
28
  toPolar,
25
29
  toSpherical,
26
30
  toString,
27
31
  unit,
32
+ zero,
28
33
  } from "./vector.js"
29
34
 
30
35
  test("it should compute the absolute value of a vector's coordinates", () => {
31
- const vector = [-2, 0, 3]
32
- const expectedResult = [2, 0, 3]
36
+ const vector = v(-2, 0, 3)
37
+ const expectedResult = v(2, 0, 3)
33
38
 
34
39
  expect(abs(vector)).toStrictEqual(expectedResult)
35
40
  })
@@ -42,7 +47,7 @@ test("it should compute the angle of a 2D vector", () => {
42
47
  })
43
48
 
44
49
  test("it should compute the angle of a 3D vector", () => {
45
- const vector = [1, 0, 1]
50
+ const vector = v(1, 0, 1)
46
51
  const expectedResult = pi() / 4
47
52
 
48
53
  expect(angle(vector)).toBe(expectedResult)
@@ -76,8 +81,8 @@ test("it should clamp the magnitude of a vector to the values of other vector bo
76
81
  })
77
82
 
78
83
  test("it should conjugate a vector", () => {
79
- const vector = [1, 2, 3]
80
- const expectedResult = [1, -2, -3]
84
+ const vector = v(1, 2, 3)
85
+ const expectedResult = v(1, -2, -3)
81
86
 
82
87
  expect(conjugate(vector)).toStrictEqual(expectedResult)
83
88
  })
@@ -85,22 +90,29 @@ test("it should conjugate a vector", () => {
85
90
  test("it should create a 3D vector given its magnitude and angle", () => {
86
91
  const magnitude = sqrt(2)
87
92
  const angle = pi() / 4
88
- const expectedResult = [1, -0, 1.0000000000000002] // close to [1, 0, 1]
93
+ const expectedResult = v(1, -0, 1.0000000000000002) // close to v(1, 0, 1)
89
94
 
90
95
  expect(createVector(magnitude, angle)).toStrictEqual(expectedResult)
91
96
  })
92
97
 
93
98
  test("it should divide a vector by a scalar", () => {
94
- const vector = [4, 8, 12]
99
+ const vector = v(4, 8, 12)
95
100
  const scalar = 4
96
- const expectedResult = [1, 2, 3]
101
+ const expectedResult = v(1, 2, 3)
97
102
 
98
103
  expect(divide(vector, scalar)).toStrictEqual(expectedResult)
99
104
  })
100
105
 
106
+ test("it should build a 3D vector from a 2D one", () => {
107
+ const vector = [3, 4]
108
+ const expectedResult = v(3, 0, 4)
109
+
110
+ expect(from2D(vector)).toStrictEqual(expectedResult)
111
+ })
112
+
101
113
  test("it should create a 3D unit vector from an angle", () => {
102
114
  const angle = pi() / 4
103
- const expectedResult = [0.7071067811865475, -0, 0.7071067811865476] // close to [cos(angle), 0, sin(angle)]
115
+ const expectedResult = v(0.7071067811865475, -0, 0.7071067811865476) // close to [cos(angle), 0, sin(angle)]
104
116
 
105
117
  expect(fromAngle(angle)).toStrictEqual(expectedResult)
106
118
  })
@@ -112,18 +124,32 @@ test("it should compute the magnitude of a vector (aka length)", () => {
112
124
  expect(magnitude(vector)).toBe(expectedResult)
113
125
  })
114
126
 
127
+ test("it should check if a value is a vector", () => {
128
+ expect(isVector(v(1, 2, 3))).toBe(true)
129
+ expect(isVector([])).toBe(true)
130
+ expect(isVector(v(1, 2, 3))).toBe(true)
131
+ })
132
+
133
+ test("it should check if a value is not a vector", () => {
134
+ expect(isVector([1, "a", 3])).toBe(false)
135
+ expect(isVector({ x: 1, y: 2 })).toBe(false)
136
+ expect(isVector(null)).toBe(false)
137
+ expect(isVector("vector")).toBe(false)
138
+ expect(isVector(123)).toBe(false)
139
+ })
140
+
115
141
  test("it should apply the mod operator (aka remainder) on a vector", () => {
116
- const vector = [10, 12, -18]
142
+ const vector = v(10, 12, -18)
117
143
  const divisor = 12
118
- const expectedResult = [10, 0, 6]
144
+ const expectedResult = v(10, 0, 6)
119
145
 
120
146
  expect(mod(vector, divisor)).toStrictEqual(expectedResult)
121
147
  })
122
148
 
123
149
  test("it should multiply a vector with a scalar (aka times)", () => {
124
- const vector = [1, 2, 3]
150
+ const vector = v(1, 2, 3)
125
151
  const scalar = 4
126
- const expectedResult = [4, 8, 12]
152
+ const expectedResult = v(4, 8, 12)
127
153
 
128
154
  expect(multiply(vector, scalar)).toStrictEqual(expectedResult)
129
155
  })
@@ -151,32 +177,32 @@ test("it should rotate a 2D vector by a certain angle", () => {
151
177
  })
152
178
 
153
179
  test("it should not rotate a vector when the angle is zero", () => {
154
- const vector = [1, 0, 0]
180
+ const vector = v(1, 0, 0)
155
181
  const angle = 0
156
- const expectedResult = [1, 0, 0]
182
+ const expectedResult = v(1, 0, 0)
157
183
 
158
184
  expect(rotate(vector, angle)).toStrictEqual(expectedResult)
159
185
  })
160
186
 
161
187
  test("it should rotate a vector by a certain angle", () => {
162
- const vector = [1, 0, 0]
188
+ const vector = v(1, 0, 0)
163
189
  const angle = pi() / 4
164
- const expectedResult = [0.7071067811865475, -0, 0.7071067811865476] // close to [cos(pi/4), 0, sin(pi/4)]
190
+ const expectedResult = v(0.7071067811865475, -0, 0.7071067811865476) // close to [cos(pi/4), 0, sin(pi/4)]
165
191
 
166
192
  expect(rotate(vector, angle)).toStrictEqual(expectedResult)
167
193
  })
168
194
 
169
195
  test("it should rotate a vector that faces left by a certain angle", () => {
170
- const vector = [-1, 0, 0]
196
+ const vector = v(-1, 0, 0)
171
197
  const angle = pi() / 4
172
- const expectedResult = [-0.7071067811865475, -0, -0.7071067811865476] // close to [cos(-3/4pi), 0, sin(-3/4pi)]
198
+ const expectedResult = v(-0.7071067811865475, -0, -0.7071067811865476) // close to [cos(-3/4pi), 0, sin(-3/4pi)]
173
199
 
174
200
  expect(rotate(vector, angle)).toStrictEqual(expectedResult)
175
201
  })
176
202
 
177
203
  test("it should change the angle of a vector", () => {
178
204
  const vector = [cos(pi() / 4), 0, sin(pi() / 4)]
179
- const expectedResult = [6.123233995736766e-17, 0, 1] // close to [0, 0, 1]
205
+ const expectedResult = [6.123233995736766e-17, 0, 1] // close to v(0, 0, 1)
180
206
 
181
207
  expect(setAngle(vector, 1.5707963267948966)).toStrictEqual(expectedResult)
182
208
  })
@@ -206,13 +232,20 @@ test("it should shift a vector at a certain index", () => {
206
232
  })
207
233
 
208
234
  test("it should snap a floating vector to a certain precision", () => {
209
- const vector = [1, 1.5, 1.7]
235
+ const vector = v(1, 1.5, 1.7)
210
236
  const precision = 1
211
- const expectedResult = [1, 1, 2]
237
+ const expectedResult = v(1, 1, 2)
212
238
 
213
239
  expect(snap(vector, precision)).toStrictEqual(expectedResult)
214
240
  })
215
241
 
242
+ test("it should build a 2D vector from a 3D one", () => {
243
+ const vector = v(3, 0, 4)
244
+ const expectedResult = [3, 4]
245
+
246
+ expect(to2D(vector)).toStrictEqual(expectedResult)
247
+ })
248
+
216
249
  test("it should convert a 2D polar vector to cartesian coordinates", () => {
217
250
  const vector = [sqrt(2), pi() / 4]
218
251
  const expectedResult = [1.0000000000000002, 1]
@@ -221,8 +254,8 @@ test("it should convert a 2D polar vector to cartesian coordinates", () => {
221
254
  })
222
255
 
223
256
  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]
257
+ const vector = v(1, 1, 1)
258
+ const expectedResult = v(1.2247448713915892, 1.224744871391589, 1)
226
259
 
227
260
  expect(toCylindrical(vector)).toStrictEqual(expectedResult)
228
261
  })
@@ -235,7 +268,7 @@ test("it should convert a 2D cartesian vector to polar coordinates", () => {
235
268
  })
236
269
 
237
270
  test("it should convert a 3D cartesian vector to spherical coordinates", () => {
238
- const vector = [1, 1, 1]
271
+ const vector = v(1, 1, 1)
239
272
  const expectedResult = [sqrt(3), Math.acos(1 / sqrt(3)), pi() / 4]
240
273
 
241
274
  expect(toSpherical(vector)).toStrictEqual(expectedResult)
@@ -257,9 +290,27 @@ test("it should create a string representation of a float vector", () => {
257
290
  })
258
291
 
259
292
  test("it should create a unit vector oriented on the X-axis", () => {
260
- expect(unit()).toStrictEqual([1, 0, 0])
293
+ expect(unit()).toStrictEqual(v(1, 0, 0))
261
294
  })
262
295
 
263
296
  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]
297
+ expect(unit(pi() / 2)).toStrictEqual([6.123233995736766e-17, 0, 1]) // close to v(0, 0, 1)
298
+ })
299
+
300
+ test("it should create a vector from a list of coordinates", () => {
301
+ expect(v(1, 2, 3)).toStrictEqual(v(1, 2, 3))
302
+ expect(v()).toStrictEqual([])
303
+ expect(v(0, -5.5)).toStrictEqual([0, -5.5])
304
+ })
305
+
306
+ test("it should create a vector with a __isVector__ property", () => {
307
+ const vector = v(1, 2, 3)
308
+ expect(vector).toStrictEqual(v(1, 2, 3))
309
+ expect(vector.__isVector__).toBe(true)
310
+ })
311
+
312
+ test("it should create a 3D zero vector", () => {
313
+ const expectedResult = v(0, 0, 0)
314
+
315
+ expect(zero()).toStrictEqual(expectedResult)
265
316
  })
@@ -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.
@@ -1,8 +1,8 @@
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.
@@ -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.
@@ -1,5 +1,6 @@
1
1
  import { expect, test } from "vitest"
2
2
 
3
+ import { v } from "../v.js"
3
4
  import { applyGravity } from "./gravity.js"
4
5
 
5
6
  test("it should apply gravity based on input parameters", () => {
@@ -8,13 +9,13 @@ test("it should apply gravity based on input parameters", () => {
8
9
  maxLeap: 10,
9
10
  maxSpeed: 10,
10
11
  vy: 10,
11
- position: [0, 10, 0],
12
+ position: v(0, 10, 0),
12
13
  }
13
14
 
14
15
  expect(applyGravity(params)).toStrictEqual({
15
16
  ay: -20,
16
17
  vy: -10,
17
- position: [0, -10, 0],
18
+ position: v(0, -10, 0),
18
19
  })
19
20
  })
20
21
 
@@ -23,13 +24,13 @@ test("it should compute no gravity when max jump height is not set", () => {
23
24
  maxLeap: 10,
24
25
  maxSpeed: 10,
25
26
  vy: 10,
26
- position: [0, 10, 0],
27
+ position: v(0, 10, 0),
27
28
  }
28
29
 
29
30
  expect(applyGravity(params)).toStrictEqual({
30
31
  ay: -0,
31
32
  vy: 10,
32
- position: [0, 20, 0],
33
+ position: v(0, 20, 0),
33
34
  })
34
35
  })
35
36
 
@@ -38,13 +39,13 @@ test("it should compute no gravity when max movement speed is not set", () => {
38
39
  maxJump: 10,
39
40
  maxLeap: 10,
40
41
  vy: 10,
41
- position: [0, 10, 0],
42
+ position: v(0, 10, 0),
42
43
  }
43
44
 
44
45
  expect(applyGravity(params)).toStrictEqual({
45
46
  ay: -0,
46
47
  vy: 10,
47
- position: [0, 20, 0],
48
+ position: v(0, 20, 0),
48
49
  })
49
50
  })
50
51
 
@@ -54,14 +55,14 @@ test("it should apply no gravity when no time has passed", () => {
54
55
  maxLeap: 10,
55
56
  maxSpeed: 10,
56
57
  vy: 10,
57
- position: [0, 10, 0],
58
+ position: v(0, 10, 0),
58
59
  }
59
60
  const dt = 0
60
61
 
61
62
  expect(applyGravity(params, dt)).toStrictEqual({
62
63
  ay: -20,
63
64
  vy: 10,
64
- position: [0, 10, 0],
65
+ position: v(0, 10, 0),
65
66
  })
66
67
  })
67
68
 
@@ -70,7 +71,7 @@ test("it should throw an error when max leap distance is not set", () => {
70
71
  maxJump: 10,
71
72
  maxSpeed: 10,
72
73
  vy: 10,
73
- position: [0, 10, 0],
74
+ position: v(0, 10, 0),
74
75
  }
75
76
 
76
77
  expect(() => applyGravity(params)).toThrow()
@@ -0,0 +1,7 @@
1
+ import * as acceleration from "./acceleration.js"
2
+ import * as friction from "./friction.js"
3
+ import * as gravity from "./gravity.js"
4
+ import * as jump from "./jump.js"
5
+ import * as velocity from "./velocity.js"
6
+
7
+ export { acceleration, friction, gravity, jump, velocity }
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @typedef {import('../math/linear-algebra/types').Vector3} Vector3
2
+ * @typedef {import('../math/types').Vector3} Vector3
3
3
  */
4
4
 
5
5
  /* eslint-disable no-magic-numbers */
@@ -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
 
8
8
  const DEFAULT_DT = 1 // Default time delta for the applyVelocity function.
9
9
  const DEFAULT_MAX_SPEED = 0 // Default maximum speed for velocity clamping.
package/src/v.js ADDED
@@ -0,0 +1,18 @@
1
+ /**
2
+ * A utility function to create a vector from a list of coordinates.
3
+ * This is a shorthand for `[x, y, z, ...]`.
4
+ * It also tags the array with a non-enumerable `__isVector__` property for
5
+ * efficient type checking.
6
+ *
7
+ * @param {...number} coords - The coordinates of the vector.
8
+ * @returns {Vector} The created vector.
9
+ */
10
+ export function v(...coords) {
11
+ Object.defineProperty(coords, "__isVector__", {
12
+ value: true,
13
+ enumerable: false,
14
+ configurable: false,
15
+ writable: false,
16
+ })
17
+ return coords
18
+ }
@@ -1,28 +0,0 @@
1
- /**
2
- * @typedef {import('../../../types').Vector2} Vector2
3
- * @typedef {import('../../../types').Vector3} Vector3
4
- */
5
-
6
- const NO_Y = 0 // Default Y-coordinate value for 3D vectors derived from 2D vectors.
7
-
8
- /**
9
- * Converts a 2D vector [x, z] into a 3D vector [x, 0, z].
10
- *
11
- * @param {Vector2} vector - A 2D vector represented as [x, z].
12
- * @returns {Vector3} A 3D vector represented as [x, 0, z].
13
- */
14
- export function from2D(vector) {
15
- const [x, z] = vector
16
- return [x, NO_Y, z]
17
- }
18
-
19
- /**
20
- * Converts a 3D vector [x, y, z] into a 2D vector [x, z].
21
- *
22
- * @param {Vector3} vector - A 3D vector represented as [x, y, z].
23
- * @returns {Vector2} A 2D vector represented as [x, z].
24
- */
25
- export function to2D(vector) {
26
- const [x, , z] = vector
27
- return [x, z]
28
- }
@@ -1,17 +0,0 @@
1
- import { expect, test } from "vitest"
2
-
3
- import { from2D, to2D } from "./2d.js"
4
-
5
- test("build a 3D vector from a 2D one", () => {
6
- const vector = [3, 4]
7
- const expectedResult = [3, 0, 4]
8
-
9
- expect(from2D(vector)).toStrictEqual(expectedResult)
10
- })
11
-
12
- test("build a 2D vector from a 3D one", () => {
13
- const vector = [3, 0, 4]
14
- const expectedResult = [3, 4]
15
-
16
- expect(to2D(vector)).toStrictEqual(expectedResult)
17
- })
@@ -1,4 +0,0 @@
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]
File without changes
File without changes