-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmathgl_test.go
More file actions
155 lines (143 loc) · 4.01 KB
/
mathgl_test.go
File metadata and controls
155 lines (143 loc) · 4.01 KB
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
package mathgl
import (
"fmt"
"testing"
)
type squareTest struct {
in, out float32
}
var squareTests = []squareTest{
squareTest{1.5, 1.224744871},
squareTest{5.5, 2.34520788},
squareTest{10.25, 3.201562119},
}
// We test if the sqrt function has more then 1% error
func TestFsqrt32(t *testing.T) {
var error, result float32
for _, st := range squareTests {
result = Fsqrt32(st.in)
error = (Fmax32(st.out, result) / Fmin32(st.out, result)) - 1
if error > 0.01 {
message := fmt.Sprintf("The error is too big: srqrt(%f) with error %f\n", st.in, error)
t.Errorf(message)
}
}
}
func TestFsincos32(t *testing.T) {
cos := Fcos32(Fdeg2rad32(450))
if !FalmostEqual32(cos, 0) {
message := fmt.Sprintf("cos(450.0) is not 0, it is %e)\n", cos)
t.Errorf(message)
}
cos = Fcos32(Fdeg2rad32(180))
if !FalmostEqual32(cos, -1) {
message := fmt.Sprintf("cos(180.0) is not -1, it is %e)\n", cos)
t.Errorf(message)
}
cos = Fcos32(Fdeg2rad32(45))
if !FalmostEqual32(cos, 0.7071) {
message := fmt.Sprintf("cos(180.0) is not -1, it is %e)\n", cos)
t.Errorf(message)
}
sin := Fsin32(Fdeg2rad32(540))
if !FalmostEqual32(sin, 0) {
message := fmt.Sprintf("sin(540.0) is not 0, it is %e)\n", sin)
t.Errorf(message)
}
sin = Fsin32(Fdeg2rad32(45))
if !FalmostEqual32(sin, 0.7071) {
message := fmt.Sprintf("sin(540.0) is not 0, it is %e)\n", sin)
t.Errorf(message)
}
}
func TestVec2(t *testing.T) {
v2 := new(Vec2)
if v2.X != 0 || v2.Y != 0 {
t.Errorf("Initialized Vec2 is not zero\n")
}
v2.Fill(4.0, 3.0)
length := v2.Length()
if length != 5.0 {
message := fmt.Sprintf("Length of Vec2(4.0,3.0) should be 5.0 but is %f\n", length)
t.Errorf(message)
}
v2.Normalize()
length = v2.Length()
if length != 1.0 {
message := fmt.Sprintf("Length of Vec2(%f,%f) should be 1.0 but is %f\n", v2.X, v2.Y, length)
t.Errorf(message)
}
v2.Add(v2)
if v2.X != 1.6 || v2.Y != 1.2 {
message := fmt.Sprintf("Vec2(%f,%f) should be Vec2(1.6,1.2) after add function\n", v2.X, v2.Y)
t.Errorf(message)
}
dot := v2.Dot(v2)
if dot != 4.0 {
message := fmt.Sprintf("Dot product of Vec2(%f,%f) should be 4.0 but is %f \n", v2.X, v2.Y, dot)
t.Errorf(message)
}
sub := new(Vec2)
sub.Fill(0.6, 0.2)
v2.Subtract(sub)
if v2.X != 1.0 || v2.Y != 1.0 {
message := fmt.Sprintf("Vector should be Vec(1.0,1.0) but is Vec2(%f,%f) after subtraction\n", v2.X, v2.Y)
t.Errorf(message)
}
var identity Mat3
identity.Identity()
v2.Transform(&identity)
if v2.X != 1.0 || v2.Y != 1.0 {
message := fmt.Sprintf("Vector should be Vec(1.0,1.0) but is Vec2(%f,%f) after transformation with identity matrix\n", v2.X, v2.Y)
t.Errorf(message)
}
v2.Scale(5.0)
if v2.X != 5.0 || v2.Y != 5.0 {
message := fmt.Sprintf("Vector should be Vec(5.0,5.0) but is Vec2(%f,%f) after scale with scalar 5.0\n", v2.X, v2.Y)
t.Errorf(message)
}
if !v2.AreEqual(v2) {
message := fmt.Sprintf("Vector is not equal with himelf. We screwed up badly!", v2.X, v2.Y)
t.Errorf(message)
}
}
func TestMat3(t *testing.T) {
m := Mat3{5.0, 8.0, 1.0, 2.0, 9.0, 3.0, 4.0, 7.0, 4.0}
n := m
det := m.Determinant()
if det != 85 {
t.Errorf("Determinant is not 85! It is %f", det)
}
if !m.Inverse() {
t.Errorf("Determinant was 0! Can't calculate inverse!")
}
m.Multiply(&n)
if !m.IsIdentity() {
t.Errorf("The Mat3 matrix is not a identity matrix after multiplying itself with its inverse.")
}
}
func TestMat4(t *testing.T) {
var m Mat4
m.Identity()
det := m.Determinant()
if det != 1 {
t.Errorf("Determinant of identity matrix is not 1! It is %f", det)
}
m = Mat4{1.0, 6.0, 2.0, 2.0, 8.0, 4.0, 2.0, 9.0, 7.0, 2.0, 4.0, 1.0, 10.0, 9.0, 5.0, 5.0}
n := m
if !m.Inverse() {
t.Errorf("Determinant was 0! Can't calculate inverse!")
}
m.Multiply(&n)
if !m.IsIdentity() {
t.Errorf("The Mat4 matrix is not a identity matrix after multiplying itself with its inverse.")
}
}
func TestPolyClipping(t *testing.T) {
var p Poly
p = append(p, Vec2{3, 0})
p = append(p, Vec2{1, 2})
p = append(p, Vec2{3, 5})
p = append(p, Vec2{6, 0})
p.Clip(&Seg2{Vec2{2,0}, Vec2{2,10}})
}