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 NoSuchBeanDefinitionException
Jackson Unmarshalling JSON with Unknown Properties
Java Program to Find the Peak Element of an Array O(n) time (Naive Method)
Using a List of Values in a JdbcTemplate IN Clause
How to Break from Java Stream forEach
Java Program to Implement Sieve Of Atkin
Guide to Escaping Characters in Java RegExps
Read an Outlook MSG file
Sort a HashMap in Java
Introduction to Spring Cloud Rest Client with Netflix Ribbon
Java Program to Implement Binary Heap
Java Program to Print the Kind of Rotation the AVL Tree is Undergoing
Tránh lỗi NullPointerException trong Java như thế nào?
Java Program to Implement Dijkstra’s Algorithm using Priority Queue
Guide to Java 8 groupingBy Collector
Intersection of Two Lists in Java
Luồng Daemon (Daemon Thread) trong Java
Difference Between Wait and Sleep in Java
Changing Annotation Parameters At Runtime
Java Program to Find a Good Feedback Edge Set in a Graph
Hướng dẫn Java Design Pattern – Singleton
Documenting a Spring REST API Using OpenAPI 3.0
Java Program to add two large numbers using Linked List
How to Iterate Over a Stream With Indices
Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching
Java Program to Implement PrinterStateReasons API
An Intro to Spring Cloud Contract
ClassNotFoundException vs NoClassDefFoundError
Java Program to Implement Johnson’s Algorithm
Checking for Empty or Blank Strings in Java
Spring Boot - Sending Email
Comparing Objects in Java