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 C++ 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
- Program for Point of Intersection of Two Lines
- Convex Hull using Divide and Conquer Algorithm
- Count maximum points on same line
- Orientation of 3 ordered points
- How to check if given four points form a square
- Count Possible Decodings of a given Digit Sequence
- Find if two rectangles overlap
- Closest Pair of Points | O(nlogn) Implementation
- Given n line segments, find if any two segments intersect
- Convex Hull | Set 2 (Graham Scan)
- Convex Hull | Set 1 (Jarvis's Algorithm or Wrapping)
- How to check if a given point lies inside or outside a polygon?
- Closest Pair of Points using Divide and Conquer algorithm
- Check whether a given point lies inside a triangle or not
- Write an Efficient Method to Check if a Number is Multiple of 3