简单的计算几何;
可以把0-2*pi分成几千份,然后找出最小的;
也可以用三分;
1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 #define pi acos(-1) 5 #define eps 1e-6 6 using namespace std; 7 8 struct node 9 { 10 double x,y; 11 node(double x=0,double y=0):x(x),y(y){ } 12 bool operator<(const node &t)const 13 { 14 if(x==t.x)return y<t.y; 15 else return x<t.x; 16 } 17 }; 18 node operator-(node u,node v){return node(u.x-v.x,u.y-v.y);} 19 node operator+(node u,node v){return node(u.x+v.x,u.y+v.y);} 20 node operator*(node u,double v){return node(u.x*v,u.y*v);} 21 node operator/(node u,double v){return node(u.x/v,u.y/v);} 22 int dcmp(double x) 23 { 24 if(fabs(x)<eps)return 0; 25 else return x<0?-1:1; 26 } 27 bool operator==(const node& a,const node& b) 28 { 29 return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)==0; 30 } 31 double dot(node a,node b){return a.x*b.x+a.y*b.y;} 32 double length(node a){return sqrt(dot(a,a));} 33 double cross(node a,node b){return a.x*b.y-a.y*b.x;} 34 double disofnode(node p,node a,node b) 35 { 36 if(a==b)return length(p-a); 37 node v1=b-a,v2=p-a,v3=p-b; 38 if(dcmp(dot(v1,v2))<0)return length(v2); 39 else if(dcmp(dot(v1,v3))>0)return length(v3); 40 else return fabs(cross(v1,v2))/length(v1); 41 } 42 43 44 45 node a,yuan,d[4]; 46 double r; 47 double dis(double alph) 48 { 49 node tmp; 50 tmp.x=yuan.x+r*cos(alph); 51 tmp.y=yuan.y+r*sin(alph); 52 double dd1=min(disofnode(tmp,d[0],d[1]),disofnode(tmp,d[0],d[2])); 53 double dd2=min(disofnode(tmp,d[1],d[3]),disofnode(tmp,d[2],d[3])); 54 double ret=min(dd1,dd2); 55 return (ret+length(a-tmp)); 56 } 57 58 int main() 59 { 60 while(scanf("%lf%lf",&a.x,&a.y)) 61 { 62 if(dcmp(a.x)==0 && dcmp(a.y)==0)break; 63 scanf("%lf%lf",&yuan.x,&yuan.y); 64 scanf("%lf",&r); 65 double x1,x2,y1,y2; 66 scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); 67 d[0].x=x1,d[0].y=y1; 68 d[1].x=x2,d[1].y=y1; 69 d[2].x=x2,d[2].y=y2; 70 d[3].x=x1,d[3].y=y2; 71 sort(d,d+4); 72 double mi=0.0,ma=2*pi; 73 double ans=9999999999.0; 74 for(int i=0;i<=5000;i++) 75 { 76 double mm=(2*pi*i)/5000; 77 double fd=dis(mm); 78 ans=min(ans,fd); 79 // double m1=mi+(ma-mi)/3;//三分的代码 80 // double m2=ma-(ma-mi)/3; 81 // if(dis(m1)>dis(m2))mi=m1; 82 // else ma=m2; 83 } 84 printf("%.2lf ",ans); 85 } 86 return 0; 87 }