Пример #1
0
        private void Subdivide()
        {
            QuadTreePointStruct halfDistance = new QuadTreePointStruct()
            {
                Index = -1,
                X     = _boundary.HalfLength.X / 2,
                Y     = _boundary.HalfLength.Y / 2
            };

            //Create our 4 regions
            UpperLeft = new QuadTree(new QuadTreeBoundingBox()
            {
                Center = new QuadTreePointStruct()
                {
                    Index = -1, X = _boundary.Center.X - halfDistance.X, Y = _boundary.Center.Y + halfDistance.Y
                }, HalfLength = halfDistance
            });
            UpperRight = new QuadTree(new QuadTreeBoundingBox()
            {
                Center = new QuadTreePointStruct()
                {
                    Index = -1, X = _boundary.Center.X + halfDistance.X, Y = _boundary.Center.Y + halfDistance.Y
                }, HalfLength = halfDistance
            });
            LowerLeft = new QuadTree(new QuadTreeBoundingBox()
            {
                Center = new QuadTreePointStruct()
                {
                    Index = -1, X = _boundary.Center.X - halfDistance.X, Y = _boundary.Center.Y - halfDistance.Y
                }, HalfLength = halfDistance
            });
            LowerRight = new QuadTree(new QuadTreeBoundingBox()
            {
                Center = new QuadTreePointStruct()
                {
                    Index = -1, X = _boundary.Center.X + halfDistance.X, Y = _boundary.Center.Y - halfDistance.Y
                }, HalfLength = halfDistance
            });

            //for each point, determine where it lies
            for (int i = 0; i < _countPoints; i++)
            {
                if (UpperLeft.Insert(_quadTreePoints[i]))
                {
                    continue;
                }
                if (UpperRight.Insert(_quadTreePoints[i]))
                {
                    continue;
                }
                if (LowerLeft.Insert(_quadTreePoints[i]))
                {
                    continue;
                }
                LowerRight.Insert(_quadTreePoints[i]);
            }

            //Set the point count to 0
            _countPoints = 0;
        }
Пример #2
0
        public bool Insert(QuadTreePointStruct p)
        {
            if (!_boundary.ContainsPoint(p))
            {
                return(false);
            }

            if (_countPoints < NODE_CAPACITY)
            {
                _quadTreePoints[_countPoints++] = p;
                return(true);
            }

            //If we don't have room, we must subdivide to make room
            if (UpperLeft == null)
            {
                Subdivide();
            }

            if (UpperLeft.Insert(p))
            {
                return(true);
            }
            if (UpperRight.Insert(p))
            {
                return(true);
            }
            if (LowerLeft.Insert(p))
            {
                return(true);
            }
            if (LowerRight.Insert(p))
            {
                return(true);
            }

            return(false);
        }
Пример #3
0
 public bool ContainsPoint(QuadTreePointStruct p)
 {
     return((p.X >= Center.X - HalfLength.X) && (p.X <= Center.X + HalfLength.X) &&
            (p.Y >= Center.Y - HalfLength.Y) && (p.Y <= Center.Y + HalfLength.Y));
 }