您的位置:首页 > 其它

hdoj4741 求空间中不平行的两条直线的最短距离及最短线段与两直线的交点

2016-10-19 21:43 741 查看


Save Labman No.004

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2115    Accepted Submission(s): 711


Problem Description

Due to the preeminent research conducted by Dr. Kyouma, human beings have a breakthrough in the understanding of time and universe. According to the research, the universe in common sense is not the only one. Multi World Line is
running simultaneously. In simplicity, let us use a straight line in three-dimensional coordinate system to indicate a single World Line.

During the research in World Line Alpha, the assistant of Dr. Kyouma, also the Labman No.004, Christina dies. Dr. Kyouma wants to save his assistant. Thus, he has to build a Time Tunnel to jump from World Line Alpha to World Line Beta in which Christina can
be saved. More specifically, a Time Tunnel is a line connecting World Line Alpha and World Line Beta. In order to minimizing the risks, Dr. Kyouma wants you, Labman No.003 to build a Time Tunnel with shortest length.

 

Input

The first line contains an integer T, indicating the number of test cases. 

Each case contains only one line with 12 float numbers (x1, y1, z1), (x2, y2, z2), (x3, y3, z3), (x4, y4, z4), correspondingly indicating two points in World Line Alpha and World Line Beta. Note that a World Line is a three-dimensional line with infinite length. 

Data satisfy T <= 10000, |x, y, z| <= 10,000.

 

Output

For each test case, please print two lines.

The first line contains one float number, indicating the length of best Time Tunnel. 

The second line contains 6 float numbers (xa, ya, za), (xb, yb, zb), seperated by blank, correspondingly indicating the endpoints of the best Time Tunnel in World Line Alpha and World Line Beta. 

All the output float number should be round to 6 digits after decimal point. Test cases guarantee the uniqueness of the best Time Tunnel.

 

Sample Input

1
1 0 1 0 1 1 0 0 0 1 1 1

 

Sample Output

0.408248
0.500000 0.500000 1.000000 0.666667 0.666667 0.666667

模板题:求空间中不平行的两条直线的最短距离及最短线段与两直线的交点

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
using namespace std;
const double eps = 1e-8;
double Sqr(double a) {
return a * a;
}
double Sqrt(double a) {
return a <= 0 ? 0 : sqrt(a);
}
struct point {
double x, y, z;
point() {}
point(double x, double y, double z) : x(x), y(y), z(z) {}
double length() const {
return Sqrt(Sqr(x)+Sqr(y)+Sqr(z));//线段OP长度
}
};
point operator + (const point &a, const point &b) {//点+点=点
return point(a.x+b.x, a.y+b.y, a.z+b.z);
}
point operator - (const point &a, const point &b) {//点-点=点
return point(a.x-b.x, a.y-b.y, a.z-b.z);
}
point operator * (const point &a, double b) { //点*数 = 点
return point(a.x*b, a.y*b, a.z*b);
}
point det(const point &a, const point &b) {//叉乘(点*点=点)
return point(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 dot(const point &a, const point &b) {//点积(点*点=数)
return a.x*b.x+a.y*b.y+a.z*b.z;
}
struct line {
point a, b;
line() {}
line(point a, point b) : a(a), b(b) {}
};
double vlen(point p) {return p.length();} //线段OP长度
double lintolin(line u, line v) { //直线到直线距离
point n = det(u.a-u.b, v.a-v.b);
return fabs(dot(u.a-v.a, n))/vlen(n);
}
double angle_cos(line u, line v) { //两直线夹角的cos值
return dot(u.a-u.b, v.a-v.b) / vlen(u.a-u.b) / vlen(v.a-v.b);
}

int T;
double x, y, z;
point a, b, c, d;
void scan(point &a) {
scanf("%lf%lf%lf", &a.x, &a.y, &a.z);
}
void print(const point &a) {
printf("%.6f %.6f %.6f", a.x, a.y, a.z);
}
int main() {
scanf("%d", &T);
while(T--) {
scan(a); scan(b); scan(c); scan(d);
line m(a, b); line n(c, d);
printf("%.6f\n", lintolin(m, n));

point p1 = b - a, p2 = d - c;
point tmp = det(p1, p2);
double t1 = dot(det(c-a,p2), det(p1,p2));
t1 /= tmp.length() * tmp.length();
double t2 = dot(det(c-a,p1), det(p1,p2));
t2 /= tmp.length() * tmp.length();
point mid1 = a + (p1 * t1);
point mid2 = c + (p2 * t2);
print(mid1); printf(" ");
print(mid2); printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  计算几何 acm模板