-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpolygon_test.go
105 lines (96 loc) · 4.31 KB
/
polygon_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
package collision2d_test
import (
"github.com/Tarliton/collision2d"
"github.com/stretchr/testify/assert"
"math"
"testing"
)
func TestPolygonString(t *testing.T) {
polygonCorners := [...]float64{
0, 0,
10, 0,
10, 10,
0, 10,
}
polygon := collision2d.NewPolygon(collision2d.NewVector(5, 5), collision2d.NewVector(0, 0), 0, polygonCorners[:])
output := string(polygon.String())
assert.Equal(t, "{Pos:{X:5.000000, Y:5.000000}\nOffset:{X:0.000000, Y:0.000000}\nAngle: 0.000000\nPoints: [{X:0.000000, Y:0.000000}\n {X:10.000000, Y:0.000000}\n {X:10.000000, Y:10.000000}\n {X:0.000000, Y:10.000000}\n]\nCalcPoints: [{X:0.000000, Y:0.000000}\n {X:10.000000, Y:0.000000}\n {X:10.000000, Y:10.000000}\n {X:0.000000, Y:10.000000}\n]}", output, "they should be equal")
}
func TestPolygonNewPolygon(t *testing.T) {
polygonCorners := [...]float64{
0, 0,
10, 0,
10, 10,
0, 10,
}
polygon := collision2d.NewPolygon(collision2d.NewVector(5, 4), collision2d.NewVector(5, 5), math.Pi/2, polygonCorners[:])
assert.Equal(t, float64(5), polygon.Pos.X, "they should be equal")
assert.Equal(t, float64(4), polygon.Pos.Y, "they should be equal")
assert.Equal(t, float64(math.Pi/2), polygon.Angle, "they should be equal")
}
func TestPolygonSetAngle(t *testing.T) {
polygonCorners := [...]float64{
0, 0,
10, 0,
10, 10,
0, 10,
}
polygon := collision2d.NewPolygon(collision2d.NewVector(5, 5), collision2d.NewVector(0, 0), 0, polygonCorners[:])
polygon = polygon.SetAngle(math.Pi / 2)
assert.Equal(t, math.Pi/2, polygon.Angle, "they should be equal")
}
func TestPolygonSetOffset(t *testing.T) {
polygonCorners := [...]float64{
0, 0,
10, 0,
10, 10,
0, 10,
}
polygon := collision2d.NewPolygon(collision2d.NewVector(5, 5), collision2d.NewVector(0, 0), 0, polygonCorners[:])
polygon = polygon.SetOffset(collision2d.NewVector(50, 50))
assert.Equal(t, collision2d.NewVector(50, 50), polygon.Offset, "they should be equal")
}
func TestPolygonRotate(t *testing.T) {
polygonCorners := [...]float64{
0, 0,
10, 0,
10, 10,
0, 10,
}
polygon := collision2d.NewPolygon(collision2d.NewVector(5, 5), collision2d.NewVector(0, 0), 0, polygonCorners[:])
polygon = polygon.Rotate(math.Pi)
assert.Equal(t, []collision2d.Vector{collision2d.Vector{X: -0, Y: 0}, collision2d.Vector{X: -10, Y: 1.2246467991473515e-15}, collision2d.Vector{X: -10.000000000000002, Y: -9.999999999999998}, collision2d.Vector{X: -1.2246467991473515e-15, Y: -10}}, polygon.Points, "they should be equal")
}
func TestPolygonTranslate(t *testing.T) {
polygonCorners := [...]float64{
0, 0,
10, 0,
10, 10,
0, 10,
}
polygon := collision2d.NewPolygon(collision2d.NewVector(5, 5), collision2d.NewVector(0, 0), 0, polygonCorners[:])
polygon = polygon.Translate(5.5, 9.2)
assert.Equal(t, []collision2d.Vector{collision2d.Vector{X: 5.5, Y: 9.2}, collision2d.Vector{X: 15.5, Y: 9.2}, collision2d.Vector{X: 15.5, Y: 19.2}, collision2d.Vector{X: 5.5, Y: 19.2}}, polygon.Points, "they should be equal")
}
func TestPolygonGetAABBOne(t *testing.T) {
polygonCorners := [...]float64{
0, 0,
10, 0,
10, 10,
0, 10,
}
polygon := collision2d.NewPolygon(collision2d.NewVector(5, 5), collision2d.NewVector(0, 0), 0, polygonCorners[:])
polygonAABB := polygon.GetAABB()
assert.Equal(t, polygon, polygonAABB, "they should be equal")
}
func TestPolygonGetAABBTwo(t *testing.T) {
polygonCorners := [...]float64{
10, 10,
0, 10,
0, 0,
10, 0,
}
polygon := collision2d.NewPolygon(collision2d.NewVector(5, 5), collision2d.NewVector(0, 0), 0, polygonCorners[:])
polygonAABB := polygon.GetAABB()
assert.Equal(t, collision2d.Polygon{Pos: collision2d.Vector{X: 5, Y: 5}, Offset: collision2d.Vector{X: 0, Y: 0}, Angle: 0, Points: []collision2d.Vector{collision2d.Vector{X: 0, Y: 0}, collision2d.Vector{X: 10, Y: 0}, collision2d.Vector{X: 10, Y: 10}, collision2d.Vector{X: 0, Y: 10}}, CalcPoints: []collision2d.Vector{collision2d.Vector{X: 0, Y: 0}, collision2d.Vector{X: 10, Y: 0}, collision2d.Vector{X: 10, Y: 10}, collision2d.Vector{X: 0, Y: 10}}, Edges: []collision2d.Vector{collision2d.Vector{X: 10, Y: 0}, collision2d.Vector{X: 0, Y: 10}, collision2d.Vector{X: -10, Y: 0}, collision2d.Vector{X: 0, Y: -10}}, Normals: []collision2d.Vector{collision2d.Vector{X: 0, Y: -1}, collision2d.Vector{X: 1, Y: -0}, collision2d.Vector{X: 0, Y: 1}, collision2d.Vector{X: -1, Y: -0}}}, polygonAABB, "they should be equal")
}