# Java Program to Check if Three Points are Collinear

In the previous article, we have discussed Java Program to Find Slope of a Line

In this article we will see how to check if three points are collinear or not.

## Java Program to Check if Three Points are Collinear

Before jumping into the program directly, let’s see first how we get the slope of a line.

Approach-1:

Formula for area of triangle :
0.5 * [x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)]

If formula for area of triangle is equal to zero then three points lie on a straight line.
Or, we can say 3 points are collinear.
Approach-2:

Slope of any pair of points :
(y3 - y2)/(x3 - x2) = (y2 - y1)/(x2 - x1)

When slope of any pair of points are same as other pair then three points lie on a straight line.
Or, we can say 3 points are collinear.

Let’s see different ways to check three points are collinear or not.

### Method-1: Java Program to Check if Three Points are Collinear By Finding Area of Triangle Formed

Approach:

1. Take the value for three points.
2. Find area of triangle formed by using formula 0.5 * [x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)]
3. Check if the area of triangle formed is equal to zero then three points are collinear.

Program:

public class Main
{
// Main method
public static void main(String args[])
{
double x1 = 1, x2 = 1, x3 = 1,
y1 = 1, y2 = 4, y3 = 5;

//checkCollinear() method called to check
//if 3 points lie on a stright line or not.
checkCollinear(x1, y1, x2, y2, x3, y3);

}

// checkCollinear Method to check if point collinear or not
static void checkCollinear(double x1, double y1, double x2,
double y2, double x3, double y3)
{

//calculating area
double area = 0.5*(x1 * (y2 - y3) +  x2 * (y3 - y1) +  x3 * (y1 - y2));
System.out.println("Area formed by triangle:"+area);

if (area == 0)
System.out.println("Area is equal to zero. So, 3 lines are collinear.");
else
System.out.println("Area is not equal to zero. So, 3 lines are not collinear.");
}
}

Output:

Area formed by triangle: 0.0
Area is equal to zero. So, 3 lines are collinear.

### Method-2: Java Program to Check if Three Points are Collinear By Finding Slope of Pairs

Approach:

1. Take the value for three points.
2. Check slope of any pair of point is equal to slope of other point i.e. ((y3 - y2) * (x2 - x1) == (y2 - y1) * (x3 - x2))
3. If both slope are equal then three points are collinear.

Program:

import java.io.*;

public class Main
{

// Main Method
public static void main (String[] args)
{
double x1 = 1, x2 = 1, x3 = 1,
y1 = 1, y2 = 4, y3 = 5;
//checkCollinear() method called to check
//if 3 points lie on a stright line or not.
checkColliner(x1, y1, x2, y2, x3, y3);
}

// Methodn to check if 3 points collinear or not
static void checkColliner(double x1, double y1, double x2, double y2, double x3, double y3)
{
if ((y3 - y2) * (x2 - x1) ==  (y2 - y1) * (x3 - x2))
System.out.println("Slope are equal. So, 3 lines are collinear.");
else
System.out.println("Slope are not equal. So, 3 lines are not collinear.");
}

}
Output:

Slope are equal. So, 3 lines are collinear.

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: