This repository has been archived by the owner on Sep 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v2d.test.ts
161 lines (144 loc) · 6.28 KB
/
v2d.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { assert } from 'chai'
import V2D from '../src/definitions/V2D'
describe('V2D', function () {
describe('Basis tests', function () {
it('Should return the same values as it is initiated with', function () {
assert.equal(new V2D(3, 5).x(), 3)
assert.equal(new V2D(3, 5).y(), 5)
assert.equal(new V2D(-1, 5).x(), -1)
assert.equal(new V2D(3, -2).y(), -2)
assert.equal(new V2D(-20, -23).x(), -20)
assert.equal(new V2D(-20, -23).y(), -23)
})
it('Should be able to tell if vectors are equal or not', function () {
assert.isTrue(new V2D(3, 5).equals(new V2D(3, 5)))
assert.isTrue(new V2D(-3, 5).equals(new V2D(-3, 5)))
assert.isTrue(new V2D(3, -5).equals(new V2D(3, -5)))
assert.isTrue(new V2D(-3, -5).equals(new V2D(-3, -5)))
assert.isFalse(new V2D(3, 5).equals(new V2D(2, 5)))
assert.isFalse(new V2D(3, 5).equals(new V2D(3, 2)))
assert.isFalse(new V2D(3, 5).equals(new V2D(3, -5)))
assert.isFalse(new V2D(-3, -5).equals(new V2D(3, 5)))
})
})
describe('Arithmetic tests', function () {
it('Should add', function () {
assert.isTrue(
new V2D(3, 5).add(new V2D(1, 3)).equals(new V2D(4, 8)),
)
assert.isTrue(
new V2D(3, 5).add(new V2D(-1, -3)).equals(new V2D(2, 2)),
)
assert.isTrue(
new V2D(1, 1).add(new V2D(3, 4)).equals(new V2D(4, 5)),
)
assert.isTrue(
new V2D(-3, -5).add(new V2D(1, 3)).equals(new V2D(-2, -2)),
)
assert.isTrue(
new V2D(3345, 5231)
.add(new V2D(1, 3))
.equals(new V2D(3346, 5234)),
)
})
it('Should subtract', function () {
assert.isTrue(
new V2D(3, 5).sub(new V2D(1, 3)).equals(new V2D(2, 2)),
)
assert.isTrue(
new V2D(3, 5).sub(new V2D(-1, -3)).equals(new V2D(4, 8)),
)
assert.isTrue(
new V2D(1, 1).sub(new V2D(3, 4)).equals(new V2D(-2, -3)),
)
assert.isTrue(
new V2D(-3, -5).sub(new V2D(1, 3)).equals(new V2D(-4, -8)),
)
assert.isTrue(
new V2D(3345, 5231)
.sub(new V2D(1, 3))
.equals(new V2D(3344, 5228)),
)
})
it('Should scale', function () {
assert.isTrue(new V2D(3, 5).sca(1).equals(new V2D(3, 5)))
assert.isTrue(new V2D(3, 5).sca(2).equals(new V2D(6, 10)))
assert.isTrue(new V2D(1, 1).sca(4).equals(new V2D(4, 4)))
assert.isTrue(new V2D(-3, -5).sca(-3).equals(new V2D(9, 15)))
assert.isTrue(
new V2D(1111, 3333).sca(3).equals(new V2D(3333, 9999)),
)
})
it('Should dot', function () {
assert.equal(new V2D(3, 5).dot(new V2D(5, -3)), 0)
assert.equal(new V2D(3, 5).dot(new V2D(5, 3)), 30)
assert.equal(new V2D(1, 2).dot(new V2D(1, 2)), 5)
assert.equal(new V2D(2, 1).dot(new V2D(1, -2)), 0)
})
it('Should abs', function () {
assert.isTrue(new V2D(3, 5).abs().equals(new V2D(3, 5)))
assert.isTrue(new V2D(-3, 5).abs().equals(new V2D(3, 5)))
assert.isTrue(new V2D(3, -5).abs().equals(new V2D(3, 5)))
assert.isTrue(new V2D(-3, -5).abs().equals(new V2D(3, 5)))
})
})
describe('Vector functions', function () {
it('Should fetch length', function () {
assert.equal(new V2D(3, 4).length(), 5)
assert.equal(new V2D(1, 2).length(), Math.sqrt(5))
assert.equal(new V2D(5, -12).length(), 13)
assert.equal(new V2D(-5, 12).length(), 13)
assert.equal(new V2D(-5, -12).length(), 13)
})
it('Should invert the vector', function () {
assert.isTrue(new V2D(3, 5).invert().equals(new V2D(5, 3)))
assert.isTrue(new V2D(1, 2).invert().equals(new V2D(2, 1)))
assert.isTrue(new V2D(-3, 5).invert().equals(new V2D(5, -3)))
assert.isTrue(new V2D(3, -5).invert().equals(new V2D(-5, 3)))
})
it('Should round', function () {
assert.isTrue(new V2D(3, 5).round(2).equals(new V2D(4, 6)))
assert.isTrue(new V2D(3, 5).round(3).equals(new V2D(3, 6)))
assert.isTrue(new V2D(3, 5).round(5).equals(new V2D(5, 5)))
assert.isTrue(new V2D(3, 5).round(10).equals(new V2D(0, 10)))
})
it('Should get middle', function () {
assert.isTrue(
new V2D(3, 5).middle(new V2D(5, 3)).equals(new V2D(4, 4)),
)
assert.isTrue(
new V2D(1, 5).middle(new V2D(5, 1)).equals(new V2D(3, 3)),
)
assert.isTrue(
new V2D(3, 5).middle(new V2D(3, 7)).equals(new V2D(3, 6)),
)
assert.isTrue(
new V2D(1, 5).middle(new V2D(2, 5)).equals(new V2D(1.5, 5)),
)
})
it('Should get tangent', function () {
assert.equal(new V2D(1, 2).dydx(), 2)
assert.equal(new V2D(2, 6).dydx(), 3)
assert.equal(new V2D(2, 1).dydx(), 0.5)
assert.equal(new V2D(2, 10).dydx(), 5)
assert.equal(new V2D(8, 2).dydx(), 0.25)
assert.equal(new V2D(-2, 10).dydx(), -5)
assert.equal(new V2D(8, -2).dydx(), -0.25)
assert.equal(new V2D(0, 1).dydx(), Infinity)
})
it('Should get x sign', function () {
assert.equal(new V2D(1, 2).xSign(), 1)
assert.equal(new V2D(0, -1).xSign(), 0)
assert.equal(new V2D(-1, -123214).xSign(), -1)
assert.equal(new V2D(102, 23123).xSign(), 1)
assert.equal(new V2D(-1213123, 0).xSign(), -1)
})
it('Should get y sign', function () {
assert.equal(new V2D(1, 2).ySign(), 1)
assert.equal(new V2D(0, -1).ySign(), -1)
assert.equal(new V2D(-1, -123214).ySign(), -1)
assert.equal(new V2D(102, 23123).ySign(), 1)
assert.equal(new V2D(-1213123, 0).ySign(), 0)
})
})
})