C++ Program to Check if a Given Set of Three Points Lie on a Single Line or Not



In this problem, you are given three coordinates in a 2D plane, and you need to check if these three points are collinear, meaning they lie on a single straight line. There are two approaches to solve this problem. In this article, we will explain both the approaches with example code in C++.

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

// Output
The points are collinear.

Check if Three Points are Collinear

If three points lie on a single line, then the points are called as collinear points. To check if three points are collinear, we can use the concept of the area of a triangle and slope of a line.

Using Area of Triangle to Check Collinearity

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 Code in C++

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.

Using Slope of Line to Check Collinearity

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 Code in C++

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-16T18:09:45+05:30

767 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements