/// <summary> /// Clears the QuadTree of all objects, including any objects living in its children. /// </summary> internal void Clear() { // Clear out the children, if we have any if (childTL != null) { childTL.Clear(); childTR.Clear(); childBL.Clear(); childBR.Clear(); } // Clear any objects at this level if (objects != null) { objects.Clear(); objects = null; } // Set the children to null childTL = null; childTR = null; childBL = null; childBR = null; }
/// <summary> /// Get the child Quad that would contain an object. /// </summary> /// <param name="item">The object to get a child for.</param> /// <returns></returns> private QuadTreeNode <T> GetDestinationTree(QuadTreeObject <T> item) { // If a child can't contain an object, it will live in this Quad QuadTreeNode <T> destTree = this; if (childTL.QuadRect.Contains(item.Data.Rect)) { destTree = childTL; } else if (childTR.QuadRect.Contains(item.Data.Rect)) { destTree = childTR; } else if (childBL.QuadRect.Contains(item.Data.Rect)) { destTree = childBL; } else if (childBR.QuadRect.Contains(item.Data.Rect)) { destTree = childBR; } return(destTree); }
/// <summary> /// Creates a QuadTree for the specified area. /// </summary> /// <param name="x">The top-left position of the area rectangle.</param> /// <param name="y">The top-right position of the area rectangle.</param> /// <param name="width">The width of the area rectangle.</param> /// <param name="height">The height of the area rectangle.</param> public QuadTree(int x, int y, int width, int height) { quadTreeRoot = new QuadTreeNode <T>(new Rectangle(x, y, width, height)); }
private QuadTreeNode(QuadTreeNode <T> parent, Rectangle rect) : this(rect) { this.parent = parent; }
/// <summary> /// Creates a QuadTree for the specified area. /// </summary> /// <param name="rect">The area this QuadTree object will encompass.</param> public QuadTree(Rectangle rect) { quadTreeRoot = new QuadTreeNode <T>(rect); }