forked from Punyaslok/snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvex_hull.cpp
More file actions
29 lines (25 loc) · 782 Bytes
/
Copy pathconvex_hull.cpp
File metadata and controls
29 lines (25 loc) · 782 Bytes
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
// Convex hull (in 2D)
//
// Given a set of points, returns the subset which forms the convex hull.
// It is assumed that points are compared lexicographically by (x, y).
//
// Note: This implementation returns the smallest subset of points. If you
// want to also include points which are on the edges of the convex hull,
// change the `<=` to `<`
//
// Time complexity: O(N log N), where N is the number of points
vector<Pt> convex_hull(vector<Pt> pts) {
sort(pts.begin(), pts.end());
vector<Pt> H;
REP(step, 2) {
auto start = H.size();
for (Pt P : pts) {
while (H.size() >= start+2 && ccw(H[H.size()-2], H[H.size()-1], P) <= 0)
H.pop_back();
H.push_back(P);
}
H.pop_back();
reverse(pts.begin(), pts.end());
}
return H;
}