【BZOJ】【P1964】【hull 三维凸包】【题解】【计算几何】

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1964

n^2的3D凸包增量法好像挺好写的……

求体积和求多边形面积差不多,随便选一个顶点,计算这个顶点和每一个面形成的三棱锥的体积加起来就好了

Code:

#include<bits/stdc++.h>
using namespace std;
const double eps=1e-6;
struct point3{
	double x,y,z;
	point3(double _x=0,double _y=0,double _z=0):x(_x),y(_y),z(_z){}
	point3 operator*(point3 o){return point3(y*o.z-o.y*z,z*o.x-o.z*x,x*o.y-o.x*y);}
	double operator^(point3 o){return x*o.x+y*o.y+z*o.z;}
	point3 operator+(point3 o){return point3(x+o.x,y+o.y,z+o.z);}
	point3 operator-(point3 o){return point3(x-o.x,y-o.y,z-o.z);}
}p[201],_p[201];
double randeps(){return (rand()/(double)RAND_MAX-0.5)*1e-7;}
point3 noise(point3 p){return point3(p.x+randeps(),p.y+randeps(),p.z+randeps());}
struct Face{
	int v[3];
	point3 normal()const{return (p[v[1]]-p[v[0]])*(p[v[2]]-p[v[0]]);}
	bool cansee(int x){return ((p[x]-p[v[0]])^normal())>0;}
};
int n,in[201],vis[201][201];
vector<Face>cur;
void CH3D(){
	cur.push_back((Face){{1,2,3}});
	cur.push_back((Face){{3,2,1}});
	for(int i=4;i<=n;i++){
		vector<Face>tmp;
		memset(vis,0,sizeof vis);
		for(int j=0;j<cur.size();j++){
			Face f=cur[j];
			if(!f.cansee(i))
				tmp.push_back(f);
			else for(int k=0;k<3;k++)vis[f.v[k]][f.v[(k+1)%3]]=1;
		}for(int j=0;j<cur.size();j++){
			for(int k=0;k<3;k++){
				int a=cur[j].v[k],b=cur[j].v[(k+1)%3];
				if(vis[a][b]!=vis[b][a]&&vis[a][b])
				tmp.push_back((Face){{a,b,i}});
			}
		}cur=tmp;
	}
}
double V6(point3 A,point3 B,point3 C,point3 D){
	return (B-A)*(C-A)^(D-A); 
}
double calc(){
	double ans=0;
	for(int i=0;i<cur.size();i++)
	ans+=fabs(V6(_p[1],_p[cur[i].v[0]],_p[cur[i].v[1]],_p[cur[i].v[2]]));
	return ans/6;
}
int main(){
	double x,y,z;//scanf("%d",&n);n=0;
	while(~scanf("%lf%lf%lf",&x,&y,&z))_p[++n]=point3(x,y,z),p[n]=noise(_p[n]);
	CH3D();
	printf("%.2lf\n",calc());
	return 0;
}


你可能感兴趣的:(bzoj)