Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1473 Accepted Submission(s): 484
有必要拿出空间解析几何出来看看了,尼玛用平面方程跟直线方程求解的时候除数为零了。最后没办法只能看大神的模版了。。。。
大概解题思路:先求出两直线的公垂线(两方向向量的叉积),以公垂线跟一直线形成一平面求另一直线与该平面的交点。
#include <iostream> #include <cstring> #include <cstdio> #include <cmath> using namespace std; const double eps = 1e-8; struct Point3//三维空间点 { double x, y, z; Point3(double x=0,double y=0,double z=0): x(x),y(y),z(z){} Point3 operator + (Point3 &t){return Point3(x+t.x, y+t.y, z+t.z);} Point3 operator - (Point3 &t) {return Point3(x-t.x, y-t.y, z-t.z);} Point3 operator * (double p) {return Point3(x*p, y*p, z*p);} Point3 operator / (double p) {return Point3(x/p, y/p, z/p);} }; typedef Point3 Vector3; struct Line//空间直线 { Point3 a,b; }; struct Plane//空间平面 { Point3 a,b,c; Plane(){} Plane(Point3 a, Point3 b, Point3 c):a(a),b(b),c(c){} }; int dcmp(double x) { if(fabs(x) < eps) return 0; return x < 0 ? -1 : 1; } double Dot(Vector3 A,Vector3 B) { return A.x*B.x + A.y*B.y + A.z*B.z; } double Length2(Vector3 A) { return Dot(A, A); } Vector3 Cross(Vector3 A, Vector3 B) { return Vector3(A.y*B.z - A.z*B.y, A.z*B.x - A.x*B.z, A.x*B.y - A.y*B.x); } double LineToLine(Line u,Line v,Vector3 &t)//空间直线间距离 { t=Cross(u.a-u.b,v.a-v.b); return fabs(Dot(u.a-v.a,t))/sqrt(Length2(t)); } Vector3 normalVector(Plane s)//取平面法向量 { return Cross(s.a-s.b,s.b-s.c); } Point3 Intersection(Line l,Plane s)//空间平面与直线的交点 { Point3 ret = normalVector(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; } void solve(Line A, Line B) { Vector3 normal; double d = LineToLine(A,B,normal); printf("%.6lf\n",d); Plane pa = Plane(A.a,A.b,A.a+normal); Plane pb = Plane(B.a,B.b,B.a+normal); Point3 u = Intersection(B,pa); Point3 v = Intersection(A,pb); printf("%.6lf %.6lf %.6lf %.6lf %.6lf %.6lf\n", v.x, v.y, v.z, u.x, u.y, u.z ); } int main() { int T; scanf("%d",&T); while(T--) { Line A,B; scanf("%lf%lf%lf", &A.a.x, &A.a.y, &A.a.z); scanf("%lf%lf%lf", &A.b.x, &A.b.y, &A.b.z); scanf("%lf%lf%lf", &B.a.x, &B.a.y, &B.a.z); scanf("%lf%lf%lf", &B.b.x, &B.b.y, &B.b.z); solve(A,B); } return 0; }
hdu 4741 Save Labman No.004异面直线间的距离既构成最小距离的两个端点,布布扣,bubuko.com
hdu 4741 Save Labman No.004异面直线间的距离既构成最小距离的两个端点
原文:http://www.cnblogs.com/xiong-/p/3847767.html