Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overlapping line segments in 2D space

I need to find whether two lines overlap each other. I have the intersection code which returns 0, if two lines are parallel. But then I need to know if these two parallel lines overlap.

Edit:

A                    C       B                D
-----------------------------------------------

Line 1: A-B

Line 2: C-D

I need to find if Line 1 overlaps Line 2, but both lines can have a slope > 0.

like image 375
user2483744 Avatar asked Jun 17 '13 13:06

user2483744


People also ask

What is it called when 2 lines overlap?

When two or more lines cross each other in a plane, they are called intersecting lines. The intersecting lines share a common point, which exists on all the intersecting lines, and is called the point of intersection.

What is the necessary and sufficient conditions for two line segments in 2d to intersect?

A necessary condition for two lines to intersect is that they are in the same plane—that is, are not skew lines. Satisfaction of this condition is equivalent to the tetrahedron with vertices at two of the points on one line and two of the points on the other line being degenerate in the sense of having zero volume.

How do you detect where two line segments intersect?

Define b = q1 - p1 and x = (s,t). If the solution of the linear system A*x = b is in the unit square, then the segments intersect. If the solution is not in the unit square, the segments do not intersect.


2 Answers

Since you know they're both parallel, then just check whether line segment CD contains either of the endpoints of the first line (point A and point B).

like image 187
Harrison Paine Avatar answered Oct 09 '22 03:10

Harrison Paine


You can compare to find if there is no over lap. you will have less comparisons in this way thus very efficient. Do following comparisons

D < A

B < C

If either case is true the lines are not overlapping. otherwise there must an overlap. You will make least number of comparisons to determine if they are not overlapping. Otherwise there will be more comparisons to make.

like image 21
Kishore Avatar answered Oct 09 '22 03:10

Kishore