跳到正文

算法·理论

凸包 tr

shimizu_kiouka约 13 分钟
原文
文章信息与版本

文章信息

发布
2026年5月19日
收录
2026年5月19日
更新
2026年10月27日
篇幅
约 13 分钟

版本

  1. v1当前

pre:本文较为写意。无图,偏记录为主。

二维凸包

平面上一堆点,求多边形包含所有点的最小周长。

我们先点一堆点。

从最无脑的开始想,沿着每个点连线。

开始优化,注意到两边之和大于第三边,所以可以优化掉一些点。

注意到优化掉的这些点在内角的度数都大于180deg,所以维护一个栈保持点集向一个方向转。

这边使用 Graham 求凸包。根据定义可知,yminy_min 的点在凸包上。

我们使用atan2求极角并排序,则遍历个点,依次压入栈中,如果发现转角了则弹出。

P2742 【模板】二维凸包 / [USACO5.1] 圈奶牛Fencing the Cows

以此为样题,上模版。

C++
#include <bits/stdc++.h>
using namespace std;
const int MAXN=1e5+1e4;
struct po{
	double x,y,ang;
	po operator-(const po& p)const{return{x-p.x,y-p.y,0};}
}p[MAXN];
int n;
int top;
int sta[MAXN];
double ans;
double dis(po p1,po p2){
	return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
bool cmp(po p1,po p2){
	if(p1.ang==p2.ang){
		return dis(p1,p[1])<dis(p2,p[1]);
	}
	return p1.ang<p2.ang;
}
double cross(po p1,po p2){
	return p1.x*p2.y-p1.y*p2.x;
}
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
    	cin>>p[i].x>>p[i].y;
	}
	for(int i=2;i<=n;i++){
		if(p[i].y<p[1].y or (p[i].y==p[1].y and p[i].x<p[1].x)){
			swap(p[1],p[i]);
		}
	}
	for(int i=2;i<=n;i++){
		p[i].ang=atan2(p[i].y-p[1].y,p[i].x-p[1].x);
	}
	sort(p+2,p+n+1,cmp);
	sta[++top]=1;
	for(int i=2;i<=n;i++){
		while(top>=2 && cross(p[sta[top]]-p[sta[top-1]],p[i]-p[sta[top]])<=0){
			top--;
		}
		sta[++top]=i;
	}
	for(int i=1;i<top;i++){
		ans+=dis(p[sta[i]],p[sta[i+1]]);
	}
	ans+=dis(p[sta[top]],p[sta[1]]);
	cout<<fixed<<setprecision(2)<<ans;
	return 0;
}

闵可夫斯基和

点集 𝑃 和点集 𝑄 的闵可夫斯基和 𝑃 +𝑄 定义为 𝑃 +𝑄 ={𝑎 +𝑏|𝑎 ∈𝑃,𝑏 ∈𝑄},即把点集 𝑄 中的每个点看做一个向量,将点集 𝑃 中每个点沿这些向量平移,最终得到的结果的集合就是点集 𝑃 +𝑄

其实就是把P沿着Q平移一遍,每个点stamp一下就行。

而闵可夫斯基和凸包的组成易得又原图形的边产生,所以假想一个向量v,则p+v=q即p-q=v

将点取反跑归并即可。

C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=1e5+1e4;
const ll eps=0;

struct po{
	ll x,y;
	po(){}
	po(ll x,ll y):x(x),y(y){}
	po operator-(const po& p)const{return po(x-p.x,y-p.y);}
	po operator+(const po& p)const{return po(x+p.x,y+p.y);}
	po operator-()const{return po(-x,-y);}
};

// 叉积
ll cross(po p1,po p2){
	return p1.x*p2.y-p1.y*p2.x;
}

// 凸包排序比较函数
bool cmp(po base, po p1, po p2){
	ll cr = cross(p1-base, p2-base);
	if(cr != 0) return cr > 0;
	return (p1.x - base.x)*(p1.x - base.x) + (p1.y - base.y)*(p1.y - base.y)
		 < (p2.x - base.x)*(p2.x - base.x) + (p2.y - base.y)*(p2.y - base.y);
}

// 凸包板子(输入点集a,输出凸包点res,返回凸包点数)
int top,stk[MAXN];
int convex_hull(po a[],po res[],int len){
	top=0;
	int pos=1;
	for(int i=2;i<=len;i++){
		if(a[i].y<a[pos].y||(a[i].y==a[pos].y&&a[i].x<a[pos].x)) pos=i;
	}
	swap(a[1],a[pos]);
	sort(a+2,a+len+1,[&](po p1, po p2){
		return cmp(a[1],p1,p2);
	});
	stk[++top]=1;
	for(int i=2;i<=len;i++){
		while(top>=2&&cross(a[stk[top]]-a[stk[top-1]],a[i]-a[stk[top]])<=eps) top--;
		stk[++top]=i;
	}
	for(int i=1;i<=top;i++) res[i]=a[stk[i]];
	return top;
}

// ===================== 核心:通用闵可夫斯基和 =====================
// 输入:凸包A(点数na)、凸包B(点数nb)
// 输出:res = A + B 的凸包点集
// 返回:闵可夫斯基和凸包的点数
int minkowski(po A[],po B[],po res[],int na,int nb){
	A[na+1]=A[1];
	B[nb+1]=B[1];
	top=0;
	int i=1,j=1;
	res[++top]=A[i]+B[j];
	while(i<=na&&j<=nb){
		ll cr=cross(A[i+1]-A[i],B[j+1]-B[j]);
		if(cr>eps) res[++top]=A[++i]+B[j];
		else res[++top]=A[i]+B[++j];
	}
	while(i<=na) res[++top]=A[++i]+B[j];
	while(j<=nb) res[++top]=A[i]+B[++j];
	// 对闵可夫斯基和结果求凸包(去重边、去共线)
	return convex_hull(res,res,top);
}
// ================================================================

// 点在凸包内判定(可选工具函数)
bool point_in_convex(po hull[],int tot,po now){
	if(cross(hull[2]-hull[1],now-hull[1])<0) return 0;
	if(cross(hull[tot]-hull[1],now-hull[1])>0) return 0;
	int l=2,r=tot;
	while(r-l>1){
		int mid=(l+r)/2;
		if(cross(hull[mid]-hull[1],now-hull[1])>=0) l=mid;
		else r=mid;
	}
	return cross(hull[l+1]-hull[l],now-hull[l])>=0;
}

// ===================== 使用示例(可直接替换) =====================
po A[MAXN], B[MAXN];    // 原始点集
po hullA[MAXN], hullB[MAXN]; // 凸包
po mink[MAXN];         // 闵可夫斯基和结果
int main(){
	ios::sync_with_stdio(0);cin.tie(0);
	int n, m;
	// 输入第一个点集
	cin >> n;
	for(int i=1;i<=n;i++) cin>>A[i].x>>A[i].y;
	// 输入第二个点集
	cin >> m;
	for(int i=1;i<=m;i++) cin>>B[i].x>>B[i].y;

	// 求两个凸包
	int na = convex_hull(A, hullA, n);
	int nb = convex_hull(B, hullB, m);

	// 求闵可夫斯基和
	int tot = minkowski(hullA, hullB, mink, na, nb);

	// 输出结果(示例)
	cout << "闵可夫斯基和凸包点数:" << tot << '\n';
	for(int i=1;i<=tot;i++){
		cout << mink[i].x << ' ' << mink[i].y << '\n';
	}
	return 0;
}

P4557 [JSOI2018] 战争

用二分跑inside,别的吃吃板即可。

C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=1e5+1e4;
const ll eps=0;
struct po{
	ll x,y;
	po(){}
	po(ll x,ll y):x(x),y(y){}
	po operator-(const po& p)const{return po(x-p.x,y-p.y);}
	po operator+(const po& p)const{return po(x+p.x,y+p.y);}
	po operator-()const{return po(-x,-y);}
}p[MAXN],q[MAXN],sum[MAXN],h1[MAXN],h2[MAXN];
int n,m,qq;
int top,stk[MAXN];
ll cross(po p1,po p2){
	return p1.x*p2.y-p1.y*p2.x;
}
bool cmp(po base, po p1, po p2){
	ll cr = cross(p1-base, p2-base);
	if(cr != 0) return cr > 0;
	return (p1.x - base.x)*(p1.x - base.x) + (p1.y - base.y)*(p1.y - base.y)
		 < (p2.x - base.x)*(p2.x - base.x) + (p2.y - base.y)*(p2.y - base.y);
}
int convex_hull(po a[],po res[],int len){
	top=0;
	int pos=1;
	for(int i=2;i<=len;i++){
		if(a[i].y<a[pos].y||(a[i].y==a[pos].y&&a[i].x<a[pos].x)) pos=i;
	}
	swap(a[1],a[pos]);
	sort(a+2,a+len+1,[&](po p1,po p2){
		return cmp(a[1],p1,p2);
	});
	stk[++top]=1;
	for(int i=2;i<=len;i++){
		while(top>=2&&cross(a[stk[top]]-a[stk[top-1]],a[i]-a[stk[top]])<=eps) top--;
		stk[++top]=i;
	}
	for(int i=1;i<=top;i++) res[i]=a[stk[i]];
	return top;
}
int minkowski(po A[],po B[],po res[],int na,int nb){
	A[na+1]=A[1]; B[nb+1]=B[1];
	top=0;
	int i=1,j=1;
	res[++top]=A[i]+B[j];
	while(i<=na&&j<=nb){
		ll cr=cross(A[i+1]-A[i],B[j+1]-B[j]);
		if(cr>eps) res[++top]=A[++i]+B[j];
		else res[++top]=A[i]+B[++j];
	}
	while(i<=na) res[++top]=A[++i]+B[j];
	while(j<=nb) res[++top]=A[i]+B[++j];
	return convex_hull(res,res,top);
}
bool inside(po hull[],int tot,po now){
	if(cross(hull[2]-hull[1],now-hull[1])<0) return 0;
	if(cross(hull[tot]-hull[1],now-hull[1])>0) return 0;
	int l=2,r=tot;
	while(r-l>1){
		int mid=(l+r)/2;
		if(cross(hull[mid]-hull[1],now-hull[1])>=0) l=mid;
		else r=mid;
	}
	return cross(hull[l+1]-hull[l],now-hull[l])>=0;
}
int main(){
	ios::sync_with_stdio(0);cin.tie(0);
	cin>>n>>m>>qq;
	for(int i=1;i<=n;i++) cin>>p[i].x>>p[i].y;
	for(int i=1;i<=m;i++) cin>>q[i].x>>q[i].y;
	int na=convex_hull(p,h1,n);
	for(int i=1;i<=m;i++) q[i]=-q[i];
	int nb=convex_hull(q,h2,m);
	int tot=minkowski(h1,h2,sum,na,nb);
	while(qq--){
		ll dx,dy;cin>>dx>>dy;
		if(inside(sum,tot,po(dx,dy))) cout<<"1\n";
		else cout<<"0\n";
	}
	return 0;
}
原文