Skip to content

Commit 01bd65e

Browse files
authored
Merge pull request #286 from neha030/dev8
Collinearity of three points added
2 parents bedc486 + 9b1f10c commit 01bd65e

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Problem Statement
3+
Given 3 co-ordinates , find if they are collinear or not .
4+
Three or more points are said to be collinear if they all lie on the same straight line.
5+
*/
6+
7+
#include <bits/stdc++.h>
8+
using namespace std;
9+
10+
struct Point {
11+
int x;
12+
int y;
13+
};
14+
15+
/*
16+
We can calculate the area formed by the three points, and if the area is
17+
zero then they lie on a same line.
18+
*/
19+
bool check_collinear(Point a, Point b, Point c) {
20+
int area = 0;
21+
22+
/*
23+
The Area of a Triangle formed by three points (x1, y1), (x2, y2), (x3, y3)
24+
is determined by the following formula
25+
26+
Area = (1/2) * {x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)}
27+
*/
28+
area = a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y);
29+
30+
if (area == 0)
31+
return true;
32+
else
33+
return false;
34+
}
35+
36+
int main() {
37+
int x, y;
38+
Point a, b, c;
39+
cout << "\nEnter the first co-ordinates: ";
40+
cin >> a.x >> a.y;
41+
cout << "Enter the second co-ordinates: ";
42+
cin >> b.x >> b.y;
43+
cout << "Enter the third co-ordinates: ";
44+
cin >> c.x >> c.y;
45+
46+
if (check_collinear(a, b, c)) {
47+
cout << "\nThe given points are collinear" << endl;
48+
} else {
49+
cout << "\nThe given points are not collinear" << endl;
50+
}
51+
return 0;
52+
}
53+
54+
/*
55+
Time Complexity: O(1)
56+
Space Complexity: O(1)
57+
58+
SAMPLE INPUT AND OUTPUT
59+
60+
Enter the first co-ordinates: 1 1
61+
Enter the second co-ordinates: 2 2
62+
Enter the third co-ordinates: 3 3
63+
64+
The given points are collinear
65+
*/

0 commit comments

Comments
 (0)