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;
}
相关文章推荐
- hdu 4741 Save Labman No.004 空间两直线最短距离及坐标
- 【C#】点到线段最短距离的那条直线与线段的交点
- 求空间内两条直线的最近距离以及最近点的坐标(C++)
- 求空间中2条线段的最短距离(用osg+C++写的)
- 计算几何模板补充(三维空间体积、平面、直线、向量相关计算。附上hdu4741,求异面直线的最短距离与交点)
- 计算空间中两条线段的最小距离
- hdu 4741 Save Labman No.004 异面直线最短距离及对应的点
- 空间两条直线求距离
- poj1269求两条直线的关系(相交(求交点)---叉乘),平行,共线)
- 求空间两条直线之间的距离
- 空间两条直线段的最短距离及最近点计算
- 三维空间两条直线的最短距离、最近点及C++算法实现
- 判断两条直线(线段)的交点问题
- 三维空间两直线/线段最短距离、线段计算算法 【转】
- 求两条直线(线段)的交点
- HDU 4741 Save Labman No.004 (2013杭州网络赛1004题,求三维空间异面直线的距离及最近点)
- 求两条直线(线段)的交点
- 叉积--求两条直线(线段)的交点
- 判断两条直线 共线或 平行 或相交 求交点 叉乘的应用 poj 1269 Intersecting Lines
- 求空间中线段上到已知直线距离最近的点