How To Check If Three Points are Collinear in Java?


Three points are said to be collinear if all the three points lie on a straight line. If the points do not lie on the same straight line, then they are not collinear points.

It means if three points (x1, y1), (x2, y2), (x3, y3) are on the same straight line then collinear.

Where, x1, y1, x2, y2, x3, y3 are the points on x and y axis and (x1, y1), (x2, y2), (x3, y3) are the coordinates.

Mathematically, there are two ways to know that three points are collinear or not.

By finding the area of a triangle using the points, if the area of the triangle is zero then three points are collinear.

Formula to find area of triangle = 0.5 * [x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)]

By finding slopes of both points are equal then all the three points are collinear.

Formula to find slope =
Slope of (x1, y1), (x2, y2)
m1 = (y2-y1) / (x2-x1)
Slope of (x2, y2), (x3, y3)
m2 = (y3-y2) / (x3-x2)

In this article we will see how we can check whether three points are collinear or not by using Java programming language.

To show you some instances

Instance-1

Suppose the given coordinates are (1,2), (3,4), (5,6)

All three points are collinear as they lie on the same straight line.

Instance-2

Suppose the given coordinates are (1,1), (1,4), (1,6)

All three points are collinear as they lie on the same straight line.

Instance-3

Suppose the given coordinates are (1,1), (2,4), (4,6)

All three points are not collinear as they do not lie on the same straight line.

Algorithm

  • Step 1 − Get the three points either by user input or by initialization.

  • Step 2 − By using any of the above said formula either check if triangle area is zero or if slopes are same then print three points are collinear else three points are not collinear.

  • Step 3 − Print the result.

Multiple Approaches

We have provided the solution in different approaches.

  • By Finding Triangle Area.

  • By Finding Slope.

Let’s see the program along with its output one by one

Approach-1: By Finding Triangle Area

In this approach, three points will be initialized in the program. Then calculate the area of the triangle by using the formula. If area is zero, then print three points are collinear.

Example

public class Main{ //main method public static void main(String args[]){ //initialized first point double x1 = 1; double y1 = 2; System.out.println("First point: "+x1+", "+y1); //initialized second point double x2 = 3; double y2 = 4; System.out.println("Second point: "+x2+", "+y2); //initialized third point double x3 = 5; double y3 = 6; System.out.println("Third point: "+x3+", "+y3); //find triangle area by using formula double triangleArea = 0.5*(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)); System.out.println("Area of triangle using three points ="+triangleArea); if (triangleArea == 0) System.out.println("Three points are collinear."); else System.out.println("Three points are not collinear."); } }

Output

First point: 1.0, 2.0
Second pointe: 3.0, 4.0
Third pointe: 5.0, 6.0
Area of triangle using three points = 0.0
Three points are collinear.
.

Approach-2: By Finding Slope

In this approach, three points will be initialized in the program. Then calculate the slope of any pair of points and check if slope is equal with slope of other pair of points by using the slope formula. If both slopes are equal then print three points are collinear.

Example

public class Main{ //main method public static void main(String args[]){ //initialized first point double x1 = 1; double y1 = 2; System.out.println("First point: "+x1+", "+y1); //initialized second point double x2 = 3; double y2 = 4; System.out.println("Second point: "+x2+", "+y2); //initialized third point double x3 = 5; double y3 = 6; System.out.println("Third point: "+x3+", "+y3); //find slope of (x1, y1) , (x2, y2) double m1 = (y2-y1) / (x2-x1); //find slope of (x2, y2) , (x3, y3) double m2 = (y3-y2) / (x3-x2); System.out.println("Slope of first pair= " + m1); System.out.println("Slope of second pair= " + m2); if (m1 == m2) System.out.println("Three points are collinear."); else System.out.println("Three points are not collinear."); } }

Output

First point: 1.0, 2.0
Second point: 3.0, 4.0
Third point: 5.0, 6.0
Slope of first pair= 1.0
Slope of second pair= 1.0
Three points are collinear.

In this article, we explored how to check if three points are collinear or not in Java by using different approaches.

Updated on: 27-Oct-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements