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:

Java Scanner hasNext() vs. hasNextLine()
Custom Cascading in Spring Data MongoDB
A Guide to the Java ExecutorService
Running Spring Boot Applications With Minikube
Java Program to do a Breadth First Search/Traversal on a graph non-recursively
Call Methods at Runtime Using Java Reflection
OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS
Java Program to Evaluate an Expression using Stacks
Tính đóng gói (Encapsulation) trong java
Spring Boot - Code Structure
Java Program to Solve Knapsack Problem Using Dynamic Programming
Java Program to Perform Right Rotation on a Binary Search Tree
Java Program to Search for an Element in a Binary Search Tree
Java Program to Solve any Linear Equations
Compare Two JSON Objects with Jackson
A Guide to WatchService in Java NIO2
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Batch Processing with Spring Cloud Data Flow
Guide to Mustache with Spring Boot
Java Program to Implement Sorted Doubly Linked List
Introduction to Spring Cloud Netflix – Eureka
Java Program to Find Path Between Two Nodes in a Graph
ThreadPoolTaskExecutor corePoolSize vs. maxPoolSize
Consumer trong Java 8
Java Program to Implement Skew Heap
Exploring the Spring 5 WebFlux URL Matching
Sử dụng CountDownLatch trong Java
Introduction to PCollections
Quick Guide to @RestClientTest in Spring Boot
Java Program to Implement Maximum Length Chain of Pairs
How to Get All Dates Between Two Dates?
Spring Boot - Rest Controller Unit Test