poj3525Most Distant Point from the Sea【半平面交求到凸多边形边界最远距离】

Language:
Most Distant Point from the Sea
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 4285 Accepted: 2010 Special Judge

Description

The main land of Japan called Honshu is an island surrounded by the sea. In such an island, it is natural to ask a question: “Where is the most distant point from the sea?” The answer to this question for Honshu was found in 1996. The most distant point is located in former Usuda Town, Nagano Prefecture, whose distance from the sea is 114.86 km.

In this problem, you are asked to write a program which, given a map of an island, finds the most distant point from the sea in the island, and reports its distance from the sea. In order to simplify the problem, we only consider maps representable by convex polygons.

Input

The input consists of multiple datasets. Each dataset represents a map of an island, which is a convex polygon. The format of a dataset is as follows.

n  
x1 y1
  
xn yn

Every input item in a dataset is a non-negative integer. Two input items in a line are separated by a space.

n in the first line is the number of vertices of the polygon, satisfying 3 ≤ n ≤ 100. Subsequent n lines are the x- and y-coordinates of the n vertices. Line segments (xiyi)–(xi+1yi+1) (1 ≤ i ≤ n − 1) and the line segment (xn,yn)–(x1y1) form the border of the polygon in counterclockwise order. That is, these line segments see the inside of the polygon in the left of their directions. All coordinate values are between 0 and 10000, inclusive.

You can assume that the polygon is simple, that is, its border never crosses or touches itself. As stated above, the given polygon is always a convex one.

The last dataset is followed by a line containing a single zero.

Output

For each dataset in the input, one line containing the distance of the most distant point from the sea should be output. An output line should not contain extra characters such as spaces. The answer should not have an error greater than 0.00001 (10−5). You may output any number of digits after the decimal point, provided that the above accuracy condition is satisfied.

Sample Input

4
0 0
10000 0
10000 10000
0 10000
3
0 0
10000 0
7000 1000
6
0 40
100 20
250 40
250 70
100 90
0 70
3
0 0
10000 10000
5000 5001
0

Sample Output

5000.000000
494.233641
34.542948
0.353553

题意:给出日本岛为凸多边形顶点坐标求岛上到岛边界的最远距离;

半平面交+二分

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define eps 1e-10
using namespace std;
struct point{
	double x,y;
}A[110],p[110],q[110];
int N,tempcnt,endcnt;
void guizheng(){
	for(int i=1;i<=N;++i){
		q[i]=A[N-i+1];
	}
	for(int i=1;i<=N;++i){
		A[i]=q[i];
	}
}
void getline(point p1,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;
}
void init(){
	for(int i=1;i<=N;++i){
		p[i]=A[i];
	}	
	p[N+1]=p[1];p[0]=p[N];
	endcnt=N;
}
point getinter(point p1,point p2,double a,double b,double c){
	double u=fabs(a*p1.x+b*p1.y+c);
	double v=fabs(a*p2.x+b*p2.y+c);
	point temp;
	temp.x=(v*p1.x+u*p2.x)/(u+v);
	temp.y=(v*p1.y+u*p2.y)/(u+v);
	return temp;
}
void cut(double a,double b,double c){
	tempcnt=0;
	for(int i=1;i<=endcnt;++i){
		if(a*p[i].x+b*p[i].y+c>=0)q[++tempcnt]=p[i];
		else{
			if(a*p[i-1].x+b*p[i-1].y+c>0){
				q[++tempcnt]=getinter(p[i],p[i-1],a,b,c);
			}
			if(a*p[i+1].x+b*p[i+1].y+c>0)
				q[++tempcnt]=getinter(p[i],p[i+1],a,b,c);
		}
	}
	for(int i=1;i<=tempcnt;++i){
		p[i]=q[i];
	}
	p[tempcnt+1]=q[1];p[0]=p[tempcnt];
	endcnt=tempcnt;
}
bool solve(double r){
	init();
	for(int i=1;i<=N;++i){
		point ta,tb,tc;
		tc.x=A[i+1].y-A[i].y;
		tc.y=A[i].x-A[i+1].x;
		double k=r/sqrt(tc.x*tc.x+tc.y*tc.y);
		tc.x*=k;tc.y*=k;
		ta.x=A[i].x+tc.x;
		ta.y=A[i].y+tc.y;
		tb.x=A[i+1].x+tc.x;
		tb.y=A[i+1].y+tc.y;
		double a,b,c;
		getline(ta,tb,a,b,c);
		cut(a,b,c);
	}
	if(endcnt<=0)
		return false;
	return true;
}
int main()
{
	int i,j,k;
	while(scanf("%d",&N),N){
		for(i=1;i<=N;++i){
			scanf("%lf%lf",&A[i].x,&A[i].y);
		}
		double left=0,right=200000000;
		guizheng();
		A[N+1]=A[1];
		while(left+eps<=right){
			double mid=(left+right)/2.0;
			if(solve(mid))
				left=mid;
			else 
				right=mid;
		}
		printf("%lf\n",right);
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值