最小圆覆盖

 1 struct Point{double x,y;};     
 2 struct Circle{Point c;double r;};     
 3 double dist(Point a,Point b){   
 4     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));     
 5 }     
 6 Circle calc(Point p1,Point p2,Point p3){     
 7     Circle temp;     
 8     double a,b,c,d,e,f;     
 9     a=p2.x-p1.x;     
10     b=p2.y-p1.y;     
11     c=(p2.x*p2.x+p2.y*p2.y-p1.x*p1.x-p1.y*p1.y)/2;     
12     d=p3.x-p1.x;     
13     e=p3.y-p1.y;     
14     f=(p3.x*p3.x+p3.y*p3.y-p1.x*p1.x-p1.y*p1.y)/2;     
15     temp.c.y=(c*d-f*a)/(b*d-e*a);     
16     temp.c.x=(c*e-f*b)/(a*e-b*d);     
17     return temp;     
18 }     
19 Circle minC(Point *p,int n){     
20     Circle O;     
21     int i,j,k;     
22     O.c=p[0];O.r=0;  
23     for(i=1;i<n;i++){    
24         if(dist(O.c,p[i])<=O.r+1e-6)continue;     
25         O.c=p[i];O.r=0;     
26         for(j=0;j<i;j++){     
27             if(dist(O.c,p[j])<=O.r+1e-6)continue;     
28             O.c.x=(p[i].x+p[j].x)/2;O.c.y=(p[i].y+p[j].y)/2;O.r=dist(O.c,p[j]);     
29             for(k=0;k<j;k++){    
30                 if(dist(O.c,p[k])<=O.r+1e-6)continue;     
31                 O=calc(p[i],p[j],p[k]);     
32                 O.r=dist(O.c,p[k]);     
33             }     
34         }     
35     }     
36     return O;     
37 }    

 

posted @ 2017-10-15 15:22  weeping  阅读(202)  评论(0编辑  收藏  举报