Given two line segments (p1, q1) and (p2, q2), find if the given line segments intersect with each other.
Before we discuss solution, let us define notion of orientation. Orientation of an ordered triplet of points in the plane can be
The following diagram shows different possible orientations of (a, b, c)
How is Orientation useful here?
Two segments (p1,q1) and (p2,q2) intersect if and only if one of the following two conditions is verified
1. General Case:
– (p1, q1, p2) and (p1, q1, q2) have different orientations and
– (p2, q2, p1) and (p2, q2, q1) have different orientations.
2. Special Case
– (p1, q1, p2), (p1, q1, q2), (p2, q2, p1), and (p2, q2, q1) are all collinear and
– the x-projections of (p1, q1) and (p2, q2) intersect
– the y-projections of (p1, q1) and (p2, q2) intersect
Following is the implementation based on above idea.
No Yes No
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Given n line segments, find if any two segments intersect
- Equation of straight line passing through a given point which bisects it into two equal line segments
- Maximum possible intersection by moving centers of line segments
- Klee's Algorithm (Length Of Union Of Segments of a line)
- Number of ways to choose K intersecting line segments on X-axis
- Count of intersections of M line segments with N vertical lines in XY plane
- Number of horizontal or vertical line segments to connect 3 points
- Check if two given circles touch or intersect each other
- Check if four segments form a rectangle
- Check if a line passes through the origin
- Check whether two points (x1, y1) and (x2, y2) lie on same side of a given line or not
- Check whether the point (x, y) lies on a given line
- Check if a line touches or intersects a circle
- Check if it is possible to draw a straight line with the given direction cosines
- Check whether a straight line can be formed using N co-ordinate points
- Check if a line at 45 degree can divide the plane into two equal weight parts
- Number of parallelograms when n horizontal parallel lines intersect m vertical parallellines
- Maximum number of segments that can contain the given points
- Split the array into odd number of segments of odd lengths
- Count of ways to split a given number into prime segments