예제 #1
0
        /// <summary>Searches for objects in any quadrants which the passed region overlaps, but not specifically within that region.</summary>
        /// <param name="rect">The search region.</param>
        /// <returns>an array of objects.</returns>
        public T[] FindObjects(QuadTreeRect rect)
        {
            List <T> foundObjects = new List <T>();

            if (_hasChildren)
            {
                foundObjects.AddRange(quad_TL.FindObjects(rect));
                foundObjects.AddRange(quad_TR.FindObjects(rect));
                foundObjects.AddRange(quad_BL.FindObjects(rect));
                foundObjects.AddRange(quad_BR.FindObjects(rect));
            }
            else
            {
                if (IsOverlapping(rect))
                {
                    foundObjects.AddRange(_objects);
                }
            }

            HashSet <T> result = new HashSet <T>();

            result.UnionWith(foundObjects);

            return(result.ToArray());
        }
예제 #2
0
        /// <summary>Initializes a new instance of the <see cref="T:Auios.QuadTree.QuadTree`1"></see> class.</summary>
        /// <param name="x">The x-coordinate of the upper-left corner of the boundary rectangle.</param>
        /// <param name="y">The y-coordinate of the upper-left corner of the boundary rectangle.</param>
        /// <param name="width">The width of the boundary rectangle.</param>
        /// <param name="height">The height of the boundary rectangle.</param>
        /// <param name="objectBounds">The set of methods for getting boundaries of an element.</param>
        /// <param name="maxObjects">The max number of elements in one rectangle.</param>
        /// <param name="maxLevel">The max depth level.</param>
        /// <param name="currentLevel">The current depth level. Leave default if this is the root QuadTree.</param>
        public QuadTree(float x, float y, float width, float height, IQuadTreeObjectBounds <T> objectBounds, int maxObjects = 10, int maxLevel = 5, int currentLevel = 0)
        {
            Area          = new QuadTreeRect(x, y, width, height);
            _objects      = new HashSet <T>();
            _objectBounds = objectBounds;

            CurrentLevel = currentLevel;
            MaxLevel     = maxLevel;
            MaxObjects   = maxObjects;

            _hasChildren = false;
        }
예제 #3
0
 /// <summary>Checks if the current quadrant is overlapping with a <see cref="T:Auios.QuadTree.QuadTreeRect"></see></summary>
 private bool IsOverlapping(QuadTreeRect rect)
 {
     if (rect.Right < Area.Left || rect.Left > Area.Right)
     {
         return(false);
     }
     if (rect.Top > Area.Bottom || rect.Bottom < Area.Top)
     {
         return(false);
     }
     Area.isOverlapped = true;
     return(true);
 }