-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
quadtree.go
356 lines (272 loc) · 7.44 KB
/
quadtree.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Quadtree for holding entities
// Can't remember where I got this from. But I modified it
// somewhat...
package main
// Quadtree - The quadtree data structure
type Quadtree struct {
Bounds Bounds
MaxObjects int // Maximum objects a node can hold before splitting into 4 subnodes
MaxLevels int // Total max levels inside root Quadtree
Level int // Depth level, required for subnodes
Objects []*Bounds
Nodes []Quadtree
Total int
}
// Bounds - A bounding box with a x,y origin and width and height
type Bounds struct {
X float64
Y float64
Width float64
Height float64
entity entity
}
//IsPoint - Checks if a bounds object is a point or not (has no width or height)
func (b *Bounds) IsPoint() bool {
if b.Width == 0 && b.Height == 0 {
return true
}
return false
}
// Intersects - Checks if a Bounds object intersects with another Bounds
func (b *Bounds) Intersects(a *Bounds) bool {
aMaxX := a.X + a.Width
aMaxY := a.Y + a.Height
bMaxX := b.X + b.Width
bMaxY := b.Y + b.Height
// a is left of b
if aMaxX < b.X {
return false
}
// a is right of b
if a.X > bMaxX {
return false
}
// a is above b
if aMaxY < b.Y {
return false
}
// a is below b
if a.Y > bMaxY {
return false
}
// The two overlap
return true
}
// TotalNodes - Retrieve the total number of sub-Quadtrees in a Quadtree
func (qt *Quadtree) TotalNodes() int {
total := 0
if len(qt.Nodes) > 0 {
for i := 0; i < len(qt.Nodes); i++ {
total++
total += qt.Nodes[i].TotalNodes()
}
}
return total
}
// split - split the node into 4 subnodes
func (qt *Quadtree) split() {
if len(qt.Nodes) == 4 {
return
}
nextLevel := qt.Level + 1
subWidth := qt.Bounds.Width / 2
subHeight := qt.Bounds.Height / 2
x := qt.Bounds.X
y := qt.Bounds.Y
//top right node (0)
qt.Nodes = append(qt.Nodes, Quadtree{
Bounds: Bounds{
X: x + subWidth,
Y: y,
Width: subWidth,
Height: subHeight,
},
MaxObjects: qt.MaxObjects,
MaxLevels: qt.MaxLevels,
Level: nextLevel,
Objects: make([]*Bounds, 0),
Nodes: make([]Quadtree, 0, 4),
})
//top left node (1)
qt.Nodes = append(qt.Nodes, Quadtree{
Bounds: Bounds{
X: x,
Y: y,
Width: subWidth,
Height: subHeight,
},
MaxObjects: qt.MaxObjects,
MaxLevels: qt.MaxLevels,
Level: nextLevel,
Objects: make([]*Bounds, 0),
Nodes: make([]Quadtree, 0, 4),
})
//bottom left node (2)
qt.Nodes = append(qt.Nodes, Quadtree{
Bounds: Bounds{
X: x,
Y: y + subHeight,
Width: subWidth,
Height: subHeight,
},
MaxObjects: qt.MaxObjects,
MaxLevels: qt.MaxLevels,
Level: nextLevel,
Objects: make([]*Bounds, 0),
Nodes: make([]Quadtree, 0, 4),
})
//bottom right node (3)
qt.Nodes = append(qt.Nodes, Quadtree{
Bounds: Bounds{
X: x + subWidth,
Y: y + subHeight,
Width: subWidth,
Height: subHeight,
},
MaxObjects: qt.MaxObjects,
MaxLevels: qt.MaxLevels,
Level: nextLevel,
Objects: make([]*Bounds, 0),
Nodes: make([]Quadtree, 0, 4),
})
}
// getIndex - Determine which quadrant the object belongs to (0-3)
func (qt *Quadtree) getIndex(pRect *Bounds) int {
index := -1 // index of the subnode (0-3), or -1 if pRect cannot completely fit within a subnode and is part of the parent node
verticalMidpoint := qt.Bounds.X + qt.Bounds.Width/2
horizontalMidpoint := qt.Bounds.Y + qt.Bounds.Height/2
//pRect can completely fit within the top quadrants
topQuadrant := pRect.Y < horizontalMidpoint && pRect.Y+pRect.Height < horizontalMidpoint
//pRect can completely fit within the bottom quadrants
bottomQuadrant := pRect.Y > horizontalMidpoint
//pRect can completely fit within the left quadrants
if pRect.X < verticalMidpoint && pRect.X+pRect.Width < verticalMidpoint {
if topQuadrant {
index = 1
} else if bottomQuadrant {
index = 2
}
} else if pRect.X > verticalMidpoint {
//pRect can completely fit within the right quadrants
if topQuadrant {
index = 0
} else if bottomQuadrant {
index = 3
}
}
return index
}
// Remove is a public method to remove an obj from the quadtree.
func (qt *Quadtree) Remove(o *Bounds) {
qt.removeObj(qt, o)
}
// Recusive removal of object from quadtree
func (qt *Quadtree) removeObj(t *Quadtree, o *Bounds) bool {
index := -1
for i := range t.Objects {
if t.Objects[i] == o {
index = i
break
}
}
if index != -1 {
t.Objects[index] = nil
t.Objects = append(t.Objects[:index], t.Objects[index+1:]...)
return true
}
for i := range t.Nodes {
if qt.removeObj(&t.Nodes[i], o) == true {
break
}
}
return false
}
// Insert - Insert the object into the node. If the node exceeds the capacity,
// it will split and add all objects to their corresponding subnodes.
func (qt *Quadtree) Insert(pRect *Bounds) {
qt.Total++
i := 0
var index int
// If we have subnodes within the Quadtree
if len(qt.Nodes) > 0 == true {
index = qt.getIndex(pRect)
if index != -1 {
qt.Nodes[index].Insert(pRect)
return
}
}
// If we don't subnodes within the Quadtree
qt.Objects = append(qt.Objects, pRect)
// If total objects is greater than max objects and level is less than max levels
if len(qt.Objects) > qt.MaxObjects && qt.Level < qt.MaxLevels {
// split if we don't already have subnodes
if len(qt.Nodes) > 0 == false {
qt.split()
}
// Add all objects to there corresponding subNodes
for i < len(qt.Objects) {
index = qt.getIndex(qt.Objects[i])
if index != -1 {
splice := qt.Objects[i] // Get the object out of the slice
qt.Objects = append(qt.Objects[:i], qt.Objects[i+1:]...) // Remove the object from the slice
qt.Nodes[index].Insert(splice)
} else {
i++
}
}
}
}
// Retrieve - Return all objects that could collide with the given object
func (qt *Quadtree) Retrieve(pRect *Bounds) []*Bounds {
index := qt.getIndex(pRect)
// Array with all detected objects
returnObjects := qt.Objects
//if we have subnodes ...
if len(qt.Nodes) > 0 {
//if pRect fits into a subnode ..
if index != -1 {
returnObjects = append(returnObjects, qt.Nodes[index].Retrieve(pRect)...)
} else {
//if pRect does not fit into a subnode, check it against all subnodes
for i := 0; i < len(qt.Nodes); i++ {
returnObjects = append(returnObjects, qt.Nodes[i].Retrieve(pRect)...)
}
}
}
return returnObjects
}
// RetrievePoints - Return all points that collide
func (qt *Quadtree) RetrievePoints(find *Bounds) []*Bounds {
var foundPoints []*Bounds
potentials := qt.Retrieve(find)
for o := 0; o < len(potentials); o++ {
// X and Ys are the same and it has no Width and Height (Point)
xyMatch := potentials[o].X == float64(find.X) && potentials[o].Y == float64(find.Y)
if xyMatch && potentials[o].IsPoint() {
foundPoints = append(foundPoints, find)
}
}
return foundPoints
}
// RetrieveIntersections - Bring back all the bounds in a Quadtree that intersect with a provided bounds
func (qt *Quadtree) RetrieveIntersections(find *Bounds) []*Bounds {
var foundIntersections []*Bounds
potentials := qt.Retrieve(find)
for o := 0; o < len(potentials); o++ {
if potentials[o].Intersects(find) {
foundIntersections = append(foundIntersections, potentials[o])
}
}
return foundIntersections
}
//Clear - Clear the Quadtree
func (qt *Quadtree) Clear() {
qt.Objects = []*Bounds{}
if len(qt.Nodes)-1 > 0 {
for i := 0; i < len(qt.Nodes); i++ {
qt.Nodes[i].Clear()
}
}
qt.Nodes = []Quadtree{}
qt.Total = 0
}