Two men are moving concurrently, one man is moving from A to B and other man is moving from C to D. Initially the first man is at A, and the second man is at C. They maintain constant velocities such that when the first man reaches B, at the same time the second man reaches D. You can assume that A, B, C and D are 2D Cartesian co-ordinates. You have to find the minimum Euclidean distance between them along their path.

Input
Input starts with an integer T (≤ 1000), denoting the number of test cases.
Each case will contain eight integers: Ax, Ay, Bx, By, Cx, Cy, Dx, Dy. All the co-ordinates are between 0 and 100. (Ax, Ay) denotes A. (Bx, By) denotes B and so on.
Output
For each case, print the case number and the minimum distance between them along their path. Errors less than 10-6 will be ignored.
Sample Input |
Output for Sample Input |
|
3 0 0 5 0 5 5 5 0 0 0 5 5 10 10 6 6 0 0 5 0 10 1 1 1 |
Case 1: 0 Case 2: 1.4142135624 Case 3: 1 |
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const double eps = 1e-8;
struct node
{
double x,y;
};
node a,b,c,d;
double dis(node a,node b)
{
return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y);
}
double cal(double mid)
{
node p1, p2;
p1.x = a.x + (b.x - a.x) * mid;
p1.y = a.y + (b.y - a.y) * mid;
p2.x = c.x + (d.x - c.x) * mid;
p2.y = c.y + (d.y - c.y) * mid;
double d = dis(p1, p2);
return d;
}
int main(void)
{
int T;
scanf("%d",&T);
int cas = 1;
while(T--)
{
cin >> a.x >> a.y >> b.x >> b.y >> c.x >> c.y >> d.x >> d.y;
double l = 0,r = 1;
double ans = r;
while(r - l > eps)
{
double mid = (l + r) /2;
double mmid = (mid + r)/2;
if(cal(mid) < cal(mmid))
{
ans = mid;
r = mmid;
}
else
l = mid;
}
printf("Case %d: %.6f\n",cas++,sqrt(cal(ans)));
}
return 0;
}

本文介绍了一种计算两个在二维平面上以恒定速度移动的点之间最小欧几里得距离的方法。通过使用三分法搜索策略,在给定的起点和终点坐标下找到最短距离。

被折叠的 条评论
为什么被折叠?



