- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to check if three points are collinear in C++

Given with three different valued points and the task is to check whether the points are collinear or not.

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.

**Input**

x1 = 1, x2 = 2, x3 = 3, y1 = 1, y2 = 4, y3 = 5

**Output**

no points are not collinear

**Input**

x1 = 1, y1 = 1, x2 = 1, y2 = 4, x3 = 1, y3 = 5

**Output**

points are collinear

## Approach used in the below program is as follow

Input the points as (x1, y1), (x2, y2), (x3, y3)

Apply the formula of area of triangle x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)

check for the conditions as −

if the area of triangle is 0 than print points are collinear

if the area of triangle is not 0 than print points are not collinear

print the final result

## Algorithm

Start Step 1→ declare function to check if points are collinear or not void check_collinear(int x1, int y1, int x2, int y2, int x3, int y3) declare int a = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2) IF (a == 0) Print "yes points are collinear" End Else Print "no points are not collinear" Step 2→ In main() Declare int x1 = 1, x2 = 2, x3 = 3, y1 = 1, y2 = 4, y3 = 5 Call check_collinear(x1, y1, x2, y2, x3, y3) Stop

## Example

#include <bits/stdc++.h> #include <math.h> #include <stdlib.h> using namespace std; //check if points are collinear or not void check_collinear(int x1, int y1, int x2, int y2, int x3, int y3){ int a = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2); if (a == 0) cout << "yes points are collinear"; else cout << "no points are not collinear"; } int main(){ int x1 = 1, x2 = 2, x3 = 3, y1 = 1, y2 = 4, y3 = 5; check_collinear(x1, y1, x2, y2, x3, y3); return 0; }

## Output

If run the above code it will generate the following output −

no points are not collinear

- Related Questions & Answers
- Finding if three points are collinear - JavaScript
- Java Program to Check if two of three Boolean variables are true
- C++ Program to Check if a Given Set of Three Points Lie on a Single Line or Not
- C Program to check if the points are parallel to X axis or Y axis
- C program to calculate distance between three points in 3D
- Check if points are inside ellipse faster than contains_point method (Matplotlib)
- Program to check three consecutive odds are present or not in Python
- Program to check points are forming convex hull or not in Python
- Program to check points are forming concave polygon or not in Python
- Count of triangles with total n points with m collinear in C++
- C# program to check if two matrices are identical
- Java Program to check if two dates are equal
- Java Program to Check if two strings are anagram
- C++ Program to check whether points in a 3-D plane are Coplanar
- Check if given four points form a Square

Advertisements