HDU 4741 Save Labman No.004

http://acm.hdu.edu.cn/showproblem.php?pid=4741

题目是输入四个点,然后确定了两条直线,然后求异面直线的距离,还有离得最近的那两个点。
纯数学题,套套数学公式就可以了。
 
#include "stdio.h"
#include "math.h"

typedef struct POINT
{
    double x;
    double y;
    double z;
}Point;

Point a,b,c,d;
Point m,n;

double sjhls( double a1, double b1, double c1, double a2, double b2, double c2, double a3, double b3, double c3 )
{
    return a1*b2*c3 + a3*b1*c2 + a2*b3*c1 - a3*b2*c1 - a2*b1*c3 -a1*b3*c2;
}

int main()
{
    int t;
    scanf( "%d", &t );
    while( t-- && scanf( "%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf", &a.x,&a.y,&a.z, &b.x,&b.y,&b.z, &c.x,&c.y,&c.z, &d.x,&d.y,&d.z ) == 12 )
    {
        double m1 = a.x - b.x;
        double n1 = a.y - b.y;
        double p1 = a.z - b.z;
        double m2 = c.x - d.x;
        double n2 = c.y - d.y;
        double p2 = c.z - d.z;
        double x1 = a.x, y1 = a.y, z1 = a.z;
        double x2 = c.x, y2 = c.y, z2 = c.z;
        double t11 = sjhls( n1*p2-n2*p1, x2-x1, m2, p1*m2-p2*m1, y2-y1, n2, m1*n2-m2*n1, z2-z1, p2 );
        double t22 = sjhls( n1*p2-n2*p1, m1, x2-x1, p1*m2-p2*m1, n1, y2-y1, m1*n2-m2*n1, p1, z2-z1 );
        double t = sjhls( n1*p2-n2*p1, m1, m2, p1*m2-p2*m1, n1, n2, m1*n2-m2*n1, p1, p2 );
        double t1 = t11/t;
        double t2 = -t22/t;
        m.x = x1 + m1 * t1;
        m.y = y1 + n1 * t1;
        m.z = z1 + p1 * t1;
        n.x = x2 + m2 * t2;
        n.y = y2 + n2 * t2;
        n.z = z2 + p2 * t2;
        double ans = sqrt( ( x2-x1+m2*t2-m1*t1 )*( x2-x1+m2*t2-m1*t1 ) + ( y2-y1+n2*t2-n1*t1 )*( y2-y1+n2*t2-n1*t1 ) + ( z2-z1+p2*t2-p1*t1 )*( z2-z1+p2*t2-p1*t1 ) );
        printf( "%.6f\n", ans );
        printf( "%.6f %.6f %.6f %.6f %.6f %.6f\n", m.x,m.y,m.z, n.x,n.y,n.z );
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/tank39/p/3911410.html