方法1:
- 已知空间中两直线AB, CD,判断它们是否相交
问题的关键是求出这两条直线之间的最短距离,以及在这个距离上最接近两线的点坐标,判断该点是否在直线AB和直线CD上。
首先将直线方程化为对称式,分别得到两直线方向向量AB=(x1,y1,z1), CD=(x2,y2,z2),
再将两向量AB, CD叉乘得到其公垂向量N=(x,y,z),在AB, CD两直线上分别选取点E,F(任意),得到向量M,求向量M在向量N方向的投影即为两异面直线间的距离了(就是最短距离啦)。
最短距离的求法:d=|向量N向量M|/|向量N|(上面是两向量的数量积,下面是取模)。*
设两直线与距离的交点分别为S,T,可带入公垂线N的对称式中得到第一个方程,又因为S,T两点分别满足直线AB和CD的方程,所以得到关于S(或T)的第二个方程,联立两个方程分别解出来即可!
方法2:
#include<iostream>
#include<cmath>
using namespace std;
struct Point
{
double x, y, z;
Point(double x = 0, double y = 0, double z = 0) : x(x), y(y), z(z) {}
};
typedef Point Vector;
Vector operator + (Vector a, Vector b)
{
return Vector(a.x + b.x, a.y + b.y, a.z + b.z);
};
Vector operator - (Vector a, Vector b)
{
return Vector(a.x - b.x, a.y - b.y, a.z - b.z);
};
Vector operator * (Vector a, double p)
{
return Vector(a.x * p, a.y * p, a.z * p);
}
Vector operator / (Vector a, double p)
{
return Vector(a.x / p, a.y / p, a.z / p);
}
double Dot(Vector a, Vector b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
double Length(Vector a)
{
return sqrt(Dot(a, a));
}
Vector Cross(Point a, Point b)
{
return Vector(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
}
Point a1, b1, a2, b2;
int main()
{
int n;
scanf("%d", &n);
while(n--)
{
scanf("%lf%lf%lf", &a1.x, &a1.y, &a1.z);
scanf("%lf%lf%lf", &b1.x, &b1.y, &b1.z);
scanf("%lf%lf%lf", &a2.x, &a2.y, &a2.z);
scanf("%lf%lf%lf", &b2.x, &b2.y, &b2.z);
Vector v1 = (a1 - b1), v2 = (a2 - b2);
Vector N = Cross(v1, v2);
Vector ab = (a1 - a2);
double ans = Dot(N, ab) / Length(N);
Point p1 = a1, p2 = a2;
Vector d1 = b1 - a1, d2 = b2 - a2;
Point ans1, ans2;
double t1, t2;
t1 = Dot((Cross(p2 - p1, d2)), Cross(d1, d2));
t2 = Dot((Cross(p2 - p1, d1)), Cross(d1, d2));
double dd = Length((Cross(d1, d2)));
t1 /= dd * dd;
t2 /= dd * dd;
ans1 = (a1 + (b1 - a1) * t1);
ans2 = (a2 + (b2 - a2) * t2);
printf("%.6f\n", fabs(ans));
printf("%.6f %.6f %.6f ", ans1.x, ans1.y, ans1.z);
printf("%.6f %.6f %.6f\n", ans2.x, ans2.y, ans2.z);
}
return 0;
}
</br>