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 Check Whether Graph is DAG
Service Registration with Eureka
Properties with Spring and Spring Boot
Encode a String to UTF-8 in Java
Biến trong java
Java Program to Implement Gauss Seidel Method
Java Program to Generate Random Numbers Using Multiply with Carry Method
Finding Max/Min of a List or Collection
Java Program for Topological Sorting in Graphs
Spring Boot - Service Components
Java Program to Check Whether a Directed Graph Contains a Eulerian Path
Java Program to Use the Bellman-Ford Algorithm to Find the Shortest Path
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
HttpClient 4 – Send Custom Cookie
Spring Boot Integration Testing with Embedded MongoDB
Java Program to Represent Graph Using Adjacency Matrix
Documenting a Spring REST API Using OpenAPI 3.0
Java Program to Find Number of Articulation points in a Graph
Hướng dẫn Java Design Pattern – Composite
SOAP Web service: Authentication trong JAX-WS
Spring – Injecting Collections
Java Program to Solve TSP Using Minimum Spanning Trees
Apache Commons Collections Bag
Creating a Web Application with Spring 5
Hướng dẫn Java Design Pattern – Prototype
A Guide to Concurrent Queues in Java
Logging a Reactive Sequence
Java Program to Implement IdentityHashMap API
HttpClient Connection Management
Custom Error Pages with Spring MVC
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Object Type Casting in Java