hdu 4741 Save Labman No.004 (求异面直线距离及交点)

Save Labman No.004

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1112    Accepted Submission(s): 357


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
 

Source


题意:
求异面直线的距离及两个交点。

思路:(ps:基本上是套用模板的)
异面直线距离及公垂线的距离,公垂线的距离(模板可求)就不多解释了,下面讲求交点:先求出公垂线的方向向量,然后用直线L1和公垂线的方向向量就可以构造出一个平面,而直线L2与该平面的交点(模板可求)就是所求的一个交点,另一个交点同理可得。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define maxn 35
#define INF 0xx3f3f3f3f
using namespace std;

const double eps=1e-8;
const double pi=acos(-1.0);
inline int cmp(double a)
{
    return a<-eps?-1:a>eps;
}
inline double Sqr(double a)
{
    return a*a;
}
inline double Sqrt(double a)
{
    return a<=0?0:sqrt(a);
}
class point
{
public:
    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));
    }
    point Unit()const ;
};
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 operator / (const point&a,double b)
{
    return point(a.x/b,a.y/b,a.z/b);
}
point point::Unit()const
{
    return *this/Length();
}
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;
}
double Mix(const point&a,const point&b,const point&c)
{
    return Dot(a,Det(b,c));
}
double dis(const point&a,const point&b)
{
    return Sqrt(Sqr(a.x-b.x)+Sqr(a.y-b.y)+Sqr(a.z-b.z));
}
class line
{
public:
    point a,b;
    line() {};
    line(point a,point b):a(a),b(b) {};
} sline[maxn];
double vlen(point p)  // 线段长度
{
    return p.Length();
}
bool zero(double x)   // 零值函数
{
    return fabs(x)<eps;
}
int dots_inline(point p1,point p2,point p3)  // 判断三点共线
{
    return vlen(Det(p1-p2,p2-p3))<eps;
}
int dot_online_in(point p,line l) // 判断点在线段内(包含端点)
{
    return zero(vlen(Det(p-l.a,p-l.b)))&&(l.a.x-p.x)*(l.b.x-p.x)<eps
           &&(l.a.y-p.y)*(l.b.y-p.y)<eps&&(l.a.z-p.z)*(l.b.z-p.z)<eps;
}
int dot_online_ex(point p,line l)  // 判断点在线段内(不包括端点)
{
    return dot_online_in(p,l)&&(!zero(p.x-l.a.x)||!zero(p.y-l.a.y)
                                      ||!zero(p.z-l.a.z))&&(!zero(p.x-l.b.x)||!zero(p.y-l.b.y)||!zero(p.z-l.b.z));
}
point intersection(line u,line v)  // 求两直线的交点
{
    point ret=u.a;
    double t;
    t=((u.a.x-v.a.x)*(v.a.y-v.b.y)-(u.a.y-v.a.y)*(v.a.x-v.b.x))/((u.a.x-u.b.x)*(v.a.y-v.b.y)-(u.a.y-u.b.y)*(v.a.x-v.b.x));
    ret=ret+(u.b-u.a)*t;
    return ret;
}
double ptoline(point p,line l)
{
    return vlen(Det(p-l.a,l.b-l.a))/dis(l.a,l.b);
}
double linetoline(line u,line v)
{
    point w=Det(u.a-u.b,v.a-v.b);
    return fabs(Dot(u.a-v.a,w))/vlen(w);
}

class plane
{
public:
    point a,b,c;
    plane() {};
    plane(point a,point b,point c):a(a),b(b),c(c) {};
};
point pvec(point s1,point s2,point s3) // 平面法向量
{
    return Det((s1-s2),(s2-s3));
}
int dots_onplane(point a,point b,point c,point d)
{
    return zero(Dot(pvec(a,b,c),d-a));
}
point intersection(line l,plane s)     // 求直线与平面的交点
{
    point ret=(pvec(s.a,s.b,s.c));
    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=l.a+(l.b-l.a)*t;
    return ret;
}

int n,m,ans;
double dd,A,B,C,A1,B1,C1,A2,B2,C2;
point pp[6],ansp[2];
line ln[2];

void solve()
{
    int i,j;
    A1=pp[1].x-pp[0].x;
    B1=pp[1].y-pp[0].y;
    C1=pp[1].z-pp[0].z;
    A2=pp[3].x-pp[2].x;
    B2=pp[3].y-pp[2].y;
    C2=pp[3].z-pp[2].z;
    A=B1*C2-B2*C1;
    B=A2*C1-A1*C2;
    C=A1*B2-A2*B1;
    pp[4].x=pp[0].x+A*5;
    pp[4].y=pp[0].y+B*5;
    pp[4].z=pp[0].z+C*5;
    pp[5].x=pp[2].x+A*5;
    pp[5].y=pp[2].y+B*5;
    pp[5].z=pp[2].z+C*5;
    plane pn1(pp[0],pp[1],pp[4]);
    ansp[0]=intersection(ln[1],pn1);
    plane pn2(pp[2],pp[3],pp[5]);
    ansp[1]=intersection(ln[0],pn2);
}
int main()
{
    int i,j,t;
    scanf("%d",&t);
    while(t--)
    {
        for(i=0; i<4; i++)
        {
            scanf("%lf%lf%lf",&pp[i].x,&pp[i].y,&pp[i].z);
        }
        ln[0].a=pp[0],ln[0].b=pp[1];
        ln[1].a=pp[2],ln[1].b=pp[3];
        dd=linetoline(ln[0],ln[1]);
        solve();
        printf("%.6f\n",dd);
        printf("%.6f %.6f %.6f ",ansp[1].x,ansp[1].y,ansp[1].z);
        printf("%.6f %.6f %.6f\n",ansp[0].x,ansp[0].y,ansp[0].z);
    }
    return 0;
}
/*
1
1 0 1 0 1 1 0 0 0 1 1 1
*/


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值