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:
Receive email using IMAP
Anonymous Classes in Java
Hướng dẫn Java Design Pattern – Bridge
Using a Spring Cloud App Starter
Hướng dẫn Java Design Pattern – Template Method
Java Program to Represent Graph Using Adjacency List
A Guide to JPA with Spring
Java Program to Generate All Subsets of a Given Set in the Gray Code Order
Java Program to Perform Sorting Using B-Tree
Optional trong Java 8
Java Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
Spring Boot - Code Structure
Java program to Implement Tree Set
Java Program to Check whether Undirected Graph is Connected using BFS
Hướng dẫn Java Design Pattern – DAO
Java Program to Evaluate an Expression using Stacks
Concurrent Test Execution in Spring 5
Java Program to Describe the Representation of Graph using Incidence Matrix
Spring Boot - Enabling Swagger2
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
Mệnh đề if-else trong java
Intro to the Jackson ObjectMapper
Converting Between an Array and a Set in Java
Java Program to Implement Disjoint Set Data Structure
The “final” Keyword in Java
Java Program to Check whether Directed Graph is Connected using DFS
Spring Boot - Thymeleaf
A Custom Media Type for a Spring REST API
Java Program to Implement Self organizing List
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
Java Program to Implement Maximum Length Chain of Pairs
Inheritance and Composition (Is-a vs Has-a relationship) in Java