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:
Spring Boot - Securing Web Applications
Java Program to Implement Find all Forward Edges in a Graph
Versioning a REST API
Java Program to Generate Random Numbers Using Middle Square Method
Quick Guide to Spring MVC with Velocity
Java Program to Implement Hopcroft Algorithm
Java Program to Implement the Vigenere Cypher
Java Program to Implement Strassen Algorithm
Spring Data JPA and Null Parameters
Guide to java.util.concurrent.BlockingQueue
Java Program to Implement Jarvis Algorithm
Optional trong Java 8
OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS
Java Program to Perform Search in a BST
Hướng dẫn sử dụng Printing Service trong Java
Java Program to Generate N Number of Passwords of Length M Each
Java Program to Implement Find all Back Edges in a Graph
Java Program to Find a Good Feedback Edge Set in a Graph
Java – Write a Reader to File
Java Program to Perform Partition of an Integer in All Possible Ways
Java Program to Check if a Given Graph Contain Hamiltonian Cycle or Not
Java Program to Solve a Matching Problem for a Given Specific Case
Introduction to Spring Cloud OpenFeign
Hướng dẫn Java Design Pattern – Dependency Injection
REST Web service: Basic Authentication trong Jersey 2.x
Java InputStream to Byte Array and ByteBuffer
Tìm hiểu về Web Service
Spring Boot - Zuul Proxy Server and Routing
Java Program to Implement Borwein Algorithm
Luồng Daemon (Daemon Thread) trong Java
Prevent Brute Force Authentication Attempts with Spring Security
JUnit 5 @Test Annotation