UVALive4589 Asteroids(三维凸包【凸包重心)

题目连接

题意:求两凸包重心的最短距离

分析:
三维凸包模板

很显然,最优放置方法就是贴住两个行星的某两个面,而最近距离为各自重心到这两个面的距离
换句话说,我们可以独立求出两颗星球的“重心到各面的最短距离”,再相加即可

如何求重心呢?
因为行星是均匀的,可以先随便找一个点,连接该点和各个面,得到若干三棱锥
把每个三棱锥等价成一个质点,再求这些质点的重心
质点的重心是质点坐标按质量加权的平均数
而质量均匀的三棱锥的重心的坐标为4个顶点坐标的平均数

tip

调了半天,发现自己的Cross函数写错了。。。go die

#include<bits/stdc++.h>

using namespace std;

const double eps=1e-8;
const double INF=1e30;
const int N=100;
struct node{
    double x,y,z;
    node (double xx=0,double yy=0,double zz=0)
    {
        x=xx;y=yy;z=zz;
    }
};

node operator +(const node &A,const node &B) {return node(A.x+B.x,A.y+B.y,A.z+B.z);}
node operator -(const node &A,const node &B) {return node(A.x-B.x,A.y-B.y,A.z-B.z);}
node operator *(const node &A,const double &B) {return node(A.x*B,A.y*B,A.z*B);}
node operator /(const node &A,const double &B) {return node(A.x/B,A.y/B,A.z/B);}

double Dot(node A,node B) {return A.x*B.x+A.y*B.y+A.z*B.z;}
node Cross(node A,node B) {return node(A.y*B.z-A.z*B.y,A.z*B.x-A.x*B.z,A.x*B.y-A.y*B.x);}

struct CH3D{
    struct face{
        int a,b,c;
        bool ff;
    };

    int n;
    node P[N];
    int num;
    face F[8*N];
    int vis[N][N];

    double lenth(node a) {
        return sqrt(Dot(a,a));
    }
    double S2(node a,node b,node c) {
        return lenth(Cross(b-a,c-a));
    }
    double V6(node a,node b,node c,node d) {
        return Dot(d-a,Cross(b-a,c-a));
    }
    int cansee(node a,face f){
        double o=Dot(a-P[f.a],Cross(P[f.b]-P[f.a],P[f.c]-P[f.a]));
        if (o>eps) return 1;
        else return 0;
    }

    void deal(int p,int a,int b) {
        int f=vis[a][b];
        face add;
        if (F[f].ff) {
            if (cansee(P[p],F[f]))
                dfs(p,f);
            else {
                add.a=b;
                add.b=a;
                add.c=p;
                add.ff=1;
                vis[b][a]=vis[a][p]=vis[p][b]=num;
                F[num++]=add;
            }
        }
    }
    //递归搜索所有应该从凸包内删除的面
    void dfs(int p,int now) {
        F[now].ff=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);
    }
    void create() {
        face add;

        num=0;
        if (n<4) return;

        bool flag=1;
        for (int i=1;i<n;i++) {
            if (lenth(P[i]-P[0])>eps) {
                swap(P[1],P[i]);
                flag=0; break;
            }
        }
        if (flag) return;
        flag=1;
        for (int i=2;i<n;i++) {
            if (lenth(Cross(P[1]-P[0],P[i]-P[0]))>eps) {
                swap(P[2],P[i]);
                flag=0; break;
            }
        }
        if (flag) return;
        flag=1;
        for (int i=3;i<n;i++) {
            if (fabs(Dot(P[i]-P[0],Cross(P[1]-P[0],P[2]-P[0])))>eps) {
                swap(P[3],P[i]);
                flag=0; 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.ff=1;
            if (cansee(P[i],add)) swap(add.b,add.c);    // 
            vis[add.a][add.b]=vis[add.b][add.c]=vis[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].ff&&cansee(P[i],F[j])) {
                    dfs(i,j);
                    break;    //
                }
        int tmp=num;
        for (int i=num=0;i<tmp;i++)
            if (F[i].ff)
                F[num++]=F[i];
    }

    double Ssum() {
        double ans=0;
        if (n==3) {
            ans=S2(P[0],P[1],P[2]);
            return ans/2.0;
        }
        for (int i=0;i<num;i++)
            ans+=S2(P[F[i].a],P[F[i].b],P[F[i].c]);
        return ans/2.0;
    }
    double Vsum() {
        double ans=0;
        node tmp(0,0,0);
        for (int i=0;i<num;i++)
            ans+=V6(tmp,P[F[i].a],P[F[i].b],P[F[i].c]);
        return fabs(ans)/6.0;
    }
    node centre() {
        node ans(0,0,0),o(0,0,0);
        double all=0;
        for (int i=0;i<num;i++)
        {
            double vol=V6(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 dis_face(node p,int i) {
        return fabs(V6(P[F[i].a],P[F[i].b],P[F[i].c],p)/lenth(Cross(P[F[i].b]-P[F[i].a],P[F[i].c]-P[F[i].a])));
    }
    double getmin() {
        node p=centre();
        double ans=INF;
        for (int i=0;i<num;i++)
        {
            double t=dis_face(p,i);
            if (t-ans<eps) ans=t;
        }
        return ans;
    }
};

CH3D hull;
int main() {
    while (scanf("%d",&hull.n)!=EOF) {
        for(int i=0; i<hull.n; i++) scanf("%lf %lf %lf",&hull.P[i].x,&hull.P[i].y,&hull.P[i].z);
        hull.create();
        double ans=0;
        ans=hull.getmin(); 

        scanf("%d",&hull.n);
        for(int i=0; i<hull.n; i++) scanf("%lf %lf %lf",&hull.P[i].x,&hull.P[i].y,&hull.P[i].z);
        hull.create();
        ans+=hull.getmin();
        printf("%lf\n",ans); 
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值