Using Above Below Primitive to Test Whether Two Lines Intersect in Java
Above Below Primitive is the method to check whether the lines are intersecting or not by comparing their endpoints. Here is a JAVA program which uses above below primitive to test whether two lines intersect with each other or not. This approach only returns true if one endpoint of the line is on the left of the other line’s left endpoint and its right endpoint is on the right of other line’s endpoint.
Input: End Points of first line x1 = 2, y1 = 3 x2 = 6, y2 = 4 End Points of second line p1 = 3, q1 = 4 p2 = 7, q2 = 1 Output: Equation 1: (1)x+(-4)y+(10) = 0 Equation 2: (-3)x+(-4)y+(25) = 0 Intersecting
Find the segment first line as a1. if a1 < 0 Find the segment of line 2 as a2 if a2 >= 0 print intersecting otherwise print not intersecting. else if a1 >0 Find the segment of line 2 as a2 if a2 <= 0 print intersecting. otherwise print not intersecting.
Below is the implementation of the above approach.
Equation 1: (1)x+(-4)y+(10) = 0 Equation 2: (-3)x+(-4)y+(25) = 0 Intersecting
Time Complexity: O(1)