潸然泪下的小飞飞

导航

最小圆覆盖

  1 struct TPoint
  2 {
  3     double x, y;
  4     TPoint operator-(TPoint &a)
  5     {
  6         TPoint p1;
  7         p1.x = x - a.x;
  8         p1.y = y - a.y;
  9         return p1;
 10     }
 11 };
 12 
 13 struct TCircle
 14 {
 15     double r;
 16     TPoint centre;
 17 };
 18 
 19 struct TTriangle
 20 {
 21     TPoint t[3];
 22 };
 23 
 24 TCircle c;
 25 TPoint a[maxn];
 26 
 27 double distance(TPoint p1, TPoint p2)
 28 {
 29     TPoint p3;
 30     p3.x = p2.x - p1.x;
 31     p3.y = p2.y - p1.y;
 32     return sqrt(p3.x * p3.x + p3.y * p3.y);
 33 }
 34 
 35 double triangleArea(TTriangle t)
 36 {
 37     TPoint p1, p2;
 38     p1 = t.t[1] - t.t[0];
 39     p2 = t.t[2] - t.t[0];
 40     return fabs(p1.x * p2.y - p1.y * p2.x) / 2;
 41 }
 42 
 43 TCircle circumcircleOfTriangle(TTriangle t)
 44 {
 45     //三角形的外接圆
 46     TCircle tmp;
 47     double a, b, c, c1, c2;
 48     double xA, yA, xB, yB, xC, yC;
 49     a = distance(t.t[0], t.t[1]);
 50     b = distance(t.t[1], t.t[2]);
 51     c = distance(t.t[2], t.t[0]);
 52     //根据S = a * b * c / R / 4;求半径R 
 53     tmp.r = a * b * c / triangleArea(t) / 4;
 54     
 55     xA = t.t[0].x; yA = t.t[0].y;
 56     xB = t.t[1].x; yB = t.t[1].y;
 57     xC = t.t[2].x; yC = t.t[2].y;
 58     c1 = (xA * xA + yA * yA - xB * xB - yB * yB) / 2;
 59     c2 = (xA * xA + yA * yA - xC * xC - yC * yC) / 2;
 60     
 61     tmp.centre.x = (c1 * (yA - yC) - c2 * (yA - yB)) / 
 62          ((xA - xB) * (yA - yC) - (xA - xC) * (yA - yB)); 
 63     tmp.centre.y = (c1 * (xA - xC) - c2 * (xA - xB)) / 
 64          ((yA - yB) * (xA - xC) - (yA - yC) * (xA - xB)); 
 65          
 66     return tmp;     
 67 }
 68 
 69 TCircle MinCircle2(int tce, TTriangle ce)
 70 {
 71     TCircle tmp;
 72     if(tce == 0) tmp.r = -2;
 73     else if(tce == 1) 
 74     {
 75         tmp.centre = ce.t[0];
 76         tmp.r = 0;
 77     }
 78     else if(tce == 2)
 79     {
 80         tmp.r = distance(ce.t[0], ce.t[1]) / 2;
 81         tmp.centre.x = (ce.t[0].x + ce.t[1].x) / 2;
 82         tmp.centre.y = (ce.t[0].y + ce.t[1].y) / 2;     
 83     }
 84     else if(tce == 3) tmp = circumcircleOfTriangle(ce);
 85     return tmp;
 86 }
 87 
 88 void MinCircle(int t, int tce, TTriangle ce)
 89 {
 90     int i, j;
 91     TPoint tmp;
 92     c = MinCircle2(tce, ce);
 93     if(tce == 3) return;
 94     for(i = 1;i <= t;i++)
 95     {
 96         if(distance(a[i], c.centre) > c.r)
 97         {
 98             ce.t[tce] = a[i];
 99             MinCircle(i - 1, tce + 1, ce);
100             tmp = a[i];
101             for(j = i;j >= 2;j--)
102             {
103                 a[j] = a[j - 1];
104             }
105             a[1] = tmp;
106         }
107     }
108 }
109 
110 void run(int n)
111 {
112     TTriangle ce;
113     int i;
114     MinCircle(n, 0, ce);
115     printf("%.2lf %.2lf %.2lf\n", c.centre.x, c.centre.y, c.r);
116 }
117 
118 int main()
119 {
120     freopen("circle.in", "r", stdin);
121     freopen("out.txt", "w", stdout);
122     int n;
123     while(scanf("%d", &n) != EOF && n)
124     {
125         for(int i = 1;i <= n;i++)
126             scanf("%lf%lf", &a[i].x, &a[i].y);
127         run(n);
128     }
129     return 0;
130 }

 

posted on 2015-03-05 16:21  潸然泪  阅读(262)  评论(0编辑  收藏  举报