思路:直接枚举圆上的角度,然后算点到圆上一点距离和圆上一点到矩形最短距离就可以了...
#include<bits/stdc++.h>
using namespace std;
#define pi acos(-1.0)
#define INF 0x7fffffff
struct Point
{
double x,y;
};
Point s,c,recta,rectb,t;
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 PointoRecDist(Point p,Point ra,Point rb)
{
double dx=0,dy=0;
if(p.x<ra.x)
dx = ra.x-p.x;
else if(p.x>rb.x)
dx = p.x-rb.x;
if(p.y<ra.y)
dy = ra.y-p.y;
else if (p.y>rb.y)
dy = p.y-rb.y;
return sqrt(dx*dx+dy*dy);
}
int main()
{
while(scanf("%lf%lf",&s.x,&s.y)!=EOF)
{
if(s.x==0 && s.y==0)break;
double r;
scanf("%lf%lf%lf",&c.x,&c.y,&r);
scanf("%lf%lf",&recta.x,&recta.y);
scanf("%lf%lf",&rectb.x,&rectb.y);
if (recta.x>rectb.x)
swap(recta.x,rectb.x);
if (recta.y>rectb.y)
swap(recta.y,rectb.y);
double ans = INF;
for(double ang = 0.0;ang<=360;ang+=0.01)
{
t.x = c.x+cos(ang/180*pi)*r;
t.y = c.y+sin(ang/180*pi)*r;
ans = min(ans,PointoRecDist(t,recta,rectb)+dist(t,s));
}
printf("%.2lf\n",ans);
}
}