码迷,mamicode.com
首页 > 其他好文 > 详细

[模板]最小圆覆盖

时间:2021-03-10 12:57:35      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:system   fabs   ble   center   sys   The   fir   bool   mes   

#include <bits/stdc++.h>
using namespace std;

const double EPS = 1e-8;
const int N = 1e5+10;

struct Point{
    double x, y;
};

int n;
Point p[N];

bool equals(double a, double b){
    return fabs(a-b) < EPS;
}

bool greater_than(double a, double b){
    return a-b > EPS;
}

double dis(Point A, Point B){
    return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
}

// 求三角形的外心:中垂线的交点
Point circum_center(Point A, Point B, Point C){
    Point ret;
    double a1 = B.x - A.x;
    double b1 = B.y - A.y;
    double c1 = (a1*a1 + b1*b1)/2;

    double a2 = C.x - A.x;
    double b2 = C.y - A.y;
    double c2 = (a2*a2 + b2*b2)/2;

    double d = a1*b2 - a2*b1;

    ret.x = A.x + (c1*b2-c2*b1)/d;
    ret.y = A.y + (a1*c2-a2*c1)/d;

    return ret;
}

// 
void min_cover_circle(Point& c, double& r){
    random_shuffle(p, p+n);
    c = p[0];
    r = 0;
    for(int i = 1; i < n; ++i){
        if(greater_than(dis(c, p[i]), r)){ // the first point
            c = p[i];
            r = 0;
            for(int j = 0; j < i; ++j){ // the second point
                if(greater_than(dis(c, p[j]), r)){
                    c.x = (p[i].x + p[j].x) / 2;
                    c.y = (p[i].y + p[j].y) / 2;
                    r = dis(c, p[j]);
                    for(int k = 0; k < j; ++k){ // the third point
                        if(greater_than(dis(c, p[k]), r)){
                            c = circum_center(p[i], p[j], p[k]);
                            r = dis(p[i], c);
                        }
                    }
                }
            }
        }
    }
}

int main(){
    scanf("%d", &n);
    for(int i = 0; i < n; ++i){
        scanf("%lf%lf", &p[i].x, &p[i].y);
    }
    Point c;
    double r;
    min_cover_circle(c, r);
    printf("%.8f", r);

    // system("pause");
    return 0;
}

[模板]最小圆覆盖

标签:system   fabs   ble   center   sys   The   fir   bool   mes   

原文地址:https://www.cnblogs.com/popodynasty/p/14505544.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!