hdu 4741 Save Labman No.004 空间两直线最短距离及坐标

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int _;
    double xa,xb,xc,xd,ya,yb,yc,yd,za,zb,zc,zd,e,f,g;
    double a1,b1,c1,a2,b2,c2;
    double h,i,j,k,l,m,n,o,p,q,t;
    scanf("%d",&_);
    while(_--)
    {
        while(~scanf("%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf",
                     &xa,&ya,&za,&xb,&yb,&zb,&xc,&yc,&zc,&xd,&yd,&zd))
        {
            e=(yb-ya)*(zd-zc)-(zb-za)*(yd-yc);
            f=(zb-za)*(xd-xc)-(zd-zc)*(xb-xa);
            g=(xb-xa)*(yd-yc)-(yb-ya)*(xd-xc);
            h=xb-xa,i=yb-ya,j=zb-za;
            k=xd-xc,l=yd-yc,m=zd-zc;
            n=h*i*l-i*i*k-j*k*j+h*j*m;
            o=h*h*l-h*i*k-i*j*m+j*j*l;
            p=h*j*k-h*h*m-i*i*m+i*j*l;
            q=-xa*n+ya*o-za*p;
            t=(o*yc-n*xc-p*zc-q)/(n*k-o*l+p*m);
            a1=k*t+xc;
            b1=l*t+yc;
            c1=m*t+zc;

            swap(xa,xc);swap(ya,yc);swap(za,zc);
            swap(xb,xd);swap(yb,yd);swap(zb,zd);

            e=(yb-ya)*(zd-zc)-(zb-za)*(yd-yc);
            f=(zb-za)*(xd-xc)-(zd-zc)*(xb-xa);
            g=(xb-xa)*(yd-yc)-(yb-ya)*(xd-xc);
            h=xb-xa,i=yb-ya,j=zb-za;
            k=xd-xc,l=yd-yc,m=zd-zc;
            n=h*i*l-i*i*k-j*k*j+h*j*m;
            o=h*h*l-h*i*k-i*j*m+j*j*l;
            p=h*j*k-h*h*m-i*i*m+i*j*l;
            q=-xa*n+ya*o-za*p;
            t=(o*yc-n*xc-p*zc-q)/(n*k-o*l+p*m);
            a2=k*t+xc;
            b2=l*t+yc;
            c2=m*t+zc;
            t=(a1-a2)*(a1-a2)+(b1-b2)*(b1-b2)+(c1-c2)*(c1-c2);
            printf("%.6lf\n",sqrt(t));
            printf("%.6lf %.6lf %.6lf %.6lf %.6lf %.6lf\n",a2,b2,c2,a1,b1,c1);
        }
    }
    return 0;
}

你可能感兴趣的:(hdu 4741 Save Labman No.004 空间两直线最短距离及坐标)