hdu 2215(凸包+最小圆覆盖)

/*
 凸包+最小圆覆盖
 枚举任意3点找其最小覆盖圆
(当为钝角三角形时不是外接圆,而是以其最长边为直径的圆)。
 当为外接圆时,半径公式为r=abc/4s;(推导为如下:
 由正弦定理,a/sinA=b/sinB=c/sinC=2R,得sinA=a/(2R),
 又三角形面积公式S=(bcsinA)/2,所以S=(abc)/(4R),故R=(abc)/(4S).
*/

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

const int N = 105;
const double eps = 1e-8;

struct point {
    int x;
    int y;
}p[N], stack[N];

bool isZero(double x) {
    return (x > 0 ? x : -x) < eps;
}

double dis(point A, point B) {
    return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
}

double crossProd(point A, point B, point C) {
    return (B.x-A.x)*(C.y-A.y) - (B.y-A.y)*(C.x-A.x);
}

int cmp(const void *a, const void *b) {
    point *c = (point *)a;
    point *d = (point *)b;
    double k = crossProd(p[0], *c, *d);
    if (k<eps || (isZero(k)&&dis(p[0], *c)>dis(p[0], *d))) return 1;
    return -1;
}

int Graham(int n) {
    int x = p[0].x;
    int y = p[0].y;
    int mi = 0;
    for (int i=1; i<n; ++i) {
        if (p[i].x<x || (p[i].x==x&&p[i].y<y)) {
            x = p[i].x;
            y = p[i].y;
            mi = i;
        }
    }
    point tmp = p[mi];
    p[mi] = p[0];
    p[0] = tmp;
    qsort(p+1, n-1, sizeof(point), cmp);
    p[n] = p[0];
    for (int i=0; i<3; ++i) stack[i] = p[i];
    int top = 2;
    for (int i=3; i<n; ++i) {
        if (crossProd(stack[top-1], stack[top], p[i])<=eps && top>=2) --top;
        stack[++top] = p[i];
    }
    return top;
}

double max(double a, double b) {
    return a > b ? a : b;
}

int main() {
    int n;
    while (scanf("%d", &n), n) {
        for (int i=0; i<n; ++i) scanf ("%d%d", &p[i].x, &p[i].y);
        if (n == 1) printf ("0.50\n");
        else if (n == 2) printf ("%.2lf\n", dis(p[0], p[1])*0.50+0.50);
        else {
            int top = Graham(n);
            double maxr = -1;
            double a, b, c, r, s;
            for (int i=0; i<top; ++i) {//枚举凸包上的点 
                for (int j=i+1; j<top; ++j) {
                    for (int k=j+1; k<=top; ++k) {
                        a = dis(stack[i], stack[j]);
                        b = dis(stack[i], stack[k]);
                        c = dis(stack[j], stack[k]);
                        if (a*a+b*b<c*c || a*a+c*c<b*b || b*b+c*c<a*a) r = max(max(a, b), c) / 2.0;//钝角 
                        else {
                            s = fabs(crossProd(stack[i], stack[j], stack[k])) / 2.0;
                            r = a * b * c / (s * 4.0);//三角形外接圆公式 
                        }                
                        if (maxr < r) maxr = r;
                    }
                }
            }
            printf ("%.2lf\n", maxr+0.5);
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/try86/archive/2012/04/23/2467067.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值