There are two points (x1, y1) and (x2, y2) on the plane. They move with the velocities (vx1, vy1) and (vx2, vy2). Find the minimal distance between them ever in future.
Input
The first line contains four space-separated integers x1, y1, x2, y2 ( - 104 ≤ x1, y1, x2, y2 ≤ 104) — the coordinates of the points.
The second line contains four space-separated integers vx1, vy1, vx2, vy2 ( - 104 ≤ vx1, vy1, vx2, vy2 ≤ 104) — the velocities of the points.
Output
Output a real number d — the minimal distance between the points. Absolute or relative error of the answer should be less than 10 - 6.
Examples
Input
1 1 2 2
0 0 -1 0
Output
1.000000000000000
Input
1 1 2 2
0 0 1 0
Output
1.414213562373095
题意:给你原始两个点坐标,(x1,y1),(x2,y2);现在这两个点分别以x方向和y方向的分速度移动,问这两个点的最短距离是什么?
思路:直接求出距离d就可以d=sqrt( (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2) );因为这两个点的位置变得的所以根据x=x0+vt;求得当时间t时刻的位置,将其带入d中可以得出一个一元二次式对其求导,求得导数=0,将其带入d中(注意t>=0)因为t是时间。
代码:
#include <bits/stdc++.h>
using namespace std;
int main()
{
double x1,x2,vx1,vx2,y1,y2,vy1,vy2;
cin>>x1>>y1>>x2>>y2>>vx1>>vy1>>vx2>>vy2;
double a=x1-x2;
double b=vx1-vx2;
double A=y1-y2;
double B=vy1-vy2;
double ans;
if(B*B+b*b==0)
{
ans=sqrt(a*a+A*A);
printf("%.15lf\n",ans);
return 0;
}
// cout<<a<<" "<<b<<" "<<A<<" "<<B<<endl;
double k=(-1.0)*(A*B+a*b)/(b*b+B*B);
double ans1=a*a+A*A+2*(A*B+a*b)*k+(B*B+b*b)*k*k;
// cout<<sqrt(a*a+A*A)<<endl;
// cout<<k<<" "<<ans1<<endl;
ans=sqrt(ans1);
if(k>=0) printf("%.15lf\n",ans);
else {
ans=sqrt(a*a+A*A);
printf("%.15lf\n",ans);
}
return 0;
}