HDU4454(几何+三分)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4454

 

题意:给一个点,一个圆和一个矩形,矩形与圆没有重叠部分,求从该点出发经过圆上一点再到矩形边上一点的距离和的最小值。

 

分析:在区间[0,2*PI]内三分角度即可。

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <iomanip>
#include <math.h>

using namespace std;
const double eps = 1e-9;
const double PI = acos(-1.0);

struct Point
{
    double x,y;
};

struct Line
{
    Point a,b;
};

double dist(Point A,Point B)
{
    return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
}

double cross(Point A,Point B,Point C)
{
    return (B.x-A.x)*(C.y-A.y)-(B.y-A.y)*(C.x-A.x);
}

double distToLine(Point p,Line s)
{
    Point t = p;
    t.x += s.a.y - s.b.y;
    t.y += s.b.x - s.a.x;
    if(cross(s.a,t,p)*cross(s.b,t,p) > eps)
        return dist(p,s.a) < dist(p,s.b) ? dist(p,s.a) : dist(p,s.b);
    return fabs(cross(p,s.a,s.b))/dist(s.a,s.b);
}

Point O,cir,A,B;
Line s[4];
Point p[4];
double r;

void Import()
{
    cin>>cir.x>>cir.y>>r;
    cin>>A.x>>A.y>>B.x>>B.y;
    if(A.y < B.y) swap(A,B);
    p[0].x = A.x;
    p[0].y = B.y;
    p[1].x = B.x;
    p[1].y = B.y;
    p[2].x = B.x;
    p[2].y = A.y;
    p[3].x = A.x;
    p[3].y = A.y;
    s[0].a = p[0];
    s[0].b = p[1];
    s[1].a = p[1];
    s[1].b = p[2];
    s[2].a = p[2];
    s[2].b = p[3];
    s[3].a = p[3];
    s[3].b = p[0];
}

double equ(double alpha)
{
    Point tmp;
    tmp.x = cir.x + r*cos(alpha);
    tmp.y = cir.y + r*sin(alpha);
    double d1 = dist(O,tmp);
    double ans = 99999999;
    for(int i=0;i<4;i++)
       ans = min(ans,distToLine(tmp,s[i]));
    return d1+ans;
}

double ternarySearch(double l,double r)
{
    while(r-l>eps)
    {
        double ll=(2*l+r)/3;
        double rr=(l+2*r)/3;
        double ans1=equ(ll);
        double ans2=equ(rr);
        if(ans1 > ans2)
            l=ll;
        else
            r=rr;
    }
    return l;
}

void Work()
{
    Import();
    cout<<fixed<<setprecision(2)<<equ(ternarySearch(0,2*PI))<<endl;
}

int main()
{
    while(true)
    {
        cin>>O.x>>O.y;
        if(fabs(O.x)<eps && fabs(O.y)<eps) break;
        Work();
    }
    return 0;
}


 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值