[POJ3525]Most Distant Point from the Sea(二分+半平面交)

题目描述

传送门
题意:给出一个多边形,求里面的一个点,其距离离多边形的边界最远,也就是多边形中最大半径圆。

题解

首先二分一个距离
然后将多边形的所有边都往里挪一个距离,求半平面交
如果有交集即为有解
将边向里平移的时候可以应用向量的旋转,然后加加减减

代码

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define N 1005

const double inf=1e9;
const double pi=acos(-1.0);
const double eps=1e-9;
int dcmp(double x)
{
    if (x<=eps&&x>=-eps) return 0;
    return (x>0)?1:-1;
}
struct Vector
{
    double x,y;
    Vector(double X=0,double Y=0)
    {
        x=X,y=Y;
    }
};
typedef Vector Point;
Vector operator + (Vector a,Vector b) {return Vector(a.x+b.x,a.y+b.y);}
Vector operator - (Vector a,Vector b) {return Vector(a.x-b.x,a.y-b.y);}
Vector operator * (Vector a,double p) {return Vector(a.x*p,a.y*p);}

int n,cnt,ncnt;
double x,y,Max,ans;
Point p[N],poly[N],npoly[N];

double Dot(Vector a,Vector b)
{
    return a.x*b.x+a.y*b.y;
}
double Cross(Vector a,Vector b)
{
    return a.x*b.y-a.y*b.x;
}
double Len(Vector a)
{
    return sqrt(Dot(a,a));
}
Vector rotate(Vector a,double rad)
{
    double sn=sin(rad),cs=cos(rad);
    return Vector(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));
}
bool insLS(Point A,Point B,Point C,Point D)
{
    Vector v=B-A,w=C-A,u=D-A;
    return dcmp(Cross(v,w))!=dcmp(Cross(v,u));
}
Point GLI(Point P,Vector v,Point Q,Vector w)
{
    Vector u=P-Q;
    double t=Cross(w,u)/Cross(v,w);
    return P+v*t;
}
void init()
{
    cnt=0;
    poly[++cnt]=Point(inf,inf);
    poly[++cnt]=Point(-inf,inf);
    poly[++cnt]=Point(-inf,-inf);
    poly[++cnt]=Point(inf,-inf);
}
void halfp(Point A,Point B)
{
    ncnt=0;
    Point C,D;
    for (int i=1;i<=cnt;++i)
    {
        C=poly[i%cnt+1];
        D=poly[(i+1)%cnt+1];
        if (dcmp(Cross(B-A,C-A))>=0)
            npoly[++ncnt]=C;
        if (insLS(A,B,C,D))
            npoly[++ncnt]=GLI(A,B-A,C,D-C);
    }
    cnt=ncnt;
    for (int i=1;i<=cnt;++i)
        poly[i]=npoly[i];
}
bool check(double mid)
{
    Point A,B,C,D;
    Vector v,w;
    init();
    for (int i=1;i<=n;++i)
    {
        A=p[i%n+1];
        B=p[(i+1)%n+1];
        v=B-A;
        w=rotate(B-A,pi/2.0);
        w=w*(mid/Len(w));
        C=A+w;
        D=C+v;
        halfp(C,D);
    }
    if (cnt) return true;
    else return false;
}
double find()
{
    double l=0,r=Max,mid,ans;
    while (dcmp(r-l)>0)
    {
        mid=(l+r)/2.0;
        if (check(mid)) ans=mid,l=mid;
        else r=mid;
    }
    return ans;
}
int main()
{
    while (~scanf("%d",&n))
    {
        if (!n) break;
        Max=0;
        for (int i=1;i<=n;++i)
        {
            scanf("%lf%lf",&x,&y);
            Max=max(Max,x);Max=max(Max,y);
            p[i]=Point(x,y);
        }
        ans=find();
        printf("%.6lf\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值