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:

Concrete Class in Java
Java Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected
Transaction Propagation and Isolation in Spring @Transactional
Xây dựng ứng dụng Client-Server với Socket trong Java
Service Registration with Eureka
CharSequence vs. String in Java
Map Serialization and Deserialization with Jackson
Lớp lồng nhau trong java (Java inner class)
Anonymous Classes in Java
Get the workstation name or IP
Java Program to Describe the Representation of Graph using Incidence List
Testing an OAuth Secured API with Spring MVC
Java Program to Perform the Shaker Sort
A Guide to Java HashMap
Tính kế thừa (Inheritance) trong java
Hướng dẫn Java Design Pattern – Flyweight
Convert Hex to ASCII in Java
Introduction to the Functional Web Framework in Spring 5
Đồng bộ hóa các luồng trong Java
Spring Boot - Enabling Swagger2
Using a List of Values in a JdbcTemplate IN Clause
An Intro to Spring Cloud Security
Java Program to Implement Euclid GCD Algorithm
Java Program to find the maximum subarray sum using Binary Search approach
Java Program to Implement Hash Tables with Double Hashing
Custom Thread Pools In Java 8 Parallel Streams
Java Program to Generate a Graph for a Given Fixed Degree Sequence
Java Program to Generate All Possible Combinations Out of a, b, c, d, e
Java Program to Compute the Volume of a Tetrahedron Using Determinants
Cài đặt và sử dụng Swagger UI
Spring Security and OpenID Connect
So sánh HashSet, LinkedHashSet và TreeSet trong Java