public IEnumerable <Rectangle> GetEnclosing(Rectangle rectangle)
        {
            var projection      = rectangle.XProjection;
            var otherProjection = rectangle.YProjection;

            if (!BoundingSegment.Contains(projection))
            {
                throw new ArgumentOutOfRangeException();
            }

            if (projection.Beginning == BoundingSegment.Beginning || projection.End == BoundingSegment.End)
            {
                return(Enumerable.Empty <Rectangle>());
            }

            var results = new List <Rectangle>();
            var node    = (Node)Root;

            while (node != null)
            {
                if (projection.End < node.Key)
                {
                    results.AddRange(node.IntersectingRectangles.GetOverlappingCorners(
                                         new Rectangle(new Segment(BoundingSegment.Beginning, (byte)(projection.Beginning - 1)),
                                                       otherProjection)));
                    node = (Node)node.Left;
                }
                else if (projection.Beginning > node.Key)
                {
                    results.AddRange(node.IntersectingRectangles.GetOverlappingCorners(
                                         new Rectangle(new Segment((byte)(projection.End + 1), BoundingSegment.End), otherProjection)));
                    node = (Node)node.Right;
                }
                else
                {
                    results.AddRange(node.IntersectingRectangles
                                     .GetOverlappingCorners(new Rectangle(
                                                                new Segment(BoundingSegment.Beginning, (byte)(projection.Beginning - 1)), otherProjection))
                                     .Where(r => r.BottomRight.X > projection.End));
                    break;
                }
            }

            return(results);
        }
Пример #2
0
        protected bool Remove(Segment projection, Rectangle rectangle)
        {
            if (projection.Length < 3)
            {
                // Ignore short segments as they can't enclose anything
                return(true);
            }

            var node = Root;

            if (!BoundingSegment.Contains(projection))
            {
                throw new ArgumentOutOfRangeException();
            }

            while (true)
            {
                if (node == null)
                {
                    return(false);
                }

                if (projection.End < node.Key)
                {
                    node = node.Left;
                    continue;
                }

                if (projection.Beginning > node.Key)
                {
                    node = node.Right;
                    continue;
                }

                return(SubtreeRemove(rectangle, node));
            }
        }