Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not

This is a Java Program to check whether three points are collinear or not. We do this by taking two points make an equation of the line passing through those two points and check whether third points lies on it. In geometry, collinearity is a property of a set of points, specifically, the property of lying on a single line.

Here is the source code of the Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not. 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 three points are collinear or not
import java.util.Scanner;
 
public class Collinear_Points
{
    public static void main(String args[])
    {
        System.out.println("Enter the points : <x>,<y>");
        Scanner scan = new Scanner(System.in);
        int x, y, x1, x2, y1, y2;
        x = scan.nextInt();
        y = scan.nextInt();
        x1 = scan.nextInt();
        x2 = scan.nextInt();
        y1 = scan.nextInt();
        y2 = scan.nextInt();
 
        /*
         * System.out.println("The Equation of the line is : (" + (y2 - y1) +
         * ")x+(" + (x1 - x2) + ")y+(" + (x2 * y1 - x1 * y2) + ") = 0");
         */
 
        int s = (y2 - y1) * x + (x1 - x2) * y + (x2 * y1 - x1 * y2);
        if (s < 0)
            System.out.println("The points are NOT collinear");
        else if (s > 0)
            System.out.println("The points are NOT collinear");
        else
            System.out.println("The points are collinear");
        scan.close();
    }
}

Output:

$ javac Collinear_Points.java
$ java Collinear_Points
 
Enter the points : <x>,<y>
3 2
1 3
1 5
The points are NOT collinear
 
Enter the points : <x>,<y>
1 1
1 5
1 9
The points are collinear

Related posts:

Jackson – Marshall String to JsonNode
Array to String Conversions
New Features in Java 8
Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching
Spring Data – CrudRepository save() Method
Java Program to Generate a Random UnDirected Graph for a Given Number of Edges
Prevent Brute Force Authentication Attempts with Spring Security
Java Program to Implement RoleList API
The Difference Between Collection.stream().forEach() and Collection.forEach()
A Guide to System.exit()
Java Program to Generate All Pairs of Subsets Whose Union Make the Set
Constructor Dependency Injection in Spring
Introduction to Spring Cloud Rest Client with Netflix Ribbon
Java Program to Find the Connected Components of an UnDirected Graph
Adding Shutdown Hooks for JVM Applications
Using Spring @ResponseStatus to Set HTTP Status Code
Filtering and Transforming Collections in Guava
Java Program to Implement Counting Sort
Spring Security Registration – Resend Verification Email
Getting the Size of an Iterable in Java
Removing all Nulls from a List in Java
Hướng dẫn Java Design Pattern – Strategy
Java Program to Implement SimpeBindings API
Guide to CountDownLatch in Java
Request a Delivery / Read Receipt in Javamail
Java Program to Find a Good Feedback Edge Set in a Graph
Giới thiệu Google Guice – Aspect Oriented Programming (AOP)
Java Program to Implement Graph Structured Stack
Tìm hiểu về xác thực và phân quyền trong ứng dụng
Java Program to Create a Random Linear Extension for a DAG
Java Program to Implement Borwein Algorithm
Auditing with JPA, Hibernate, and Spring Data JPA