Beispiel #1
0
        public int CompareTo(IntersectionSweepEvent other)
        {
            if (Equals(other))
            {
                return(0);
            }
            if (Point.x > other.Point.x)
            {
                return(1);
            }

            if (Point.x < other.Point.x)
            {
                return(-1);
            }

            if (!Point.y.Equals(other.Point.y))
            {
                return(Point.y > other.Point.y ? 1 : -1);
            }

            if (IsStart != other.IsStart)
            {
                return(IsStart ? 1 : -1);
            }
            if (Segment != other.Segment)
            {
                return(Below(other.OtherEvent.Point) ? -1 : 1);
            }
            return(0);
        }
        public void AddEvents(LineSegment segment, List <IntersectionSweepEvent> events)
        {
            Vector2 point1 = segment.Point1;
            Vector2 point2 = segment.Point2;

            var ev1 = new IntersectionSweepEvent(point1, false, false, segment, null as IntersectionSweepEvent);
            var ev2 = new IntersectionSweepEvent(point2, false, false, segment, ev1);

            ev1.OtherEvent = ev2;

            if (point1.Equals(point2))
            {
                return;
            }

            if (ev1.CompareTo(ev2) < 1)
            {
                ev1.IsStart = true;
                ev2.IsEnd   = true;
            }
            else
            {
                ev1.IsEnd   = true;
                ev2.IsStart = true;
            }

            events.Add(ev1);
            events.Add(ev2);
        }
Beispiel #3
0
 public IntersectionSweepEvent(Vector2 point, bool isStart, bool isEnd, LineSegment segment,
                               IntersectionSweepEvent otherEvent)
 {
     Point      = new Vector2D(point);
     IsStart    = isStart;
     IsEnd      = isEnd;
     Segment    = segment;
     OtherEvent = otherEvent;
 }
Beispiel #4
0
        public int CompareTo(IntersectionStatusItem other)
        {
            if (Equals(other))
            {
                return(0);
            }

            IntersectionSweepEvent ev      = SweepEvent;
            IntersectionSweepEvent otherEv = other.SweepEvent;

            if (ev.Equals(otherEv))
            {
                return(0);
            }

            if (MathUtil.SignedArea(ev.Point, ev.OtherEvent.Point, otherEv.Point) != 0 ||
                MathUtil.SignedArea(ev.Point, ev.OtherEvent.Point, otherEv.OtherEvent.Point) != 0)
            {
                if (ev.Point.Equals(otherEv.Point))
                {
                    return(ev.Below(otherEv.OtherEvent.Point) ? -1 : 1);
                }

                if (ev.Point.x.Equals(otherEv.Point.x))
                {
                    return(ev.Point.y < otherEv.Point.y ? -1 : 1);
                }

                if (ev.CompareTo(otherEv) == 1)
                {
                    return(!otherEv.Below(ev.Point) ? -1 : 1);
                }

                return(ev.Below(otherEv.Point) ? -1 : 1);
            }

            if (ev.Point.Equals(otherEv.Point) && ev.OtherEvent.Point.Equals(otherEv.OtherEvent.Point))
            {
                return(0);
            }

            return(ev.CompareTo(otherEv) == 1 ? 1 : -1);
        }
Beispiel #5
0
 public bool Equals(IntersectionSweepEvent other)
 {
     return(this == other);
 }
        public void HandleEvent(IBST <IntersectionSweepEvent> events, IBST <IntersectionStatusItem> status,
                                IntersectionSweepEvent ev)
        {
            if (ev.IsStart)
            {
                ev.StatusItem = new IntersectionStatusItem(ev);
                status.Insert(ev.StatusItem);

                IntersectionStatusItem prev, next;
                bool prevFound = status.FindNextSmallest(ev.StatusItem, out prev);
                bool nextFound = status.FindNextBiggest(ev.StatusItem, out next);

                if (prevFound)
                {
                    LineSegment otherSegment = prev.SweepEvent.Segment;
                    Vector2?    intersection = ev.Segment.IntersectProper(otherSegment);
                    if (intersection != null)
                    {
                        events.Insert(new IntersectionSweepEvent(intersection.Value, false, false,
                                                                 ev.Segment, otherSegment));
                    }
                }

                if (nextFound)
                {
                    LineSegment otherSegment = next.SweepEvent.Segment;
                    Vector2?    intersection = ev.Segment.IntersectProper(otherSegment);
                    if (intersection != null)
                    {
                        events.Insert(new IntersectionSweepEvent(intersection.Value, false, false,
                                                                 ev.Segment, otherSegment));
                    }
                }
            }
            else if (ev.IsEnd)
            {
                ev = ev.OtherEvent;
                if (ev.StatusItem == null)
                {
                    return;
                }

                IntersectionStatusItem prev, next;
                bool prevFound = status.FindNextSmallest(ev.StatusItem, out prev);
                bool nextFound = status.FindNextBiggest(ev.StatusItem, out next);

                status.Delete(ev.StatusItem);

                if (nextFound && prevFound)
                {
                    LineSegment segment      = prev.SweepEvent.Segment;
                    LineSegment otherSegment = next.SweepEvent.Segment;
                    Vector2?    intersection = segment.IntersectProper(otherSegment);
                    if (intersection != null)
                    {
                        events.Insert(new IntersectionSweepEvent(intersection.Value, false, false,
                                                                 segment, otherSegment));
                    }
                }
            }
            else if (ev.IsIntersection)
            {
                // stop on first intersection
                intersected = new List <Edge>
                {
                    new Edge(new Vertex(ev.Segment.Point1), new Vertex(ev.Segment.Point2)),
                    new Edge(new Vertex(ev.OtherSegment.Point1), new Vertex(ev.OtherSegment.Point2))
                };
                events.Clear();
                status.Clear();
            }
            else
            {
                throw new Exception("Invalid event type");
            }
        }
Beispiel #7
0
 public IntersectionStatusItem(IntersectionSweepEvent sweepEvent)
 {
     SweepEvent = sweepEvent;
 }