/// <summary> /// Inserts an item into the QuadTree /// </summary> /// <param name="item">The item to insert</param> /// <remarks>Checks to see if the world needs resizing and does so if needed</remarks> public void Insert(QuadTreePositionItem <T> item) { // check if the world needs resizing if (!headNode.ContainsRect(item.Rect)) { Resize(new FRect( Vector2.Min(headNode.Rect.TopLeft, item.Rect.TopLeft) * 2, Vector2.Max(headNode.Rect.BottomRight, item.Rect.BottomRight) * 2)); } headNode.Insert(item); }
/// <summary> /// Push an item up to this node's parent /// </summary> /// <param name="i">The index of the item to push up</param> public void PushItemUp(int i) { QuadTreePositionItem <T> m = Items[i]; RemoveItem(i); ParentNode.Insert(m); }
/// <summary> /// Inserts an item into one of this node's children /// </summary> /// <param name="item">The item to insert in a child</param> /// <returns>Whether or not the insert succeeded</returns> protected bool InsertInChild(QuadTreePositionItem <T> item) { if (!IsPartitioned) { return(false); } if (TopLeftNode.ContainsRect(item.Rect)) { TopLeftNode.Insert(item); } else if (TopRightNode.ContainsRect(item.Rect)) { TopRightNode.Insert(item); } else if (BottomLeftNode.ContainsRect(item.Rect)) { BottomLeftNode.Insert(item); } else if (BottomRightNode.ContainsRect(item.Rect)) { BottomRightNode.Insert(item); } else { return(false); // insert in child failed } return(true); }
/// <summary> /// Resizes the Quadtree field /// </summary> /// <param name="newWorld">The new field</param> /// <remarks>This is an expensive operation, so try to initialize the world to a big enough size</remarks> public void Resize(FRect newWorld) { // Get all of the items in the tree List <QuadTreePositionItem <T> > Components = new List <QuadTreePositionItem <T> >(); GetAllItems(ref Components); // Destroy the head node headNode.Destroy(); headNode = null; // Create a new head headNode = new QuadTreeNode <T>(newWorld, maxItems, Resize); // Reinsert the items foreach (QuadTreePositionItem <T> m in Components) { headNode.Insert(m); } }