/// <summary> /// Subdivide this QuadTree and move it's children into the appropriate Quads where applicable. /// </summary> void subdivide() { // We've reached capacity, subdivide... var size = new Point(_rect.width / 2, _rect.height / 2); var mid = new Point(_rect.x + size.x, _rect.y + size.y); _childTL = new QuadTreeNode <T>(this, new Rectangle(_rect.Left(), _rect.Top(), size.x, size.y)); _childTR = new QuadTreeNode <T>(this, new Rectangle(mid.x, _rect.Top(), size.x, size.y)); _childBL = new QuadTreeNode <T>(this, new Rectangle(_rect.Left(), mid.y, size.x, size.y)); _childBR = new QuadTreeNode <T>(this, new Rectangle(mid.x, mid.y, size.x, size.y)); // If they're completely contained by the quad, bump objects down for (var i = 0; i < _objects.Count; i++) { var destTree = getDestinationTree(_objects[i]); if (destTree != this) { // Insert to the appropriate tree, remove the object, and back up one in the loop destTree.insert(_objects[i]); remove(_objects[i]); i--; } } }
public static bool Intersects(this Rectangle lhs, Rectangle value) { return(value.Left() < lhs.Right() && lhs.Left() < value.Right() && value.Top() < lhs.Bottom() && lhs.Top() < value.Bottom()); }