1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| # 万能头不是好习惯,但是先这样吧.. #include <bits/stdc++.h> using namespace std;
int N;
#define eps 1e-9
struct Point { double x; double y; Point(double x, double y) : x(x), y(y) {} };
double distance(Point u, Point v) { auto dx = abs(u.x - v.x); auto dy = abs(u.y - v.y); return sqrt(dx * dx + dy * dy); }
struct Circle { Point o = Point(0, 0);
double r;
Circle(){ r = 0; }
Circle(Point o, double r) : o(o), r(r) {}
Circle circle_from(Point p1, Point p2) { auto _o = Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2); auto _r = distance(p1, p2) / 2;
return Circle(_o, _r); }
static Circle circle_from(Point u, Point v, Point w) { auto a1 = 2 * (v.x - u.x); auto b1 = 2 * (v.y - u.y); auto c1 = v.x * v.x + v.y * v.y - u.x * u.x - u.y * u.y; auto a2 = 2 * (w.x - v.x); auto b2 = 2 * (w.y - v.y); auto c2 = w.x * w.x + w.y * w.y - v.x * v.x - v.y * v.y;
auto x = ((c1 * b2) - c2 * b1) / ((a1 * b2) - (a2 * b1)); auto y = ((a1 * c2) - (a2 * c1)) / ((a1 * b2) - (a2 * b1)); auto o = Point(x, y); auto r = distance(o, u);
return Circle(o, r); } };
bool inside(Point p, Point o, double r) { return distance(p, o) <= r + eps; }
Circle smallest_circle_cover(vector<Point>& p) { Point o = p[0]; double r = 0;
for (int i = 0; i < N; i++) { if (inside(p[i], o, r)) continue;
o.x = (p[i].x + p[0].x) / 2; o.y = (p[i].y + p[0].y) / 2; r = distance(p[i], p[0]) / 2;
for (int j = 1; j < i; j++) { if (inside(p[j], o, r)) continue;
o.x = (p[i].x + p[j].x) / 2; o.y = (p[i].y + p[j].y) / 2; r = distance(p[i], p[j]) / 2;
for (int k = 0; k < j; k++) { if (inside(p[k], o, r)) continue;
Circle c = Circle::circle_from(p[i], p[j], p[k]); o = c.o; r = c.r; } } }
return Circle(o, r); }
int main() { cin >> N; vector<Point> ps; ps.reserve(N); double x, y; for (int i = 0; i < N; i++) { cin >> x >> y; ps.emplace_back(x, y); }
srand(static_cast<unsigned>(time(nullptr))); for (int i = N - 1; i > 0; i--) { swap(ps[i], ps[rand() % (i + 1)]); }
auto c = smallest_circle_cover(ps);
printf("%.9f\n%.9f %.9f\n", c.r, c.o.x, c.o.y); }
|
评 论