-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
check_collinearity_of_three_points.cpp
62 lines (49 loc) · 1.33 KB
/
check_collinearity_of_three_points.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// C++ program to check collinearity of three given points
#include <bits/stdc++.h>
using namespace std;
struct Point {
int x;
int y;
};
/*
We can calculate the area formed by the three points, and if the area is
zero then they lie on a same line.
*/
bool check_collinear(Point a, Point b, Point c) {
int area = 0;
/*
The Area of a Triangle formed by three points (x1, y1), (x2, y2), (x3, y3)
is determined by the following formula
Area = (1/2) * {x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)}
*/
area = a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y);
if (area == 0)
return true;
else
return false;
}
int main() {
int x, y;
Point a, b, c;
cout << "\nEnter the first co-ordinates: ";
cin >> a.x >> a.y;
cout << "Enter the second co-ordinates: ";
cin >> b.x >> b.y;
cout << "Enter the third co-ordinates: ";
cin >> c.x >> c.y;
if (check_collinear(a, b, c)) {
cout << "\nThe given points are collinear" << endl;
} else {
cout << "\nThe given points are not collinear" << endl;
}
return 0;
}
/*
Time Complexity: O(1)
Space Complexity: O(1)
SAMPLE INPUT AND OUTPUT
Enter the first co-ordinates: 1 1
Enter the second co-ordinates: 2 2
Enter the third co-ordinates: 3 3
The given points are collinear
*/