public EventPoint(Point p, EventPoint s1, EventPoint s2, int type) { this.point = p; this.event_type = type; this.intersection_segment_1 = s1; this.intersection_segment_2 = s2; }
public int Compare_X(EventPoint E1, EventPoint E2) { if (E2.point.X < E1.point.X) { return(1); } else if (E2.point.X > E1.point.X) { return(-1); } if (E2.point.Y < E1.point.Y) { return(1); } else if (E2.point.Y > E1.point.Y) { return(-1); } if (EventPoint.lines[E2.index].End.X < EventPoint.lines[E1.index].End.X) { return(1); } else if (EventPoint.lines[E2.index].End.X > EventPoint.lines[E1.index].End.X) { return(-1); } return(0); }
private static int CompareByLeastX(EventPoint ePoint1, EventPoint ePoint2) { int res = 0; if (ePoint1._point.X > ePoint2._point.X) { res = 1; } else if (ePoint1._point.X < ePoint2._point.X) { res = -1; } else if (ePoint1._point.X == ePoint2._point.X) { if (ePoint1._point.Y < ePoint2._point.Y) { res = 1; } else if (ePoint1._point.Y > ePoint2._point.Y) { res = -1; } } return(res); }
// 0 intersection, 1 start, -1 end public EventPoint(Point p, int index, int type) { this.point = p; this.index = index; this.event_type = type; this.intersection_segment_1 = null; this.intersection_segment_2 = null; }
public override void Run(List <CGUtilities.Point> points, List <CGUtilities.Line> lines, List <CGUtilities.Polygon> polygons, ref List <CGUtilities.Point> outPoints, ref List <CGUtilities.Line> outLines, ref List <CGUtilities.Polygon> outPolygons) { intersections = new List <Point>(); S = lines; InitializeEvents(); while (Q.Count != 0) { currentEvent = Q[0]; HandleEvent(); Q.RemoveFirst(); } outPoints = new List <Point>(intersections); }
public int Compare_Y(EventPoint E1, EventPoint E2) { if (E2.point.Y < E1.point.Y) { return(1); } if (E2.point.Y > E1.point.Y) { return(-1); } if (E2.point.X < E1.point.X) { return(1); } if (E2.point.X > E1.point.X) { return(-1); } return(0); }
public override void Run(List <CGUtilities.Point> points, List <CGUtilities.Line> lines, List <CGUtilities.Polygon> polygons, ref List <CGUtilities.Point> outPoints, ref List <CGUtilities.Line> outLines, ref List <CGUtilities.Polygon> outPolygons) { OrderedSet <Tuple <int, int> > Intersected_lines = new OrderedSet <Tuple <int, int> >(); EventPoint.lines = lines; OrderedSet <EventPoint> Q = new OrderedSet <EventPoint>(Compare_X); OrderedSet <EventPoint> L = new OrderedSet <EventPoint>(Compare_Y); for (int i = 0; i < lines.Count; ++i) { if (ComparePoints(lines[i].Start, lines[i].End) == false) { lines[i] = new Line(lines[i].End, lines[i].Start); } Q.Add(new EventPoint(lines[i].Start, i, 1)); Q.Add(new EventPoint(lines[i].End, i, -1)); } int counter = lines.Count; while (Q.Count != 0) { EventPoint current_event = Q.GetFirst(); Q.RemoveFirst(); if (current_event.event_type == 1) { L.Add(current_event); if (lines[current_event.index].Start.X == lines[current_event.index].End.Y) { IEnumerable <EventPoint> vertical__ = L; foreach (EventPoint e in vertical__) { if (TwoSegmentsIntersectionTest(lines[e.index], lines[current_event.index])) { Point point_of_intersection = twoLinesIntersectionPoint(lines[e.index], lines[current_event.index]); if (HasValue(point_of_intersection.X) && HasValue(point_of_intersection.Y) && !Intersected_lines.Contains(new Tuple <int, int>(current_event.index, e.index))) { outPoints.Add(point_of_intersection); Intersected_lines.Add(new Tuple <int, int>(current_event.index, e.index)); Intersected_lines.Add(new Tuple <int, int>(e.index, current_event.index)); Q.Add(new EventPoint(point_of_intersection, e, current_event, 0)); } } } continue; } EventPoint pre = L.DirectUpperAndLower(current_event).Key; EventPoint next = L.DirectUpperAndLower(current_event).Value; if (pre != null) { if (TwoSegmentsIntersectionTest(lines[pre.index], lines[current_event.index])) { Point point_of_intersection = twoLinesIntersectionPoint(lines[pre.index], lines[current_event.index]); if (!Q.Contains(new EventPoint(point_of_intersection, pre, current_event, 0)) && !Intersected_lines.Contains(new Tuple <int, int>(pre.index, current_event.index))) { outPoints.Add(point_of_intersection); Intersected_lines.Add(new Tuple <int, int>(current_event.index, pre.index)); Intersected_lines.Add(new Tuple <int, int>(pre.index, current_event.index)); Q.Add(new EventPoint(point_of_intersection, pre, current_event, 0)); } } } if (next != null) { if (TwoSegmentsIntersectionTest(lines[current_event.index], lines[next.index])) { Point point_of_intersection = twoLinesIntersectionPoint(lines[current_event.index], lines[next.index]); if (!Q.Contains(new EventPoint(point_of_intersection, current_event, next, 0)) && !Intersected_lines.Contains(new Tuple <int, int>(current_event.index, next.index))) { outPoints.Add(point_of_intersection); Q.Add(new EventPoint(point_of_intersection, current_event, next, 0)); Intersected_lines.Add(new Tuple <int, int>(next.index, current_event.index)); Intersected_lines.Add(new Tuple <int, int>(current_event.index, next.index)); } } } } else if (current_event.event_type == -1) { EventPoint pre = L.DirectUpperAndLower(current_event).Key; EventPoint next = L.DirectUpperAndLower(current_event).Value; if (pre != null && next != null) { if (TwoSegmentsIntersectionTest(lines[pre.index], lines[next.index])) { Point point_of_intersection = twoLinesIntersectionPoint(lines[pre.index], lines[next.index]); if (!Q.Contains(new EventPoint(point_of_intersection, pre, next, 0)) && !Intersected_lines.Contains(new Tuple <int, int>(pre.index, next.index))) { Intersected_lines.Add(new Tuple <int, int>(pre.index, next.index)); Intersected_lines.Add(new Tuple <int, int>(next.index, pre.index)); Q.Add(new EventPoint(point_of_intersection, pre, next, 0)); outPoints.Add(point_of_intersection); } } } if (current_event.index != 0) { List <EventPoint> LL = L.RemoveAll(p => p.index == current_event.index).ToList(); for (int i = 0; i < LL.Count; ++i) { L.Remove(LL[i]); } } else { L.Remove(current_event); } } else { EventPoint s1 = current_event.intersection_segment_1; EventPoint s2 = current_event.intersection_segment_2; /* if (HasValue(current_event.point.X) && HasValue(current_event.point.Y) && !Intersected_lines.Contains(new Tuple<int, int>(s1.index, s2.index))) * { * outPoints.Add(current_event.point); * Intersected_lines.Add(new Tuple<int, int>(s1.index, s2.index)); * Intersected_lines.Add(new Tuple<int, int>(s2.index, s1.index)); * * }*/ EventPoint s1_prev = L.DirectUpperAndLower(s1).Key; EventPoint s2_next = L.DirectUpperAndLower(s2).Value; if (s1_prev != null) { if (TwoSegmentsIntersectionTest(lines[s1_prev.index], lines[s2.index])) { Point point_of_intersection = twoLinesIntersectionPoint(lines[s1_prev.index], lines[s2.index]); if (!Q.Contains(new EventPoint(point_of_intersection, s1_prev, s2, 0)) && !Intersected_lines.Contains(new Tuple <int, int>(s1_prev.index, s2.index))) { Q.Add(new EventPoint(point_of_intersection, s1_prev, s2, 0)); outPoints.Add(point_of_intersection); Intersected_lines.Add(new Tuple <int, int>(s1_prev.index, s2.index)); Intersected_lines.Add(new Tuple <int, int>(s2.index, s1_prev.index)); } } } if (s2_next != null) { if (TwoSegmentsIntersectionTest(lines[s1.index], lines[s2_next.index])) { Point point_of_intersection = twoLinesIntersectionPoint(lines[s1.index], lines[s2_next.index]); if (!Q.Contains(new EventPoint(point_of_intersection, s1, s2_next, 0)) && !Intersected_lines.Contains(new Tuple <int, int>(s1.index, s2_next.index))) { Q.Add(new EventPoint(point_of_intersection, s1, s2_next, 0)); outPoints.Add(point_of_intersection); Intersected_lines.Add(new Tuple <int, int>(s1.index, s2_next.index)); Intersected_lines.Add(new Tuple <int, int>(s2_next.index, s1.index)); } } } L.Remove(s1); L.Remove(s2); double PX = current_event.point.X + 0.3; double PY1 = current_event.point.Y + 10000; double PY2 = current_event.point.Y - 10000; Line sweepL = new Line(new Point(PX, PY1), new Point(PX, PY2)); Point P1 = twoLinesIntersectionPoint(sweepL, lines[s1.index]); Point P2 = twoLinesIntersectionPoint(sweepL, lines[s2.index]); s1.point = P1; s2.point = P2; L.Add(s1); L.Add(s2); } } }
void HandleEvent() { KeyValuePair <Line, Line> UpperAndLower = L.DirectUpperAndLower(currentEvent.seg1); if (currentEvent.eventType == Enums.EventType.Start) { Line newSegment = currentEvent.seg1; L.Add(newSegment); if (UpperAndLower.Key != null) { Point intersectionPoint = CheckIntersection(newSegment, UpperAndLower.Key); if (intersectionPoint != null && intersectionPoint.X > currentEvent._point.X) { EventPoint newEvent = new EventPoint(intersectionPoint, Enums.EventType.Intersection, UpperAndLower.Key, newSegment); if (!Q.Contains(newEvent)) { Q.Add(newEvent); } } } if (UpperAndLower.Value != null) { Point intersectionPoint = CheckIntersection(newSegment, UpperAndLower.Value); if (intersectionPoint != null && intersectionPoint.X > currentEvent._point.X) { EventPoint newEvent = new EventPoint(intersectionPoint, Enums.EventType.Intersection, newSegment, UpperAndLower.Value); if (!Q.Contains(newEvent)) { Q.Add(newEvent); } } } } else if (currentEvent.eventType == Enums.EventType.End) { if (UpperAndLower.Key != null && UpperAndLower.Value != null) { Point intersectionPoint = CheckIntersection(UpperAndLower.Key, UpperAndLower.Value); if (intersectionPoint != null && intersectionPoint.X > currentEvent._point.X) { EventPoint newEvent = new EventPoint(intersectionPoint, Enums.EventType.Intersection, UpperAndLower.Key, UpperAndLower.Value); if (!Q.Contains(newEvent)) { Q.Add(newEvent); } } } L.Remove(currentEvent.seg1); } else if (currentEvent.eventType == Enums.EventType.Intersection) { intersections.Add(currentEvent._point); Line S1below = L.DirectUpperAndLower(currentEvent.seg1).Key; Line S2upper = L.DirectUpperAndLower(currentEvent.seg2).Value; if (S1below != null) { Point intersectionPoint = CheckIntersection(S1below, currentEvent.seg2); if (intersectionPoint != null && intersectionPoint.X > currentEvent._point.X) { EventPoint newEvent = new EventPoint(intersectionPoint, Enums.EventType.Intersection, S1below, currentEvent.seg2); if (!Q.Contains(newEvent)) { Q.Add(newEvent); } } } if (S2upper != null) { Point intersectionPoint = CheckIntersection(S2upper, currentEvent.seg1); if (intersectionPoint != null && intersectionPoint.X > currentEvent._point.X) { EventPoint newEvent = new EventPoint(intersectionPoint, Enums.EventType.Intersection, S2upper, currentEvent.seg1); if (!Q.Contains(newEvent)) { Q.Add(newEvent); } } } L.Remove(currentEvent.seg1); L.Remove(currentEvent.seg2); //currentEvent._point.X++; L.Add(currentEvent.seg1); L.Add(currentEvent.seg2); //currentEvent._point.X--; // L.Remove(currentEvent.seg1); // L.Remove(currentEvent.seg2); // L.Add(new Line(currentEvent._point,currentEvent.seg1.End)); // L.Add(new Line(currentEvent._point, currentEvent.seg2.End)); } }