Java Program to use Above Below Primitive to Test Whether Two Lines Intersect

This is a Java Program to whether two lines intersect or not. The above-below primitive can be used to test whether a line intersects a line segment. It does iff one endpoint of the segment is to the left of the line and the other is to the right. Segment intersection is similar but more complicated, and we refer you to implementations described below. The decision whether two segments intersect if they share an endpoint depends upon your application and is representative of the problems of degeneracy.

Here is the source code of the Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a java program to find whether two lines intersect or not using above and below primitive
  2. import java.util.Random;
  3.  
  4. public class Line_Intersection
  5. {
  6.     public static void main(String args[])
  7.     {
  8.         Random random = new Random();
  9.  
  10.         int x1, x2, y1, y2;
  11.         x1 = random.nextInt(10);
  12.         x2 = random.nextInt(10);
  13.         y1 = random.nextInt(10);
  14.         y2 = random.nextInt(10);
  15.  
  16.         System.out.println("The Equation of the 1st line is : (" + (y2 - y1)
  17.                 + ")x+(" + (x1 - x2) + ")y+(" + (x2 * y1 - x1 * y2) + ") = 0");
  18.  
  19.         int p1, p2, q1, q2;
  20.         p1 = random.nextInt(10);
  21.         p2 = random.nextInt(10);
  22.         q1 = random.nextInt(10);
  23.         q2 = random.nextInt(10);
  24.  
  25.         System.out.println("The Equation of the 2nd line is : (" + (q2 - q1)
  26.                 + ")x+(" + (p1 - p2) + ")y+(" + (p2 * q1 - p1 * q2) + ") = 0");
  27.  
  28.         int s1 = (y2 - y1) * p1 + (x1 - x2) * q1 + (x2 * y1 - x1 * y2);
  29.         if (s1 < 0)
  30.         {
  31.             int s2 = (y2 - y1) * p2 + (x1 - x2) * q2 + (x2 * y1 - x1 * y2);
  32.             if (s2 >= 0)
  33.                 System.out.println("The lines intersect");
  34.             else if (s2 < 0)
  35.                 System.out.println("The lines doesn't intersect");
  36.  
  37.         }
  38.         else if (s1 > 0)
  39.         {
  40.             int s2 = (y2 - y1) * p2 + (x1 - x2) * q2 + (x2 * y1 - x1 * y2);
  41.             if (s2 <= 0)
  42.                 System.out.println("The lines intersect");
  43.             else if (s2 > 0)
  44.                 System.out.println("The lines doesn't intersect");
  45.         }
  46.         else
  47.             System.out.println("The point lies on the line");
  48.     }
  49. }

Output:

$ javac Line_Intersection.java
$ java Line_Intersection
 
The Equation of the line is : (2)x+(9)y+(-63) = 0
The Equation of the line is : (-4)x+(2)y+(-4) = 0
The lines doesn't intersect
 
The Equation of the line is : (-4)x+(-3)y+(43) = 0
The Equation of the line is : (-1)x+(-8)y+(73) = 0
The lines intersect

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.