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:
“Stream has already been operated upon or closed” Exception in Java
Java Program to Implement Singly Linked List
Creating Docker Images with Spring Boot
@DynamicUpdate with Spring Data JPA
Spring AMQP in Reactive Applications
Java Program to Generate Random Numbers Using Middle Square Method
The Modulo Operator in Java
Kết hợp Java Reflection và Java Annotations
Java Program to Implement Caesar Cypher
Extract links from an HTML page
How to Read a File in Java
Java Program to Implement DelayQueue API
Java Program to Implement Cubic convergence 1/pi Algorithm
Write/Read cookies using HTTP and Read a file from the internet
Java 8 Stream API Analogies in Kotlin
Java Program to Compute Cross Product of Two Vectors
Guide to java.util.concurrent.BlockingQueue
Java Program to Implement SimpeBindings API
Tính kế thừa (Inheritance) trong java
Java Program to Check Whether a Given Point is in a Given Polygon
Các nguyên lý thiết kế hướng đối tượng – SOLID
Registration – Activate a New Account by Email
Supplier trong Java 8
Java Program to Implement HashMap API
Java Program to Implement Sorted Array
How to Get All Spring-Managed Beans?
Java Program to Find Maximum Element in an Array using Binary Search
Comparing Two HashMaps in Java
Java Program to Implement Stack using Two Queues
Remove All Occurrences of a Specific Value from a List
Java Program to Solve the Fractional Knapsack Problem
Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset