[日常摸鱼]HDU3007Buried memory-最小圆覆盖

最小圆覆盖裸题

我求外接圆的方法比较奇怪…不过还是过掉了

#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
const int N=505;
struct Point
{
    double x,y;
    int rnd;
    Point(double x=0,double y=0):x(x),y(y){}
}p[N];
struct Line
{
    double k,b;
};
inline Line calcLine(double k,Point a)
{
    Line res;res.k=k;
    res.b=a.y-k*a.x;
    return res;
}
inline Point operator +(Point a,Point b)
{
    return Point(a.x+b.x,a.y+b.y);
}
inline Point operator -(Point a,Point b)
{
    return Point(a.x-b.x,a.y-b.y);
}
inline Point operator /(Point a,double d)
{
    return Point(a.x/d,a.y/d);
}
inline Point lineIntersection(Line l1,Line l2)
{
    Point res;
    res.x=(l2.b-l1.b)/(l1.k-l2.k);
    res.y=l1.k*res.x+l1.b;
    return res;
}
/*
inline Point getCircle(Point a,Point b,Point c)
{
    double k1=-(b.x-a.x)/(b.y-a.y);
    double k2=-(c.x-a.x)/(c.y-a.y);
    Point p1=(a+b)/2,p2=(a+c)/2;
    Line l1=calcLine(k1,p1),l2=calcLine(k2,p2);
    return lineIntersection(l1,l2);
}
*/
inline Point getCircle(Point a,Point b,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;
}
inline bool cmp(Point a,Point b)
{
    return a.rnd<b.rnd;
}
inline double sqr2(double x){return x*x;}
inline double dot(Point a,Point b)
{
    return a.x*b.x+a.y*b.y;
}
inline double dist(Point a,Point b)
{
    return sqrt(dot(a-b,a-b));
}
inline int dblcmp(double x)
{
    if(fabs(x)<1e-6)return 0;
    return (x>0?1:-1);
}
int n;
inline Point minCircle(double &r)
{
    sort(p+1,p+n+1,cmp);
    Point o=p[1];r=0;
    for(register int i=2;i<=n;i++)if(r<dist(o,p[i]))
    {
        o=p[i];r=0;
        for(register int j=1;j<i;j++)if(r<dist(o,p[j]))
        {
            o=(p[i]+p[j])/2;
            r=dist(o,p[j]);
            for(register int k=1;k<j;k++)if(r<dist(o,p[k]))
            {
                o=getCircle(p[i],p[j],p[k]);
                r=dist(o,p[i]);
            }
        }
    }
    return o;
}
int main()
{
    while(scanf("%d",&n)==1&&n)
    {
        for(register int i=1;i<=n;i++)scanf("%lf%lf",&p[i].x,&p[i].y),p[i].rnd=rand();
        double r;Point res=minCircle(r);printf("%.2lf %.2lf %.2lf\n",res.x,res.y,r);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/yoooshinow/p/8318544.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值