-
Notifications
You must be signed in to change notification settings - Fork 1
/
vec2f_test.go
241 lines (192 loc) · 7.47 KB
/
vec2f_test.go
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package vmath
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestVec2f_String(t *testing.T) {
assert.Equal(t, "Vec2f[5.000000 x -3.100000]", Vec2f{5, -3.1}.String())
}
func TestVec2f_Format(t *testing.T) {
assert.Equal(t, "vec 5, -3", Vec2f{5, -3.1}.Format("vec %.0f, %.0f"))
}
func TestVec2f_IsOrthogonal(t *testing.T) {
assert.True(t, Vec2f{2, 0}.IsOrthogonal())
assert.True(t, Vec2f{-2, 0}.IsOrthogonal())
assert.True(t, Vec2f{0, 32}.IsOrthogonal())
assert.True(t, Vec2f{0, -12}.IsOrthogonal())
assert.False(t, Vec2f{2, 2}.IsOrthogonal())
assert.False(t, Vec2f{-0.0001, 1}.IsOrthogonal())
}
func TestVec2f_Abs(t *testing.T) {
AssertVec2f(t, Vec2f{2, 9}, Vec2f{2, 9}.Abs())
AssertVec2f(t, Vec2f{2, 9}, Vec2f{-2, 9}.Abs())
AssertVec2f(t, Vec2f{2, 9}, Vec2f{2, -9}.Abs())
AssertVec2f(t, Vec2f{2, 9}, Vec2f{-2, -9}.Abs())
}
func TestVec2f_Add(t *testing.T) {
AssertVec2f(t, Vec2f{4, 6}, Vec2f{2, 9}.Add(Vec2f{2, -3}))
}
func TestVec2f_IsZero(t *testing.T) {
assert.True(t, Vec2f{0, 0}.IsZero())
assert.False(t, Vec2f{0.0001, 0}.IsZero())
assert.False(t, Vec2f{0, 0.0001}.IsZero())
}
func TestVec2f_AddScalar(t *testing.T) {
AssertVec2f(t, Vec2f{4, 11}, Vec2f{2, 9}.AddScalar(2))
AssertVec2f(t, Vec2f{1, -2}, Vec2f{4, 1}.AddScalar(-3))
}
func TestVec2f_Angle(t *testing.T) {
AssertFloat(t, 0, Vec2f{0, 0}.Angle(Vec2f{5, 0}))
AssertFloat(t, pi/2, Vec2f{1, 0}.Angle(Vec2f{0, 1}))
AssertFloat(t, pi, Vec2f{8, 0}.Angle(Vec2f{-2, 0}))
AssertFloat(t, -pi/2, Vec2f{1, 0}.Angle(Vec2f{0, -1}))
AssertFloat(t, pi, Vec2f{1, 0}.Angle(Vec2f{-4, 0}))
AssertFloat(t, pi/2, Vec2f{1, 1}.Angle(Vec2f{-4, 4}))
}
func TestVec2f_Clamp(t *testing.T) {
AssertVec2f(t, Vec2f{0, 0}, Vec2f{0, 0}.Clamp(-5, 5))
AssertVec2f(t, Vec2f{-5, -5}, Vec2f{-8, -9}.Clamp(-5, 5))
AssertVec2f(t, Vec2f{5, 5}, Vec2f{8, 12}.Clamp(-5, 5))
}
func TestVec2f_Distance(t *testing.T) {
AssertFloat(t, 2, Vec2f{0, 0}.Distance(Vec2f{2, 0}))
AssertFloat(t, 0, Vec2f{3, -4}.Distance(Vec2f{3, -4}))
AssertFloat(t, 12.165525, Vec2f{3, -4}.Distance(Vec2f{1, 8}))
}
func TestVec2f_Div(t *testing.T) {
AssertVec2f(t, Vec2f{3, -8}, Vec2f{12, 56}.Div(Vec2f{4, -7}))
}
func TestVec2f_DivScalar(t *testing.T) {
AssertVec2f(t, Vec2f{2.5, 7}, Vec2f{20, 56}.DivScalar(8))
}
func TestVec2f_Dot(t *testing.T) {
AssertFloat(t, -20, Vec2f{4, 7}.Dot(Vec2f{2, -4}))
}
func TestVec2f_Equal(t *testing.T) {
assert.True(t, Vec2f{0, 0}.Equal(Vec2f{0, 0}))
assert.True(t, Vec2f{6, 7}.Equal(Vec2f{6, 7}))
assert.True(t, Vec2f{-4, -9}.Equal(Vec2f{-4, -9}))
assert.False(t, Vec2f{6, 7}.Equal(Vec2f{7, 6}))
}
func TestVec2f_FlatAngle(t *testing.T) {
AssertFloat(t, 0, Vec2f{0, 0}.FlatAngle())
AssertFloat(t, 0, Vec2f{6, 0}.FlatAngle())
AssertFloat(t, pi/4, Vec2f{1, 1}.FlatAngle())
AssertFloat(t, pi/2, Vec2f{0, 1}.FlatAngle())
AssertFloat(t, pi, Vec2f{-5, 0}.FlatAngle())
AssertFloat(t, 3*pi/2, NormalizeRadians(Vec2f{0, -2}.FlatAngle()))
}
func TestVec2f_IsParallel(t *testing.T) {
assert.True(t, Vec2f{1, 1}.IsParallel(Vec2f{5, 5}))
assert.True(t, Vec2f{1, 1}.IsParallel(Vec2f{-1, -1}))
assert.True(t, Vec2f{1, 0}.IsParallel(Vec2f{1, 0}))
assert.False(t, Vec2f{1, 1}.IsParallel(Vec2f{-1, 2}))
}
func TestVec2f_IsParallelEps(t *testing.T) {
assert.True(t, Vec2f{1, 1}.IsParallelEps(Vec2f{5, 5}, Epsilon))
assert.True(t, Vec2f{1, 1}.IsParallelEps(Vec2f{-1, -1}, Epsilon))
assert.True(t, Vec2f{1, 0}.IsParallelEps(Vec2f{1, 0}, Epsilon))
assert.False(t, Vec2f{1, 1}.IsParallelEps(Vec2f{-1, 2}, Epsilon))
}
func TestVec2f_IsCollinear(t *testing.T) {
assert.True(t, Vec2f{1, 1}.IsCollinear(Vec2f{5, 5}))
assert.False(t, Vec2f{1, 1}.IsCollinear(Vec2f{-1, -1}))
assert.True(t, Vec2f{0, 1}.IsCollinear(Vec2f{0, 1}))
assert.False(t, Vec2f{0, 1}.IsCollinear(Vec2f{0, -1}))
assert.False(t, Vec2f{1, 1}.IsCollinear(Vec2f{-1, 2}))
}
func TestVec2f_Length(t *testing.T) {
AssertFloat(t, 5, Vec2f{5, 0}.Length())
AssertFloat(t, 5, Vec2f{0, -5}.Length())
AssertFloat(t, 2.236068, Vec2f{2, 1}.Length())
}
func TestVec2f_Lerp(t *testing.T) {
AssertVec2f(t, Vec2f{-1, 0}, Vec2f{-1, 0}.Lerp(Vec2f{1, 0}, 0))
AssertVec2f(t, Vec2f{-0.5, 0}, Vec2f{-1, 0}.Lerp(Vec2f{1, 0}, 0.25))
AssertVec2f(t, Vec2f{0, 0}, Vec2f{-1, 0}.Lerp(Vec2f{1, 0}, 0.5))
AssertVec2f(t, Vec2f{0.5, 0}, Vec2f{-1, 0}.Lerp(Vec2f{1, 0}, 0.75))
AssertVec2f(t, Vec2f{1, 0}, Vec2f{-1, 0}.Lerp(Vec2f{1, 0}, 1))
AssertVec2f(t, Vec2f{0.5, 0.5}, Vec2f{0, 1}.Lerp(Vec2f{1, 0}, 0.5))
}
func TestVec2f_MagCross(t *testing.T) {
AssertFloat(t, -1, Vec2f{0, 1}.MagCross(Vec2f{1, 0}))
AssertFloat(t, -6, Vec2f{0, 2}.MagCross(Vec2f{3, 0}))
AssertFloat(t, 47, Vec2f{5, -3}.MagCross(Vec2f{4, 7}))
}
func TestVec2f_Mul(t *testing.T) {
AssertVec2f(t, Vec2f{6, -27}, Vec2f{3, 9}.Mul(Vec2f{2, -3}))
}
func TestVec2f_MulScalar(t *testing.T) {
AssertVec2f(t, Vec2f{8, 18}, Vec2f{4, 9}.MulScalar(2))
AssertVec2f(t, Vec2f{-12, -3}, Vec2f{4, 1}.MulScalar(-3))
}
func TestVec2f_Negate(t *testing.T) {
AssertVec2f(t, Vec2f{6, -4}, Vec2f{-6, 4}.Negate())
}
func TestVec2f_NormalVec(t *testing.T) {
AssertVec2f(t, Vec2f{-1, 1}, Vec2f{1, 1}.NormalVec(true))
AssertVec2f(t, Vec2f{1, -1}, Vec2f{1, 1}.NormalVec(false))
AssertVec2f(t, Vec2f{0, -1}, Vec2f{-1, 0}.NormalVec(true))
AssertVec2f(t, Vec2f{0, 1}, Vec2f{-1, 0}.NormalVec(false))
}
func TestVec2f_Normalize(t *testing.T) {
AssertVec2f(t, Vec2f{1, 0}, Vec2f{8, 0}.Normalize())
AssertVec2f(t, Vec2f{0, 1}, Vec2f{0, 23}.Normalize())
AssertVec2f(t, Vec2f{-1, 0}, Vec2f{-15, 0}.Normalize())
AssertVec2f(t, Vec2f{0, -1}, Vec2f{0, -54}.Normalize())
AssertVec2f(t, Vec2f{0.8, 0.6}, Vec2f{4, 3}.Normalize())
AssertVec2f(t, Vec2f{0, 0}, Vec2f{0, 0}.Normalize())
}
func TestVec2f_Project(t *testing.T) {
AssertVec2f(t, Vec2f{0, 0}, Vec2f{0, 1}.Project(Vec2f{1, 0}))
AssertVec2f(t, Vec2f{1, 0}, Vec2f{1, 1}.Project(Vec2f{1, 0}))
AssertVec2f(t, Vec2f{1, 0}, Vec2f{1, 2}.Project(Vec2f{2, 0}))
AssertVec2f(t, Vec2f{0.5, 0.5}, Vec2f{0, 1}.Project(Vec2f{1, 1}))
AssertVec2f(t, Vec2f{0.5, 0.5}, Vec2f{1, 0}.Project(Vec2f{1, 1}))
}
func TestVec2f_Rotate(t *testing.T) {
AssertVec2f(t, Vec2f{0, 0}, Vec2f{0, 0}.Rotate(pi))
AssertVec2f(t, Vec2f{0, 1}, Vec2f{1, 0}.Rotate(pi/2))
AssertVec2f(t, Vec2f{-1, 0}, Vec2f{0, -1}.Rotate(-pi/2))
}
func TestVec2f_Round(t *testing.T) {
assert.Equal(t, Vec2i{-6, 7}, Vec2f{-6, 7}.Round())
assert.Equal(t, Vec2i{-5, 7}, Vec2f{-5.2, 7.2}.Round())
assert.Equal(t, Vec2i{-6, 8}, Vec2f{-5.8, 7.8}.Round())
assert.Equal(t, Vec2i{-6, 8}, Vec2f{-5.5, 7.5}.Round())
}
func TestVec2f_Split(t *testing.T) {
x, y := Vec2f{6, -9}.Split()
AssertFloat(t, 6, x)
AssertFloat(t, -9, y)
}
func TestVec2f_SquareDistance(t *testing.T) {
AssertFloat(t, 0, Vec2f{3, 4}.SquareDistance(Vec2f{3, 4}))
AssertFloat(t, 8, Vec2f{3, 4}.SquareDistance(Vec2f{1, 2}))
}
func TestVec2f_SquareLength(t *testing.T) {
AssertFloat(t, 13, Vec2f{2, -3}.SquareLength())
}
func TestVec2f_Sub(t *testing.T) {
AssertVec2f(t, Vec2f{0, 12}, Vec2f{2, 9}.Sub(Vec2f{2, -3}))
}
func TestVec2f_SubScalar(t *testing.T) {
AssertVec2f(t, Vec2f{-1, 7}, Vec2f{1, 9}.SubScalar(2))
AssertVec2f(t, Vec2f{7, 4}, Vec2f{4, 1}.SubScalar(-3))
}
func TestVec2f_Vec2i(t *testing.T) {
assert.Equal(t, Vec2i{-6, 7}, Vec2f{-6, 7}.Vec2i())
assert.Equal(t, Vec2i{-5, 7}, Vec2f{-5.8, 7.5}.Vec2i())
}
func TestVec2f_Vec3f(t *testing.T) {
AssertVec3f(t, Vec3f{1, 2, 3}, Vec2f{1, 2}.Vec3f(3))
}
func TestVec2f_Vec4f(t *testing.T) {
AssertVec4f(t, Vec4f{1, 2, 3, 4}, Vec2f{1, 2}.Vec4f(3, 4))
}
func TestVec2f_X(t *testing.T) {
AssertFloat(t, -45, Vec2f{-45, 12}.X())
}
func TestVec2f_Y(t *testing.T) {
AssertFloat(t, 12, Vec2f{-45, 12}.Y())
}