基础计算几何——————最小圆覆盖

本文详细介绍了最小圆覆盖问题的解决方法,通过给出n个点,寻找包含所有点的最小圆。文章首先提供了完整的C++代码实现,随后将深入探讨算法原理及背后的数学逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最小圆覆盖定义:给出n个点,找出一个最小的圆,圆内包含所有的点。

先给出代码,后续证明+自己的想法。

板子题是HDU 3007

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=5e2+10;
struct node
{
	double x,y;
}p[maxn];
double dis(node a,node b)//距离 
{
	return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
 } 
node getcircle(node a,node b,node c)//获取外心//中垂线方程以及直线方程交点求出 
{   node o;
	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;  
    o.x = a.x + (c1 * b2 - c2 * b1) / d;  
    o.y = a.y + (a1 * c2 - a2 * c1) / d;
    return o;
}
int main()
{
	int n;
	while(~scanf("%d",&n)&&n)
	{
		for(int i=1;i<=n;i++)
		{
			scanf("%lf %lf",&p[i].x,&p[i].y);
		}
		double r=0;
		node o=p[1];
		for(int i=1;i<=n;i++)
		{
			if(dis(p[i],o)>r)
			{
				r=0;
				o=p[i];
				for(int j=1;j<i;j++)//第二个点 
				{
					if(dis(p[j],o)>r) //如果这个点不在Ci-1上,那么pj肯定在新圆的边上 
					{
						o.x=(p[j].x+p[i].x)/2;
						o.y=(p[j].y+p[i].y)/2;
						r=dis(p[j],o);
						for(int k=1;k<j;k++)//第三个点 
						{
							if(dis(p[k],o)>r)//如果这个点不在Ci-1上,那么这个点肯定在Ci的边上 
							{
								o=getcircle(p[i],p[j],p[k]);
								r=dis(p[k],o);
							}
						 } 
					}
				}
			}
		}
		printf("%.2lf %.2lf %.2lf\n",o.x,o.y,r);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值