-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeometry.cpp
More file actions
86 lines (62 loc) · 1.58 KB
/
Geometry.cpp
File metadata and controls
86 lines (62 loc) · 1.58 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*** Point Structure ***/
class point
{
public:
int x, y;
/*** Use for Polar Sort ***/
bool operator < (point b)
{
if (y != b.y)
return y < b.y;
return x < b.x;
}
/*** Use for Montone Chain ***/
bool operator < (point b)
{
return (x < b.x || (x == b.x && y < b.y));
}
};
double dist(point a, point b)
{
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
long long area(point a, point b, point c)
{
return (a.x - b.x) * (b.y - c.y) - (a.y - b.y) * (b.x - c.x);
}
/*** -1 = anti - clockwise, 0 = co - linear, 1 = clockwise ***/
int clockwiseCheck(point a, point b, point c)
{
int area = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
if (area > 0)
return -1;
else if (area < 0)
return 1;
return 0;
}
/*** Used in Graham Scan to Sort the Point according to their Polar Angle ***/
bool polarOrder(point a, point b)
{
int order = clockwiseCheck(pivot, a, b);
if (order == 0)
return dist(pivot, a) < dist(pivot, b);
return (order == -1);
}
/*** Montone Chain Method for finding Convex Hull ***/
void convexHull()
{
sort(arr, arr + n);
for (int i = 0; i < n; ++i)
{
while (idx > 1 && area(cvx[idx - 2], cvx[idx - 1], arr[i]) <= 0)
--idx;
cvx[idx++] = arr[i];
}
int var = idx;
for (int i = n - 2; i >= 0; --i)
{
while (idx > var && area(cvx[idx - 2], cvx[idx - 1], arr[i]) <= 0)
--idx;
cvx[idx++] = arr[i];
}
}