zoukankan      html  css  js  c++  java
  • 三维凸包(两个没有公共点)经过旋转平移后使其重心相距最近(POJ3862)

    Asteroids
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 481   Accepted: 152   Special Judge

    Description

    Association of Collision Management (ACM) is planning to perform the controlled collision of two asteroids. The asteroids will be slowly brought together and collided at negligible speed. ACM expects asteroids to get attached to each other and form a stable object. 
    Each asteroid has the form of a convex polyhedron. To increase the chances of success of the experiment ACM wants to bring asteroids together in such manner that their centers of mass are as close as possible. To achieve this, ACM operators can rotate the asteroids and move them independently before bringing them together. 
    Help ACM to find out what minimal distance between centers of mass can be achieved. 
    For the purpose of calculating center of mass both asteroids are considered to have constant density.

    Input

    Input file contains two descriptions of convex polyhedra. 
    The first line of each description contains integer number n - the number of vertices of the polyhedron (4 <= n <= 60). The following n lines contain three integer numbers xi, yi, zi each - the coordinates of the polyhedron vertices (-104 <= xi, yi, zi <= 104). It is guaranteed that the given points are vertices of a convex polyhedron, in particular no point belongs to the convex hull of other points. Each polyhedron is non-degenerate. 
    The two given polyhedra have no common points.

    Output

    Output one floating point number - the minimal distance between centers of mass of the asteroids that can be achieved. Your answer must be accurate up to 10-5.

    Sample Input

    8
    0 0 0
    0 0 1
    0 1 0
    0 1 1
    1 0 0
    1 0 1
    1 1 0
    1 1 1
    5
    0 0 5
    1 0 6
    -1 0 6
    0 1 6
    0 -1 6

    Sample Output

    0.75
    分析:分别求出重心到面的最短距离:
    #include"stdio.h"
    #include"string.h"
    #include"iostream"
    #include"map"
    #include"string"
    #include"queue"
    #include"stack"
    #include"vector"
    #include"stdlib.h"
    #include"algorithm"
    #include"math.h"
    #define M 533
    #define eps 1e-10
    #define inf 0x3f3f3f3f
    #define mod 1070000009
    #define PI acos(-1.0)
    using namespace std;
    struct node
    {
        double x,y,z,dis;
        node(){}
        node(double xx,double yy,double zz):x(xx),y(yy),z(zz){}
        node operator +(const node p)
        {
            return node(x+p.x,y+p.y,z+p.z);
        }
        node operator -(const node p)
        {
            return node(x-p.x,y-p.y,z-p.z);
        }
        node operator *(const node p)
        {
            return node(y*p.z-z*p.y,z*p.x-x*p.z,x*p.y-y*p.x);
        }
        node operator *(const double p)
        {
            return node(x*p,y*p,z*p);
        }
        node operator /(const double p)
        {
            return node(x/p,y/p,z/p);
        }
        double operator ^(const node p)
        {
            return x*p.x+y*p.y+z*p.z;
        }
    };
    struct threeD_convex_hull
    {
        struct face
        {
            int a,b,c;
            int ok;
        };
        int n;
        int cnt;
        node p[M];
        face f[M*8];
        int to[M][M];
        double len(node p)
        {
            return sqrt(p.x*p.x+p.y*p.y+p.z*p.z);
        }
        double area(node a,node b,node c)
        {
            return len((b-a)*(c-a));
        }
        double volume(node a,node b,node c,node d)
        {
            return (b-a)*(c-a)^(d-a);
        }
        double ptof(node q,face f)
        {
            node m=p[f.b]-p[f.a];
            node n=p[f.c]-p[f.a];
            node t=q-p[f.a];
            return m*n^t;
        }
        void dfs(int q,int cur)
        {
            f[cur].ok=0;
            deal(q,f[cur].b,f[cur].a);
            deal(q,f[cur].c,f[cur].b);
            deal(q,f[cur].a,f[cur].c);
        }
        void deal(int q,int a,int b)
        {
            int fa=to[a][b];
            face add;
            if(f[fa].ok)
            {
                if(ptof(p[q],f[fa])>eps)
                    dfs(q,fa);
                else
                {
                    add.a=b;
                    add.b=a;
                    add.c=q;
                    add.ok=1;
                    to[b][a]=to[a][q]=to[q][b]=cnt;
                    f[cnt++]=add;
                }
            }
        }
        int same(int s,int t)
        {
            node a=p[f[s].a];
            node b=p[f[s].b];
            node c=p[f[s].c];
            if(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)
                return 1;
            return 0;
        }
        void make()
        {
            cnt=0;
            if(n<4)
                return;
            int sb=1;
            for(int i=1;i<n;i++)
            {
                if(len(p[0]-p[i])>eps)
                {
                    swap(p[1],p[i]);
                    sb=0;
                    break;
                }
            }
            if(sb)return;
            sb=1;
            for(int i=2;i<n;i++)
            {
                if(len((p[1]-p[0])*(p[i]-p[0]))>eps)
                {
                    swap(p[2],p[i]);
                    sb=0;
                    break;
                }
            }
            if(sb)return;
            sb=1;
            for(int i=3;i<n;i++)
            {
                if(fabs(volume(p[0],p[1],p[2],p[i]))>eps)
                {
                    swap(p[3],p[i]);
                    sb=0;
                    break;
                }
            }
            if(sb)return;
            face add;
            for(int i=0;i<4;i++)
            {
                add.a=(i+1)%4;
                add.b=(i+2)%4;
                add.c=(i+3)%4;
                add.ok=1;
                if(ptof(p[i],add)>eps)
                    swap(add.c,add.b);
                to[add.a][add.b]=to[add.b][add.c]=to[add.c][add.a]=cnt;
                f[cnt++]=add;
            }
            for(int i=4;i<n;i++)
            {
                for(int j=0;j<cnt;j++)
                {
                    if(f[j].ok&&ptof(p[i],f[j])>eps)
                    {
                        dfs(i,j);
                        break;
                    }
                }
            }
            int tmp=cnt;
            cnt=0;
            for(int i=0;i<tmp;i++)
                if(f[i].ok)
                f[cnt++]=f[i];
        }
        double Area()//表面积
        {
            double S=0;
            if(n==3)
            {
                S=area(p[0],p[1],p[2])/2.0;
                return S;
            }
            for(int i=0;i<cnt;i++)
                S+=area(p[f[i].a],p[f[i].b],p[f[i].c]);
            return S/2.0;
        }
        double Volume()//体积
        {
            double V=0;
            node mid(0,0,0);
            for(int i=0;i<cnt;i++)
                V+=volume(p[f[i].a],p[f[i].b],p[f[i].c],mid);
            V=fabs(V)/6.0;
            return V;
        }
        int tringleCnt()
        {
            return cnt;
        }
        int faceCnt()
        {
            int num=0;
            for(int i=0;i<cnt;i++)
            {
                int flag=1;
                for(int j=0;j<i;j++)
                {
                    if(same(i,j))
                    {
                        flag=0;
                        break;
                    }
                }
                num+=flag;
            }
            return num;
        }
        double pf_dis(face f,node q)//点到面的距离
        {
            double V=volume(p[f.a],p[f.b],p[f.c],q);
            double S=area(p[f.a],p[f.b],p[f.c]);
            return fabs(V/S);
        }
        double min_dis(node q)//暴力搜索内部的点q到面的最短距离即体积/面积
        {
            double mini=inf;
            for(int i=0;i<cnt;i++)
            {
                double h=pf_dis(f[i],q);
                if(mini>h)
                    mini=h;
            }
            return mini;
        }
        node barycenter()
        {
            node ret(0,0,0),mid(0,0,0);
            double sum=0;
            for(int i=0;i<cnt;i++)
            {
                double V=volume(p[f[i].a],p[f[i].b],p[f[i].c],mid);
                ret=ret+(mid+p[f[i].a]+p[f[i].b]+p[f[i].c])/4.0*V;
                sum+=V;
            }
            ret=ret/sum;
            return ret;
        }
    
    }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.make();
            printf("%d
    ",hull.faceCnt());
        }
        return 0;
    }*/
    int main()
    {
        while(scanf("%d",&hull.n)!=-1)
        {
            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.make();
            node center=hull.barycenter();
            double min1=hull.min_dis(center);
            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.make();
            center=hull.barycenter();
            double min2=hull.min_dis(center);
            printf("%.5lf
    ",min1+min2);
        }
        return 0;
    }


  • 相关阅读:
    大数据Hadoop-2
    大数据Hadoop-1
    Consistent Hashing
    分支的创建、删除、切换、合并以及冲突解决
    windows WEB 高可用/可伸缩
    Oracle行转列、列转行的Sql语句总结
    从零到百亿互联网金融架构发展史---架构变迁
    WebJars
    springcloud(五):熔断监控Hystrix Dashboard和Turbine
    SpringBoot编写自定义的starter 专题
  • 原文地址:https://www.cnblogs.com/mypsq/p/4348135.html
Copyright © 2011-2022 走看看