-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeometry.cpp
More file actions
53 lines (43 loc) · 1.24 KB
/
geometry.cpp
File metadata and controls
53 lines (43 loc) · 1.24 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
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
// Point Structure
struct Point {
long long x, y;
};
// CCW (Counter Clockwise)
// 양수: 반시계, 음수: 시계, 0: 일직선
long long ccw(Point a, Point b, Point c) {
return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}
// Convex Hull (Monotone Chain)
// 반환: 껍질을 구성하는 점들 (반시계 방향 정렬)
vector<Point> convexHank(vector<Point> &points) {
int n = points.size();
if (n <= 2)
return points;
// x좌표 오름차순 (같으면 y 오름차순) 정렬
sort(points.begin(), points.end(),
[](Point a, Point b) { return a.x < b.x || (a.x == b.x && a.y < b.y); });
vector<Point> hull;
// Lower Hull
for (int i = 0; i < n; i++) {
while (hull.size() >= 2 &&
ccw(hull[hull.size() - 2], hull.back(), points[i]) <= 0) {
hull.pop_back();
}
hull.push_back(points[i]);
}
// Upper Hull
int lower_size = hull.size();
for (int i = n - 2; i >= 0; i--) {
while (hull.size() > lower_size &&
ccw(hull[hull.size() - 2], hull.back(), points[i]) <= 0) {
hull.pop_back();
}
hull.push_back(points[i]);
}
hull.pop_back(); // 시작점 중복 제거
return hull;
}