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:

Guide to Java OutputStream
Đồng bộ hóa các luồng trong Java
Multi Dimensional ArrayList in Java
Serialization và Deserialization trong java
Simplify the DAO with Spring and Java Generics
Registration with Spring Security – Password Encoding
Java Program to Implement the Hill Cypher
Giới thiệu SOAP UI và thực hiện test Web Service
Hướng dẫn Java Design Pattern – Observer
Java Program to Implement Gift Wrapping Algorithm in Two Dimensions
Từ khóa throw và throws trong Java
Java Program to Print the Kind of Rotation the AVL Tree is Undergoing
Java Program to implement Associate Array
A Guide to JUnit 5 Extensions
Java Program to Implement Threaded Binary Tree
Java Program to Implement Quick Hull Algorithm to Find Convex Hull
Java Program for Douglas-Peucker Algorithm Implementation
Rest Web service: Filter và Interceptor với Jersey 2.x (P2)
Java Program to Implement Patricia Trie
Java Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
Java Program to Find Basis and Dimension of a Matrix
Quick Guide on Loading Initial Data with Spring Boot
Intro to Inversion of Control and Dependency Injection with Spring
Java Program to Solve TSP Using Minimum Spanning Trees
Java Program to Implement LinkedHashSet API
Spring Security and OpenID Connect
Java Program to Compute Discrete Fourier Transform Using Naive Approach
Java Program to Implement AVL Tree
Guide to Escaping Characters in Java RegExps
Spring Boot - Internationalization
Java Program to Check Multiplicability of Two Matrices
ETags for REST with Spring