poj 3525 Most Distant Point from the Sea

题意:转换过来就是求凸多边形内的最大圆半径

思路:二分半径枚举答案,用半平面交来cheak

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#define inf 100000000
using namespace std;
const double pi=acos(-1.0);
const double eps=1e-8;
int n,ln,dq[100000],top,bot;
struct P{
	double x,y;
	P (){}
	P(double x,double y):x(x),y(y){
	}
	P operator +(P p){
		return P(x+p.x,y+p.y);
	}
	P operator -(P p){
		return P(x-p.x,y-p.y);
	}
	P operator *(double d){
		return P(x*d,y*d);
	}
	double det(P p){
		return x*p.y-y*p.x;
	}
};
P p[10000000];
struct Line
{
	P a,b;
	double angle;
};
Line l[100000],l1[100000];
P in(Line a,Line b){
	return a.a+(a.b-a.a)*((b.b-b.a).det(b.a-a.a) / (b.b-b.a).det(a.b-a.a));
}
int dblcmp(double k)
{
	if(fabs(k)<eps)
		return 0;
	return k>0?1:-1;
}
bool cmp(Line a,Line b){
	int d=dblcmp(a.angle-b.angle);
	if(!d)
	return dblcmp((b.a-a.a).det(b.b-a.a))<0;
	return d<0;
}
bool judge(Line a,Line b,Line c){
	P r=in(b,c);
	return dblcmp((a.a-r).det(a.b-r))<0;
}
double dis(P a,P b){
	double c=(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
	return sqrt(c);
}
//向量向内侧平移r距离函数
void getmove(double r)
{
	for(int i=0;i<ln;i++)
	{
		double x=l[i].a.x-l[i].b.x;
		double y=l[i].a.y-l[i].b.y;
		double L=dis(l[i].a,l[i].b);
		l1[i].a.x=(r*y/L+l[i].a.x);
		l1[i].a.y=(l[i].a.y-r*x/L);
		l1[i].b.x=(r*y/L+l[i].b.x);
		l1[i].b.y=(l[i].b.y-r*x/L);
		l1[i].angle=l[i].angle;
	}
}
bool check(double r)
{
	int len;
	int i,j;
	getmove(r);
	for(i=0,j=0;i<ln;i++)
	{
		if(dblcmp(l1[i].angle-l1[j].angle)>0)
			l1[++j]=l1[i];
	}
	len=j+1;
	top=1;
	bot=0;
	dq[0]=0;
	dq[1]=1;
	for(i=2;i<len;i++)
	{
		while(top>bot && judge(l1[i],l1[dq[top]],l1[dq[top-1]])) top--;
		while(top>bot && judge(l1[i],l1[dq[bot]],l1[dq[bot+1]])) bot++;
		dq[++top]=i;
	}
	while(top>bot && judge(l1[dq[bot]],l1[dq[top]],l1[dq[top-1]])) top--;
	while(top>bot && judge(l1[dq[top]],l1[dq[bot]],l1[dq[bot+1]])) bot++;
	if(top-bot>1)
		return true;
	return false;
}
int main()
{
	while(~scanf("%d",&n) && n)
	{
		ln=0;
		for(int i=0;i<n;i++)
			scanf("%lf%lf",&p[i].x,&p[i].y);
		for(int i=0;i<n-1;i++)
		{
			l[ln].a=p[i];
			l[ln].b=p[i+1];
			l[ln].angle=atan2(l[ln].b.y-l[ln].a.y,l[ln].b.x-l[ln].a.x);
			ln++;
		}
		l[ln].a=p[n-1];
		l[ln].b=p[0];
		l[ln].angle=atan2(l[ln].b.y-l[ln].a.y,l[ln].b.x-l[ln].a.x);
		ln++;
		sort(l,l+ln,cmp);
		double l=0,lr=20000.0,m;
		while(fabs(l-lr)>eps)
		{
			m=(l+lr)/2;
			if(check(m))
				l=m;
			else
				lr=m;
		}
		m=(l+lr)/2;
		printf("%.6lf\n",m);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值