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 Program to Implement First Fit Decreasing for 1-D Objects and M Bins
Hướng dẫn Java Design Pattern – Command
Show Hibernate/JPA SQL Statements from Spring Boot
Object cloning trong java
Java – Reader to Byte Array
Hướng dẫn Java Design Pattern – Composite
The Spring @Controller and @RestController Annotations
Luồng Daemon (Daemon Thread) trong Java
Hashing a Password in Java
The Dining Philosophers Problem in Java
Java Program to Perform Addition Operation Using Bitwise Operators
Setting a Request Timeout for a Spring REST API
OAuth 2.0 Resource Server With Spring Security 5
Java Program to Find Path Between Two Nodes in a Graph
Java Program to Implement Shunting Yard Algorithm
Java 9 Stream API Improvements
Java Program to Implement Splay Tree
Java Program to Generate All Pairs of Subsets Whose Union Make the Set
A Quick Guide to Using Keycloak with Spring Boot
Anonymous Classes in Java
Java Program to Implement LinkedHashSet API
Apache Commons Collections Bag
Hướng dẫn sử dụng Printing Service trong Java
Mảng (Array) trong Java
Java Program to Check if an UnDirected Graph is a Tree or Not Using DFS
HttpClient 4 – Send Custom Cookie
Refactoring Design Pattern với tính năng mới trong Java 8
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
The Order of Tests in JUnit
Java Program to Implement Bucket Sort
Java Program to Implement EnumMap API
Lớp Arrarys trong Java (Arrays Utility Class)