private void Subdivide() { Point size = new Point(rect.Width / 2, rect.Height / 2); Point 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)); for (int i = 0; i < objects.Count; i++) { QuadTreeNode <T> destTree = GetDestinationTree(objects[i]); if (destTree != this) { destTree.Insert(objects[i]); Remove(objects[i]); i--; } } }
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; }
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); }
public QuadTree(int x, int y, int width, int height) { quadTreeRoot = new QuadTreeNode <T>(new Rectangle(x, y, width, height)); }
public QuadTree(Rectangle rect) { quadTreeRoot = new QuadTreeNode <T>(rect); }
private QuadTreeNode(QuadTreeNode <T> parent, Rectangle rect) : this(rect) { this.parent = parent; }