Java Program to Find the Position of a Point with Respect to a Line using Above-Below-on Test

«
»
This is a Java Program to check whether a point lies below, above or on the line. For any point t (xt, yt) on the plane, its position with respect to the line L connecting p and q is found by calculating the scalar s:
s = A xt + B yt + C
If s < 0, t lies in the clockwise halfplane of L; if s > 0, t lies on the counter-clockwise halfplane; if s = 0, t lies on L.
For example, the equation of the line connecting points (2, 2) and (4, 5) is -3x + 2y + 2 = 0. The point (6, 3) lies in the clockwise halfplane of this line, because (-3)(6) + (2)(3) + 2 = -10. Conversely, the point (0, 5) lies in the other halfplane as (-3)(0) +(2)(5) +2 = 12.

Here is the source code of the Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line. 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 check whether a point lies on, above or below the gievn line
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5. public class Position_Point_WRT_Line
  6. {
  7.     public static void main(String args[])
  8.     {
  9.         Random random = new Random();
  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 line is : (" + (y2 - y1)
  17.                 + ")x+(" + (x1 - x2) + ")y+(" + (x2 * y1 - x1 * y2) + ") = 0");
  18.  
  19.         System.out.println("Enter the point : <x>,<y>");
  20.         Scanner scan = new Scanner(System.in);
  21.         int x, y;
  22.         x = scan.nextInt();
  23.         y = scan.nextInt();
  24.  
  25.         int s = (y2 - y1) * x + (x1 - x2) * y + (x2 * y1 - x1 * y2);
  26.         if (s < 0)
  27.             System.out
  28.                     .println("The point lies below the line or left side of the line");
  29.         else if (s > 0)
  30.             System.out
  31.                     .println("The point lies above the line or right side of the line");
  32.         else
  33.             System.out.println("The point lies on the line");
  34.         scan.close();
  35.     }
  36. }

Output:

advertisement
$ javac Position_Point_WRT_Line.java
$ java Position_Point_WRT_Line
 
The Equation of the line is : (-2)x+(-9)y+(81) = 0
Enter the point : <x>,<y>
2
3
The point lies above the line or right side of the line

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

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

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 & technical discussions at Telegram SanfoundryClasses.