凸包 Template

Gavin
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// #START - math/convex.hpp
#include <algorithm>
#include <vector>

template<typename T>
struct Point{
T x,y;
Point(T x=0,T y=0):x(x),y(y){}
Point operator+(const Point& o) const{return Point(x+o.x,y+o.y);}
Point operator-(const Point& o) const{return Point(x-o.x,y-o.y);}
bool operator==(const Point& o) const{return x==o.x&&y==o.y;}
T operator*(const Point& o) const{return x*o.x+y*o.y;}
T operator^(const Point& o) const{return x*o.y-y*o.x;}
bool operator<(const Point& o) const{
if(x==o.x){
return y<o.y;
}
return x<o.x;
}
};

// Andrew
template<typename T>
vector<Point<T>> convexHull(vector<Point<T>> pts){
int n=pts.size();
sort(pts.begin(),pts.end());
if(n<=2){
return pts;
}

vector<Point<T>> hull;
for(auto& p:pts){
while(hull.size()>=2&&((hull.back()-hull[hull.size()-2])^(p-hull.back()))<=0){
hull.pop_back();
}
hull.push_back(p);
}

size_t lwr=hull.size();
for(int i=pts.size()-2;i>=0;i--){
Point<T> p=pts[i];
while(hull.size()>lwr&&((hull.back()-hull[hull.size()-2])^(p-hull.back()))<=0){
hull.pop_back();
}
hull.push_back(p);
}

if(hull.size()>1){
hull.pop_back();
}
return hull;
}

template<typename T>
vector<Point<T>> standardize(const vector<Point<T>>& poly){
if(poly.empty()){
return poly;
}

int idx=0;
for(int i=1;i<(int)poly.size();i++){
if(poly[i].y<poly[idx].y||(poly[i].y==poly[idx].y&&poly[i].x<poly[idx].x)){
idx = i;
}
}

vector<Point<T>> ret;
for(int i=idx;i<(int)poly.size();i++){
ret.push_back(poly[i]);
}
for(int i=0;i<idx;i++){
ret.push_back(poly[i]);
}
return ret;
}

// Minkowski Sum
template<typename T>
vector<Point<T>> minkowskiSum(const vector<Point<T>>& a,const vector<Point<T>>& b){
vector<Point<T>> polyA=standardize(a),polyB=standardize(b);
int n=polyA.size(),m=polyB.size();
vector<Point<T>> edA,edB;
for(int i=0;i<n;i++){
edA.push_back(polyA[(i+1)%n]-polyA[i]);
}
for(int i=0;i<m;i++){
edB.push_back(polyB[(i+1)%m]-polyB[i]);
}

vector<Point<T>> merged;
int i=0,j=0;
while(i<n&&j<m){
ll cr=edA[i]^edB[j];
if(cr>0){
merged.push_back(edA[i]);
i++;
}
else if(cr<0){
merged.push_back(edB[j]);
j++;
}
else{
Point<T> sum=edA[i]+edB[j];
if(sum.x!=0||sum.y!=0){
merged.push_back(sum);
}
i++;
j++;
}
}

while(i<n){
merged.push_back(edA[i]);
i++;
}
while(j<m){
merged.push_back(edB[j]);
j++;
}

Point<T> cur=polyA[0]+polyB[0];
vector<Point<T>> c={cur};
for(auto& pt:merged){
cur = cur+pt;
c.push_back(cur);
}
if(c.size()>1&&c.back()==c[0]){
c.pop_back();
}
return c;
}
// #END - math/convex.hpp
  • 标题: 凸包 Template
  • 作者: Gavin
  • 创建于 : 2026-05-12 16:52:00
  • 更新于 : 2026-05-12 16:52:00
  • 链接: https://gavin-blog.pages.dev/2026/凸包-template/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
目录
凸包 Template