Detecting coincident subset of two coincident line segments

Posted by Jared Updike on Stack Overflow See other posts from Stack Overflow or by Jared Updike
Published on 2010-02-12T23:45:42Z Indexed on 2010/05/12 23:44 UTC
Read the original article Hit count: 175

Filed under:
|
|
|
|

This question is related to:

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

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET