POJ3525(二分+半平面交)

31 篇文章 0 订阅

题目:题目链接

 

题目:就是给你一个多边形,让你判断这个多边形的最大内切圆

 

分析:二分半径r,把每条边向内收缩r,半平面交判可行

 

半平面交还是太弱:

 

代码:

 

#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <map>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
#include <functional>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cassert>
#include <bitset>
#include <stack>
#include <ctime>
#include <list>
#define INF 0x7fffffff
#define max3(a,b,c) (max(a,b)>c?max(a,b):c)
#define min3(a,b,c) (min(a,b)<c?min(a,b):c)
#define mem(a,b) memset(a,b,sizeof(a))
#define eps 1e-8
#define oo 1e5
const int maxn = 105;
using namespace std;

struct point
{
    double x, y;
    point() {};
    point(double xx, double yy)
    {
        x = xx;
        y = yy;
    }
} p[maxn];

int n;
int sig(double x)
{
    if(x < -eps)
        return -1;
    if(x > eps)
        return 1;
    return 0;
}

void get(const point &p1, const point &p2, double &a, double &b, double &c)
{
    a = p2.y - p1.y;
    b = p1.x - p2.x;
    c = p2.x*p1.y - p2.y*p1.x;
}

point jiao(point u, point v, double a, double b, double c)
{
    double d, e, f;
    get(u, v, d, e, f);
    point ret;
    ret.x = (b*f - c*e)/(a*e - b*d);
    ret.y = (a*f - c*d)/(b*d - a*e);
    return ret;
}

void cut(int &n, point *p, double a, double b, double c)
{
    int m = 0, i, t;
    point pp[maxn];
    for(i = 0; i < n; ++i)
    {
        if(sig(a*p[i].x + b*p[i].y + c)  <= 0 )
            pp[m++] = p[i];
        else
        {
            t = (i-1+n)%n;
            if(sig(a*p[t].x + b*p[t].y + c) < 0)
                pp[m++] = jiao(p[t], p[i], a, b, c);
            t = (i+1)%n;
            if(sig(a*p[t].x + b*p[t].y + c) < 0)
                pp[m++] = jiao(p[t], p[i], a, b, c);
        }
    }
    for(i = 0; i < m; ++i)
        p[i] = pp[i];
    n = m;
}

bool ok(double mid)
{
    double a, b, c;
    int nn = 4, i;
    point tp[maxn];
    tp[0] = point(-oo, oo);
    tp[1] = point(-oo, -oo);
    tp[2] = point(oo, -oo);
    tp[3] = point(oo, oo);
    for(i = 0; i < n; ++i)
    {
        get(p[i], p[(i+1)%n], a, b, c);
        c += mid*sqrt(a*a + b*b);
        cut(nn, tp, a, b, c);
        if(nn == 0)
            break;
    }
    return nn > 0;
}

double work()
{
    double ll = 0, rr = 1e4, mid;
    while(rr - ll >= 1e-6)
    {
        mid = (ll+rr)/2;
        if(ok(mid))
            ll = mid;
        else
            rr = mid;
    }
    return ll;
}
int main()
{
    while(scanf("%d", &n) && n)
    {
        for(int i = 0; i < n; ++i)
            scanf("%lf%lf", &p[i].x, &p[i].y);
        printf("%.6lf\n", work());
    }
    return 0;
}


 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值