最小圆覆盖 ( C++ )

本文介绍了一种基于随机化和优化的算法,用于在一个三维点集中找到最小覆盖圆。通过`min_cover_circle`函数,计算并更新圆心和半径,以实现对点集的有效覆盖。主要涉及了距离计算、点集操作和圈心计算等技术。
摘要由CSDN通过智能技术生成
#include<iostream>
#include<cstdio>
#include<cmath>
#include <algorithm>
#define eps 1e-8
using namespace std;

const int MAX=5000;
int n;
double a[MAX][3];
struct Point {
    double x,y;
};

int sgn(double x) {
    if(fabs(x)<eps) return 0;
    else return x<0?-1:1;
}
double Distance (Point A, Point B) {
    return hypot(A.x-B.x,A.y-B.y);
}
Point circle_center(const Point a, const Point b,const Point c) {
    Point center;
    double a1=b.x-a.x, b1=b.y-a.y, c1=(a1*a1+b1*b1)/2;
    double a2=c.x-a.x, b2=c.y-a.y, c2=(a2*a2+b2*b2)/2;
    double d=a1*b2-a2*b1;
    center.x=a.x+(c1*b2-c2*b1)/d;
    center.y=a.y+(a1*c2-a2*c1)/d;
    return center;
}
void min_cover_circle(Point* p, int nn, Point& c, double& r) {
    random_shuffle(p, p+nn);
    c=p[0];
    r=0;
    for(int i=1; i<nn; i++)
        if(sgn(Distance(p[i],c)-r)>0) {
            c=p[i];
            r=0;
            for(int j=0; j<i; j++)
                if(sgn(Distance(p[j],c)-r)>0) {
                    c.x=(p[i].x+p[j].x)/2;
                    c.y=(p[i].y+p[j].y)/2;
                    r=Distance(p[j],c);
                    for(int k=0; k<j; k++)
                        if(sgn(Distance(p[k],c)-r)>0) {
                            c=circle_center(p[i],p[j],p[k]);
                            r=Distance(p[i],c);
                        }
                }
        }
}

double f1() {
    double ans;
    Point p[n];
    Point c;
    double r;
    for(int i=0; i<n; i++) {
        p[i].x=a[i][0];
        p[i].y=a[i][2];
    }
//    for(int i=0; i<n; i++) {
//        printf("x = %0.10lf, y = %0.10lf\n",p[i].x,p[i].y);
//    }
    min_cover_circle(p,n,c,r);
    ans=r*2;
//    printf("r = %0.10lf\n",r);
    return ans;
}
double f2() {
    double ans;
    Point p[n];
    Point c;
    double r;
    for(int i=0; i<n; i++) {
        p[i].x=a[i][1];
        p[i].y=a[i][2];
    }
//    for(int i=0; i<n; i++) {
//        printf("x = %0.10lf, y = %0.10lf\n",p[i].x,p[i].y);
//    }
    min_cover_circle(p,n,c,r);
    ans=r*2;
    return ans;
}
double f3() {
    double ans;
    Point p[n];
    Point c;
    double r;
    for(int i=0; i<n; i++) {
        p[i].x=a[i][0];
        p[i].y=a[i][1];
    }
//    for(int i=0; i<n; i++) {
//        printf("x = %0.10lf, y = %0.10lf\n",p[i].x,p[i].y);
//    }
    min_cover_circle(p,n,c,r);
    ans=r*2;
    return ans;
}

int main() {
    while(~scanf("%d",&n)) {
        for(int i=0; i<n; i++) {
            for(int j=0; j<3; j++) {
                scanf("%lf",&a[i][j]);
            }
        }

        double ans=f1();
        double tmp1=f2();
        double tmp2=f3();
        if(ans>tmp1) {
            ans=tmp1;
        }
        if(ans>tmp2) {
            ans=tmp2;
        }

        printf("%.10lf\n",ans);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值