[计算几何笔记3]最小圆覆盖

随机增量算法

正确性在于半径的是单调递增的


void min_cover_circle(Point *p,int n,Point &c,double &r){ //c为圆心,r为半径   
    random_shuffle(p,p+n); //   
    c=p[0]; r=0;  
    for(int i=1;i<n;i++)  
    {  
        if(dis(p[i],c)>r+eps)  //第一个点  
        {   
            c=p[i]; r=0;  
            for(int j=0;j<i;j++)  
                if(dis(p[j],c)>r+eps) //第二个点  
                {  
                    c.x=(p[i].x+p[j].x)/2;  
                    c.y=(p[i].y+p[j].y)/2;  
                    r=dis(p[j],c);  
                    for(int k=0;k<j;k++)  
                        if(dis(p[k],c)>r+eps) //第三个点  
                        {//求外接圆圆心,三点必不共线   
                            c=circumcenter(p[i],p[j],p[k]);   
                            r=dis(p[i],c);   
                        }   
                }    
        }      
    }   
}   



bzoj2823 点<=10^6


感觉解圆心好困难= =



所以我是要背过么。。


蓝书P268

bzoj2823:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define maxn 1000010
using namespace std;
//-----------------------------------------------------------------//
int n;

const double eps = 1e-8;
int Filter(double x){
	return x > eps ? 1 : (x < -eps ? -1 : 0);
}

struct Point{
	double x, y;
	Point(double x = 0, double y = 0):x(x), y(y){}
	void read(){
		scanf("%lf%lf", &x, &y);
	}
}b[maxn];


Point operator+(const Point& a, const Point& b){
	return Point(a.x + b.x, a.y + b.y);
}
Point operator-(const Point& a, const Point& b){
	return Point(a.x - b.x, a.y - b.y);
}
Point operator*(const double& t, const Point& a){
	return Point(t * a.x, t * a.y);
}
Point operator/(const Point& a, const double& t){
	return Point(a.x / t, a.y / t);
}
double Cross(const Point& a, const Point& b){
	return a.x * b.y - a.y * b.x;
}
double dis(const Point& a){
	return sqrt(a.x * a.x + a.y * a.y);
}
//-----------------------------------------------------------------//

Point circumcenter(const Point& a, const Point& b, const Point& c){
	Point ret;
	double Bx = b.x - a.x, By = b.y - a.y;
	double Cx = c.x - a.x, Cy = c.y - a.y;
	double D = 2 * (Bx * Cy - By * Cx);
	double B = Bx * Bx + By * By;
	double C = Cx * Cx + Cy * Cy;
	ret.x = (Cy * B - By * C) / D + a.x;
	ret.y = (Bx * C - Cx * B) / D + a.y;
	return ret;
}

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; i ++)
	    b[i].read();
	random_shuffle(b+1, b+1+n);
	Point o = b[1];
	double r = 0, Eps = 1e-6;
	for(int i = 2; i <= n; i ++){
		if(dis(b[i] - o) > r + Eps){
			o = b[i];	r = 0;
			for(int j = 1; j < i; j ++){
				if(dis(b[j] - o) > r + Eps){
					o = (b[i] + b[j]) / 2;
					r = dis(b[i] - o);
					for(int k = 1; k < j; k ++){
						if(dis(b[k] - o) > r + Eps){
							o = circumcenter(b[i], b[j], b[k]);
							r = dis(b[i] - o);
						}
					}
				}
			}
		}
	}
	printf("%.2lf %.2lf %.2lf\n", o.x, o.y, r);
	return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值