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:
The HttpMediaTypeNotAcceptableException in Spring MVC
Hướng dẫn Java Design Pattern – Factory Method
Spring Webflux with Kotlin
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
Netflix Archaius with Various Database Configurations
RestTemplate Post Request with JSON
How to Get All Spring-Managed Beans?
Java Program to Implement Kosaraju Algorithm
Java Program to Implement SimpeBindings API
Java 8 Predicate Chain
Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach
How To Serialize and Deserialize Enums with Jackson
Java Program to Find the Vertex Connectivity of a Graph
Java Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
A Guide to System.exit()
Java Program to Implement Leftist Heap
Java Program to Implement Bellman-Ford Algorithm
Một số từ khóa trong Java
Filtering a Stream of Optionals in Java
Java Program to Implement Sieve Of Atkin
Java Program to Optimize Wire Length in Electrical Circuit
Java – InputStream to Reader
Java Program to Implement Gauss Seidel Method
Overflow and Underflow in Java
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Write/Read cookies using HTTP and Read a file from the internet
Mockito and JUnit 5 – Using ExtendWith
Java Program to Implement Triply Linked List
Testing in Spring Boot
Java Program to Implement Interpolation Search Algorithm
Inheritance and Composition (Is-a vs Has-a relationship) in Java
Spring Boot - Admin Client