这种题每次都是抄袭模板,自己都写不出来。。。
题意:给你一个3D凸包图形(题目保证是一个3D凸包图形)。问你它的重心到每个平面的最近距离。
解法:模板题。关键是求重心,类似于求平面凸包的重心,先假设一个初始点,然后求每个面与这个点的有向体积,因为这个物体的密度是均匀的,所以可以根据体积算出平移向量,之后求这个向量和,就是重心。
代码:
#include <iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<cmath>
#include<vector>
#define inf 0x3f3f3f3f
#define Inf 0x3FFFFFFFFFFFFFFFLL
#define pi acos(-1.0)
#define eps 1e-8
using namespace std;
const int maxn = 1050;
struct Point
{
double x, y, z;
Point(double x=0,double y=0,double z=0):x(x),y(y),z(z){}
Point operator+(const Point& p) const {return Point(x+p.x,y+p.y,z+p.z);}
Point operator-(const Point& p) const {return Point(x-p.x,y-p.y,z-p.z);}
Point operator*(const double p) const {return Point(x*p,y*p,z*p);}
Point operator/(const double p) const {return Point(x/p,y/p,z/p);}
Point operator*(const Point& p) const {return Point(y*p.z-z*p.y,z*p.x-x*p.z,x*p.y-y*p.x);}
double operator^(const Point& p) const {return (x*p.x+y*p.y+z*p.z);}
void read(){scanf("%lf%lf%lf", &x, &y, &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 Length(const 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 (b-a)*(c-a);}
double area(const Point &a, const Point& b, const Point& c){return Length((b-a)*(c-a));}
double volume(const Point &a, const Point& b, const Point& c, const Point& d) {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 = true;
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 create()
{
face add;
num = 0;
if(n<4) return;
///*******************************************
bool flag = true;
for(int i = 1; i < n ; ++ i)
{
if(Length(P[0]-P[i])>eps)
{
swap(P[1],P[i]);
flag = false;
break;
}
}
if(flag) return;
flag = true;
for(int i = 2; i < n ; ++ i)
{
if(area(P[0],P[1],P[i])>eps)
{
swap(P[2],P[i]);
flag = false;
break;
}
}
if(flag) return;
flag = true;
for(int i = 3 ; i < n ; ++ i)
{
if(fabs(volume(P[0],P[1],P[2],P[i]))>eps)
{
swap(P[3],P[i]);
flag = false;
break;
}
}
if(flag) return;
///**************************************
for(int 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(int i = 4 ; i < n ; ++ i)///
{
for(int j = 0 ; j < num ; ++ j)
{
if(F[j].ok&&dblcmp(P[i],F[j])>eps)
{
dfs(i, j);
break;
}
}
}
int tmp = num;
for(int i = num = 0; i < tmp ; ++ i)
if(F[i].ok) F[num++]=F[i];
}
double area()
{
double res = 0;
if(n==3)
{
Point p = Cross(P[0],P[1],P[2]);
res = Length(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;
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(int i = res = 0 ; i < num ; ++ i)
{
flag = 1;
for(int j = 0 ; j < i ; ++ j)
{
if(same(i,j))
{
flag = 0;
break;
}
}
res += flag;
}
return res;
}
Point barycenter()
{
Point ans(0,0,0),o(0,0,0);
double all = 0;
for(int i = 0 ; i < num ; ++ i)
{
double vol = volume(o, P[F[i].a], P[F[i].b], P[F[i].c]);
ans = ans+(o+P[F[i].a]+P[F[i].b]+P[F[i].c])/4.0*vol;
all += vol;
}
ans = ans/all;
return ans;
}
double ptoface(Point p, int i)
{
return fabs(volume(P[F[i].a],P[F[i].b],P[F[i].c],p)/Length((P[F[i].b]-P[F[i].a])*(P[F[i].c]-P[F[i].a])));
}
}hull;
int main()
{
//freopen("in.txt","r",stdin);
while(cin>>hull.n)
{
for(int i = 0 ; i < hull.n ; ++ i)
{
hull.P[i].read();
}
hull.create();
Point pp = hull.barycenter();
int num = hull.Polygon();
double minn = 1e15;
for(int i = 0 ; i < num ; ++ i)
{
minn = min(minn,hull.ptoface(pp,i));
}
printf("%.3lf\n",minn);
}
return 0;
}