-
Notifications
You must be signed in to change notification settings - Fork 84
/
polygon_test.go
173 lines (164 loc) · 3.43 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
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
package floatgeom
import (
"math/rand"
"testing"
"time"
)
func TestPolygon_Contains(t *testing.T) {
p := NewPolygon2(
Point2{10, 10},
Point2{20, 10},
Point2{10, 20},
)
if !p.Contains(11, 11) {
t.Fatalf("polygon did not contain 11,11")
}
if p.Contains(16, 16) {
t.Fatalf("polygon contained 16,16")
}
if p.Contains(40, 40) {
t.Fatalf("polygon contained 40,40")
}
if !p.ConvexContains(11, 11) {
t.Fatalf("convex polygon did not contain 11,11")
}
if p.ConvexContains(16, 16) {
t.Fatalf("convex polygon contained 16,16")
}
if p.ConvexContains(40, 40) {
t.Fatalf("convex polygon contained 40,40")
}
}
func TestPolygon_getSide(t *testing.T) {
v := Point2{2, 2}
if getSide(v, v) != 0 {
t.Fatalf("getSide did not confirm identical points")
}
p := NewPolygon2(
Point2{0, 0},
Point2{0, 10},
Point2{10, 10},
Point2{10, 0},
)
if p.ConvexContains(0, 5) {
t.Fatalf("point in line with polygon was contained")
}
}
func TestPolygon2_RectCollides(t *testing.T) {
type testCase struct {
poly Polygon2
rect Rect2
shouldCollide bool
}
tcs := []testCase{
{
poly: NewPolygon2(
Point2{0, 0},
Point2{0, 1},
Point2{1, 1},
Point2{1, 0},
),
rect: NewRect2(0, 0, 1, 1),
shouldCollide: true,
},
{
poly: NewPolygon2(
Point2{0, 0},
Point2{0, 1},
Point2{1, 1},
),
rect: NewRect2(0, 0, 1, 1),
shouldCollide: true,
},
{
poly: NewPolygon2(
Point2{-1, -1},
Point2{-1, 2},
Point2{2, 2},
),
rect: NewRect2(0, 0, 1, 1),
shouldCollide: true,
},
{
poly: NewPolygon2(
Point2{1.1, -1},
Point2{3.1, -1},
Point2{3.1, 1},
),
rect: NewRect2(0, 0, 2, 2),
shouldCollide: false,
},
{
poly: NewPolygon2(
Point2{-1.1, -1.1},
Point2{-1.1, -1.1},
Point2{-1.1, -1.1},
),
rect: NewRect2(0, 0, 2, 2),
shouldCollide: false,
},
{
poly: NewPolygon2(
Point2{4.1, 4.1},
Point2{4.1, 4.1},
Point2{4.1, 4.1},
),
rect: NewRect2(0, 0, 2, 2),
shouldCollide: false,
},
{
poly: NewPolygon2(
Point2{1.1, 1.1},
Point2{1.1, 1.1},
Point2{1.1, 1.1},
),
rect: NewRect2(0, 0, 2, 2),
shouldCollide: true,
},
{
poly: NewPolygon2(
Point2{1.1, 1.1},
Point2{1.1, 1.1},
Point2{1.1, 1.1},
),
rect: NewRect2(0, 2, 2, 2),
shouldCollide: false,
},
}
for i, tc := range tcs {
if tc.poly.RectCollides(tc.rect) != tc.shouldCollide {
t.Errorf("test case %d failed", i)
}
}
}
var benchContains bool
func BenchmarkPolygonContains(b *testing.B) {
curSeed := time.Now().UTC().UnixNano()
rand.Seed(curSeed)
points := []Point2{}
for i := 0; i < 100; i++ {
points = append(points, Point2{rand.Float64() * 640, rand.Float64() * 480})
}
poly := NewPolygon2(points[0], points[1], points[2], points[3:]...)
b.ResetTimer()
for i := 0; i < b.N; i++ {
x := rand.Float64() * 640
y := rand.Float64() * 480
benchContains = poly.Contains(x, y)
}
}
func BenchmarkPolygonConvexContains(b *testing.B) {
curSeed := time.Now().UTC().UnixNano()
rand.Seed(curSeed)
points := []Point2{}
for i := 0; i < 100; i++ {
points = append(points, Point2{rand.Float64() * 640, rand.Float64() * 480})
}
poly := NewPolygon2(points[0], points[1], points[2], points[3:]...)
b.ResetTimer()
for i := 0; i < b.N; i++ {
x := rand.Float64() * 640
y := rand.Float64() * 480
benchContains = poly.ConvexContains(x, y)
}
}