/// <summary> /// Destroys this node /// </summary> public void Destroy() { // Destroy all child nodes if (IsPartitioned) { TopLeftNode.Destroy(); TopRightNode.Destroy(); BottomLeftNode.Destroy(); BottomRightNode.Destroy(); TopLeftNode = null; TopRightNode = null; BottomLeftNode = null; BottomRightNode = null; } // Remove all items while (Items.Count > 0) { RemoveItem(0); } }
/// <summary> /// Repartitions this node /// </summary> protected void Partition() { // Create the nodes Vector2 MidPoint = Vector2.Divide(Vector2.Add(Rect.TopLeft, Rect.BottomRight), 2.0f); TopLeftNode = new QuadTreeNode <T>(this, new FRect(Rect.TopLeft, MidPoint), MaxItems); TopRightNode = new QuadTreeNode <T>(this, new FRect(new Vector2(MidPoint.X, Rect.Top), new Vector2(Rect.Right, MidPoint.Y)), MaxItems); BottomLeftNode = new QuadTreeNode <T>(this, new FRect(new Vector2(Rect.Left, MidPoint.Y), new Vector2(MidPoint.X, Rect.Bottom)), MaxItems); BottomRightNode = new QuadTreeNode <T>(this, new FRect(MidPoint, Rect.BottomRight), MaxItems); IsPartitioned = true; // Try to push items down to child nodes int i = 0; while (i < Items.Count) { if (!PushItemDown(i)) { i++; } } }
/// <summary> /// QuadTree constructor /// </summary> /// <param name="worldRect">The world rectangle for this QuadTree (a rectangle containing all items at all times)</param> /// <param name="maxItems">Maximum number of items in any cell of the QuadTree before partitioning</param> public QuadTree(FRect worldRect, int maxItems) { this.headNode = new QuadTreeNode <T>(worldRect, maxItems, Resize); this.maxItems = maxItems; }