getIndex - Determine which quadrant the object belongs to (0-3)
(pRect AABB)
| 215 | |
| 216 | // getIndex - Determine which quadrant the object belongs to (0-3) |
| 217 | func (qt *quadtreeNode) getIndex(pRect AABB) int { |
| 218 | horzMidpoint := qt.Bounds.Min.X + (aabbWidth(qt.Bounds) / 2) |
| 219 | vertMidpoint := qt.Bounds.Min.Y + (aabbHeight(qt.Bounds) / 2) |
| 220 | |
| 221 | //pRect can completely fit within the top quadrants |
| 222 | topQuadrant := (pRect.Min.Y < vertMidpoint) && (pRect.Max.Y < vertMidpoint) |
| 223 | |
| 224 | //pRect can completely fit within the bottom quadrants |
| 225 | bottomQuadrant := (pRect.Min.Y > vertMidpoint) |
| 226 | |
| 227 | //pRect can completely fit within the left quadrants |
| 228 | if (pRect.Min.X < horzMidpoint) && (pRect.Max.X < horzMidpoint) { |
| 229 | if topQuadrant { |
| 230 | return 1 |
| 231 | } else if bottomQuadrant { |
| 232 | return 2 |
| 233 | } |
| 234 | } else if pRect.Min.X > horzMidpoint { |
| 235 | //pRect can completely fit within the right quadrants |
| 236 | if topQuadrant { |
| 237 | return 0 |
| 238 | } else if bottomQuadrant { |
| 239 | return 3 |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | return -1 // index of the subnode (0-3), or -1 if pRect cannot completely fit within a subnode and is part of the parent node |
| 244 | } |
| 245 | |
| 246 | // Insert inserts the given item to the quadtree |
| 247 | func (qt *Quadtree) Insert(item AABBer) { |
no test coverage detected