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 - Cloud Configuration Client
Overflow and Underflow in Java
Guide to BufferedReader
How to Find an Element in a List with Java
Jackson – Marshall String to JsonNode
Java Program to Create a Random Linear Extension for a DAG
Ép kiểu trong Java (Type casting)
A Guide to JPA with Spring
Java Program to Perform Insertion in a BST
Java Program to Check the Connectivity of Graph Using BFS
Java Program to Implement AttributeList API
Spring WebClient Filters
Composition, Aggregation, and Association in Java
Tránh lỗi NullPointerException trong Java như thế nào?
Logout in an OAuth Secured Application
Truyền giá trị và tham chiếu trong java
Working with Kotlin and JPA
Jackson – Change Name of Field
Map to String Conversion in Java
Connect through a Proxy
Apache Commons Collections Bag
How to Get All Dates Between Two Dates?
Java Program to Implement Counting Sort
Hướng dẫn sử dụng Printing Service trong Java
Flattening Nested Collections in Java
Java Program to Implement Splay Tree
So sánh HashMap và HashSet trong Java
Wrapper Classes in Java
Hướng dẫn Java Design Pattern – State
Guide to the Synchronized Keyword in Java
Tìm hiểu về Web Service
Spring Boot - Build Systems