[ACM_几何] The Deadly Olympic Returns!!! (空间相对运动之最短距离)

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28235#problem/B

题目大意: 有两个同时再空间中匀速运动的导弹,告诉一个时间以及各自的初始坐标和该时间时的坐标,求运动过程中的最短距离
解题思路:  求出相对初位置、相对速度,则答案就是原点到射线型轨迹的距离,注意是射线!!!

 

//*******************************************************************************
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stdio.h>
using namespace std;
#define eps 1e-8

int dcmp(double x){
    if(fabs(x)<eps)return 0;
    else return x<0 ? -1:1;
}

struct Point3{
    double x,y,z;
    Point3(double x=0,double y=0,double z=0):x(x),y(y),z(z){}
    
};
bool operator==(const Point3& a,const Point3& b){
    return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)==0 && dcmp(a.z-b.z)==0 ;
}
typedef Point3 Vector3;
Vector3 operator+(Vector3 A,Vector3 B){
    return Vector3(A.x+B.x,A.y+B.y,A.z+B.z);
}
Vector3 operator-(Vector3 A,Vector3 B){
    return Vector3(A.x-B.x,A.y-B.y,A.z-B.z);
}
Vector3 operator*(Vector3 A,double p){
    return Vector3(A.x*p,A.y*p,A.z*p);
}
Vector3 operator/(Vector3 A,double p){
    return Vector3(A.x/p,A.y/p,A.z/p);
}

double Dot(Vector3 A,Vector3 B){return A.x*B.x+A.y*B.y+A.z*B.z;}
double Length(Vector3 A){return sqrt(Dot(A,A));}
double Angle(Vector3 A,Vector3 B){return acos(Dot(A,B)/Length(A)/Length(B));}
//叉积
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);
}
//点p到射线AB的距离
double DDSXJL(Point3 p,Point3 A,Point3 B){
    if(A==B)return Length(p-A);
    Vector3 v1=B-A,v2=p-A;
    if(dcmp(Dot(v1,v2))<0)return Length(v2);
    else return Length(Cross(v1,v2))/Length(v1);
}
//******************************************************************************
int main(){
    Point3 now[2],fut[2],delta;
    int T;cin>>T;
    
    for(int kase=1;kase<=T;kase++){

        int time;cin>>time;
        cin>>now[0].x>>now[0].y>>now[0].z;
        cin>>fut[0].x>>fut[0].y>>fut[0].z;
        cin>>now[1].x>>now[1].y>>now[1].z;
        cin>>fut[1].x>>fut[1].y>>fut[1].z;

        Point3 B;//坐标原点
        delta=now[1]-now[0];//相对初位置
        Vector3 speed=((fut[1]-now[1])-(fut[0]-now[0]));//相对速度
        printf("Case %d: %.4lf\n",kase,DDSXJL(B,delta,delta+speed));//答案就是原点到轨迹的距离
    }return 0;

}
//*******************************************************************************
View Code

 

posted @ 2013-08-08 20:15  beautifulzzzz  阅读(418)  评论(0编辑  收藏  举报