随机增量法学习笔记

· · 个人记录

是计算几何中的一个重要算法,常用于求最小圆覆盖。

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

inline int read()
{
    int x=0;int f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

const double eps = 1e-8;
struct Point{double x,y;}p[100005],o;
double r; int n;

inline double sqr(double x)
    {return x*x;}

inline double dis(Point a,Point b)
    {return sqrt(sqr(a.x-b.x)+sqr(a.y-b.y));}

inline bool cmp(double a,double b)
    {return fabs(a-b)<eps;}

inline Point geto(Point a,Point b,Point c)
{
    double a1,a2,b1,b2,c1,c2;
    Point ans;
    a1=2*(b.x-a.x),b1=2*(b.y-a.y),c1=sqr(b.x)-sqr(a.x)+sqr(b.y)-sqr(a.y);
    a2=2*(c.x-a.x),b2=2*(c.y-a.y),c2=sqr(c.x)-sqr(a.x)+sqr(c.y)-sqr(a.y);
    if(cmp(a1,0)) 
    { 
        ans.y=c1/b1; 
        ans.x=(c2-ans.y*b2)/a2; 
    } 
    else if(cmp(b1,0)) 
    { 
        ans.x=c1/a1; 
        ans.y=(c2-ans.x*a2)/b2; 
    } 
    else
    { 
        ans.x=(c2*b1-c1*b2)/(a2*b1-a1*b2); 
        ans.y=(c2*a1-c1*a2)/(b2*a1-b1*a2); 
    } 
    return ans; 
}

int main()
{
    n=read();
    for(int i=1;i<=n;i++)
        scanf("%lf%lf",&p[i].x,&p[i].y);
    for(int i=1;i<=n;i++)
        swap(p[rand()%n+1],p[rand()%n+1]);
    o=p[1];
    for(int i=1;i<=n;i++)
    {
        if(dis(o,p[i])<r||cmp(dis(o,p[i]),r)) continue;
        o.x=(p[i].x+p[1].x)/2;
        o.y=(p[i].y+p[1].y)/2;
        r=dis(p[i],p[1])/2;
        for(int j=2;j<i;j++)
        {
            if(dis(o,p[j])<r||cmp(dis(o,p[j]),r)) continue;
            o.x=(p[i].x+p[j].x)/2;
            o.y=(p[i].y+p[j].y)/2;
            r=dis(p[i],p[j])/2;
            for(int k=1;k<j;k++)
            {
                if(dis(o,p[k])<r||cmp(dis(o,p[k]),r)) continue;
                o=geto(p[i],p[j],p[k]);
                r=dis(o,p[i]);
            }
        }
    }
    printf("%.10lf\n%.10lf %.10lf",r,o.x,o.y);
    return 0;
}