Detecting coincident subset of two coincident line segments
- by Jared Updike
This question is related to:
How do I determine the intersection point of two lines in GDI+? (great explanation of algebra but no code)
How do you detect where two line segments intersect? (accepted answer doesn't actually work)
But note that an interesting sub-problem is completely glossed over in most solutions which just return null for the coincident case even though there are three sub-cases:
coincident but do not overlap
touching just points and coincident
overlap/coincident line sub-segment
For example we could design a C# function like this:
public static PointF[] Intersection(PointF a1, PointF a2, PointF b1, PointF b2)
where (a1,a2) is one line segment and (b1,b2) is another.
This function would need to cover all the weird cases that most implementations or explanations gloss over. In order to account for the weirdness of coincident lines, the function could return an array of PointF's:
zero result points (or null) if the lines are parallel or do not intersect (infinite lines intersect but line segments are disjoint, or lines are parallel)
one result point (containing the intersection location) if they do intersect or if they are coincident at one point
two result points (for the overlapping part of the line segments) if the two lines are coincident