二维凸包
const int maxn=1e5+100; const double eps=1e-8; int sgn(double x){///判断x是否等于0 if(fabs(x)<eps) return 0; if(x<0) return -1; else return 1; } struct point{ double x,y; point(){} point(double x,double y):x(x),y(y){} point operator+(point b){ return point(x+b.x,y+b.y); } point operator-(point b){ return point(x-b.x,y-b.y); } bool operator == (point b){ return sgn(x-b.x)==0&&sgn(y-b.y)==0; } bool operator<(point b)const{ if(sgn(x-b.x)==0) return sgn(y-b.y)<0; return sgn(x-b.x)<0; } }; point s[maxn],g[maxn],h[maxn]; double dot(point a,point b){return a.x*b.x+a.y*b.y;} double cross(point a,point b){return a.x*b.y-a.y*b.x;} struct line{ point p1,p2; line(){} line(point p1,point p2):p1(p1),p2(p2){} }; double mult(point a,point b,point o){ ///计算叉乘ao和bo return (a.x-o.x)*(b.y-o.y)>=(b.x-o.x)*(a.y-o.y); } int Graham(int n){ int idx=1; sort(s,s+n); if(!n) return 0; h[0]=s[0]; if(n==1) return 0; h[1]=s[1]; if(n==2) return 0; h[2]=s[2]; ///求凸包的上半部分 for(int i=2;i<n;i++){ while(idx&&(mult(s[i],h[idx],h[idx-1]))) idx--; h[++idx]=s[i]; } int tmp=idx; h[++idx]=s[n-2]; for(int i=n-3;i>=0;i--){ while(idx!=tmp&&(mult(s[i],h[idx],h[idx-1]))) idx--; h[++idx]=s[i]; } return idx; } struct polygon{ int n;point p[1010];line v[1010]; }; ///点和线段的关系 0不在1在 bool point_on_seg(point p,line v){ return sgn(cross(p-v.p1,v.p2-v.p1))==0&&sgn(dot(p-v.p1,v.p2-v.p1))==0; } ///判断点和任意多边形的关系:3在点上 2在边上 1在内部 0在外部 int Point_in_polygon(point pt,point* p,int n){ int i; for(i=0;i<n;i++) if(p[i]==pt) return 3; for(i=0;i<n;i++){ line v=line(p[i],p[(i+1)%n]); if(point_on_seg(pt,v)) return 2; } int num=0; for(int i=0;i<n;i++){ int j=(i+1)%n; int c=sgn(cross(pt-p[j],p[i]-p[j])); int u=sgn(p[i].y-pt.y); int v=sgn(p[j].y-pt.y); if(c>0&&u<0&&v>=0) num++; if(c<0&&u>=0&&v<0) num--; } return num!=0; } double culdis(point a,point b){ return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } //说明 返回值为凸包点的个数,h数组存储的是凸包中的点。
三维凸包
const int MAXN=505; const double EPS=1e-8; struct Point{ double x,y,z; Point(){} Point(double xx,double yy,double zz):x(xx),y(yy),z(zz){} Point operator -(const Point p1){ //两向量之差 return Point(x-p1.x,y-p1.y,z-p1.z); } Point operator *(Point p){ //叉乘 return Point(y*p.z-z*p.y,z*p.x-x*p.z,x*p.y-y*p.x); } double operator ^(Point p){ //点乘 return (x*p.x+y*p.y+z*p.z); } }; struct CH3D{ struct face{ int a,b,c; //表示凸包一个面上三个点的编号 bool ok; //表示该面是否属于最终凸包中的面 }; int n; //初始顶点数 Point P[MAXN]; //初始顶点 int num; //凸包表面的三角形数 face F[8*MAXN]; int g[MAXN][MAXN]; //凸包表面的三角形 double vlen(Point a){ //向量长度 return sqrt(a.x*a.x+a.y*a.y+a.z*a.z); } Point cross(const Point &a, const Point &b, const Point &c){ //叉乘 return Point((b.y-a.y)*(c.z-a.z)-(b.z-a.z)*(c.y-a.y),-((b.x-a.x)*(c.z-a.z) -(b.z-a.z)*(c.x-a.x)),(b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x)); } double area(Point a,Point b,Point c){ //三角形面积*2 return vlen((b-a)*(c-a)); } double volume(Point a,Point b,Point c,Point d){ //四面体有向体积*6 return (b-a)*(c-a)^(d-a); } double dblcmp(Point &p,face &f){ //正:点在面同向 Point m=P[f.b]-P[f.a]; Point n=P[f.c]-P[f.a]; Point t=p-P[f.a]; return (m*n)^t; } void deal(int p,int a,int b){ int f=g[a][b]; face add; if(F[f].ok){ if(dblcmp(P[p],F[f])>EPS) dfs(p,f); else{ add.a=b; add.b=a; add.c=p; add.ok=1; g[p][b]=g[a][p]=g[b][a]=num; F[num++]=add; } } } void dfs(int p,int now){ F[now].ok=0; deal(p,F[now].b,F[now].a); deal(p,F[now].c,F[now].b); deal(p,F[now].a,F[now].c); } bool same(int s,int t){ Point &a=P[F[s].a]; Point &b=P[F[s].b]; Point &c=P[F[s].c]; return fabs(volume(a,b,c,P[F[t].a]))<EPS && fabs(volume(a,b,c,P[F[t].b]))<EPS && fabs(volume(a,b,c,P[F[t].c]))<EPS; } void solve(){ //构建三维凸包 int i,j,tmp; face add; bool flag=true; num=0; if(n<4) return; for(i=1;i<n;i++){ //此段是为了保证前四个点不共面,若以保证,则可去掉 if(vlen(P[0]-P[i])>EPS){ swap(P[1],P[i]); flag=false; break; } } if(flag) return; flag=true; for(i=2;i<n;i++){ //使前三点不共线 if(vlen((P[0]-P[1])*(P[1]-P[i]))>EPS){ swap(P[2],P[i]); flag=false; break; } } if(flag) return; flag=true; for(i=3;i<n;i++){ //使前四点不共面 if(fabs((P[0]-P[1])*(P[1]-P[2])^(P[0]-P[i]))>EPS){ swap(P[3],P[i]); flag=false; break; } } if(flag) return; for(i=0;i<4;i++){ add.a=(i+1)%4; add.b=(i+2)%4; add.c=(i+3)%4; add.ok=true; if(dblcmp(P[i],add)>0) swap(add.b,add.c); g[add.a][add.b]=g[add.b][add.c]=g[add.c][add.a]=num; F[num++]=add; } for(i=4;i<n;i++){ for(j=0;j<num;j++){ if(F[j].ok && dblcmp(P[i],F[j])>EPS){ dfs(i,j); break; } } } tmp=num; for(i=num=0;i<tmp;i++) if(F[i].ok){ F[num++]=F[i]; } } double area(){ //表面积 double res=0.0; if(n==3){ Point p=cross(P[0],P[1],P[2]); res=vlen(p)/2.0; return res; } for(int i=0;i<num;i++) res+=area(P[F[i].a],P[F[i].b],P[F[i].c]); return res/2.0; } double volume(){ //体积 double res=0.0; Point tmp(0,0,0); for(int i=0;i<num;i++) res+=volume(tmp,P[F[i].a],P[F[i].b],P[F[i].c]); return fabs(res/6.0); } int triangle(){ //表面三角形个数 return num; } int polygon(){ //表面多边形个数 int i,j,res,flag; for(i=res=0;i<num;i++){ flag=1; for(j=0;j<i;j++) if(same(i,j)){ flag=0; break; } res+=flag; } return res; } Point getcent(){ //求凸包质心 Point ans(0,0,0),temp=P[F[0].a]; double v = 0.0,t2; for(int i=0;i<num;i++){ if(F[i].ok == true){ Point p1=P[F[i].a],p2=P[F[i].b],p3=P[F[i].c]; t2 = volume(temp,p1,p2,p3)/6.0; //体积大于0,也就是说,点 temp 不在这个面上 if(t2>0){ ans.x += (p1.x+p2.x+p3.x+temp.x)*t2; ans.y += (p1.y+p2.y+p3.y+temp.y)*t2; ans.z += (p1.z+p2.z+p3.z+temp.z)*t2; v += t2; } } } ans.x /= (4*v); ans.y /= (4*v); ans.z /= (4*v); return ans; } double function(Point fuck){ //点到凸包上的最近距离(枚举每个面到这个点的距离) double min=99999999; for(int i=0;i<num;i++){ if(F[i].ok==true){ Point p1=P[F[i].a] , p2=P[F[i].b] , p3=P[F[i].c]; double a = ( (p2.y-p1.y)*(p3.z-p1.z)-(p2.z-p1.z)*(p3.y-p1.y) ); double b = ( (p2.z-p1.z)*(p3.x-p1.x)-(p2.x-p1.x)*(p3.z-p1.z) ); double c = ( (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x) ); double d = ( 0-(a*p1.x+b*p1.y+c*p1.z) ); double temp = fabs(a*fuck.x+b*fuck.y+c*fuck.z+d)/sqrt(a*a+b*b+c*c); if(temp<min)min = temp; } } return min; } }; CH3D hull; /* * 用法: * input: n: 点数 * 下标从0开始 * p[i].x p[i].y p[i].z: 坐标 * solve(): 构建三维凸包 * return : double area(): 凸包表面积 * double volume(): 体积 * int polygon(): 表面三角形数 * int polygon(): 表面多边形数 * Point getcent(): 凸包质心 * double function(Point fuck): 点到凸包上的距离 * */
旋转卡壳
跟凸包的板子不大一样。
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=50000+100; const double eps=1e-8; int sgn(ll x){///判断x是否等于0 if(fabs(x)<eps) return 0; if(x<0) return -1; else return 1; } struct point{ ll x,y; point(){} point(ll x,ll y):x(x),y(y){} point operator+(point b){ return point(x+b.x,y+b.y); } point operator-(point b){ return point(x-b.x,y-b.y); } bool operator == (point b){ return sgn(x-b.x)==0&&sgn(y-b.y)==0; } }; point s[maxn],g[maxn],h[maxn],p; bool cmp(point a,point b){ double A=atan2(a.y-p.y,a.x-p.x); double B=atan2(b.y-p.y,b.x-p.x); if(A!=B) return A<B; else return a.x<b.x; } int n; ll dot(point a,point b){return a.x*b.x+a.y*b.y;} ll cross(point a,point b){return a.x*b.y-a.y*b.x;} ll compare(point a,point b,point c){ return cross({b.x-a.x,b.y-a.y},{c.x-a.x,c.y-a.y}); } ll cul(point a,point b){ return ((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } int idx=1; int Graham(int n){ p={1000000000,1000000000}; int k=0; for(int i=0;i<n;i++){ if(p.y>s[i].y||(p.y==s[i].y&&p.x>s[i].x)){ p=s[i],k=i; } } swap(s[k],s[0]); sort(&s[1],&s[n],cmp); h[0]=s[0]; h[1]=s[1]; idx=1; for(int i=2;i<n;){ if(idx&&(compare(h[idx-1],s[i],h[idx])>=0)) idx--; else h[++idx]=s[i++]; } return idx; } //说明 返回值为凸包点的个数,h数组存储的是凸包中的点。 ll getmax(){ ll res=0; if(idx==1) return cul(h[0],h[1]); h[++idx]=h[0]; int j=2; for(int i=0;i<idx;i++){ while(compare(h[i],h[i+1],h[j])<compare(h[i],h[i+1],h[j+1])) j=(j+1)%idx; res=max(res,max(cul(h[i],h[j]),cul(h[i+1],h[j]))); } return res; } int main(){ int n;cin>>n; for(int i=0;i<n;i++) cin>>s[i].x>>s[i].y; int t=Graham(n); cout<<getmax()<<endl; return 0; }