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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- 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
- Check if two given circles touch or intersect each other
- Klee's Algorithm (Length Of Union Of Segments of a line)
- Number of horizontal or vertical line segments to connect 3 points
- Maximum possible intersection by moving centers of line segments
- 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 parallelograms when n horizontal parallel lines intersect m vertical parallellines
- Slope of the line parallel to the line with the given slope
- Check if four segments form a rectangle
- Check whether two points (x1, y1) and (x2, y2) lie on same side of a given line or not
- Maximum number of segments that can contain the given points
- Count of ways to split a given number into prime segments
- Check if a line at 45 degree can divide the plane into two equal weight parts
- Find element using minimum segments in Seven Segment Display
- Minimum number of cuts required to make circle segments equal sized
- Split the array into odd number of segments of odd lengths
- Maximum number that can be display on Seven Segment Display using N segments
- Minimum splits required to convert a number into prime segments