-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathcheck_collinearity_of_three_points.c
65 lines (55 loc) · 1.36 KB
/
check_collinearity_of_three_points.c
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
63
64
65
// C program to check collinearity of three given points
#include <stdio.h>
#include <stdbool.h>
typedef struct Point
{
int x;
int y;
} Point;
/*
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;
printf("\nEnter the first co-ordinates: ");
scanf("%d %d", &a.x, &a.y);
printf("Enter the second co-ordinates: ");
scanf("%d %d", &b.x, &b.y);
printf("Enter the third co-ordinates: ");
scanf("%d %d", &c.x, &c.y);
if (check_collinear(a, b, c))
{
printf("\nThe given points are collinear\n");
}
else
{
printf("\nThe given points are not collinear");
}
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
*/