题目链接:Save Labman No.004
解题思路:给你两条直线,异面的空间直线,求直线间最短距离,并且求出最短距离的这两个点。下面是模板,先求出公垂线的法向量,在构造两个平面分别是两条直线各自跟公垂线的平面,会与另一条直线有交点,这两个交点就是所求的点。
#include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const double EPS = 1e-9; const int MAXN = 40; struct Point3 //空间点 { double x, y, z; Point3( double x=0, double y=0, double z=0 ): x(x), y(y), z(z) { } Point3( const Point3& a ) { x = a.x; y = a.y; z = a.z; return; } void showP() { printf("%f %f %f \n", x, y, z); } Point3 operator+( Point3& rhs ) { return Point3( x+rhs.x, y+rhs.y, z+rhs.z ); } }; struct Line3 //空间直线 { Point3 a, b; Line3 (){} Line3 (Point3 x, Point3 y){ a = x, b = y; } }; struct plane3 //空间平面 { Point3 a, b, c; plane3() {} plane3( Point3 a, Point3 b, Point3 c ): a(a), b(b), c(c) { } void showPlane() { a.showP(); b.showP(); c.showP(); return; } }; double dcmp( double a ) { if ( fabs( a ) < EPS ) return 0; return a < 0 ? -1 : 1; } //三维叉积 Point3 Cross3( Point3 u, Point3 v ) { Point3 ret; ret.x = u.y * v.z - v.y * u.z; ret.y = u.z * v.x - u.x * v.z; ret.z = u.x * v.y - u.y * v.x; return ret; } //三维点积 double Dot3( Point3 u, Point3 v ) { return u.x * v.x + u.y * v.y + u.z * v.z; } //矢量差 Point3 Subt( Point3 u, Point3 v ) { Point3 ret; ret.x = u.x - v.x; ret.y = u.y - v.y; ret.z = u.z - v.z; return ret; } //两点距离 double TwoPointDistance( Point3 p1, Point3 p2 ) { return sqrt( (p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y) + (p1.z - p2.z)*(p1.z - p2.z) ); } //向量的模 double VectorLenth( Point3 p ) { return sqrt( p.x*p.x + p.y*p.y + p.z*p.z ); } //空间直线距离 double LineToLine( Line3 u, Line3 v, Point3& tmp ) { tmp = Cross3( Subt( u.a, u.b ), Subt( v.a, v.b ) ); return fabs( Dot3( Subt(u.a, v.a), tmp ) ) / VectorLenth(tmp); } //取平面法向量 Point3 pvec( plane3 s ) { return Cross3( Subt( s.a, s.b ), Subt( s.b, s.c ) ); } //空间平面与直线的交点 Point3 Intersection( Line3 l, plane3 s ) { Point3 ret = pvec(s); double t = ( ret.x*(s.a.x-l.a.x)+ret.y*(s.a.y-l.a.y)+ret.z*(s.a.z-l.a.z) )/( ret.x*(l.b.x-l.a.x)+ret.y*(l.b.y-l.a.y)+ret.z*(l.b.z-l.a.z) ); ret.x = l.a.x + ( l.b.x - l.a.x ) * t; ret.y = l.a.y + ( l.b.y - l.a.y ) * t; ret.z = l.a.z + ( l.b.z - l.a.z ) * t; return ret; } int main(){ int t; Point3 a, b, c, d, tem; Point3 u, v, n; plane3 o, p; 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); double dis = LineToLine(Line3(a, b), Line3(c, d), tem); printf("%lf\n", dis); u = Subt(a, b), v = Subt(c, d); n = Cross3(u, v); o = plane3(a, b, Point3(a.x + n.x, a.y + n.y, a.z + n.z)); p = plane3(c, d, Point3(c.x + n.x, c.y + n.y, c.z + n.z)); u = Intersection(Line3(a, b), p); v = Intersection(Line3(c, d), o); printf("%.6lf %.6lf %.6lf %.6lf %.6lf %.6lf\n", u.x, u.y, u.z, v.x, v.y, v.z); } return 0; }