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

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
double a, b, c, d;
double x, y, z, w;
double solve(double n) {
    double ans;
    double x1, x2, y1, y2;
    x1 = a + c * n;
    x2 = x + z * n;
    y1 = b + d * n;
    y2 = y + w * n;
    return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int main() {
    scanf("%lf%lf%lf%lf", &a, &b, &x, &y);
    scanf("%lf%lf%lf%lf", &c, &d, &z, &w);
    double l = 0, r = 1e5, cnt = 500;
    while (cnt--) {
        double mid = (l + r) / 2;
        double m = (mid + r) / 2;
        if (solve(mid) < solve(m))
            r = m;
        else
            l = mid;
    }
    printf("%.15f\n", solve(r));
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值