-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathConvex hull.cpp
57 lines (52 loc) · 1.39 KB
/
Convex hull.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
struct pt {
double x, y;
int id;
};
int orientation(pt a, pt b, pt c) {
double v = a.x * (b.y - c.y) +
b.x * (c.y - a.y) +
c.x * (a.y - b.y);
if (v < 0) return -1; // clockwise
if (v > 0) return +1; // counter-clockwise
return 0;
}
bool cw(pt a, pt b, pt c,
bool include_collinear) {
int o = orientation(a, b, c);
return o < 0 || (include_collinear && o == 0);
}
bool collinear(pt a, pt b, pt c) {
return orientation(a, b, c) == 0;
}
void convex_hull(vector<pt> &pts,
bool include_collinear = false) {
pt p0 = *min_element(all(pts), [](pt a, pt b) {
return make_pair(a.y, a.x) <
make_pair(b.y, b.x);
});
sort(all(pts), [&p0](const pt &a, const pt &b) {
int o = orientation(p0, a, b);
if (o == 0)
return (p0.x - a.x) * (p0.x - a.x) +
(p0.y - a.y) * (p0.y - a.y) <
(p0.x - b.x) * (p0.x - b.x) +
(p0.y - b.y) * (p0.y - b.y);
return o < 0;
});
if (include_collinear) {
int i = len(pts) - 1;
while (i >= 0 &&
collinear(p0, pts[i], pts.back()))
i--;
reverse(pts.begin() + i + 1, pts.end());
}
vector<pt> st;
for (int i = 0; i < len(pts); i++) {
while (st.size() > 1 &&
!cw(st[len(st) - 2], st.back(), pts[i],
include_collinear))
st.pop_back();
st.push_back(pts[i]);
}
pts = st;
}