-
Notifications
You must be signed in to change notification settings - Fork 2
/
vector_test.go
97 lines (72 loc) · 1.91 KB
/
vector_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
package cirno_test
import (
"fmt"
"math"
"testing"
"github.com/stretchr/testify/assert"
"github.com/zergon321/cirno"
)
func TestVectorAdd(t *testing.T) {
v1 := cirno.NewVector(2, -34)
v2 := cirno.NewVector(-5, 12)
res := cirno.NewVector(-3, -22)
assert.Equal(t, v1.Add(v2), res)
}
func TestVectorSubtract(t *testing.T) {
v1 := cirno.NewVector(2, -34)
v2 := cirno.NewVector(-5, 12)
res := cirno.NewVector(7, -46)
assert.Equal(t, v1.Subtract(v2), res)
}
func TestVectorMagnitude(t *testing.T) {
v := cirno.NewVector(1, -2)
assert.Equal(t, v.Magnitude(), math.Sqrt(5))
}
func TestVectorRotate(t *testing.T) {
v := cirno.NewVector(2, 0)
res := cirno.NewVector(0, 2)
assert.True(t, v.Rotate(90).X-res.X < 0.00000000000001)
assert.True(t, v.Rotate(90).Y-res.Y < 0.00000000000001)
}
func TestVectorProject(t *testing.T) {
v := cirno.NewVector(2, 6)
axis := cirno.NewVector(2, 2)
res := cirno.NewVector(4, 4)
assert.Equal(t, v.Project(axis), res)
}
func TestVectorNormalize(t *testing.T) {
v := cirno.NewVector(22, -14)
n, err := v.Normalize()
assert.Nil(t, err)
angle, err := cirno.Angle(v, n)
assert.Nil(t, err)
assert.Equal(t, math.Ceil(n.Magnitude()), 1.0)
assert.Equal(t, angle, 0.0)
}
func TestVectorDotProduct(t *testing.T) {
v1 := cirno.NewVector(2, 6)
v2 := cirno.NewVector(2, 2)
res := 16.0
assert.Equal(t, cirno.Dot(v1, v2), res)
}
func TestVectorAngle(t *testing.T) {
v1 := cirno.NewVector(2, 0)
v2 := cirno.NewVector(0, 2)
res := 90.0
angle, err := cirno.Angle(v1, v2)
assert.Nil(t, err)
assert.Equal(t, angle, res)
}
func TestVectorPerpendicular(t *testing.T) {
v1 := cirno.NewVector(2, 3)
vp1 := v1.PerpendicularClockwise()
vp2 := v1.PerpendicularCounterClockwise()
fmt.Println(vp1)
fmt.Println(vp2)
angle1, err := cirno.Angle(v1, vp1)
assert.Nil(t, err)
angle2, err := cirno.Angle(v1, vp1)
assert.Nil(t, err)
assert.Equal(t, angle1, 90.0)
assert.Equal(t, angle2, 90.0)
}