@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/rng.test.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import { choose, random, randomBinomial } from "./rng.js"
|
|
4
|
-
|
|
5
|
-
test("it should choose a random value from the given arguments", () => {
|
|
6
|
-
const values = ["a", "b", "c"]
|
|
7
|
-
const chosenValue = choose(...values)
|
|
8
|
-
|
|
9
|
-
expect(values).toContain(chosenValue)
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
test("it should create a random number in the range [0, 1)", () => {
|
|
13
|
-
const randomNumber = random()
|
|
14
|
-
|
|
15
|
-
expect(randomNumber).toBeGreaterThanOrEqual(0)
|
|
16
|
-
expect(randomNumber).toBeLessThan(1)
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
test("it should create a random integer in the range [0, 6]", () => {
|
|
20
|
-
const randomNumber = random(6)
|
|
21
|
-
|
|
22
|
-
expect(randomNumber).toBeGreaterThanOrEqual(0)
|
|
23
|
-
expect(randomNumber).toBeLessThanOrEqual(6)
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
test("it should create a random integer in the range [1, 6]", () => {
|
|
27
|
-
const randomNumber = random(1, 6)
|
|
28
|
-
|
|
29
|
-
expect(randomNumber).toBeGreaterThanOrEqual(1)
|
|
30
|
-
expect(randomNumber).toBeLessThanOrEqual(6)
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
test("it should create a random even number in the range [2, 6]", () => {
|
|
34
|
-
const randomNumber = random(2, 6, 2)
|
|
35
|
-
|
|
36
|
-
expect(randomNumber).toBeGreaterThanOrEqual(1)
|
|
37
|
-
expect(randomNumber).toBeLessThanOrEqual(6)
|
|
38
|
-
expect(randomNumber % 2).toBe(0)
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
test("it should create a random float in the range [1.5, 6.5)", () => {
|
|
42
|
-
const from = 1.5
|
|
43
|
-
const to = 6.5
|
|
44
|
-
const randomNumber = random(from, to)
|
|
45
|
-
|
|
46
|
-
expect(randomNumber).toBeGreaterThanOrEqual(from)
|
|
47
|
-
expect(randomNumber).toBeLessThan(to)
|
|
48
|
-
})
|
|
49
|
-
|
|
50
|
-
test("it should create a random number in the range (-1, 1)", () => {
|
|
51
|
-
const randomNumber = randomBinomial()
|
|
52
|
-
|
|
53
|
-
expect(randomNumber).toBeGreaterThan(-1)
|
|
54
|
-
expect(randomNumber).toBeLessThan(1)
|
|
55
|
-
})
|
package/src/math/segment.test.js
DELETED
|
@@ -1,184 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import { v } from "../v.js"
|
|
4
|
-
import { isClose } from "./numbers.js"
|
|
5
|
-
import {
|
|
6
|
-
closestPoint,
|
|
7
|
-
coefficients,
|
|
8
|
-
distanceFromPoint,
|
|
9
|
-
intersectsCircle,
|
|
10
|
-
} from "./segment.js"
|
|
11
|
-
|
|
12
|
-
expect.extend({
|
|
13
|
-
toStrictEqualVector(received, expected) {
|
|
14
|
-
const { isNot } = this
|
|
15
|
-
return {
|
|
16
|
-
pass: received.every((coord, index) => isClose(coord, expected[index])),
|
|
17
|
-
message: () =>
|
|
18
|
-
`${received} is ${isNot ? "" : "not"} close to ${expected}`,
|
|
19
|
-
}
|
|
20
|
-
},
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
test("it should retrieve values of *a*, *b*, and *c* from a segment, so it looks like a line in the form *ax + by + c = 0*", () => {
|
|
24
|
-
const segment = {
|
|
25
|
-
from: v(0, 0, 0),
|
|
26
|
-
to: v(1, 0, 2),
|
|
27
|
-
}
|
|
28
|
-
const expectedResult = v(-2, 1, 0)
|
|
29
|
-
|
|
30
|
-
expect(coefficients(segment)).toStrictEqual(expectedResult)
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
test("it should find the closest point in a segment to a point projectable on it", () => {
|
|
34
|
-
const segment = {
|
|
35
|
-
from: v(0, 0, 0),
|
|
36
|
-
to: v(2, 0, 2),
|
|
37
|
-
}
|
|
38
|
-
const point = v(2, 0, 0)
|
|
39
|
-
const expectedResult = v(1, 0, 1)
|
|
40
|
-
|
|
41
|
-
expect(closestPoint(segment, point)).toStrictEqualVector(expectedResult)
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
test("it should find the closest point as the point itself when resting on the segment", () => {
|
|
45
|
-
const segment = {
|
|
46
|
-
from: v(0, 0, 0),
|
|
47
|
-
to: v(2, 0, 2),
|
|
48
|
-
}
|
|
49
|
-
const point = v(1, 0, 1)
|
|
50
|
-
const expectedResult = v(1, 0, 1)
|
|
51
|
-
|
|
52
|
-
expect(closestPoint(segment, point)).toStrictEqualVector(expectedResult)
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
test("it return the start of a segment when the point is close to its start", () => {
|
|
56
|
-
const segment = {
|
|
57
|
-
from: v(0, 0, 0),
|
|
58
|
-
to: v(3, 0, 4),
|
|
59
|
-
}
|
|
60
|
-
const point = v(-2, 0, 0)
|
|
61
|
-
const expectedResult = v(0, 0, 0)
|
|
62
|
-
|
|
63
|
-
expect(closestPoint(segment, point)).toStrictEqualVector(expectedResult)
|
|
64
|
-
})
|
|
65
|
-
|
|
66
|
-
test("it should return the end of a segment when the point is close to its end", () => {
|
|
67
|
-
const segment = {
|
|
68
|
-
from: v(0, 0, 0),
|
|
69
|
-
to: v(3, 0, 4),
|
|
70
|
-
}
|
|
71
|
-
const point = v(5, 0, 4)
|
|
72
|
-
const expectedResult = v(3, 0, 4)
|
|
73
|
-
|
|
74
|
-
expect(closestPoint(segment, point)).toStrictEqualVector(expectedResult)
|
|
75
|
-
})
|
|
76
|
-
|
|
77
|
-
test("it should compute the distance between a segment and a point projectable on it", () => {
|
|
78
|
-
const segment = {
|
|
79
|
-
from: v(0, 0, 0),
|
|
80
|
-
to: v(3, 0, 4),
|
|
81
|
-
}
|
|
82
|
-
const point = v(5, 0, 0)
|
|
83
|
-
const expectedResult = 4
|
|
84
|
-
|
|
85
|
-
expect(distanceFromPoint(segment, point)).toBe(expectedResult)
|
|
86
|
-
})
|
|
87
|
-
|
|
88
|
-
test("it should compute the distance between a segment and a point resting on it", () => {
|
|
89
|
-
const segment = {
|
|
90
|
-
from: v(0, 0, 0),
|
|
91
|
-
to: v(2, 0, 2),
|
|
92
|
-
}
|
|
93
|
-
const point = v(1, 0, 1)
|
|
94
|
-
const expectedResult = 0
|
|
95
|
-
|
|
96
|
-
expect(distanceFromPoint(segment, point)).toBeCloseTo(expectedResult)
|
|
97
|
-
})
|
|
98
|
-
|
|
99
|
-
test("it should compute the distance between a segment and a point close to the start", () => {
|
|
100
|
-
const segment = {
|
|
101
|
-
from: v(0, 0, 0),
|
|
102
|
-
to: v(3, 0, 4),
|
|
103
|
-
}
|
|
104
|
-
const point = v(-2, 0, 0)
|
|
105
|
-
const expectedResult = 2
|
|
106
|
-
|
|
107
|
-
expect(distanceFromPoint(segment, point)).toBe(expectedResult)
|
|
108
|
-
})
|
|
109
|
-
|
|
110
|
-
test("it should compute the distance between a segment and a point close to the end", () => {
|
|
111
|
-
const segment = {
|
|
112
|
-
from: v(0, 0, 0),
|
|
113
|
-
to: v(3, 0, 4),
|
|
114
|
-
}
|
|
115
|
-
const point = v(5, 0, 4)
|
|
116
|
-
const expectedResult = 2
|
|
117
|
-
|
|
118
|
-
expect(distanceFromPoint(segment, point)).toBeCloseTo(expectedResult)
|
|
119
|
-
})
|
|
120
|
-
|
|
121
|
-
test("it should prove that a segment that crosses a circle intersects with it", () => {
|
|
122
|
-
const segment = {
|
|
123
|
-
from: v(0, 0, 0),
|
|
124
|
-
to: v(2, 0, 2),
|
|
125
|
-
}
|
|
126
|
-
const circle = {
|
|
127
|
-
position: v(1, 0, 1),
|
|
128
|
-
radius: 1,
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
expect(intersectsCircle(segment, circle)).toBe(true)
|
|
132
|
-
})
|
|
133
|
-
|
|
134
|
-
test("it should prove that a segment tangent to a circle intersects with it", () => {
|
|
135
|
-
const segment = {
|
|
136
|
-
from: v(0, 0, 0),
|
|
137
|
-
to: v(0, 0, 2),
|
|
138
|
-
}
|
|
139
|
-
const circle = {
|
|
140
|
-
position: v(1, 0, 1),
|
|
141
|
-
radius: 1,
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
expect(intersectsCircle(segment, circle)).toBe(true)
|
|
145
|
-
})
|
|
146
|
-
|
|
147
|
-
test("it should prove that a segment inside of a circle intersects with it", () => {
|
|
148
|
-
const segment = {
|
|
149
|
-
from: v(0.5, 0, 0.5),
|
|
150
|
-
to: v(1.5, 0, 1.5),
|
|
151
|
-
}
|
|
152
|
-
const circle = {
|
|
153
|
-
position: v(1, 0, 1),
|
|
154
|
-
radius: 1,
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
expect(intersectsCircle(segment, circle)).toBe(true)
|
|
158
|
-
})
|
|
159
|
-
|
|
160
|
-
test("it should prove that a segment that does not cross a circle does not intersect with it", () => {
|
|
161
|
-
const segment = {
|
|
162
|
-
from: v(0, 0, 0),
|
|
163
|
-
to: v(-2, 0, 2),
|
|
164
|
-
}
|
|
165
|
-
const circle = {
|
|
166
|
-
position: v(1, 0, 1),
|
|
167
|
-
radius: 1,
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
expect(intersectsCircle(segment, circle)).toBe(false)
|
|
171
|
-
})
|
|
172
|
-
|
|
173
|
-
test("it should prove that a segment that reaches outside of a circle does not intersect with it", () => {
|
|
174
|
-
const segment = {
|
|
175
|
-
from: v(0, 0, 0),
|
|
176
|
-
to: v(-1, 0, -2),
|
|
177
|
-
}
|
|
178
|
-
const circle = {
|
|
179
|
-
position: v(1, 0, 1),
|
|
180
|
-
radius: 1,
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
expect(intersectsCircle(segment, circle)).toBe(false)
|
|
184
|
-
})
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import { mean, median, mode } from "./statistics.js"
|
|
4
|
-
|
|
5
|
-
test("it should return the average of the given numbers", () => {
|
|
6
|
-
const numbers = [1, 3, 2]
|
|
7
|
-
const expectedResult = 2
|
|
8
|
-
|
|
9
|
-
expect(mean(...numbers)).toBeCloseTo(expectedResult)
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
test("it should return the median value from an odd list of numbers", () => {
|
|
13
|
-
const numbers = [1, 3, 2]
|
|
14
|
-
const expectedResult = 2
|
|
15
|
-
|
|
16
|
-
expect(median(...numbers)).toBe(expectedResult)
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
test("it should return the median value from an even list of numbers", () => {
|
|
20
|
-
const numbers = [1, 3, 2, 4]
|
|
21
|
-
const expectedResult = 2.5
|
|
22
|
-
|
|
23
|
-
expect(median(...numbers)).toBe(expectedResult)
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
test("it should find the mode of a list of values", () => {
|
|
27
|
-
const values = [
|
|
28
|
-
"rarely",
|
|
29
|
-
"seldom",
|
|
30
|
-
"seldom",
|
|
31
|
-
"sometimes",
|
|
32
|
-
"sometimes",
|
|
33
|
-
"sometimes",
|
|
34
|
-
"often",
|
|
35
|
-
"often",
|
|
36
|
-
"often",
|
|
37
|
-
"often",
|
|
38
|
-
"always",
|
|
39
|
-
"always",
|
|
40
|
-
"always",
|
|
41
|
-
"always",
|
|
42
|
-
"always",
|
|
43
|
-
]
|
|
44
|
-
const expectedResult = "always"
|
|
45
|
-
|
|
46
|
-
expect(mode(...values)).toBe(expectedResult)
|
|
47
|
-
})
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import { hypothenuse } from "./triangle.js"
|
|
4
|
-
|
|
5
|
-
test("it should compute the hypothenuse of two catheti (aka pythagoras)", () => {
|
|
6
|
-
const cat1 = 3
|
|
7
|
-
const cat2 = 4
|
|
8
|
-
const expectedResult = 5
|
|
9
|
-
|
|
10
|
-
expect(hypothenuse(cat1, cat2)).toBe(expectedResult)
|
|
11
|
-
})
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import { pi, toDegrees, toRadians, toRange } from "./trigonometry.js"
|
|
4
|
-
|
|
5
|
-
test("it should convert radians to degrees", () => {
|
|
6
|
-
const radians = pi() / 4
|
|
7
|
-
const expectedResult = 45
|
|
8
|
-
|
|
9
|
-
expect(toDegrees(radians)).toBeCloseTo(expectedResult)
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
test("it should convert radians to degrees", () => {
|
|
13
|
-
const degrees = 45
|
|
14
|
-
const expectedResult = pi() / 4
|
|
15
|
-
|
|
16
|
-
expect(toRadians(degrees)).toBeCloseTo(expectedResult)
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
test("it should convert an angle greater than pi to a range between -pi and pi", () => {
|
|
20
|
-
const angle = (5 / 4) * pi()
|
|
21
|
-
const expectedResult = (-3 / 4) * pi()
|
|
22
|
-
|
|
23
|
-
expect(toRange(angle)).toBeCloseTo(expectedResult)
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
test("it should convert an angle greater than 2pi to a range between -pi and pi", () => {
|
|
27
|
-
const angle = (13 / 4) * pi()
|
|
28
|
-
const expectedResult = (-3 / 4) * pi()
|
|
29
|
-
|
|
30
|
-
expect(toRange(angle)).toBeCloseTo(expectedResult)
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
test("it should convert an angle less than pi to a range between -pi and pi", () => {
|
|
34
|
-
const angle = (-5 / 4) * pi()
|
|
35
|
-
const expectedResult = (3 / 4) * pi()
|
|
36
|
-
|
|
37
|
-
expect(toRange(angle)).toBeCloseTo(expectedResult)
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
test("it should convert an angle less than 2pi to a range between -pi and pi", () => {
|
|
41
|
-
const angle = (-13 / 4) * pi()
|
|
42
|
-
const expectedResult = (3 / 4) * pi()
|
|
43
|
-
|
|
44
|
-
expect(toRange(angle)).toBeCloseTo(expectedResult)
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
test("it should not convert an angle already in the range [-pi, pi]", () => {
|
|
48
|
-
const angle = (3 / 4) * pi()
|
|
49
|
-
const expectedResult = (3 / 4) * pi()
|
|
50
|
-
|
|
51
|
-
expect(toRange(angle)).toBe(expectedResult)
|
|
52
|
-
})
|
package/src/math/vector.test.js
DELETED
|
@@ -1,345 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import { v } from "../v.js"
|
|
4
|
-
import { sqrt } from "./numbers.js"
|
|
5
|
-
import { cos, pi, sin } from "./trigonometry.js"
|
|
6
|
-
import {
|
|
7
|
-
abs,
|
|
8
|
-
angle,
|
|
9
|
-
clamp,
|
|
10
|
-
conjugate,
|
|
11
|
-
createVector,
|
|
12
|
-
divide,
|
|
13
|
-
divideBy,
|
|
14
|
-
from2D,
|
|
15
|
-
fromAngle,
|
|
16
|
-
isVector,
|
|
17
|
-
magnitude,
|
|
18
|
-
mod,
|
|
19
|
-
modOf,
|
|
20
|
-
multiply,
|
|
21
|
-
normalize,
|
|
22
|
-
power,
|
|
23
|
-
powerOf,
|
|
24
|
-
rotate,
|
|
25
|
-
setAngle,
|
|
26
|
-
setMagnitude,
|
|
27
|
-
shift,
|
|
28
|
-
snap,
|
|
29
|
-
to2D,
|
|
30
|
-
toCartesian,
|
|
31
|
-
toCylindrical,
|
|
32
|
-
toPolar,
|
|
33
|
-
toSpherical,
|
|
34
|
-
toString,
|
|
35
|
-
unit,
|
|
36
|
-
zero,
|
|
37
|
-
} from "./vector.js"
|
|
38
|
-
|
|
39
|
-
test("it should compute the absolute value of a vector's coordinates", () => {
|
|
40
|
-
const vector = v(-2, 0, 3)
|
|
41
|
-
const expectedResult = v(2, 0, 3)
|
|
42
|
-
|
|
43
|
-
expect(abs(vector)).toStrictEqual(expectedResult)
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
test("it should compute the angle of a 2D vector", () => {
|
|
47
|
-
const vector = v(1, 1)
|
|
48
|
-
const expectedResult = pi() / 4
|
|
49
|
-
|
|
50
|
-
expect(angle(vector)).toBe(expectedResult)
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
test("it should compute the angle of a 3D vector", () => {
|
|
54
|
-
const vector = v(1, 0, 1)
|
|
55
|
-
const expectedResult = pi() / 4
|
|
56
|
-
|
|
57
|
-
expect(angle(vector)).toBe(expectedResult)
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
test("it should clamp the magnitude of a vector to a certain length if too long", () => {
|
|
61
|
-
const vector = v(6, 8)
|
|
62
|
-
const min = 0
|
|
63
|
-
const max = 5
|
|
64
|
-
const expectedResult = v(3, 4)
|
|
65
|
-
|
|
66
|
-
expect(clamp(vector, min, max)).toStrictEqual(expectedResult)
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
test("it should not clamp the magnitude of a vector to a certain length if not too long", () => {
|
|
70
|
-
const vector = v(3, 4)
|
|
71
|
-
const min = 0
|
|
72
|
-
const max = 6
|
|
73
|
-
const expectedResult = v(3, 4)
|
|
74
|
-
|
|
75
|
-
expect(clamp(vector, min, max)).toStrictEqual(expectedResult)
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
test("it should clamp the magnitude of a vector to the values of other vector bounds", () => {
|
|
79
|
-
const vector = v(6, 8)
|
|
80
|
-
const lowerBound = [0, 0]
|
|
81
|
-
const upperBound = [3, 4]
|
|
82
|
-
const expectedResult = v(3, 4)
|
|
83
|
-
|
|
84
|
-
expect(clamp(vector, lowerBound, upperBound)).toStrictEqual(expectedResult)
|
|
85
|
-
})
|
|
86
|
-
|
|
87
|
-
test("it should conjugate a vector", () => {
|
|
88
|
-
const vector = v(1, 2, 3)
|
|
89
|
-
const expectedResult = v(1, -2, -3)
|
|
90
|
-
|
|
91
|
-
expect(conjugate(vector)).toStrictEqual(expectedResult)
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
test("it should create a 3D vector given its magnitude and angle", () => {
|
|
95
|
-
const magnitude = sqrt(2)
|
|
96
|
-
const angle = pi() / 4
|
|
97
|
-
const expectedResult = v(1, -0, 1.0000000000000002) // close to v(1, 0, 1)
|
|
98
|
-
|
|
99
|
-
expect(createVector(magnitude, angle)).toStrictEqual(expectedResult)
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
test("it should divide a vector by a scalar", () => {
|
|
103
|
-
const vector = v(4, 8, 12)
|
|
104
|
-
const scalar = 4
|
|
105
|
-
const expectedResult = v(1, 2, 3)
|
|
106
|
-
|
|
107
|
-
expect(divide(vector, scalar)).toStrictEqual(expectedResult)
|
|
108
|
-
})
|
|
109
|
-
|
|
110
|
-
test("it should divide a scalar by a vector", () => {
|
|
111
|
-
const scalar = 12
|
|
112
|
-
const vector = v(4, 8, 12)
|
|
113
|
-
const expectedResult = v(3, 1.5, 1)
|
|
114
|
-
|
|
115
|
-
expect(divideBy(scalar, vector)).toStrictEqual(expectedResult)
|
|
116
|
-
})
|
|
117
|
-
|
|
118
|
-
test("it should build a 3D vector from a 2D one", () => {
|
|
119
|
-
const vector = v(3, 4)
|
|
120
|
-
const expectedResult = v(3, 0, 4)
|
|
121
|
-
|
|
122
|
-
expect(from2D(vector)).toStrictEqual(expectedResult)
|
|
123
|
-
})
|
|
124
|
-
|
|
125
|
-
test("it should create a 3D unit vector from an angle", () => {
|
|
126
|
-
const angle = pi() / 4
|
|
127
|
-
const expectedResult = v(0.7071067811865475, -0, 0.7071067811865476) // close to [cos(angle), 0, sin(angle)]
|
|
128
|
-
|
|
129
|
-
expect(fromAngle(angle)).toStrictEqual(expectedResult)
|
|
130
|
-
})
|
|
131
|
-
|
|
132
|
-
test("it should compute the magnitude of a vector (aka length)", () => {
|
|
133
|
-
const vector = v(3, 4)
|
|
134
|
-
const expectedResult = 5
|
|
135
|
-
|
|
136
|
-
expect(magnitude(vector)).toBe(expectedResult)
|
|
137
|
-
})
|
|
138
|
-
|
|
139
|
-
test("it should check if a value is a vector", () => {
|
|
140
|
-
expect(isVector([1, 2, 3])).toBe(false)
|
|
141
|
-
expect(isVector(v(1, 2, 3))).toBe(true)
|
|
142
|
-
})
|
|
143
|
-
|
|
144
|
-
test("it should check if a value is not a vector", () => {
|
|
145
|
-
expect(isVector([1, "a", 3])).toBe(false)
|
|
146
|
-
expect(isVector({ x: 1, y: 2 })).toBe(false)
|
|
147
|
-
expect(isVector(null)).toBe(false)
|
|
148
|
-
expect(isVector("vector")).toBe(false)
|
|
149
|
-
expect(isVector(123)).toBe(false)
|
|
150
|
-
})
|
|
151
|
-
|
|
152
|
-
test("it should apply the mod operator (aka remainder) on a vector", () => {
|
|
153
|
-
const vector = v(10, 12, -18)
|
|
154
|
-
const divisor = 12
|
|
155
|
-
const expectedResult = v(10, 0, 6)
|
|
156
|
-
|
|
157
|
-
expect(mod(vector, divisor)).toStrictEqual(expectedResult)
|
|
158
|
-
})
|
|
159
|
-
|
|
160
|
-
test("it should apply the mod operator of a scalar with a vector", () => {
|
|
161
|
-
const scalar = 18
|
|
162
|
-
const vector = v(10, 12, -18)
|
|
163
|
-
const expectedResult = v(8, 6, -0)
|
|
164
|
-
|
|
165
|
-
expect(modOf(scalar, vector)).toStrictEqual(expectedResult)
|
|
166
|
-
})
|
|
167
|
-
|
|
168
|
-
test("it should multiply a vector with a scalar (aka times)", () => {
|
|
169
|
-
const vector = v(1, 2, 3)
|
|
170
|
-
const scalar = 4
|
|
171
|
-
const expectedResult = v(4, 8, 12)
|
|
172
|
-
|
|
173
|
-
expect(multiply(vector, scalar)).toStrictEqual(expectedResult)
|
|
174
|
-
})
|
|
175
|
-
|
|
176
|
-
test("it should normalize a vector so it has unit length", () => {
|
|
177
|
-
const vector = v(3, 4)
|
|
178
|
-
const expectedResult = v(0.6, 0.8)
|
|
179
|
-
|
|
180
|
-
expect(normalize(vector)).toStrictEqual(expectedResult)
|
|
181
|
-
})
|
|
182
|
-
|
|
183
|
-
test("it should normalize a negative vector", () => {
|
|
184
|
-
const vector = v(-3, -4)
|
|
185
|
-
const expectedResult = v(-0.6, -0.8)
|
|
186
|
-
|
|
187
|
-
expect(normalize(vector)).toStrictEqual(expectedResult)
|
|
188
|
-
})
|
|
189
|
-
|
|
190
|
-
test("it should raise each component of a vector to the given exponent (aka pow)", () => {
|
|
191
|
-
const vector = v(1, 2, 3)
|
|
192
|
-
const exponent = 2
|
|
193
|
-
const expectedResult = v(1, 4, 9)
|
|
194
|
-
|
|
195
|
-
expect(power(vector, exponent)).toStrictEqual(expectedResult)
|
|
196
|
-
})
|
|
197
|
-
|
|
198
|
-
test("it should raise a scalar to the power of each component of a vector", () => {
|
|
199
|
-
const scalar = 2
|
|
200
|
-
const vector = v(1, 2, 3)
|
|
201
|
-
const expectedResult = v(2, 4, 8)
|
|
202
|
-
|
|
203
|
-
expect(powerOf(scalar, vector)).toStrictEqual(expectedResult)
|
|
204
|
-
})
|
|
205
|
-
|
|
206
|
-
test("it should rotate a 2D vector by a certain angle", () => {
|
|
207
|
-
const vector = v(1, 0)
|
|
208
|
-
const angle = pi() / 4
|
|
209
|
-
const expectedResult = v(0.7071067811865475, 0.7071067811865476) // close to [cos(pi/4), sin(pi/4)]
|
|
210
|
-
|
|
211
|
-
expect(rotate(vector, angle)).toStrictEqual(expectedResult)
|
|
212
|
-
})
|
|
213
|
-
|
|
214
|
-
test("it should not rotate a vector when the angle is zero", () => {
|
|
215
|
-
const vector = v(1, 0, 0)
|
|
216
|
-
const angle = 0
|
|
217
|
-
const expectedResult = v(1, 0, 0)
|
|
218
|
-
|
|
219
|
-
expect(rotate(vector, angle)).toStrictEqual(expectedResult)
|
|
220
|
-
})
|
|
221
|
-
|
|
222
|
-
test("it should rotate a vector by a certain angle", () => {
|
|
223
|
-
const vector = v(1, 0, 0)
|
|
224
|
-
const angle = pi() / 4
|
|
225
|
-
const expectedResult = v(0.7071067811865475, -0, 0.7071067811865476) // close to [cos(pi/4), 0, sin(pi/4)]
|
|
226
|
-
|
|
227
|
-
expect(rotate(vector, angle)).toStrictEqual(expectedResult)
|
|
228
|
-
})
|
|
229
|
-
|
|
230
|
-
test("it should rotate a vector that faces left by a certain angle", () => {
|
|
231
|
-
const vector = v(-1, 0, 0)
|
|
232
|
-
const angle = pi() / 4
|
|
233
|
-
const expectedResult = v(-0.7071067811865475, -0, -0.7071067811865476) // close to [cos(-3/4pi), 0, sin(-3/4pi)]
|
|
234
|
-
|
|
235
|
-
expect(rotate(vector, angle)).toStrictEqual(expectedResult)
|
|
236
|
-
})
|
|
237
|
-
|
|
238
|
-
test("it should change the angle of a vector", () => {
|
|
239
|
-
const vector = v(cos(pi() / 4), 0, sin(pi() / 4))
|
|
240
|
-
const expectedResult = v(6.123233995736766e-17, 0, 1) // close to v(0, 0, 1)
|
|
241
|
-
|
|
242
|
-
expect(setAngle(vector, 1.5707963267948966)).toStrictEqual(expectedResult)
|
|
243
|
-
})
|
|
244
|
-
|
|
245
|
-
test("it should change magnitude of a vector (aka setLength)", () => {
|
|
246
|
-
const vector = v(3, 4)
|
|
247
|
-
const magnitude = 10
|
|
248
|
-
const expectedResult = v(6, 8)
|
|
249
|
-
|
|
250
|
-
expect(setMagnitude(vector, magnitude)).toStrictEqual(expectedResult)
|
|
251
|
-
})
|
|
252
|
-
|
|
253
|
-
test("it should change magnitude of a negative vector", () => {
|
|
254
|
-
const vector = v(-3, -4)
|
|
255
|
-
const magnitude = 10
|
|
256
|
-
const expectedResult = v(-6, -8)
|
|
257
|
-
|
|
258
|
-
expect(setMagnitude(vector, magnitude)).toStrictEqual(expectedResult)
|
|
259
|
-
})
|
|
260
|
-
|
|
261
|
-
test("it should shift a vector at a certain index", () => {
|
|
262
|
-
const vector = v(1, 2, 3, 4, 5)
|
|
263
|
-
const index = 2
|
|
264
|
-
const expectedResult = v(3, 4, 5, 1, 2)
|
|
265
|
-
|
|
266
|
-
expect(shift(vector, index)).toStrictEqual(expectedResult)
|
|
267
|
-
})
|
|
268
|
-
|
|
269
|
-
test("it should snap a floating vector to a certain precision", () => {
|
|
270
|
-
const vector = v(1, 1.5, 1.7)
|
|
271
|
-
const precision = 1
|
|
272
|
-
const expectedResult = v(1, 1, 2)
|
|
273
|
-
|
|
274
|
-
expect(snap(vector, precision)).toStrictEqual(expectedResult)
|
|
275
|
-
})
|
|
276
|
-
|
|
277
|
-
test("it should build a 2D vector from a 3D one", () => {
|
|
278
|
-
const vector = v(3, 0, 4)
|
|
279
|
-
const expectedResult = v(3, 4)
|
|
280
|
-
|
|
281
|
-
expect(to2D(vector)).toStrictEqual(expectedResult)
|
|
282
|
-
})
|
|
283
|
-
|
|
284
|
-
test("it should convert a 2D polar vector to cartesian coordinates", () => {
|
|
285
|
-
const vector = v(sqrt(2), pi() / 4)
|
|
286
|
-
const expectedResult = v(1.0000000000000002, 1)
|
|
287
|
-
|
|
288
|
-
expect(toCartesian(vector)).toStrictEqual(expectedResult)
|
|
289
|
-
})
|
|
290
|
-
|
|
291
|
-
test("it should convert a 3D cartesian vector to cylindrical coordinates", () => {
|
|
292
|
-
const vector = v(1, 1, 1)
|
|
293
|
-
const expectedResult = v(1.2247448713915892, 1.224744871391589, 1)
|
|
294
|
-
|
|
295
|
-
expect(toCylindrical(vector)).toStrictEqual(expectedResult)
|
|
296
|
-
})
|
|
297
|
-
|
|
298
|
-
test("it should convert a 2D cartesian vector to polar coordinates", () => {
|
|
299
|
-
const vector = v(1, 1)
|
|
300
|
-
const expectedResult = v(sqrt(2), pi() / 4)
|
|
301
|
-
|
|
302
|
-
expect(toPolar(vector)).toStrictEqual(expectedResult)
|
|
303
|
-
})
|
|
304
|
-
|
|
305
|
-
test("it should convert a 3D cartesian vector to spherical coordinates", () => {
|
|
306
|
-
const vector = v(1, 1, 1)
|
|
307
|
-
const expectedResult = v(sqrt(3), Math.acos(1 / sqrt(3)), pi() / 4)
|
|
308
|
-
|
|
309
|
-
expect(toSpherical(vector)).toStrictEqual(expectedResult)
|
|
310
|
-
})
|
|
311
|
-
|
|
312
|
-
test("it should create a string representation of an integer vector", () => {
|
|
313
|
-
const vector = v(3, 4)
|
|
314
|
-
const expectedResult = "[3, 4]"
|
|
315
|
-
|
|
316
|
-
expect(toString(vector)).toBe(expectedResult)
|
|
317
|
-
})
|
|
318
|
-
|
|
319
|
-
test("it should create a string representation of a float vector", () => {
|
|
320
|
-
const vector = v(pi() / 4, pi() / 4)
|
|
321
|
-
const decimals = 2
|
|
322
|
-
const expectedResult = "[0.79, 0.79]"
|
|
323
|
-
|
|
324
|
-
expect(toString(vector, decimals)).toBe(expectedResult)
|
|
325
|
-
})
|
|
326
|
-
|
|
327
|
-
test("it should create a unit vector oriented on the X-axis", () => {
|
|
328
|
-
expect(unit()).toStrictEqual(v(1, 0, 0))
|
|
329
|
-
})
|
|
330
|
-
|
|
331
|
-
test("it should create a unit vector oriented on the Z-axis", () => {
|
|
332
|
-
expect(unit(pi() / 2)).toStrictEqual(v(6.123233995736766e-17, 0, 1)) // close to v(0, 0, 1)
|
|
333
|
-
})
|
|
334
|
-
|
|
335
|
-
test("it should create a vector from a list of coordinates", () => {
|
|
336
|
-
expect(v(1, 2, 3)).toStrictEqual([1, 2, 3])
|
|
337
|
-
expect(v()).toStrictEqual([])
|
|
338
|
-
expect(v(0, -5.5)).toStrictEqual([0, -5.5])
|
|
339
|
-
})
|
|
340
|
-
|
|
341
|
-
test("it should create a 3D zero vector", () => {
|
|
342
|
-
const expectedResult = v(0, 0, 0)
|
|
343
|
-
|
|
344
|
-
expect(zero()).toStrictEqual(expectedResult)
|
|
345
|
-
})
|