Java Program to Check Whether a Given Points are Colinear or Not

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.

  1. //This is a java program to check whether three points are collinear or not
  2. import java.util.Scanner;
  3.  
  4. public class Collinear_Points
  5. {
  6.     public static void main(String args[])
  7.     {
  8.         System.out.println("Enter the points : <x>,<y>");
  9.         Scanner scan = new Scanner(System.in);
  10.         int x, y, x1, x2, y1, y2;
  11.         x = scan.nextInt();
  12.         y = scan.nextInt();
  13.         x1 = scan.nextInt();
  14.         x2 = scan.nextInt();
  15.         y1 = scan.nextInt();
  16.         y2 = scan.nextInt();
  17.  
  18.         /*
  19.          * System.out.println("The Equation of the line is : (" + (y2 - y1) +
  20.          * ")x+(" + (x1 - x2) + ")y+(" + (x2 * y1 - x1 * y2) + ") = 0");
  21.          */
  22.  
  23.         int s = (y2 - y1) * x + (x1 - x2) * y + (x2 * y1 - x1 * y2);
  24.         if (s < 0)
  25.             System.out.println("The points are NOT collinear");
  26.         else if (s > 0)
  27.             System.out.println("The points are NOT collinear");
  28.         else
  29.             System.out.println("The points are collinear");
  30.         scan.close();
  31.     }
  32. }

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

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.