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 CountDownLatch in Java
Guide to the Java Clock Class
Spring 5 Testing with @EnabledIf Annotation
Java – Rename or Move a File
New Features in Java 10
Spring 5 Functional Bean Registration
Java Program to Implement SimpeBindings API
Java String Conversions
Java Program to Implement Hash Tables Chaining with Doubly Linked Lists
Receive email using POP3
Sao chép các phần tử của một mảng sang mảng khác như thế nào?
What is Thread-Safety and How to Achieve it?
Immutable ArrayList in Java
Handling Errors in Spring WebFlux
Introduction to PCollections
Converting String to Stream of chars
Consuming RESTful Web Services
Java Program to Implement Disjoint Sets
Hướng dẫn Java Design Pattern – Singleton
Intersection of Two Lists in Java
Lớp Properties trong java
Java Program to Implement Ternary Heap
Spring Boot Integration Testing with Embedded MongoDB
DynamoDB in a Spring Boot Application Using Spring Data
Java Program to Perform Searching Using Self-Organizing Lists
Java Program to Implement Floyd-Warshall Algorithm
Spring Cloud – Securing Services
Arrays.asList vs new ArrayList(Arrays.asList())
Chuyển đổi từ HashMap sang ArrayList
Using Spring @ResponseStatus to Set HTTP Status Code
Spring Security Logout
Tạo ứng dụng Java RESTful Client với thư viện OkHttp