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
- 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 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 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 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