ZOJ 1450
最小圆覆盖
#include <iostream> #include <algorithm> #include <cstdio> #include <cmath> using namespace std; const double eps=0.00000001; struct point { double x,y; }p[110]; struct circle{ point cent; double rad; }cir; int n; double TriangleArea(point t1,point t2,point t3){ point p1,p2; p1.x=t1.x-t3.x; p1.y=t1.y-t3.y; p2.x=t2.x-t3.x; p2.y=t2.y-t3.y; return fabs(p1.x*p2.y-p2.x*p1.y)/2; } double dist(point p1,point p2){ return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); } void TriangleCircle(point ta,point tb,point tc){ double a=dist(ta,tb); double b=dist(tb,tc); double c=dist(tc,ta); cir.rad=(a*b*c)/TriangleArea(ta,tb,tc)/4; double xa=ta.x; double ya=ta.y; double xb=tb.x; double yb=tb.y; double xc=tc.x; double yc=tc.y; double c1=(xa*xa+ya*ya-xb*xb-yb*yb)/2; double c2=(xa*xa+ya*ya-xc*xc-yc*yc)/2; cir.cent.x=(c1*(ya-yc)-c2*(ya-yb))/((xa-xb)*(ya-yc)-(xa-xc)*(ya-yb)); cir.cent.y=(c1*(xa-xc)-c2*(xa-xb))/((ya-yb)*(xa-xc)-(ya-yc)*(xa-xb)); } void slove(){ random_shuffle(p,p+n); cir.cent=p[0]; cir.rad=0; for(int i=1;i<n;i++){ if(dist(p[i],cir.cent)-eps>cir.rad ){ cir.cent=p[i]; cir.rad=0; for(int j=0;j<i;j++){ if(dist(p[j],cir.cent)-eps>cir.rad ){ cir.cent.x=(p[j].x+p[i].x)/2; cir.cent.y=(p[i].y+p[j].y)/2; cir.rad=dist(p[j],p[i])/2; for(int k=0;k<j;k++){ if(dist(p[k],cir.cent)-eps>cir.rad ){ TriangleCircle(p[i],p[j],p[k]); } } } } } } } int main(){ while(scanf("%d",&n)!=EOF){ if(n==0) break; for(int i=0;i<n;i++){ scanf("%lf%lf",&p[i].x,&p[i].y); } slove(); printf("%0.2f %0.2f %0.2f\n",cir.cent.x,cir.cent.y,cir.rad); } return 0; }