@inglorious/utils 3.7.0 → 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 +1 -1
- 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 -393
- 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
package/src/math/circle.test.js
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import { v } from "../v.js"
|
|
4
|
-
import {
|
|
5
|
-
intersectsCircle,
|
|
6
|
-
intersectsPoint,
|
|
7
|
-
intersectsRectangle,
|
|
8
|
-
} from "./circle.js"
|
|
9
|
-
|
|
10
|
-
test("it should prove that a circle around a point intersects with it", () => {
|
|
11
|
-
const circle = {
|
|
12
|
-
position: v(1, 1, 0),
|
|
13
|
-
radius: 1,
|
|
14
|
-
}
|
|
15
|
-
const point = v(1.5, 1.5, 0)
|
|
16
|
-
|
|
17
|
-
expect(intersectsPoint(circle, point)).toBe(true)
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
test("it should prove that two equal circles intersect", () => {
|
|
21
|
-
const circle1 = { position: v(1, 1, 0), radius: 1 }
|
|
22
|
-
const circle2 = { position: v(1, 1, 0), radius: 1 }
|
|
23
|
-
|
|
24
|
-
expect(intersectsCircle(circle1, circle2)).toBe(true)
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
test("it should prove that two circles shifted by a small x intersect", () => {
|
|
28
|
-
const circle1 = { position: v(1, 1, 0), radius: 1 }
|
|
29
|
-
const circle2 = { position: v(2, 1, 0), radius: 1 }
|
|
30
|
-
|
|
31
|
-
expect(intersectsCircle(circle1, circle2)).toBe(true)
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
test("it should prove that two touching circles intersect", () => {
|
|
35
|
-
const circle1 = { position: v(1, 1, 0), radius: 1 }
|
|
36
|
-
const circle2 = { position: v(3, 1, 0), radius: 1 }
|
|
37
|
-
|
|
38
|
-
expect(intersectsCircle(circle1, circle2)).toBe(true)
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
test("it should prove that two non-touching circles do not intersect", () => {
|
|
42
|
-
const circle1 = { position: v(1, 1, 0), radius: 1 }
|
|
43
|
-
const circle2 = { position: v(4, 1, 0), radius: 1 }
|
|
44
|
-
|
|
45
|
-
expect(intersectsCircle(circle1, circle2)).toBe(false)
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
test("it should prove that a circle inside of a rectangle intersects with it", () => {
|
|
49
|
-
const circle = {
|
|
50
|
-
position: v(1, 1, 0),
|
|
51
|
-
radius: 1,
|
|
52
|
-
}
|
|
53
|
-
const rectangle = {
|
|
54
|
-
position: v(-1, -1, 0),
|
|
55
|
-
size: v(4, 4, 0),
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
expect(intersectsRectangle(circle, rectangle)).toBe(true)
|
|
59
|
-
})
|
|
60
|
-
|
|
61
|
-
test("it should prove that a circle touching the border of a rectangle intersects with it", () => {
|
|
62
|
-
const circle = {
|
|
63
|
-
position: v(1, 1, 0),
|
|
64
|
-
radius: 1,
|
|
65
|
-
}
|
|
66
|
-
const rectangle = {
|
|
67
|
-
position: v(-1, 1, 0), // Rectangle's right edge is at x=0, touching the circle's left edge.
|
|
68
|
-
size: v(2, 2, 0),
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
expect(intersectsRectangle(circle, rectangle)).toBe(true)
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
test("it should prove that a circle crossing a rectangle intersects with it", () => {
|
|
75
|
-
const circle = {
|
|
76
|
-
position: v(1, 1, 0),
|
|
77
|
-
radius: 1,
|
|
78
|
-
}
|
|
79
|
-
const rectangle = {
|
|
80
|
-
position: v(0, 0, 0),
|
|
81
|
-
size: v(2, 2, 0),
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
expect(intersectsRectangle(circle, rectangle)).toBe(true)
|
|
85
|
-
})
|
|
86
|
-
|
|
87
|
-
test("it should prove that a circle outside of a rectangle does not intersect with it", () => {
|
|
88
|
-
const circle = {
|
|
89
|
-
position: v(1, 1, 0),
|
|
90
|
-
radius: 1,
|
|
91
|
-
}
|
|
92
|
-
const rectangle = {
|
|
93
|
-
position: v(-3, 0, 0),
|
|
94
|
-
size: v(2, 2, 0),
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
expect(intersectsRectangle(circle, rectangle)).toBe(false)
|
|
98
|
-
})
|
package/src/math/hitmask.test.js
DELETED
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import { v } from "../v.js"
|
|
4
|
-
import { findCollisions } from "./hitmask.js"
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Given a rectangle located at (x=1.5, y=0, z=2.5) with a size of (1, 1, 1),
|
|
8
|
-
* which is touching a wall tile, this test proves a collision is detected.
|
|
9
|
-
*
|
|
10
|
-
* Tilemap layout:
|
|
11
|
-
* ⬛️🟩🟩⬛️
|
|
12
|
-
* ⬛️⬛️⬛️⬛️
|
|
13
|
-
* ⬛️⬛️⬛️⬛️
|
|
14
|
-
*
|
|
15
|
-
* Rectangle position (X,Z): R at (1.5, 2.5)
|
|
16
|
-
*
|
|
17
|
-
* Expected outcome: true
|
|
18
|
-
*/
|
|
19
|
-
test("it should prove that a rectangle touching a wall intersects with it", () => {
|
|
20
|
-
const hitmask = {
|
|
21
|
-
position: v(2, 0, 1.5),
|
|
22
|
-
tileSize: [1, 1],
|
|
23
|
-
columns: 4,
|
|
24
|
-
// The top row (indices 0, 1, 2, 3) now has the walls.
|
|
25
|
-
heights: [0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
26
|
-
}
|
|
27
|
-
const rectangle = {
|
|
28
|
-
shape: "rectangle",
|
|
29
|
-
position: v(1.5, 0, 2.5),
|
|
30
|
-
size: v(1, 1, 1),
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
expect(findCollisions(hitmask, rectangle)).toBe(true)
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Given the same scenario as the previous test, but all coordinates
|
|
38
|
-
* are shifted by a constant value, this test proves the outcome remains
|
|
39
|
-
* unchanged, ensuring the collision detection is relative.
|
|
40
|
-
*
|
|
41
|
-
* Tilemap layout:
|
|
42
|
-
* ⬛️🟩🟩⬛️
|
|
43
|
-
* ⬛️⬛️⬛️⬛️
|
|
44
|
-
* ⬛️⬛️⬛️⬛️
|
|
45
|
-
*
|
|
46
|
-
* Rectangle position (X,Z): R at (11.5, 12.5)
|
|
47
|
-
*
|
|
48
|
-
* Expected outcome: true
|
|
49
|
-
*/
|
|
50
|
-
test("it should prove that shifting operands does not change the outcome", () => {
|
|
51
|
-
const SHIFT = 10
|
|
52
|
-
const hitmask = {
|
|
53
|
-
position: [2 + SHIFT, 0, 1.5 + SHIFT],
|
|
54
|
-
tileSize: [1, 1],
|
|
55
|
-
columns: 4,
|
|
56
|
-
// The top row (indices 0, 1, 2, 3) now has the walls.
|
|
57
|
-
heights: [0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
58
|
-
}
|
|
59
|
-
const rectangle = {
|
|
60
|
-
shape: "rectangle",
|
|
61
|
-
position: v(11.5, 0, 12.5),
|
|
62
|
-
size: v(1, 1, 1),
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
expect(findCollisions(hitmask, rectangle)).toBe(true)
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Given a rectangle located at (x=0.5, y=0, z=0.5) with a size of (1, 1, 1),
|
|
70
|
-
* which is not touching any walls, this test proves no collision is detected.
|
|
71
|
-
*
|
|
72
|
-
* Tilemap layout:
|
|
73
|
-
* ⬛️🟩🟩⬛️
|
|
74
|
-
* ⬛️⬛️⬛️⬛️
|
|
75
|
-
* ⬛️⬛️⬛️⬛️
|
|
76
|
-
*
|
|
77
|
-
* Rectangle position (X,Z): R at (0.5, 0.5)
|
|
78
|
-
*
|
|
79
|
-
* Expected outcome: false
|
|
80
|
-
*/
|
|
81
|
-
test("it should prove that a rectangle not touching a wall does not intersect with it", () => {
|
|
82
|
-
const hitmask = {
|
|
83
|
-
position: v(2, 0, 1.5),
|
|
84
|
-
tileSize: [1, 1],
|
|
85
|
-
columns: 4,
|
|
86
|
-
// The top row (indices 0, 1, 2, 3) now has the walls.
|
|
87
|
-
heights: [0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
88
|
-
}
|
|
89
|
-
const rectangle = {
|
|
90
|
-
shape: "rectangle",
|
|
91
|
-
position: v(0.5, 0, 0.5),
|
|
92
|
-
size: v(1, 1, 1),
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
expect(findCollisions(hitmask, rectangle)).toBe(false)
|
|
96
|
-
})
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Given a rectangle located above the tile, this test proves no
|
|
100
|
-
* collision is detected even when the X and Z positions overlap.
|
|
101
|
-
*
|
|
102
|
-
* Tilemap layout:
|
|
103
|
-
* ⬛️🟩🟩⬛️
|
|
104
|
-
* ⬛️⬛️⬛️⬛️
|
|
105
|
-
* ⬛️⬛️⬛️⬛️
|
|
106
|
-
*
|
|
107
|
-
* Rectangle position: R at (x=1.5, y=1.6, z=2.5)
|
|
108
|
-
*
|
|
109
|
-
* Expected outcome: false
|
|
110
|
-
*/
|
|
111
|
-
test("it should prove that a rectangle above the tile of a hitmask does not intersect with it", () => {
|
|
112
|
-
const hitmask = {
|
|
113
|
-
position: v(2, 0, 1.5),
|
|
114
|
-
tileSize: [1, 1],
|
|
115
|
-
columns: 4,
|
|
116
|
-
// The top row (indices 0, 1, 2, 3) now has the walls.
|
|
117
|
-
heights: [0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
118
|
-
}
|
|
119
|
-
const rectangle = {
|
|
120
|
-
shape: "rectangle",
|
|
121
|
-
position: v(1.5, 1.6, 2.5),
|
|
122
|
-
size: v(1, 1, 1),
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
expect(findCollisions(hitmask, rectangle)).toBe(false)
|
|
126
|
-
})
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Given a rectangle located outside of the tilemap, this test proves
|
|
130
|
-
* no collision is detected.
|
|
131
|
-
*
|
|
132
|
-
* Tilemap layout:
|
|
133
|
-
* ⬛️🟩🟩⬛️
|
|
134
|
-
* ⬛️⬛️⬛️⬛️
|
|
135
|
-
* ⬛️⬛️⬛️⬛️
|
|
136
|
-
*
|
|
137
|
-
* Rectangle position: R at (x=5.5, y=0.5, z=1.5)
|
|
138
|
-
*
|
|
139
|
-
* Expected outcome: false
|
|
140
|
-
*/
|
|
141
|
-
test("it should prove that a rectangle outside of a hitmask does not intersect with it", () => {
|
|
142
|
-
const hitmask = {
|
|
143
|
-
position: v(2, 0, 1.5),
|
|
144
|
-
tileSize: [1, 1],
|
|
145
|
-
columns: 4,
|
|
146
|
-
// The top row (indices 0, 1, 2, 3) now has the walls.
|
|
147
|
-
heights: [0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
148
|
-
}
|
|
149
|
-
const rectangle = {
|
|
150
|
-
shape: "rectangle",
|
|
151
|
-
position: v(5.5, 0.5, 1.5),
|
|
152
|
-
size: v(1, 1, 1),
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
expect(findCollisions(hitmask, rectangle)).toBe(false)
|
|
156
|
-
})
|
package/src/math/line.test.js
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import { v } from "../v.js"
|
|
4
|
-
import { distanceFromPoint, intersectsCircle } from "./line.js"
|
|
5
|
-
|
|
6
|
-
test("it should compute the distance between a line and a point", () => {
|
|
7
|
-
const line = v(-4, 3, 0)
|
|
8
|
-
const point = v(5, 0, 0)
|
|
9
|
-
const expectedResult = 4
|
|
10
|
-
|
|
11
|
-
expect(distanceFromPoint(line, point)).toBe(expectedResult)
|
|
12
|
-
})
|
|
13
|
-
|
|
14
|
-
test("it should not compute the distance between a line and a point", () => {
|
|
15
|
-
const line = v(-1, 1, 0)
|
|
16
|
-
const point = v(2, 0, 2)
|
|
17
|
-
const expectedResult = 0
|
|
18
|
-
|
|
19
|
-
expect(distanceFromPoint(line, point)).toBe(expectedResult)
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
test("it should prove that a line that crosses a circle intersects with it", () => {
|
|
23
|
-
const line = v(-2, 2, 0)
|
|
24
|
-
const circle = {
|
|
25
|
-
position: v(1, 1, 0),
|
|
26
|
-
radius: 1,
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
expect(intersectsCircle(line, circle)).toBe(true)
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
test("it should prove that a line tangent to a circle intersects with it", () => {
|
|
33
|
-
const line = v(-2, 0, 0)
|
|
34
|
-
const circle = {
|
|
35
|
-
position: v(1, 0, 1),
|
|
36
|
-
radius: 1,
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
expect(intersectsCircle(line, circle)).toBe(true)
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
test("it should prove that a line that does not cross a circle does not intersect with it", () => {
|
|
43
|
-
const line = v(1, 1, 0)
|
|
44
|
-
const circle = {
|
|
45
|
-
position: v(1, 0, 1),
|
|
46
|
-
radius: 1,
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
expect(intersectsCircle(line, circle)).toBe(false)
|
|
50
|
-
})
|
package/src/math/numbers.test.js
DELETED
|
@@ -1,198 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
abs,
|
|
5
|
-
clamp,
|
|
6
|
-
isBetween,
|
|
7
|
-
isClose,
|
|
8
|
-
mod,
|
|
9
|
-
sign,
|
|
10
|
-
snap,
|
|
11
|
-
sqrt,
|
|
12
|
-
} from "./numbers.js"
|
|
13
|
-
|
|
14
|
-
test("it should return the same number if already positive", () => {
|
|
15
|
-
const num = 42
|
|
16
|
-
const expectedResult = 42
|
|
17
|
-
|
|
18
|
-
expect(abs(num)).toBe(expectedResult)
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
test("it should return the absolute value of a number", () => {
|
|
22
|
-
const num = -42
|
|
23
|
-
const expectedResult = 42
|
|
24
|
-
|
|
25
|
-
expect(abs(num)).toBe(expectedResult)
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
test("it should not clamp a number in the range", () => {
|
|
29
|
-
const num = 42
|
|
30
|
-
const min = 0
|
|
31
|
-
const max = 100
|
|
32
|
-
const expectedResult = 42
|
|
33
|
-
|
|
34
|
-
expect(clamp(num, min, max)).toBe(expectedResult)
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
test("it should clamp a number too small", () => {
|
|
38
|
-
const num = -42
|
|
39
|
-
const min = 0
|
|
40
|
-
const max = 100
|
|
41
|
-
const expectedResult = 0
|
|
42
|
-
|
|
43
|
-
expect(clamp(num, min, max)).toBe(expectedResult)
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
test("it should clamp a number too big", () => {
|
|
47
|
-
const num = 142
|
|
48
|
-
const min = 0
|
|
49
|
-
const max = 100
|
|
50
|
-
const expectedResult = 100
|
|
51
|
-
|
|
52
|
-
expect(clamp(num, min, max)).toBe(expectedResult)
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
test("it should return true if a number is between min and max", () => {
|
|
56
|
-
const num = 50
|
|
57
|
-
const min = 0
|
|
58
|
-
const max = 100
|
|
59
|
-
const expectedResult = true
|
|
60
|
-
|
|
61
|
-
expect(isBetween(num, min, max)).toBe(expectedResult)
|
|
62
|
-
})
|
|
63
|
-
|
|
64
|
-
test("it should return true if a number is equal to min", () => {
|
|
65
|
-
const num = 0
|
|
66
|
-
const min = 0
|
|
67
|
-
const max = 100
|
|
68
|
-
const expectedResult = true
|
|
69
|
-
|
|
70
|
-
expect(isBetween(num, min, max)).toBe(expectedResult)
|
|
71
|
-
})
|
|
72
|
-
|
|
73
|
-
test("it should return true if a number is equal to max", () => {
|
|
74
|
-
const num = 100
|
|
75
|
-
const min = 0
|
|
76
|
-
const max = 100
|
|
77
|
-
const expectedResult = true
|
|
78
|
-
|
|
79
|
-
expect(isBetween(num, min, max)).toBe(expectedResult)
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
test("it should return false if a number is lower than min", () => {
|
|
83
|
-
const num = -1
|
|
84
|
-
const min = 0
|
|
85
|
-
const max = 100
|
|
86
|
-
const expectedResult = false
|
|
87
|
-
|
|
88
|
-
expect(isBetween(num, min, max)).toBe(expectedResult)
|
|
89
|
-
})
|
|
90
|
-
|
|
91
|
-
test("it should return false if a number is greater than max", () => {
|
|
92
|
-
const num = 101
|
|
93
|
-
const min = 0
|
|
94
|
-
const max = 100
|
|
95
|
-
const expectedResult = false
|
|
96
|
-
|
|
97
|
-
expect(isBetween(num, min, max)).toBe(expectedResult)
|
|
98
|
-
})
|
|
99
|
-
|
|
100
|
-
test("it should check if two numbers are close given some tolerance", () => {
|
|
101
|
-
const num1 = 42
|
|
102
|
-
const num2 = 42.1
|
|
103
|
-
const tolerance = 0.2
|
|
104
|
-
const expectedResult = true
|
|
105
|
-
|
|
106
|
-
expect(isClose(num1, num2, tolerance)).toBe(expectedResult)
|
|
107
|
-
})
|
|
108
|
-
|
|
109
|
-
test("it should return zero if dividend equals divisor", () => {
|
|
110
|
-
const num = 2
|
|
111
|
-
const divisor = 2
|
|
112
|
-
const expectedResult = 0
|
|
113
|
-
|
|
114
|
-
expect(mod(num, divisor)).toBe(expectedResult)
|
|
115
|
-
})
|
|
116
|
-
|
|
117
|
-
test("it should return one if number is odd", () => {
|
|
118
|
-
const num = 3
|
|
119
|
-
const divisor = 2
|
|
120
|
-
const expectedResult = 1
|
|
121
|
-
|
|
122
|
-
expect(mod(num, divisor)).toBe(expectedResult)
|
|
123
|
-
})
|
|
124
|
-
|
|
125
|
-
test("it should return one if negative number turns to positive one", () => {
|
|
126
|
-
const num = -3
|
|
127
|
-
const divisor = 2
|
|
128
|
-
const expectedResult = 1
|
|
129
|
-
|
|
130
|
-
expect(mod(num, divisor)).toBe(expectedResult)
|
|
131
|
-
})
|
|
132
|
-
|
|
133
|
-
test("it should convert a float number greater than 1 to a range between -1 and 1", () => {
|
|
134
|
-
const num = 5 / 4
|
|
135
|
-
const divisor = 1
|
|
136
|
-
const expectedResult = 1 / 4
|
|
137
|
-
|
|
138
|
-
expect(mod(num, divisor)).toBe(expectedResult)
|
|
139
|
-
})
|
|
140
|
-
|
|
141
|
-
test("it should convert a float number greater than 2 to a range between -1 and 1", () => {
|
|
142
|
-
const num = 13 / 4
|
|
143
|
-
const divisor = 1
|
|
144
|
-
const expectedResult = 1 / 4
|
|
145
|
-
|
|
146
|
-
expect(mod(num, divisor)).toBe(expectedResult)
|
|
147
|
-
})
|
|
148
|
-
|
|
149
|
-
test("it should convert a negative float number to a range between -1 and 1", () => {
|
|
150
|
-
const num = -5 / 4
|
|
151
|
-
const divisor = 1
|
|
152
|
-
const expectedResult = 3 / 4
|
|
153
|
-
|
|
154
|
-
expect(mod(num, divisor)).toBe(expectedResult)
|
|
155
|
-
})
|
|
156
|
-
|
|
157
|
-
test("it should return the dividend if the divisor is zero", () => {
|
|
158
|
-
const num = 42
|
|
159
|
-
const divisor = 0
|
|
160
|
-
|
|
161
|
-
expect(mod(num, divisor)).toBe(num)
|
|
162
|
-
})
|
|
163
|
-
|
|
164
|
-
test("it should return one if number is positive", () => {
|
|
165
|
-
const num = 42
|
|
166
|
-
const expectedResult = 1
|
|
167
|
-
|
|
168
|
-
expect(sign(num)).toBe(expectedResult)
|
|
169
|
-
})
|
|
170
|
-
|
|
171
|
-
test("it should return minus one if number is negative", () => {
|
|
172
|
-
const num = -42
|
|
173
|
-
const expectedResult = -1
|
|
174
|
-
|
|
175
|
-
expect(sign(num)).toBe(expectedResult)
|
|
176
|
-
})
|
|
177
|
-
|
|
178
|
-
test("it should return the number itself if the sign is zero", () => {
|
|
179
|
-
const num = 0
|
|
180
|
-
const expectedResult = 0
|
|
181
|
-
|
|
182
|
-
expect(sign(num)).toBe(expectedResult)
|
|
183
|
-
})
|
|
184
|
-
|
|
185
|
-
test("it should snap the number to the given precision", () => {
|
|
186
|
-
const num = 42
|
|
187
|
-
const precision = 10
|
|
188
|
-
const expectedResult = 40
|
|
189
|
-
|
|
190
|
-
expect(snap(num, precision)).toBe(expectedResult)
|
|
191
|
-
})
|
|
192
|
-
|
|
193
|
-
test("it should compute the square root of the number", () => {
|
|
194
|
-
const num = 9
|
|
195
|
-
const expectedResult = 3
|
|
196
|
-
|
|
197
|
-
expect(sqrt(num)).toBe(expectedResult)
|
|
198
|
-
})
|
package/src/math/point.test.js
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import { v } from "../v.js"
|
|
4
|
-
import {
|
|
5
|
-
intersectsCircle,
|
|
6
|
-
intersectsPoint,
|
|
7
|
-
intersectsRectangle,
|
|
8
|
-
} from "./point.js"
|
|
9
|
-
|
|
10
|
-
test("it should prove that two equal points intersect", () => {
|
|
11
|
-
const point1 = v(1.5, 1.5, 0)
|
|
12
|
-
const point2 = v(1.5, 1.5, 0)
|
|
13
|
-
|
|
14
|
-
expect(intersectsPoint(point1, point2)).toBe(true)
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
test("it should prove that two different points do not intersect", () => {
|
|
18
|
-
const point1 = v(1.5, 1.5, 0)
|
|
19
|
-
const point2 = v(2, 1, 0)
|
|
20
|
-
|
|
21
|
-
expect(intersectsPoint(point1, point2)).toBe(false)
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
test("it should prove that a point inside of a circle intersects with it", () => {
|
|
25
|
-
const point = v(1.5, 1.5, 0)
|
|
26
|
-
const circle = {
|
|
27
|
-
position: v(1, 1, 0),
|
|
28
|
-
radius: 1,
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
expect(intersectsCircle(point, circle)).toBe(true)
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
test("it should prove that a point on the border of a circle intersects with it", () => {
|
|
35
|
-
const point = v(2, 1, 0)
|
|
36
|
-
const circle = {
|
|
37
|
-
position: v(1, 1, 0),
|
|
38
|
-
radius: 1,
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
expect(intersectsCircle(point, circle)).toBe(true)
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
test("it should prove that a point outside of a circle does not intersect with it", () => {
|
|
45
|
-
const point = v(2, 2, 0)
|
|
46
|
-
const circle = {
|
|
47
|
-
position: v(1, 1, 0),
|
|
48
|
-
radius: 1,
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
expect(intersectsCircle(point, circle)).toBe(false)
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
test("it should prove that a point inside of a rectangle intersects with it", () => {
|
|
55
|
-
const point = v(1.5, 1.5, 0)
|
|
56
|
-
const rectangle = {
|
|
57
|
-
position: v(1, 1, 0),
|
|
58
|
-
size: v(2, 2, 0),
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
expect(intersectsRectangle(point, rectangle)).toBe(true)
|
|
62
|
-
})
|
|
63
|
-
|
|
64
|
-
test("it should prove that a point on the border of a rectangle intersects with it", () => {
|
|
65
|
-
const point = v(2, 1, 0)
|
|
66
|
-
const rectangle = {
|
|
67
|
-
position: v(1, 1, 0),
|
|
68
|
-
size: v(2, 2, 0),
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
expect(intersectsRectangle(point, rectangle)).toBe(true)
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
test("it should prove that a point outside of a rectangle does not intersect with it", () => {
|
|
75
|
-
const point = v(2.5, 2.5, 0)
|
|
76
|
-
const rectangle = {
|
|
77
|
-
position: v(1, 1, 0),
|
|
78
|
-
size: v(2, 2, 0),
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
expect(intersectsRectangle(point, rectangle)).toBe(false)
|
|
82
|
-
})
|
|
83
|
-
|
|
84
|
-
test("it should work even if the point is an object with a position", () => {
|
|
85
|
-
const point = {
|
|
86
|
-
position: v(1.5, 1.5, 0),
|
|
87
|
-
}
|
|
88
|
-
const rectangle = {
|
|
89
|
-
position: v(1, 1, 0),
|
|
90
|
-
size: v(2, 2, 0),
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
expect(intersectsRectangle(point, rectangle)).toBe(true)
|
|
94
|
-
})
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import { quaternion } from "./quaternion.js"
|
|
4
|
-
import { cos, pi, sin } from "./trigonometry.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
|
-
})
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import { quaternion } from "./quaternion.js"
|
|
4
|
-
import { combine } from "./quaternions.js"
|
|
5
|
-
import { cos, pi, sin } from "./trigonometry.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
|
-
})
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import { v } from "../v.js"
|
|
4
|
-
import { intersectsRectangle } from "./rectangle.js"
|
|
5
|
-
|
|
6
|
-
test("it should prove that a rectangle crossing another one intersects with it", () => {
|
|
7
|
-
const rectangle1 = {
|
|
8
|
-
position: v(0, 0, 0),
|
|
9
|
-
size: v(2, 2, 2),
|
|
10
|
-
}
|
|
11
|
-
const rectangle2 = {
|
|
12
|
-
position: v(1, 1, 1),
|
|
13
|
-
size: v(2, 2, 2),
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
expect(intersectsRectangle(rectangle1, rectangle2)).toBe(true)
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
test("it should prove that a rectangle right on top of another intersects with it", () => {
|
|
20
|
-
const rectangle1 = {
|
|
21
|
-
position: v(0, 0, 0),
|
|
22
|
-
size: v(2, 2, 2),
|
|
23
|
-
}
|
|
24
|
-
const rectangle2 = {
|
|
25
|
-
position: v(0, 0, 0),
|
|
26
|
-
size: v(2, 0, 2),
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
expect(intersectsRectangle(rectangle1, rectangle2)).toBe(true)
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
test("it should prove that a rectangle not crossing another one does not intersect with it", () => {
|
|
33
|
-
const rectangle1 = {
|
|
34
|
-
position: v(0, 0, 0),
|
|
35
|
-
size: v(2, 2, 2),
|
|
36
|
-
}
|
|
37
|
-
const rectangle2 = {
|
|
38
|
-
position: v(3, 3, 3),
|
|
39
|
-
size: v(2, 2, 2),
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
expect(intersectsRectangle(rectangle1, rectangle2)).toBe(false)
|
|
43
|
-
})
|