public static SegmentIntersectionType ColinearSegmentIntersection(SegRat2 s, SegRat2 t, ref VecRat2 pointIntersection, ref SegRat2 segmentIntersection) { //This check is important for handling degenerate cases like s = [(x,y), (x,y)). if (s.IsEmpty() || t.IsEmpty()) { return(SegmentIntersectionType.None); } //This check is important because the LineProjectionTransform can only be formed with a non-point segment. if (s.IsPoint()) { if (ColinearPointInSegment(s.A, t)) { pointIntersection = s.A; return(SegmentIntersectionType.Point); } else { return(SegmentIntersectionType.None); } } LineProjectionTransform transform = new LineProjectionTransform(s); SegRat1 proj_s = transform.Project(s); SegRat1 proj_t = transform.Project(t); Rational proj_pointIntersection = new Rational(); SegRat1 proj_segmentIntersection = new SegRat1(); SegmentIntersectionType result = SegmentIntersection( proj_s, proj_t, ref proj_pointIntersection, ref proj_segmentIntersection); if (result == SegmentIntersectionType.Point) { pointIntersection = transform.Unproject(proj_pointIntersection); } else if (result == SegmentIntersectionType.Segment) { segmentIntersection = transform.Unproject(proj_segmentIntersection); } return(result); }
//If the two segments are disjoint, then None is returned. //Else, if the two segments are colinear, then either Segment is returned. //Else, if the two segments are not colinear, Point is returned. public static SegmentIntersectionType SegmentIntersection(SegRat2 s, SegRat2 t, ref VecRat2 pointIntersection, ref SegRat2 segmentIntersection) { //This check is important for handling degenerate cases like s = [(x,y), (x,y)). if (s.IsEmpty() || t.IsEmpty()) { return(SegmentIntersectionType.None); } //This check is important because the LineProjectionTransform can only be formed with a non-point segment. if (s.IsPoint()) { if (PointInSegment(s.A, t)) { segmentIntersection = s; return(SegmentIntersectionType.Segment); } else { return(SegmentIntersectionType.None); } } int turn_s_ta = TurnTest(s, t.A); int turn_s_tb = TurnTest(s, t.B); if (turn_s_ta == 0 && turn_s_tb == 0) { return(ColinearSegmentIntersection(s, t, ref pointIntersection, ref segmentIntersection)); } if (s.AClosed) { if (t.AClosed && s.A == t.A) { pointIntersection = s.A; return(SegmentIntersectionType.Point); } else if (t.BClosed && s.B == t.B) { pointIntersection = s.B; return(SegmentIntersectionType.Point); } } if (s.BClosed) { if (t.AClosed && s.B == t.A) { pointIntersection = s.B; return(SegmentIntersectionType.Point); } else if (t.BClosed && s.B == t.B) { pointIntersection = s.B; return(SegmentIntersectionType.Point); } } int turn_t_sa = TurnTest(t, s.A); int turn_t_sb = TurnTest(t, s.B); int val_s = turn_s_ta * turn_s_tb; int val_t = turn_t_sa * turn_t_sb; if (val_s < 0 && val_t < 0) { pointIntersection = LineIntersection(s, t); return(SegmentIntersectionType.Point); } else { return(SegmentIntersectionType.None); } }