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:
Java Program to Implement Singly Linked List
Java Program to Implement the Bin Packing Algorithm
Iterating over Enum Values in Java
Java Program to Solve any Linear Equations
Java Program to Implement Hash Tables Chaining with List Heads
A Guide to Spring Boot Admin
Read an Outlook MSG file
Apache Camel with Spring Boot
Custom Thread Pools In Java 8 Parallel Streams
New Features in Java 15
How to Read HTTP Headers in Spring REST Controllers
Java Program to Implement Network Flow Problem
Hướng dẫn tạo và sử dụng ThreadPool trong Java
Vector trong Java
The Modulo Operator in Java
Hướng dẫn Java Design Pattern – Composite
Java Program to Implement ConcurrentHashMap API
Java Program to Implement Brent Cycle Algorithm
Lập trình đa luồng với CompletableFuture trong Java 8
Pagination and Sorting using Spring Data JPA
Spring Boot - Bootstrapping
Converting a List to String in Java
Instance Profile Credentials using Spring Cloud
An Intro to Spring Cloud Contract
Deque và ArrayDeque trong Java
Sorting in Java
Spring Boot - Twilio
Guide to ThreadLocalRandom in Java
Spring Boot - Database Handling
Java Program to Perform Complex Number Multiplication
Uploading MultipartFile with Spring RestTemplate
Encode/Decode to/from Base64