Monday, November 18, 2024
Google search engine
HomeLanguagesJavaUsing Above Below Primitive to Test Whether Two Lines Intersect in Java

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.

Example

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

Approach

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.

Java




// This is a java program to find whether two lines
// intersect or not using above and below primitive
public class Main {
    public static void check(int x1, int x2, int y1, int y2,
                             int p1, int p2, int q1, int q2)
    { // Segment of line 1 is stored as a1
 
        int a1 = (y2 - y1) * p1 + (x1 - x2) * q1
                 + (x2 * y1 - x1 * y2);
 
        if (a1 < 0) {
            // Segment of line 2 is stored as a2
            int a2 = (y2 - y1) * p2 + (x1 - x2) * q2
                     + (x2 * y1 - x1 * y2);
 
            if (a2 >= 0)
                System.out.println("Intersecting");
 
            else if (a2 < 0)
                System.out.println("Not intersecting");
        }
 
        else if (a1 > 0) {
 
            int a2 = (y2 - y1) * p2 + (x1 - x2) * q2
                     + (x2 * y1 - x1 * y2);
 
            if (a2 <= 0)
                System.out.println("Intersecting");
 
            else if (a2 > 0)
                System.out.println("Not intersecting");
        }
 
        // lines are coincinding
        else
            System.out.println(
                "points are lying on the line");
    }
    // Driver Code
    public static void main(String args[])
    {
        // Taking the coordinates of first line as input
        int x1 = 2, y1 = 3;
        int x2 = 6, y2 = 4;
 
        // Equation of line using slope point form
        System.out.println("Equation 1: (" + (y2 - y1)
                           + ")x+(" + (x1 - x2) + ")y+("
                           + (x2 * y1 - x1 * y2) + ") = 0");
 
        // Taking the coordinates of second line as input
        int p1 = 3, q1 = 4;
        int p2 = 7, q2 = 1;
 
        // Equation of line using slope point form
        System.out.println("Equation 2: (" + (q2 - q1)
                           + ")x+(" + (p1 - p2) + ")y+("
                           + (p2 * q1 - p1 * q2) + ") = 0");
 
        check(x1, x2, y1, y2, p1, p2, q1, q2);
    }
}


Output

Equation 1: (1)x+(-4)y+(10) = 0
Equation 2: (-3)x+(-4)y+(25) = 0
Intersecting

Time Complexity: O(1)

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments