Problem Description
There is a big round cake on the ground. A small ant plans to steal a small piece of cake. He starts from a certain point, reaches the cake, and then carry the piece back home. He does not want to be detected, so he is going to design a shortest path to achieve his goal. The big cake can be considered as a circle on a 2D plane. The ant’s home can be considered as a rectangle. The ant can walk through the cake. Please find out the shortest path for the poor ant.
Input
The input consists of several test cases. The first line of each test case contains x,y, representing the coordinate of the starting point. The second line contains x, y, r. The center of the cake is point (x,y) and the radius of the cake is r. The third line contains x1,y1,x2,y2, representing the coordinates of two opposite vertices of the rectangle --- the ant's home. All numbers in the input are real numbers range from -10000 to 10000. It is guaranteed that the cake and the ant's home don't overlap or contact, and the ant's starting point also is not inside the cake or his home, and doesn't contact with the cake or his home. If the ant touches any part of home, then he is at home. Input ends with a line of 0 0. There may be a blank line between two test cases.
Output
For each test case, print the shortest distance to achieve his goal. Please round the result to 2 digits after decimal point.
Sample Input
1 1 -1 1 1 0 -1 1 0 0 2 -1 1 1 0 -1 1 0 0 0
Sample Output
1.75 2.00#include<iostream> #include<cstring> #include<string> #include<cmath> #include<cstdio> using namespace std; double xx1,yy1,xx2,yy2; const double eps=1e-8; const double PI=acos(-1.0); struct Point { double x; double y; }; Point ant; struct mm { double x; double y; double r; //圆的半径 }; mm cake; double cal(double angle) { double sum1,sum2,ans; double xx,yy; //表示圆上角度刚好为angle的点 xx=cake.x+cake.r*cos(angle); yy=cake.y+cake.r*sin(angle); sum1=sqrt((xx-ant.x)*(xx-ant.x)+(yy-ant.y)*(yy-ant.y)); //sum1为ant到cake的距离 sum2=0; //sum2为cake到home的距离 if(xx<xx1) //在矩形的左半边 { if(yy>=yy1&&yy<=yy2) sum2=xx1-xx; else if(yy<yy1) sum2=sqrt((xx-xx1)*(xx-xx1)+(yy-yy1)*(yy-yy1)); else sum2=sqrt((xx-xx1)*(xx-xx1)+(yy-yy2)*(yy-yy2)); } else if(xx>xx2) //在矩形的右半边 { if(yy>=yy1&&yy<=yy2) sum2=xx-xx2; else if(yy<yy1) sum2=sqrt((xx-xx2)*(xx-xx2)+(yy-yy1)*(yy-yy1)); else sum2=sqrt((xx-xx2)*(xx-xx2)+(yy-yy2)*(yy-yy2)); } else { if(yy<=yy1) sum2=yy1-yy; else sum2=yy-yy2; } ans=sum1+sum2; return ans; } int main() { double txx1,tyy1,txx2,tyy2; while(~scanf("%lf%lf",&ant.x,&ant.y)) { if(ant.x==0&&ant.y==0) break; scanf("%lf%lf%lf",&cake.x,&cake.y,&cake.r); scanf("%lf%lf%lf%lf",&txx1,&tyy1,&txx2,&tyy2); //xx1,yy1,xx2,yy2; 两个对角线构成home矩形 xx1=txx1<txx2?txx1:txx2; yy1=tyy1<tyy2?tyy1:tyy2; xx2=txx1>txx2?txx1:txx2; yy2=tyy1>tyy2?tyy1:tyy2; double l,r,mid,mimid; double mid1,mid2; //把圆分成两半来三分,因为从0~2*PI二分,在ant的另一边也有个最小值,但那个并不是最小的 l=0,r=PI; while(r-l>eps) { mid=(l+r)/2.0,mimid=(r+mid)/2.0; if(cal(mid)<cal(mimid)) r=mimid; else l=mid; } mid1=mid; l=PI,r=2*PI; while(r-l>eps) { mid=(l+r)/2.0,mimid=(r+mid)/2.0; if(cal(mid)<cal(mimid)) r=mimid; else l=mid; } mid2=mid; double res=cal(mid1)<cal(mid2)?cal(mid1):cal(mid2); printf("%.2f\n",res); } return 0; }