Save Labman No.004

来源:互联网 发布:天府广场美食 知乎 编辑:程序博客网 时间:2024/06/08 09:44
#include <stdio.h>#include <math.h>using namespace std;int main(){    int ca;double x1,x2,x3,x4,y1,y2,y3,y4,z1,z2,z3,z4,s,t,a,b,c,d,e,f,g,h,i,p,q,r,D,t1,t2,D1,D2;    scanf("%d",&ca);    while(ca--){    scanf("%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&y1,&z1,&x2,&y2,&z2,&x3,&y3,&z3,&x4,&y4,&z4);    p=(y2-y1)*(z4-z3)-(y4-y3)*(z2-z1);    q=(x4-x3)*(z2-z1)-(x2-x1)*(z4-z3);    r=(x2-x1)*(y4-y3)-(x4-x3)*(y2-y1);    a=x2-x1,b=y2-y1,c=z2-z1,d=x4-x3,e=y4-y3,f=z4-z3;    g=x1-x3,h=y1-y3,i=z1-z3;    D=(b*r-c*q)*(e*p-d*q)-(a*q-p*b)*(f*q-e*r);    D1=(q*i-h*r)*(e*p-d*q)-(f*q-e*r)*(p*h-g*q);    D2=(b*r-c*q)*(p*h-g*q)-(a*q-p*b)*(q*i-h*r);    t1=D1/D,t2=D2/D;    x1+=t1*a,y1+=t1*b,z1+=t1*c;    x3+=t2*d,y3+=t2*e,z3+=t2*f;    printf("%.6lf\n%.6lf %.6lf %.6lf %.6lf %.6lf %.6lf\n",sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3)+(z1-z3)*(z1-z3)),x1,y1,z1,x3,y3,z3);    }    return 0;}
空间中已知4点,求出两直线间最短距离,不存在平行情况
原创粉丝点击