HDU - 4793 Collision

水几何。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
using namespace std;

struct Point
{
    double x, y;
    Point(double x=0, double y=0):x(x), y(y){}
};
typedef Point Vector;

Vector operator + (Vector a, Vector b)
{
    return Vector(a.x+b.x, a.y+b.y);
}
Vector operator - (Vector a, Vector b)
{
    return Vector(a.x-b.x, a.y-b.y);
}
Vector operator * (Vector a, double p)
{
    return Vector(a.x*p, a.y*p);
}
Vector operator / (Vector a, double p)
{
    return Vector(a.x/p, a.y/p);
}
bool operator <(const Point &a, const Point &b)
{
    return a.x<b.x || (a.x==b.x && a.y<b.y);
}
const double eps=1e-10;
int dcmp(double x)
{
    if(fabs(x)<eps)return 0;
    else return x<0?-1:1;
}
double dot(Vector a, Vector b)
{
    return a.x*b.x+a.y*b.y;
}
double Length(Vector a)
{
    return sqrt(dot(a, a));
}

double cross(Vector a, Vector b)
{
    return a.x*b.y-a.y*b.x;
}

Point get_line_projection(Point p, Point a, Point b)
{
    Vector v=b-a;
    return a+v*(dot(v, p-a)/dot(v, v));
}

double gougu(double c, double a)
{
    return sqrt(c*c-a*a);
}

double Rm, R, r;
Point s, v;
Vector L;

int main()
{
    while(~scanf("%lf%lf%lf%lf%lf%lf%lf", &Rm, &R, &r, &s.x, &s.y, &v.x, &v.y))
    {
        if(Length(s+v)>Length(s-v))
        {
            printf("0.000\n");
            continue ;
        }

        if(!dcmp(v.y))
        {
            L.x=0;
            L.y=10;
        }
        else
        {
            L.x=10.0;
            L.y=-1.0*v.x*10.0/v.y;
        }

        Point P=get_line_projection(s, Point(0, 0), L); //投影点

        double dis=Length(P);  //投影点到原点距离

        if(dcmp(dis-R-r)>0)
        {
            printf("0.000\n");
            continue ;
        }

        double dis1=gougu(R+r, dis);

        if(dcmp(dis-Rm-r)>0)
        {
            printf("%.3lf\n", 2*dis1/sqrt(v.x*v.x+v.y*v.y));
            continue ;
        }

        double dis2=gougu(Rm+r, dis);

        printf("%.3lf\n", 2*(dis1-dis2)/sqrt(v.x*v.x+v.y*v.y));
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值