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 Concurrency Interview Questions and Answers
Easy Ways to Write a Java InputStream to an OutputStream
Java Program to Compute Determinant of a Matrix
Sắp xếp trong Java 8
The DAO with JPA and Spring
Introduction to Spring Security Expressions
OAuth 2.0 Resource Server With Spring Security 5
A Guide to Java HashMap
Intro to the Jackson ObjectMapper
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Explain about URL and HTTPS protocol
Java Program to Perform Quick Sort on Large Number of Elements
Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach
Sử dụng CyclicBarrier trong Java
Introduction to Netflix Archaius with Spring Cloud
What is a POJO Class?
Spring Security 5 for Reactive Applications
Guide to Dynamic Tests in Junit 5
Spring AMQP in Reactive Applications
Java Program to Emulate N Dice Roller
Java Program to Implement Ford–Fulkerson Algorithm
Java Program to Implement Hash Tables with Linear Probing
Spring Boot - Zuul Proxy Server and Routing
Java Program to Implement Sorted Circularly Singly Linked List
Java Program to Implement Circular Singly Linked List
Spring Cloud – Tracing Services with Zipkin
Hướng dẫn Java Design Pattern – Iterator
Java Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
Redirect to Different Pages after Login with Spring Security
Lớp LinkedHashMap trong Java
Java Program to Perform Polygon Containment Test