数学一本通——【计算几何】Minimal Circle(最小圆覆盖模板)

Description
You are to write a program to find a circle which covers a set of points and has the minimal area. There will be no more than 100 points in one problem.

Input
The input contains several problems. The first line of each problem is a line containing only one integer N which indicates the number of points to be covered. The next N lines contain N points. Each point is represented by x and y coordinates separated by a space. After the last problem, there will be a line contains only a zero.

Output
For each input problem, you should give a one-line answer which contains three numbers separated by spaces. The first two numbers indicate the x and y coordinates of the result circle, and the third number is the radius of the circle. (use escape sequence %.2f)

Samples
Input 复制
2
0.0 0.0
3 0
5
0 0
0 1
1 0
1 1
2 2
0
Output
1.50 0.00 1.50
1.00 1.00 1.41

代码:
转自

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll>PLL;
typedef pair<int,int>PII;
typedef pair<double,double>PDD;
#define I_int ll
inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
#define read read()
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define perr(i,a,b) for(int i=(a);i>(b);i--)
ll ksm(ll a,ll b,ll p)
{
    ll res=1;
    while(b)
    {
        if(b&1)res=res*a%p;
        a=a*a%p;
        b>>=1;
    }
    return res;
}
#define PI acos(-1)
const double eps=1e-8;
const int maxn=5100;
#define EPS 1e-8

struct point{
    double x, y;
};
int sgn(double x)
{
    if (fabs(x) < EPS)
        return 0;
    return x < 0 ? -1 : 1;
}
double get_distance(const point a, const point b)//两点之间的距离
{
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
point get_circle_center(const point a, const point b, const point c)//得到三角形外接圆的圆心
{
    point center;
    double a1 = b.x - a.x;
    double b1 = b.y - a.y;
    double c1 = (a1 * a1 + b1 * b1) / 2.0;
    double a2 = c.x - a.x;
    double b2 = c.y - a.y;
    double c2 = (a2 * a2 + b2 * b2) / 2.0;
    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;
}
//p表示定点, n表示顶点的个数, c代表最小覆盖圆圆心, r是半径
void min_cover_circle(point *p, int n, point &c, double &r)//找最小覆盖圆(这里没有用全局变量p[], 因为是为了封装一个函数便于调用)
{
    random_shuffle(p, p + n);//随机函数,使用了之后使程序更快点,也可以不用
    c = p[0];
    r = 0;
    for (int i = 1; i < n; i++)
    {
        if (sgn(get_distance(p[i], c) - r) > 0)//如果p[i]在当前圆的外面, 那么以当前点为圆心开始找
        {
            c = p[i];//圆心为当前点
            r = 0;//这时候这个圆只包括他自己.所以半径为0
            for (int j = 0; j < i; j++)//找它之前的所有点
            {
                if (sgn(get_distance(p[j], c) - r) > 0)//如果之前的点有不满足的, 那么就是以这两点为直径的圆
                {
                    c.x = (p[i].x + p[j].x) / 2.0;
                    c.y = (p[i].y + p[j].y) / 2.0;
                    r = get_distance(p[j], c);
                    for (int k = 0; k < j; k++)
                    {
                        if (sgn(get_distance(p[k], c) - r) > 0)//找新作出来的圆之前的点是否还有不满足的, 如果不满足一定就是三个点都在圆上了
                        {
                            c = get_circle_center(p[i], p[j], p[k]);
                            r = get_distance(p[i], c);
                        }
                    }
                }
            }
        }
    }
}
point p[maxn];
int main()
{
    int n;
    point c; double r;
    while (~scanf("%d", &n) && n)
    {
        for (int i = 0; i < n; i++)
            scanf("%lf %lf", &p[i].x, &p[i].y);
        min_cover_circle(p, n, c, r);
        printf("%.2lf %.2lf %.2lf\n", c.x, c.y, r);
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 不是一样的。最小函数覆盖最小函数依赖都是关于关系型数据库设计中的概念,但它们的含义是不同的。 最小函数覆盖指的是在一个关系模式中,过消除冗余的函数依赖得到的最小集合。具体来说,如果在一个关系模式中存在多个函数依赖,其中一些函数依赖是可以由其他函数依赖推导出来的,那么这些函数依赖就可以被称为冗余的。最小函数覆盖就是指过消除这些冗余的函数依赖,得到的最小集合。 而最小函数依赖则是指在一个关系模式中,过消除冗余的属性依赖得到的最小集合。具体来说,如果在一个关系模式中存在多个属性依赖,其中一些属性依赖是可以由其他属性依赖推导出来的,那么这些属性依赖就可以被称为冗余的。最小函数依赖就是指过消除这些冗余的属性依赖,得到的最小集合。 ### 回答2: 最小函数覆盖最小函数依赖是两个不同的概念。 最小函数覆盖指的是在逻辑或者数学模型中,用最少的函数来完整地描述某个问题或者系统。这个概念常出现在形式化方法和软件工程中,用来精确地描述问题的特征和要求。最小函数覆盖的目的是简化问题,提高模型的可读性和可执行性。 而最小函数依赖则指的是在数据库设计中,过分析数据之间的依赖关系,找到最小的依赖集合,保证数据的完整性和一致性。最小函数依赖是数据规范化的一部分,过将数据分解为不重复的关系表,减少数据冗余和更新异常。最小函数依赖的目的是优化数据库的设计和操作效率。 虽然最小函数覆盖最小函数依赖都涉及到“最小”的概念,但它们所描述的对象和应用领域是不同的。最小函数覆盖主要关注问题或者系统的模型描述,而最小函数依赖主要关注数据库的设计和数据依赖关系。因此,最小函数覆盖最小函数依赖是不一样的。 ### 回答3: 最小函数覆盖最小函数依赖不是一样的。 最小函数覆盖Minimal Function Coverage)指的是在软件测试中,过选择最少的测试用例集合,覆盖到程序中所有的语句、分支和路径。其目的是尽可能地测试到程序中可能出现的各种情况,以提高软件的可靠性和健壮性。 最小函数依赖(Minimal Function Dependency)指的是在关系数据库中,过消除关系模式中的冗余依赖,得到不再依赖于其他属性的最小关系模式。其目的是提高数据库的性能和数据一致性。 虽然两者在名称上都带有"最小"的概念,但其应用领域和目标不同。最小函数覆盖关注的是软件测试,旨在最小的测试用例集合来覆盖到尽量多的程序代码;而最小函数依赖关注的是数据库设计,旨在消除冗余依赖,使得数据库更加高效和规范。 因此,最小函数覆盖最小函数依赖是两个不同的概念。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值