码迷,mamicode.com
首页 > 其他好文 > 详细

【模板】最小圆覆盖——bzoj1336

时间:2020-02-24 20:47:53      阅读:60      评论:0      收藏:0      [点我收藏+]

标签:include   固定   旋转   ==   poi   shuffle   net   oss   ace   

博客链接

https://blog.csdn.net/commonc/article/details/52291822

#include<bits/stdc++.h>
using namespace std;
#define N 100005
typedef double db;
const db eps=1e-10;
const db pi=acos(-1);
int sign(db k){
    if (k>eps) return 1; else if (k<-eps) return -1; return 0;
}
int cmp(db k1,db k2){return sign(k1-k2);}
int inmid(db k1,db k2,db k3){return sign(k1-k3)*sign(k2-k3)<=0;}// k3 在 [k1,k2] 内 
struct point{
    db x,y;
    point operator + (const point &k1) const{return (point){k1.x+x,k1.y+y};}
    point operator - (const point &k1) const{return (point){x-k1.x,y-k1.y};}
    point operator * (db k1) const{return (point){x*k1,y*k1};}
    point operator / (db k1) const{return (point){x/k1,y/k1};}
    int operator == (const point &k1) const{return cmp(x,k1.x)==0&&cmp(y,k1.y)==0;}
    // 逆时针旋转 
    point turn(db k1){return (point){x*cos(k1)-y*sin(k1),x*sin(k1)+y*cos(k1)};}
    point turn90(){return (point){-y,x};}
    bool operator < (const point k1) const{
        int a=cmp(x,k1.x);
        if (a==-1) return 1; else if (a==1) return 0; else return cmp(y,k1.y)==-1;
    }
    db abs(){return sqrt(x*x+y*y);}
    db abs2(){return x*x+y*y;}
    db dis(point k1){return ((*this)-k1).abs();}
    point unit(){db w=abs(); return (point){x/w,y/w};}
    void scan(){double k1,k2; scanf("%lf%lf",&k1,&k2); x=k1; y=k2;}
    void print(){printf("%.11lf %.11lf\n",x,y);}
    db getw(){return atan2(y,x);} 
    point getdel(){if (sign(x)==-1||(sign(x)==0&&sign(y)==-1)) return (*this)*(-1); else return (*this);}
    int getP() const{return sign(y)==1||(sign(y)==0&&sign(x)>=0);}
};
int inmid(point k1,point k2,point k3){return inmid(k1.x,k2.x,k3.x)&&inmid(k1.y,k2.y,k3.y);}
db cross(point k1,point k2){return k1.x*k2.y-k1.y*k2.x;}
db dot(point k1,point k2){return k1.x*k2.x+k1.y*k2.y;}
 
struct circle{
    point o; db r;
    void scan(){o.scan(); scanf("%lf",&r);}
    int inside(point k){//-1:圆外,0边上,1:圆内 
        return cmp(r,o.dis(k));
    }
};

int n;
point p[N];
circle now;
circle getcircle(point k1,point k2,point k3){//通过三点求元
    db a1=k2.x-k1.x,b1=k2.y-k1.y,c1=(a1*a1+b1*b1)/2;
    db a2=k3.x-k1.x,b2=k3.y-k1.y,c2=(a2*a2+b2*b2)/2;
    db d=a1*b2-a2*b1;
    point o=(point){k1.x+(c1*b2-c2*b1)/d,k1.y+(a1*c2-a2*c1)/d};
    return (circle){o,k1.dis(o)};
}


int main(){
    cin>>n;
    for(int i=1;i<=n;i++)
        scanf("%lf%lf",&p[i].x,&p[i].y);
    random_shuffle(p+1,p+1+n);
    
    now.r=0;
    now.o=p[1];
    for(int i=2;i<=n;i++)if(now.inside(p[i])<0){//把点p[i]固定在新圆边上 
        now.o=p[i];now.r=0;
        for(int j=1;j<i;j++)if(now.inside(p[j])<0){//把点p[j]固定在新圆边上 
            now.o=(p[i]+p[j])*0.5;
            now.r=now.o.dis(p[i]);
            for(int k=1;k<j;k++)if(now.inside(p[k])<0){//三点求圆 
                now=getcircle(p[i],p[j],p[k]);
            }
        }
    }
    printf("%.10lf\n%.10lf %.10lf\n",now.r,now.o.x,now.o.y);
}

 

【模板】最小圆覆盖——bzoj1336

标签:include   固定   旋转   ==   poi   shuffle   net   oss   ace   

原文地址:https://www.cnblogs.com/zsben991126/p/12358375.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!