Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find overlap between collinear lines

Given two collinear line segments AB and CD, how do I find if they overlap? How do I locate the start and end points of the overlap?

Below is the approach I am using. I am first ensuring that A < B and C < D.

if(pa < pc){
  if(pc < pb){
    if(pd < pb){
      //  overlap exists; CD falls entirely within AB
    }
    else {
      //  overlap exists; CB is the overlapping segment
    }
  }
  else {
    //  no overlap exists; AB lies before CD
  }
}
else {
  if(pa < pd){
    if(pb < pd){
      //  overlap exists; AB lies entirely within CD
    }
    else {
      //  overlap exists; AD is the overlapping segment
    }
  }
  else {
    //  no overlap exists; CD lies before AB
  }
}

Now, isn't there a simpler solution to do this?

Update:there is another way... compare the sum of the lengths of both segments with the distance between the outermost points. If the latter is the lesser, overlap exists.

like image 531
Agnel Kurian Avatar asked Mar 31 '13 05:03

Agnel Kurian


People also ask

How do you find where two line segments intersect?

To find the point at which the two lines intersect, we simply need to solve the two equations for the two unknowns, x and y. Finally, divide both sides by A 1B 2 - A 2B 1, and you get the equation for x. The equation for y can be derived similarly.

Are two overlapping lines parallel?

Yes they are parallel because they don't intersect at an angle.

What is an overlapping line?

For example, physical objects can't quite coincide, but if one lies on top of another, and doesn't cover it completely, it can be said to overlap it. For the particular situation you describe above, when the line segments are visible, they don't coincide.


2 Answers

Ensure A<B, C<D, and A<=C (which you can do by simple swapping). Then:

  • if B<C, the segments are disjoint
  • if B=C, then the intersection is the single point B=C
  • if B>C, then the intersection is the segment [C, min(B, D)]
like image 138
Timothy Shields Avatar answered Sep 27 '22 09:09

Timothy Shields


Ensure A<B, C<D:

if (pb - pc >= 0 and pd - pa >=0 ) { // overlap
    OverlapInterval = [ max(pa, pc), min(pb, pd) ] // it could be a point [x, x]
} // else: not overlap
like image 28
Edoot Avatar answered Sep 23 '22 09:09

Edoot