-
Notifications
You must be signed in to change notification settings - Fork 2
/
collision_test.go
252 lines (232 loc) · 8.43 KB
/
collision_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
242
243
244
245
246
247
248
249
250
251
252
package cirno_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/zergon321/cirno"
)
func TestRectanglesCollision(t *testing.T) {
r1, err := cirno.NewRectangle(cirno.NewVector(2, 2), 2, 2, 0)
assert.Nil(t, err)
r2, err := cirno.NewRectangle(cirno.NewVector(6, 3), 4, 4, 0)
assert.Nil(t, err)
r3, err := cirno.NewRectangle(cirno.NewVector(3, 2), 2, 2, 0)
assert.Nil(t, err)
r4, err := cirno.NewRectangle(cirno.NewVector(3, 6), 4, 4, 0)
assert.Nil(t, err)
r5, err := cirno.NewRectangle(cirno.NewVector(3, 2), 2, 2, 45)
assert.Nil(t, err)
r6, err := cirno.NewRectangle(cirno.NewVector(3, 6), 4, 4, 0)
assert.Nil(t, err)
r7, err := cirno.NewRectangle(cirno.NewVector(5, 3), 2, 2, 0)
assert.Nil(t, err)
r8, err := cirno.NewRectangle(cirno.NewVector(4, 4), 6, 6, 0)
assert.Nil(t, err)
r9, err := cirno.NewRectangle(cirno.NewVector(5, 3), 2, 2, 0)
assert.Nil(t, err)
r10, err := cirno.NewRectangle(cirno.NewVector(3, 3), 4, 4, 0)
assert.Nil(t, err)
r11, err := cirno.NewRectangle(cirno.NewVector(4, 2), 2, 2, 0)
assert.Nil(t, err)
r12, err := cirno.NewRectangle(cirno.NewVector(3, 5), 4, 2, 85)
assert.Nil(t, err)
result0, err := cirno.CollisionRectangleToRectangle(r1, r2)
assert.Nil(t, err)
result1, err := cirno.CollisionRectangleToRectangle(r3, r4)
assert.Nil(t, err)
result2, err := cirno.CollisionRectangleToRectangle(r5, r6)
assert.Nil(t, err)
result3, err := cirno.CollisionRectangleToRectangle(r7, r8)
assert.Nil(t, err)
result4, err := cirno.CollisionRectangleToRectangle(r9, r10)
assert.Nil(t, err)
result5, err := cirno.CollisionRectangleToRectangle(r11, r12)
assert.Nil(t, err)
assert.False(t, result0)
assert.False(t, result1)
assert.False(t, result2)
assert.True(t, result3)
assert.True(t, result4)
assert.True(t, result5)
}
func TestRectangleToCircleCollision(t *testing.T) {
r1, err := cirno.NewRectangle(cirno.NewVector(3, 2), 4, 2, 45)
assert.Nil(t, err)
c1, err := cirno.NewCircle(cirno.NewVector(6, 5), 1.0)
assert.Nil(t, err)
r2, err := cirno.NewRectangle(cirno.NewVector(3, 2), 4, 2, 85)
assert.Nil(t, err)
c2, err := cirno.NewCircle(cirno.NewVector(3, 4.5), 1.0)
assert.Nil(t, err)
r3, err := cirno.NewRectangle(cirno.NewVector(3, 2), 4, 2, 85)
assert.Nil(t, err)
c3, err := cirno.NewCircle(cirno.NewVector(3, 2), 0.5)
assert.Nil(t, err)
r4, err := cirno.NewRectangle(cirno.NewVector(3, 2), 4, 2, 85)
assert.Nil(t, err)
c4, err := cirno.NewCircle(cirno.NewVector(3, 2), 3)
assert.Nil(t, err)
res0, err := cirno.CollisionRectangleToCircle(r1, c1)
assert.Nil(t, err)
res1, err := cirno.CollisionRectangleToCircle(r2, c2)
assert.Nil(t, err)
res2, err := cirno.CollisionRectangleToCircle(r3, c3)
assert.Nil(t, err)
res3, err := cirno.CollisionRectangleToCircle(r4, c4)
assert.Nil(t, err)
assert.False(t, res0)
assert.True(t, res1)
assert.True(t, res2)
assert.True(t, res3)
}
func TestLinesIntersection(t *testing.T) {
l1, err := cirno.NewLine(cirno.NewVector(1, 2), cirno.NewVector(5, 5))
assert.Nil(t, err)
l2, err := cirno.NewLine(cirno.NewVector(4, 3), cirno.NewVector(6, 1))
assert.Nil(t, err)
l3, err := cirno.NewLine(cirno.NewVector(1, 1), cirno.NewVector(4, 4))
assert.Nil(t, err)
l4, err := cirno.NewLine(cirno.NewVector(2, 3), cirno.NewVector(4, 1))
assert.Nil(t, err)
l5, err := cirno.NewLine(cirno.NewVector(1, 1), cirno.NewVector(3, 3))
assert.Nil(t, err)
l6, err := cirno.NewLine(cirno.NewVector(4, 4), cirno.NewVector(6, 6))
assert.Nil(t, err)
l7, err := cirno.NewLine(cirno.NewVector(1, 1), cirno.NewVector(3, 3))
assert.Nil(t, err)
l8, err := cirno.NewLine(cirno.NewVector(2, 2), cirno.NewVector(4, 4))
assert.Nil(t, err)
l9, err := cirno.NewLine(cirno.NewVector(1, 1), cirno.NewVector(4, 4))
assert.Nil(t, err)
l10, err := cirno.NewLine(cirno.NewVector(2, 2), cirno.NewVector(3, 3))
assert.Nil(t, err)
l11, err := cirno.NewLine(cirno.NewVector(5, 1), cirno.NewVector(5, 4))
assert.Nil(t, err)
l12, err := cirno.NewLine(cirno.NewVector(5, 2), cirno.NewVector(5, 3))
assert.Nil(t, err)
l13, err := cirno.NewLine(cirno.NewVector(5, 4), cirno.NewVector(8, 8))
assert.Nil(t, err)
l14, err := cirno.NewLine(cirno.NewVector(2, 5), cirno.NewVector(7, 1))
assert.Nil(t, err)
l15, err := cirno.NewLine(cirno.NewVector(3, 3), cirno.NewVector(4, 3))
assert.Nil(t, err)
l16, err := cirno.NewLine(cirno.NewVector(3, 4), cirno.NewVector(4, 4))
assert.Nil(t, err)
l17, err := cirno.NewLine(cirno.NewVector(3, 3), cirno.NewVector(4, 4))
assert.Nil(t, err)
res0, err := cirno.IntersectionLineToLine(l1, l2)
assert.Nil(t, err)
res1, err := cirno.IntersectionLineToLine(l3, l4)
assert.Nil(t, err)
res2, err := cirno.IntersectionLineToLine(l5, l6)
assert.Nil(t, err)
res3, err := cirno.IntersectionLineToLine(l9, l10)
assert.Nil(t, err)
res4, err := cirno.IntersectionLineToLine(l7, l8)
assert.Nil(t, err)
res5, err := cirno.IntersectionLineToLine(l11, l12)
assert.Nil(t, err)
res6, err := cirno.IntersectionLineToLine(l13, l14)
assert.Nil(t, err)
res7, err := cirno.IntersectionLineToLine(l3, l15)
assert.Nil(t, err)
res8, err := cirno.IntersectionLineToLine(l3, l16)
assert.Nil(t, err)
res9, err := cirno.IntersectionLineToLine(l3, l3)
assert.Nil(t, err)
res10, err := cirno.IntersectionLineToLine(l5, l17)
assert.Nil(t, err)
assert.False(t, res0)
assert.True(t, res1)
assert.False(t, res2)
assert.True(t, res3)
assert.True(t, res4)
assert.True(t, res5)
assert.False(t, res6)
assert.True(t, res7)
assert.True(t, res8)
assert.True(t, res9)
assert.True(t, res10)
}
func TestLineCircleIntersection(t *testing.T) {
l1, err := cirno.NewLine(cirno.NewVector(1, 1), cirno.NewVector(5, 6))
assert.Nil(t, err)
c1, err := cirno.NewCircle(cirno.NewVector(3, 4), 2)
assert.Nil(t, err)
l2, err := cirno.NewLine(cirno.NewVector(1, 1), cirno.NewVector(5, 2))
assert.Nil(t, err)
c2, err := cirno.NewCircle(cirno.NewVector(3, 4), 2)
assert.Nil(t, err)
l3, err := cirno.NewLine(cirno.NewVector(1, 1), cirno.NewVector(1, 6))
assert.Nil(t, err)
c3, err := cirno.NewCircle(cirno.NewVector(3, 4), 2)
assert.Nil(t, err)
l4, err := cirno.NewLine(cirno.NewVector(1, 4), cirno.NewVector(5, 4))
assert.Nil(t, err)
c4, err := cirno.NewCircle(cirno.NewVector(6, 4), 3)
assert.Nil(t, err)
res0, err := cirno.IntersectionLineToCircle(l1, c1)
assert.Nil(t, err)
res1, err := cirno.IntersectionLineToCircle(l2, c2)
assert.Nil(t, err)
res2, err := cirno.IntersectionLineToCircle(l3, c3)
assert.Nil(t, err)
res3, err := cirno.IntersectionLineToCircle(l4, c4)
assert.Nil(t, err)
assert.True(t, res0)
assert.False(t, res1)
assert.True(t, res2)
assert.True(t, res3)
}
func TestLineRectangleIntersection(t *testing.T) {
l1, err := cirno.NewLine(cirno.NewVector(4, 1), cirno.NewVector(7, 4))
assert.Nil(t, err)
r1, err := cirno.NewRectangle(cirno.NewVector(3, 4), 4, 2, 0.0)
assert.Nil(t, err)
l2, err := cirno.NewLine(cirno.NewVector(4, 1), cirno.NewVector(7, 4))
assert.Nil(t, err)
r2, err := cirno.NewRectangle(cirno.NewVector(6, 2), 2, 2, 0.0)
assert.Nil(t, err)
l3, err := cirno.NewLine(cirno.NewVector(4, 1), cirno.NewVector(7, 4))
assert.Nil(t, err)
r3, err := cirno.NewRectangle(cirno.NewVector(9, 4), 6, 2, 30.0)
assert.Nil(t, err)
l4, err := cirno.NewLine(cirno.NewVector(2, 2), cirno.NewVector(4, 6))
assert.Nil(t, err)
r4, err := cirno.NewRectangle(cirno.NewVector(4, 4), 6, 6, 0.0)
assert.Nil(t, err)
l5, err := cirno.NewLine(cirno.NewVector(480, 240), cirno.NewVector(720, 240))
assert.Nil(t, err)
r5, err := cirno.NewRectangle(cirno.NewVector(600, 228.2171355), 150, 50, 0.0)
assert.Nil(t, err)
res0, err := cirno.IntersectionLineToRectangle(l1, r1)
assert.Nil(t, err)
res1, err := cirno.IntersectionLineToRectangle(l2, r2)
assert.Nil(t, err)
res2, err := cirno.IntersectionLineToRectangle(l3, r3)
assert.Nil(t, err)
res3, err := cirno.IntersectionLineToRectangle(l4, r4)
assert.Nil(t, err)
res4, err := cirno.IntersectionLineToRectangle(l5, r5)
assert.Nil(t, err)
assert.False(t, res0)
assert.True(t, res1)
assert.True(t, res2)
assert.True(t, res3)
assert.True(t, res4)
}
func TestCirclesCollision(t *testing.T) {
c1, err := cirno.NewCircle(cirno.NewVector(6, 3), 1)
assert.Nil(t, err)
c2, err := cirno.NewCircle(cirno.NewVector(3, 4), 2)
assert.Nil(t, err)
c3, err := cirno.NewCircle(cirno.NewVector(6, 3), 2)
assert.Nil(t, err)
c4, err := cirno.NewCircle(cirno.NewVector(3, 4), 2)
assert.Nil(t, err)
res0, err := cirno.CollisionCircleToCircle(c1, c2)
assert.Nil(t, err)
res1, err := cirno.CollisionCircleToCircle(c3, c4)
assert.Nil(t, err)
assert.False(t, res0)
assert.True(t, res1)
}