Check If Three Points Are Collinear in C++



Given coordinates of three points in 2D plane, and the task is to check whether the points are collinear or not.

For Example,

// Input: Coordinates of three points 
points = {{2, 3}, {4, 6}, {6, 9}};

// Output
The points are collinear.

What are Collinear Points?

Points are said to be collinear if they lie on the same line and they are not collinear if they are on the different lines. Given below is the figure of collinear and non-collinear points.

collinear points

Checking Collinearity with Area of Triangle

To form a triangle, we need three points. If the area of the triangle formed by these three points is zero, then the points are laying on a single line. So in this method, we will check the area of the triangle formed by the given three points. If the area is zero, then the points are collinear.

To find the area of a triangle formed by three points (x1, y1), (x2, y2), and (x3, y3), we can use the formula:

Area = 0.5 * |x1(y2 - y3) + x2(y3 - y1) + x3(y1 - y2)|

Example

The code below defines a function areCollinear that takes the coordinates of three points as input and returns true if the points are collinear, otherwise false.

#include <iostream>
using namespace std;

bool areCollinear(int x1, int y1, int x2, int y2, int x3, int y3) {
    // Calculate the area of the triangle formed by the three points
    int area = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2);
    
    // If the area is zero, the points are collinear
    return (area == 0);
}

int main() {
    int x1 = 2, y1 = 3;
    int x2 = 4, y2 = 6;
    int x3 = 6, y3 = 9;

    if (areCollinear(x1, y1, x2, y2, x3, y3)) {
        cout << "The points are collinear." << endl;
    } else {
        cout << "The points are not collinear." << endl;
    }

    return 0;
}

The output of the above code will be:

The points are collinear.

Checking Collinearity with Slope of Line

In this method, we will calculate the slope of the line formed by the first two points and compare it with the slope of the line formed by the first and third points. If both slopes are equal, then the three points are collinear.

The slope of a line formed by two points (x1, y1) and (x2, y2) is given by the formula:

Slope = (y2 - y1) / (x2 - x1)

Example

The code below define a function areCollinear that takes the coordinates of three points as input and returns true if the points are collinear, otherwise false.

#include <iostream>
using namespace std;

bool areCollinear(int x1, int y1, int x2, int y2, int x3, int y3) {
    // Calculate the slope of the line formed by the first two points
    double slope1 = (double)(y2 - y1) / (x2 - x1);
    
    // Calculate the slope of the line formed by the first and third points
    double slope2 = (double)(y3 - y1) / (x3 - x1);
    
    // If both slopes are equal, the points are collinear
    return (slope1 == slope2);
}

int main() {
    int x1 = 2, y1 = 3;
    int x2 = 4, y2 = 6;
    int x3 = 6, y3 = 9;

    if (areCollinear(x1, y1, x2, y2, x3, y3)) {
        cout << "The points are collinear." << endl;
    } else {
        cout << "The points are not collinear." << endl;
    }

    return 0;
}

The output of the above code will be:

The points are collinear.
Farhan Muhamed
Farhan Muhamed

No Code Developer, Vibe Coder

Updated on: 2025-06-17T19:15:26+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements