@inglorious/utils 3.8.0 → 4.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 (67) hide show
  1. package/README.md +67 -3
  2. package/package.json +88 -12
  3. package/src/algorithms/decision-tree.js +3 -3
  4. package/src/algorithms/path-finding.js +6 -6
  5. package/src/data-structures/board.js +97 -15
  6. package/src/data-structures/index.js +1 -2
  7. package/src/data-structures/objects.js +352 -34
  8. package/src/data-structures/tree.js +1 -1
  9. package/src/functions/functions.js +168 -0
  10. package/src/functions/index.js +1 -4
  11. package/src/math/circle.js +5 -7
  12. package/src/math/index.js +0 -4
  13. package/src/math/line.js +3 -3
  14. package/src/math/point.js +10 -9
  15. package/src/math/quaternions.js +20 -4
  16. package/src/math/rectangle.js +4 -4
  17. package/src/math/segment.js +11 -5
  18. package/src/math/vectors.js +471 -65
  19. package/src/physics/acceleration.js +2 -3
  20. package/src/physics/friction.js +2 -2
  21. package/src/physics/gravity.js +2 -2
  22. package/src/physics/jump.js +0 -4
  23. package/src/physics/velocity.js +2 -3
  24. package/src/v.js +1 -1
  25. package/types/algorithms/decision-tree.d.ts +27 -0
  26. package/types/algorithms/index.d.ts +2 -0
  27. package/types/algorithms/path-finding.d.ts +69 -0
  28. package/types/data-structures/array.d.ts +56 -0
  29. package/types/data-structures/board.d.ts +121 -0
  30. package/types/data-structures/boolean.d.ts +4 -0
  31. package/types/data-structures/heap.d.ts +69 -0
  32. package/types/data-structures/index.d.ts +8 -0
  33. package/types/data-structures/objects.d.ts +237 -0
  34. package/types/data-structures/set.d.ts +4 -0
  35. package/types/data-structures/tree.d.ts +21 -0
  36. package/types/functions/functions.d.ts +57 -0
  37. package/types/functions/index.d.ts +1 -0
  38. package/types/index.d.ts +6 -4
  39. package/types/math/circle.d.ts +22 -0
  40. package/types/math/hitmask.d.ts +20 -0
  41. package/types/math/index.d.ts +14 -0
  42. package/types/math/line.d.ts +11 -0
  43. package/types/math/linear-interpolation.d.ts +2 -0
  44. package/types/math/numbers.d.ts +26 -0
  45. package/types/math/point.d.ts +25 -0
  46. package/types/math/quaternions.d.ts +10 -0
  47. package/types/math/rectangle.d.ts +36 -0
  48. package/types/math/rng.d.ts +14 -0
  49. package/types/math/segment.d.ts +23 -0
  50. package/types/math/statistics.d.ts +11 -0
  51. package/types/math/triangle.d.ts +5 -0
  52. package/types/math/trigonometry.d.ts +26 -0
  53. package/types/math/vectors.d.ts +190 -0
  54. package/types/physics/acceleration.d.ts +24 -0
  55. package/types/physics/friction.d.ts +11 -0
  56. package/types/physics/gravity.d.ts +20 -0
  57. package/types/physics/index.d.ts +5 -0
  58. package/types/physics/jump.d.ts +10 -0
  59. package/types/physics/velocity.d.ts +20 -0
  60. package/types/v.d.ts +7 -3
  61. package/src/data-structures/object.js +0 -320
  62. package/src/functions/function.js +0 -9
  63. package/src/math/quaternion.js +0 -22
  64. package/src/math/vector.js +0 -426
  65. package/types/algorithms.d.ts +0 -26
  66. package/types/data-structures.d.ts +0 -53
  67. package/types/math.d.ts +0 -90
package/src/math/point.js CHANGED
@@ -1,8 +1,9 @@
1
1
  /**
2
- * @typedef {import("../../types/math").Point} Point
3
- * @typedef {import("../../types/math").Line} Line
4
- * @typedef {import("../../types/math").Circle} Circle
5
- * @typedef {import("../../types/math").Rectangle} Rectangle
2
+ * @typedef {import("../../types/math/point").Point} Point
3
+ * @typedef {import("../../types/math/point").PointInput} PointInput
4
+ * @typedef {import("../../types/math/line").Line} Line
5
+ * @typedef {import("../../types/math/circle").Circle} Circle
6
+ * @typedef {import("../../types/math/rectangle").Rectangle} Rectangle
6
7
  */
7
8
 
8
9
  const SQUARED = 2
@@ -10,7 +11,7 @@ const HALF = 2
10
11
 
11
12
  import { distanceFromPoint } from "./line.js"
12
13
  import { isBetween } from "./numbers.js"
13
- import { isVector } from "./vector.js"
14
+ import { isVector } from "./vectors.js"
14
15
 
15
16
  /**
16
17
  * Calculates the distance from a point to a line.
@@ -24,8 +25,8 @@ export function getDistanceFromLine(point, line) {
24
25
 
25
26
  /**
26
27
  * Checks if two points intersect.
27
- * @param {Point} point1 - The first point as a 3D coordinate [x, y, z].
28
- * @param {Point} point2 - The second point as a 3D coordinate [x, y, z].
28
+ * @param {PointInput} point1 - The first point as a 3D coordinate [x, y, z].
29
+ * @param {PointInput} point2 - The second point as a 3D coordinate [x, y, z].
29
30
  * @returns {boolean} True if the points intersect, false otherwise.
30
31
  */
31
32
  export function intersectsPoint(point1, point2) {
@@ -36,7 +37,7 @@ export function intersectsPoint(point1, point2) {
36
37
 
37
38
  /**
38
39
  * Checks if a point intersects with a circle.
39
- * @param {Point} point - The point as a 3D coordinate [x, y, z].
40
+ * @param {PointInput} point - The point as a 3D coordinate [x, y, z].
40
41
  * @param {Circle} circle - The circle with a position and radius.
41
42
  * @returns {boolean} True if the point intersects the circle, false otherwise.
42
43
  */
@@ -53,7 +54,7 @@ export function intersectsCircle(point, circle) {
53
54
 
54
55
  /**
55
56
  * Checks if a point intersects with a rectangle.
56
- * @param {Point} point - The point as a 3D coordinate [x, y, z].
57
+ * @param {PointInput} point - The point as a 3D coordinate [x, y, z].
57
58
  * @param {Rectangle} rectangle - The rectangle with a position and size.
58
59
  * @returns {boolean} True if the point intersects the rectangle, false otherwise.
59
60
  */
@@ -1,10 +1,16 @@
1
1
  /**
2
- * @typedef {import("../../types/math").Quaternion} Quaternion
2
+ * @typedef {import("../../types/math/vectors").Vector3} Vector3
3
+ * @typedef {import("../../types/math/quaternions").Quaternion} Quaternion
3
4
  */
4
5
 
5
6
  import { v } from "../v.js"
6
- import { times } from "./vector.js"
7
- import { cross, dot, sum } from "./vectors.js"
7
+ import { cos, sin } from "./trigonometry.js"
8
+ import { cross, dot, multiply, sum } from "./vectors.js"
9
+
10
+ const DEFAULT_ANGLE = 0
11
+ const HALF = 2 // Constant representing the divisor for halving an angle.
12
+ // eslint-disable-next-line no-magic-numbers
13
+ const Y_AXIS = v(0, 1, 0) // Default axis of rotation (Y-axis).
8
14
 
9
15
  /**
10
16
  * Combines two quaternions using the Hamilton product.
@@ -18,6 +24,16 @@ export function combine(q1, q2) {
18
24
 
19
25
  return v(
20
26
  s1 * s2 - dot(v1, v2),
21
- ...sum(times(v2, s1), times(v1, s2), cross(v1, v2)),
27
+ ...sum(multiply(v2, s1), multiply(v1, s2), cross(v1, v2)),
22
28
  )
23
29
  }
30
+
31
+ /**
32
+ * Computes a quaternion representing a rotation around a given axis.
33
+ * @param {number} [angle=0] - The angle of rotation in radians. Defaults to 0.
34
+ * @param {Vector3} [axis=Y_AXIS] - The axis of rotation as a 3D vector Defaults to the Y-axis.
35
+ * @returns {Quaternion} The quaternion as an array [w, x, y, z].
36
+ */
37
+ export function quaternion(angle = DEFAULT_ANGLE, axis = Y_AXIS) {
38
+ return v(cos(angle / HALF), ...axis.map((coord) => coord * sin(angle / HALF)))
39
+ }
@@ -1,7 +1,7 @@
1
1
  /**
2
- * @typedef {import("../../types/math").Circle} Circle
3
- * @typedef {import("../../types/math").Rectangle} Rectangle
4
- * @typedef {import("../../types/math").Platform} Platform
2
+ * @typedef {import("../../types/math/circle").Circle} Circle
3
+ * @typedef {import("../../types/math/point").PointInput} PointInput
4
+ * @typedef {import("../../types/math/rectangle").Rectangle} Rectangle
5
5
  */
6
6
 
7
7
  import { intersectsRectangle as circleIntersectsRectangle } from "./circle.js"
@@ -12,7 +12,7 @@ const HALF = 2
12
12
  /**
13
13
  * Checks if a rectangle intersects with a point.
14
14
  * @param {Rectangle} rectangle - The rectangle to check.
15
- * @param {Point} point - The point to check.
15
+ * @param {PointInput} point - The point to check.
16
16
  * @returns {boolean} True if the point intersects the circle, false otherwise.
17
17
  */
18
18
  export function intersectsPoint(rectangle, point) {
@@ -1,11 +1,17 @@
1
1
  /**
2
- * @typedef {import("../../types/math").Segment} Segment
3
- * @typedef {import("../../types/math").Point} Point
4
- * @typedef {import("../../types/math").Circle} Circle
2
+ * @typedef {import("../../types/math/segment").Segment} Segment
3
+ * @typedef {import("../../types/math/point").Point} Point
4
+ * @typedef {import("../../types/math/circle").Circle} Circle
5
5
  */
6
6
 
7
- import { magnitude, setMagnitude } from "./vector.js"
8
- import { distance, dot, subtract, sum } from "./vectors.js"
7
+ import {
8
+ distance,
9
+ dot,
10
+ magnitude,
11
+ setMagnitude,
12
+ subtract,
13
+ sum,
14
+ } from "./vectors.js"
9
15
 
10
16
  const BEFORE_SEGMENT = 0
11
17