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

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.

//This is a java program to check whether a point lies on, above or below the gievn line
import java.util.Random;
import java.util.Scanner;
 
public class Position_Point_WRT_Line
{
    public static void main(String args[])
    {
        Random random = new Random();
        int x1, x2, y1, y2;
        x1 = random.nextInt(10);
        x2 = random.nextInt(10);
        y1 = random.nextInt(10);
        y2 = random.nextInt(10);
 
        System.out.println("The Equation of the line is : (" + (y2 - y1)
                + ")x+(" + (x1 - x2) + ")y+(" + (x2 * y1 - x1 * y2) + ") = 0");
 
        System.out.println("Enter the point : <x>,<y>");
        Scanner scan = new Scanner(System.in);
        int x, y;
        x = scan.nextInt();
        y = scan.nextInt();
 
        int s = (y2 - y1) * x + (x1 - x2) * y + (x2 * y1 - x1 * y2);
        if (s < 0)
            System.out
                    .println("The point lies below the line or left side of the line");
        else if (s > 0)
            System.out
                    .println("The point lies above the line or right side of the line");
        else
            System.out.println("The point lies on the line");
        scan.close();
    }
}

Output:

$ 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

Related posts:

Using JWT with Spring Security OAuth
Java Program to Implement Hash Trie
Java Program to Implement the Bin Packing Algorithm
Jackson Exceptions – Problems and Solutions
Send email with SMTPS (eg. Google GMail)
Java Program to Implement Park-Miller Random Number Generation Algorithm
Spring Boot - Twilio
Truyền giá trị và tham chiếu trong java
Prevent Brute Force Authentication Attempts with Spring Security
Java Program to Implement Suffix Array
Java Program to Implement Queue using Two Stacks
Java Program to Implement Iterative Deepening
Using JWT with Spring Security OAuth (legacy stack)
Một số ký tự đặc biệt trong Java
A Guide to the Java ExecutorService
The Thread.join() Method in Java
Java Program to Represent Graph Using Incidence List
Java Program to Implement Fermat Factorization Algorithm
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Apache Tiles Integration with Spring MVC
Guide to Spring @Autowired
The Difference Between Collection.stream().forEach() and Collection.forEach()
Optional trong Java 8
Java Program to Check for balanced parenthesis by using Stacks
Lập trình đa luồng với CompletableFuture trong Java 8
Copy a List to Another List in Java
A Guide to WatchService in Java NIO2
Integer Constant Pool trong Java
Automatic Property Expansion with Spring Boot
Java Web Services – Jersey JAX-RS – REST và sử dụng REST API testing tools với Postman
Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach
Spring Boot Security Auto-Configuration