
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- How To Check If Three Points are Collinear in Java?
- Finding if three points are collinear - JavaScript
- Determine if the points (1,5) (2,3) and (−2,−11) are collinear.
- Java Program to Check if two of three Boolean variables are true
- Swift Program to Check if two of three boolean variables are true
- What are Collinear Points and Concurrent Lines ?
- How to draw collinear points?
- If the points ( (-8,4),(-2,4) ) and ( (5, a) ) are collinear points, find the value of ( a ).
- Haskell Program to Check if two of the 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
- Name the line segments determined by the three collinear points P, Q and R.
- Program to check points are forming convex hull or not in Python
- Program to check points are forming concave polygon or not in Python
- Find the value of $k$ if points $(k, 3), (6, -2)$ and $(-3, 4)$ are collinear.

Advertisements