UVa 10794 - The Deadly Olympic Returns!!!

其实博主第一反应也是三分 .......  


提示:

1. 本题一个较好的做法是相对运动 , 即假设一个物体静止 , 然而另一个物体沿着某一个速度运动(两个运动的速度的差,,,这里不准确 , 毋喷)

2. 然后就是一个线段和点距离的裸题目了


想说的是 , 首先这跟某一个例题不同 , 当时我们只求两个线段的距离 , 但并不要求最近的两个点在同一时刻


注意: 博主有一个地方查了好久 , 这一次不是精度问题 , 是二维几何到三维几何的水土不服 , 那个operator == 一定要注意是x , y , z都相等


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;
const double eps = 1e-8;

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

struct points
{
	double x , y , z;
	void read() { scanf("%lf%lf%lf",&x,&y,&z); } 
	points(double x =0 , double y =0  , double z = 0):x(x),y(y),z(z){}
};

typedef points 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 b) { return Vector(a.x*b   , a.y*b   , a.z*b  ); }
Vector operator /(Vector a , double b) { return Vector(a.x/b   , a.y/b   , a.z/b  ); }
bool operator ==(Vector a , Vector b) { return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)==0 && dcmp(a.z-b.z)==0; }

double Dot(Vector a , Vector b) { return a.x*b.x+a.y*b.y+a.z*b.z; }
Vector Cross(Vector a , Vector 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); }
double Length(Vector a) { return sqrt(Dot(a, a)); }

double DistanceOfPointsAndSegment(points p , points a , points b)
{
	if(a==b) return Length(p-a); 
	Vector v1 = b-a , v2 = p-a , v3= p-b;
	if(dcmp(Dot(v1, v2))<0) return Length(v2);
	if(dcmp(Dot(v1, v3))>0) return Length(v3);
	return Length(Cross(v1, v2))/Length(v1); 
}

int main(int argc, char *argv[]) {
	
	int t;
	scanf("%d",&t); 
	
	for(int Case=1;Case<=t;Case++)
	{
		double times ;
		scanf("%lf",×); 
		points a , b , c , d;
		a.read(); b.read(); c.read(); d.read();
		Vector u = (b-a)/times , v = (d-c)/times; 
		
		printf("Case %d: %.4lf\n",Case,DistanceOfPointsAndSegment(a , c , c+(v-u)*(1e10)));
	}
	
	
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值