题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2936
题意:给出两个四面体。求最近距离。
思路:最近距离无非来源于以下三种:
(1)顶点与另外一个某条边的最近距离;(点到线段最近距离)
(2)某个边与另外一个的某条边的最近距离;(线段和线段最近距离)
(3)顶点与另一个的某个面的最近距离;(点和面的最近距离)
int sgn(double x)
{
if(x>EPS) return 1;
if(x<-EPS) return -1;
return 0;
}
struct point
{
double x,y;
point(){}
point(double _x,double _y)
{
x=_x;
y=_y;
}
void get()
{
RD(x); RD(y);
}
point operator+(point a)
{
return point(x+a.x,y+a.y);
}
point operator-(point a)
{
return point(x-a.x,y-a.y);
}
double operator*(point a)
{
return x*a.y-y*a.x;
}
point operator*(double t)
{
return point(x*t,y*t);
}
double operator^(point a)
{
return x*a.x+y*a.y;
}
double len()
{
return sqrt(x*x+y*y);
}
void print()
{
printf("%.3lf %.3lf\n",x+EPS,y+EPS);
}
};
double len(point a)
{
return a.len();
}
struct point3
{
double x,y,z;
point3(){}
point3(double _x,double _y,double _z)
{
x=_x;
y=_y;
z=_z;
}
void get()
{
cin>>x>>y>>z;
}
point3 operator+(point3 a)
{
return point3(x+a.x,y+a.y,z+a.z);
}
point3 operator-(point3 a)
{
return point3(x-a.x,y-a.y,z-a.z);
}
point3 operator*(point3 a)
{
return point3(y*a.z-z*a.y,z*a.x-x*a.z,x*a.y-y*a.x);
}
point3 operator*(double t)
{
return point3(x*t,y*t,z*t);
}
double operator^(point3 a)
{
return x*a.x+y*a.y+z*a.z;
}
point3 operator/(double t)
{
return point3(x/t,y/t,z/t);
}
double len()
{
return sqrt(x*x+y*y+z*z);
}
point3 adjust(double L)
{
double t=len();
L/=t;
return point3(x*L,y*L,z*L);
}
void print()
{
printf("%.10lf %.10lf %.10lf\n",x+EPS,y+EPS,z+EPS);
}
};
double len(point3 a)
{
return a.len();
}
double getArea(point3 a,point3 b,point3 c)
{
double x=len((b-a)*(c-a));
return x/2;
}
double getVolume(point3 a,point3 b,point3 c,point3 d)
{
double x=(b-a)*(c-a)^(d-a);
return x/6;
}
point3 pShadowOnPlane(point3 p,point3 a,point3 b,point3 c)
{
point3 v=(b-a)*(c-a);
if(sgn(v^(a-p))<0) v=v*-1;
v=v.adjust(1);
double d=fabs(v^(a-p));
return p+v*d;
}
double lineToLine(point3 a,point3 b,point3 p,point3 q)
{
point3 v=(b-a)*(q-p);
return fabs((a-p)^v)/len(v);
}
int pInPlane(point3 p,point3 a,point3 b,point3 c)
{
double S=getArea(a,b,c);
double S1=getArea(a,b,p);
double S2=getArea(a,c,p);
double S3=getArea(b,c,p);
return sgn(S-S1-S2-S3)==0;
}
int opposite(point3 p,point3 q,point3 a,point3 b,point3 c)
{
point3 v=(b-a)*(c-a);
double x=v^(p-a);
double y=v^(q-a);
return sgn(x*y)<0;
}
int segCrossTri(point3 p,point3 q,point3 a,point3 b,point3 c)
{
return opposite(p,q,a,b,c)&&
opposite(a,b,p,q,c)&&
opposite(a,c,p,q,b)&&
opposite(b,c,p,q,a);
}
double pToPlane(point3 p,point3 a,point3 b,point3 c)
{
double v=((b-a)*(c-a)^(p-a))/6;
double s=len((b-a)*(c-a))/2;
return fabs(3*v/s);
}
double pToLine(point3 p,point3 a,point3 b)
{
double S=len((a-p)*(b-p));
return S/len(a-b);
}
double pToSeg(point3 p,point3 a,point3 b)
{
if(sgn((p-a)^(b-a))<=0) return len(a-p);
if(sgn((p-b)^(a-b))<=0) return len(b-p);
return pToLine(p,a,b);
}
double pToPlane1(point3 p,point3 a,point3 b,point3 c)
{
point3 k=pShadowOnPlane(p,a,b,c);
if(pInPlane(k,a,b,c)) return pToPlane(p,a,b,c);
double x=pToSeg(p,a,b);
double y=pToSeg(p,a,c);
double z=pToSeg(p,b,c);
return min(x,min(y,z));
}
double getAng(point3 a,point3 b)
{
double x=(a^b)/len(a)/len(b);
return acos(x);
}
double segToSeg(point3 a,point3 b,point3 p,point3 q)
{
point3 v=(b-a)*(q-p);
double A,B,A1,B1;
A=((b-a)*v)^(p-a);
B=((b-a)*v)^(q-a);
A1=((p-q)*v)^(a-q);
B1=((p-q)*v)^(b-q);
if(sgn(A*B)<=0&&sgn(A1*B1)<=0)
{
return lineToLine(a,b,p,q);
}
double x=min(pToSeg(a,p,q),pToSeg(b,p,q));
double y=min(pToSeg(p,a,b),pToSeg(q,a,b));
return min(x,y);
}
struct face
{
int a,b,c,ok;
face(){}
face(int _a,int _b,int _c,int _ok)
{
a=_a;
b=_b;
c=_c;
ok=_ok;
}
};
struct _3DCH
{
face F[N<<2];
int b[N][N],cnt,n;
point3 p[N];
int getDir(point3 t,face F)
{
double x=(p[F.b]-p[F.a])*(p[F.c]-p[F.a])^(t-p[F.a]);
return sgn(x);
}
void deal(int i,int x,int y)
{
int f=b[x][y];
if(!F[f].ok) return;
if(getDir(p[i],F[f])==1) DFS(i,f);
else
{
b[y][x]=b[x][i]=b[i][y]=cnt;
F[cnt++]=face(y,x,i,1);
}
}
void DFS(int i,int j)
{
F[j].ok=0;
deal(i,F[j].b,F[j].a);
deal(i,F[j].c,F[j].b);
deal(i,F[j].a,F[j].c);
}
void construct()
{
int i,j,k=0;
for(i=1;i<n;i++) if(sgn(len(p[i]-p[0])))
{
swap(p[i],p[1]);
k++;
break;
}
if(k!=1) return;
for(i=2;i<n;i++) if(sgn(getArea(p[0],p[1],p[i])))
{
swap(p[i],p[2]);
k++;
break;
}
if(k!=2) return;
for(i=3;i<n;i++) if(sgn(getVolume(p[0],p[1],p[2],p[i])))
{
swap(p[i],p[3]);
k++;
break;
}
if(k!=3) return;
cnt=0;
FOR0(i,4)
{
face k=face((i+1)%4,(i+2)%4,(i+3)%4,1);
if(getDir(p[i],k)==1) swap(k.b,k.c);
b[k.a][k.b]=b[k.b][k.c]=b[k.c][k.a]=cnt;
F[cnt++]=k;
}
for(i=4;i<n;i++) FOR0(j,cnt)
{
if(F[j].ok&&getDir(p[i],F[j])==1)
{
DFS(i,j);
break;
}
}
j=0;
FOR0(i,cnt) if(F[i].ok) F[j++]=F[i];
cnt=j;
}
point3 getCenter()
{
point3 ans=point3(0,0,0),o=point3(0,0,0);
double s=0,temp;
int i;
FOR0(i,cnt)
{
face k=F[i];
temp=getVolume(o,p[k.a],p[k.b],p[k.c]);
ans=ans+(o+p[k.a]+p[k.b]+p[k.c])/4*temp;
s+=temp;
}
ans=ans/s;
return ans;
}
double getMinDis(point3 a)
{
double ans=dinf;
int i;
FOR0(i,cnt)
{
face k=F[i];
ans=min(ans,pToPlane(a,p[k.a],p[k.b],p[k.c]));
}
return ans;
}
};
/********************模板*************************/
point3 a[4],b[4];
int main()
{
rush()
{
int i,j,k,t;
FOR0(i,4) a[i].get();
FOR0(i,4) b[i].get();
double ans=dinf;
FOR0(i,4) FOR0(j,4) FOR0(k,4) if(j!=k)
{
ans=min(ans,pToSeg(a[i],b[j],b[k]));
ans=min(ans,pToSeg(b[i],a[j],a[k]));
}
FOR0(i,4) FOR0(j,4) if(i!=j)
{
FOR0(k,4) FOR0(t,4) if(k!=t)
{
ans=min(ans,segToSeg(a[i],a[j],b[k],b[t]));
}
}
FOR0(i,4)
{
ans=min(ans,pToPlane1(a[i],b[0],b[1],b[2]));
ans=min(ans,pToPlane1(a[i],b[0],b[1],b[3]));
ans=min(ans,pToPlane1(a[i],b[0],b[3],b[2]));
ans=min(ans,pToPlane1(a[i],b[3],b[1],b[2]));
ans=min(ans,pToPlane1(b[i],a[0],a[1],a[2]));
ans=min(ans,pToPlane1(b[i],a[0],a[1],a[3]));
ans=min(ans,pToPlane1(b[i],a[0],a[3],a[2]));
ans=min(ans,pToPlane1(b[i],a[3],a[1],a[2]));
}
PR(ans);
}
}