示例#1
0
        public void Add(Point2Int point)
        {
            if (!BoundingBox.ContainsPoint(point))
            {
                throw new ArgumentOutOfRangeException($"point:{point} is not inside bounding box:{BoundingBox}");
            }

            // invariant: if points == null then the children are active
            if (Points == null)
            {
                AddPointToChildren(point);
                return;
            }

            var nextIndex = Points.Length;

            if (nextIndex + 1 > MaxItems)
            {
                SpawnChildrenAndSubdivide(point);
                return;
            }

            var newPoints = new Point2Int[Points.Length + 1];

            Points.CopyTo(newPoints, 0);
            Points = newPoints;
            Points[Points.Length - 1] = point;
        }
示例#2
0
        /// <summary>
        /// Checks to see if the actual point is in the quad tree as opposed to being bounded by it
        /// </summary>
        /// <param name="point"></param>
        /// <returns></returns>
        public bool Contains(Point2Int point)
        {
            if (!BoundingBox.ContainsPoint(point))
            {
                return(false);
            }

            if (Points != null)
            {
                return(Points.Contains(point));
            }

            var center = BoundingBox.CenterPoint();

            if (point.X < center.X)
            {
                if (point.Y < center.Y)
                {
                    // bottom left
                    return(SouthWest.Contains(point));
                }

                //top left
                return(NorthWest.Contains(point));
            }

            if (point.Y < center.Y)
            {
                // bottom right
                return(SouthEast.Contains(point));
            }
            //top right
            return(NorthEast.Contains(point));
        }
 /// <summary>
 /// Returns whether a given point lies inside the bounding box
 /// </summary>
 /// <param name="point"></param>
 /// <returns></returns>
 public bool ContainsPoint(Point2Int point)
 {
     return(point.X < UpperRight.X &&
            point.X >= LowerLeft.X &&
            point.Y < UpperRight.Y &&
            point.Y >= LowerLeft.Y);
 }
        public Bounding2DBox(Point2Int lowerLeft, Point2Int upperRight)
        {
            if (lowerLeft.X > upperRight.X ||
                lowerLeft.Y > upperRight.Y)
            {
                throw new ArgumentOutOfRangeException($"Bottom Left is not bottom left of top right, bottomLeft:{lowerLeft}, topRight:{upperRight}");
            }

            LowerLeft  = lowerLeft;
            UpperRight = upperRight;
        }
示例#5
0
        public QuadTree(Bounding2DBox boundingBox, int maxItems, IQuadTreeDivisionStrategy divisionStrategy)
        {
            if (divisionStrategy == null)
            {
                throw new ArgumentNullException(nameof(divisionStrategy));
            }

            MaxItems         = maxItems;
            DivisionStrategy = divisionStrategy;
            BoundingBox      = boundingBox;
            Points           = new Point2Int[0];
        }
示例#6
0
        private void SpawnChildrenAndSubdivide(Point2Int point)
        {
            QuadTree northWest;
            QuadTree northEast;
            QuadTree southWest;
            QuadTree southEast;

            DivisionStrategy.SubDivide(Points, point, BoundingBox, out northWest, out northEast, out southWest, out southEast);

            NorthWest = northWest;
            NorthEast = northEast;
            SouthWest = southWest;
            SouthEast = southEast;

            Points = null;
        }
示例#7
0
        private void AddPointToChildren(Point2Int point)
        {
            var center = BoundingBox.CenterPoint();

            if (point.X < center.X)
            {
                if (point.Y < center.Y)
                {
                    SouthWest.Add(point);
                    return;
                }
                NorthWest.Add(point);
                return;
            }

            if (point.Y < center.Y)
            {
                SouthEast.Add(point);
                return;
            }
            NorthEast.Add(point);
        }