数学(F - Two Points )

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;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值