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:
Guide to the Volatile Keyword in Java
Ignore Null Fields with Jackson
Spring Security – security none, filters none, access permitAll
Find the Registered Spring Security Filters
Introduction to Spring Data JDBC
Java Program to Implement Stack using Two Queues
The StackOverflowError in Java
Spring Data – CrudRepository save() Method
Request Method Not Supported (405) in Spring
Java – Combine Multiple Collections
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Spring NoSuchBeanDefinitionException
Java Program to Implement TreeMap API
Apache Commons Collections Bag
Java Program to Implement Aho-Corasick Algorithm for String Matching
Send email with authentication
Guava CharMatcher
Kiểu dữ liệu Ngày Giờ (Date Time) trong java
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
Java Program to Check whether Undirected Graph is Connected using BFS
Java Program to Implement Randomized Binary Search Tree
Giới thiệu Aspect Oriented Programming (AOP)
Java – InputStream to Reader
New Stream Collectors in Java 9
Hướng dẫn Java Design Pattern – Facade
Java Program to Implement Caesar Cypher
Custom JUnit 4 Test Runners
Spring Boot - Creating Docker Image
Java Program to find the maximum subarray sum O(n^2) time(naive method)
Class Loaders in Java
Guide to CopyOnWriteArrayList
Programmatic Transaction Management in Spring