/// <summary>
 /// Tests whether this node contains a rectangle
 /// </summary>
 /// <param name="rect">The rectangle to test</param>
 /// <returns>Whether or not this node contains the specified rectangle</returns>
 public bool ContainsRect(FRect rect)
 {
     return(rect.TopLeft.X >= Rect.TopLeft.X &&
            rect.TopLeft.Y >= Rect.TopLeft.Y &&
            rect.BottomRight.X <= Rect.BottomRight.X &&
            rect.BottomRight.Y <= Rect.BottomRight.Y);
 }
 /// <summary>
 /// Gets a list of items intersecting a specified rectangle
 /// </summary>
 /// <param name="Rect">The rectangle</param>
 /// <param name="ItemsFound">The list to add found items to (list will not be cleared first)</param>
 public void GetItems(FRect Rect, ref List <QuadTreePositionItem <T> > ItemsList)
 {
     if (ItemsList != null)
     {
         headNode.GetItems(Rect, ref ItemsList);
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// Checks if this rectangle intersects another rectangle
 /// </summary>
 /// <param name="Rect">The rectangle to check</param>
 /// <returns>Whether or not this rectangle intersects the other</returns>
 public bool Intersects(FRect Rect)
 {
     return(!(Bottom < Rect.Top ||
              Top > Rect.Bottom ||
              Right < Rect.Left ||
              Left > Rect.Right));
 }
 /// <summary>
 /// QuadTreeNode constructor
 /// </summary>
 /// <param name="parentNode">The parent node of this QuadTreeNode</param>
 /// <param name="rect">The rectangle of the QuadTreeNode</param>
 /// <param name="maxItems">Maximum number of items in the QuadTreeNode before partitioning</param>
 public QuadTreeNode(QuadTreeNode <T> parentNode, FRect rect, int maxItems)
 {
     ParentNode    = parentNode;
     Rect          = rect;
     MaxItems      = maxItems;
     IsPartitioned = false;
     Items         = new List <QuadTreePositionItem <T> >();
 }
 /// <summary>
 /// QuadTreeNode constructor
 /// </summary>
 /// <param name="rect">The rectangle of the QuadTreeNode</param>
 /// <param name="maxItems">Maximum number of items in the QuadTreeNode before partitioning</param>
 /// <param name="worldResize">The function to return the size</param>
 public QuadTreeNode(FRect rect, int maxItems, ResizeDelegate worldResize)
 {
     ParentNode    = null;
     Rect          = rect;
     MaxItems      = maxItems;
     WorldResize   = worldResize;
     IsPartitioned = false;
     Items         = new List <QuadTreePositionItem <T> >();
 }
        /// <summary>
        /// Creates a position item in a QuadTree
        /// </summary>
        /// <param name="parent">The parent of this item</param>
        /// <param name="position">The position of this item</param>
        /// <param name="size">The size of this item</param>
        public QuadTreePositionItem(T parent, Vector2 position, Vector2 size)
        {
            this.rect = new FRect(0f, 0f, 1f, 1f);

            this.parent   = parent;
            this.position = position;
            this.size     = size;
            OnMove();
        }
        /// <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);
            }
        }
        /// <summary>
        /// Gets a list of items intersecting a specified rectangle
        /// </summary>
        /// <param name="Rect">The rectangle</param>
        /// <param name="ItemsFound">The list to add found items to (list will not be cleared first)</param>
        /// <remarks>ItemsFound is assumed to be initialized, and will not be cleared</remarks>
        public void GetItems(FRect Rect, ref List <QuadTreePositionItem <T> > ItemsFound)
        {
            // test the point against this node
            if (Rect.Intersects(Rect))
            {
                // test the point in each item
                foreach (QuadTreePositionItem <T> Item in Items)
                {
                    if (Item.Rect.Intersects(Rect))
                    {
                        ItemsFound.Add(Item);
                    }
                }

                // query all subtrees
                if (IsPartitioned)
                {
                    TopLeftNode.GetItems(Rect, ref ItemsFound);
                    TopRightNode.GetItems(Rect, ref ItemsFound);
                    BottomLeftNode.GetItems(Rect, ref ItemsFound);
                    BottomRightNode.GetItems(Rect, ref ItemsFound);
                }
            }
        }
Exemplo n.º 9
0
 /// <summary>
 /// Checks if this rectangle intersects another rectangle
 /// </summary>
 /// <param name="Rect">The rectangle to check</param>
 /// <returns>Whether or not this rectangle intersects the other</returns>
 public bool Intersects(FRect Rect)
 {
     return (!( Bottom < Rect.Top ||
                Top > Rect.Bottom ||
                Right < Rect.Left ||
                Left > Rect.Right ));
 }
 /// <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;
 }