【POJ 3525】Most Distant Point from the Sea(直线平移、半平面交)

按逆时针顺序给出n个点,求它们组成的多边形的最大内切圆半径。

二分这个半径,将所有直线向多边形中心平移r距离,如果半平面交不存在那么r大了,否则r小了。

平移直线就是对于向量ab,因为是逆时针的,向中心平移就是向向量左手边平移,求出长度为r方向指向向量左手边的向量p,a+p指向b+p就是平移后的向量。

半平面交就是对于每个半平面ax+by+c>0,将当前数组里的点(一开始是所有点)带入,如果满足条件,那么保留该点,否则,先看i-1号点是否满足条件,如果满足,那么将i-1和i点所在直线和直线ax+by+c=0的交点加入数组,再看i+1号点如果满足条件,那么将i和i+1号点所在直线和直线ax+by+c=0的交点加入数组。最后看数组里有多少个点,如果0个点那么就是不存在半平面交。

要注意一下向量方向,半平面的直线的方向。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#define dd double
#define eps 1e-5
#define N 505
using namespace std;
int n;
struct Point{
	dd x,y;
}p[N],tp[N],q[N];
dd osXoe(const Point &po,const Point &ps,const Point &pe){
	return (ps.x-po.y)*(pe.y-po.y)-(pe.x-po.x)*(ps.y-po.y);
}
void eq(const Point &p1,const Point &p2,dd &a,dd &b,dd &c){
	a=p2.y-p1.y;
	b=p1.x-p2.x;
	c=p2.x*p1.y-p1.x*p2.y;
}
Point cross(Point p1,Point p2,dd a,dd b,dd c){
	dd u=fabs(a*p1.x+b*p1.y+c);
	dd v=fabs(a*p2.x+b*p2.y+c);
	Point t;
	t.x=(p1.x*v+p2.x*u)/(u+v);
	t.y=(p1.y*v+p2.y*u)/(u+v);
	return t;
}
int Cut(dd a,dd b,dd c,int cnt){
	int tmp=0;
	for (int i=1;i<=cnt;i++){
		if(a*p[i].x+b*p[i].y+c>-eps)tp[++tmp]=p[i];
		else{
		    if(a*p[i-1].x+b*p[i-1].y+c>eps)
		        tp[++tmp]=cross(p[i-1],p[i],a,b,c);
		    if(a*p[i+1].x+b*p[i+1].y+c>eps)
			tp[++tmp]=cross(p[i],p[i+1],a,b,c);
		}
	}
	for (int i=1;i<=tmp;i++)p[i]=tp[i];
	p[0]=p[tmp];p[tmp+1]=p[1];
	return tmp;
}
int solve(dd r){
	q[0]=q[n];q[n+1]=q[1];
	for (int i=0;i<=n+1;i++) p[i]=q[i];
	int cnt=n;
	for (int i=1;i<=n;i++){
		dd a,b,c;
		Point p1,p2,p3;
		p1.y=q[i+1].x-q[i].x;p1.x=q[i].y-q[i+1].y;
		dd k=r/sqrt(p1.x*p1.x+p1.y*p1.y);
		p1.x=k*p1.x;p1.y=k*p1.y;
//p1是垂直q[i+1]->q[i]指向右手边的长度为r的向量。如果是q[i]->q[i+1]则求指向左手边的。
		p2.x=p1.x+q[i].x;p2.y=p1.y+q[i].y;
		p3.x=p1.x+q[i+1].x;p3.y=p1.y+q[i+1].y;
		eq(p3,p2,a,b,c);//过p3->p2的直线方程ax+by+c=0
		cnt=Cut(a,b,c,cnt);//求半平面交剩下的点
	}
	return cnt;
}
int main(){
	while(cin>>n,n){
		for (int i=1;i<=n;i++)
			scanf("%lf%lf",&q[i].x,&q[i].y);
		dd l=0,r=1<<30,m;
		while(fabs(r-l)>eps){
			m=(l+r)/2.0;
			if(solve(m))l=m;
			else r=m;
		}
		printf("%.6f\n",m);
	}
}

  

时间: 2024-07-31 22:52:10

【POJ 3525】Most Distant Point from the Sea(直线平移、半平面交)的相关文章

poj 3525 Most Distant Point from the Sea 半平面交 + 二分

题目来源: http://poj.org/problem?id=3525 分析: 题意:给定一个凸多边形,求多边形中距离边界最远的点到边界的距离. 思路 : 每次将凸多边形每条边往里平移d,判断是否存在核:二分d即可. 多边形边上的点(x , y)往里平移d 后的 坐标: s , e  为向量的 起点和终点, len 为起点和终点的距离, h 为平移的距离 x' = x + dx y' = y + dy dx = ( s.y - e.y ) / len * h ,( 原理 是 利用 三角形的相似

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

Most Distant Point from the Sea Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3955   Accepted: 1847   Special Judge Description The main land of Japan called Honshu is an island surrounded by the sea. In such an island, it is natural t

Poj 3525 Most Distant Point from the Sea

地址:http://poj.org/problem?id=3525 题目: Most Distant Point from the Sea Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5167   Accepted: 2331   Special Judge Description The main land of Japan called Honshu is an island surrounded by the s

POJ 3525 Most Distant Point from the Sea (半平面交向内推进+二分半径)

题目链接 题意 : 给你一个多边形,问你里边能够盛的下的最大的圆的半径是多少. 思路 :先二分半径r,半平面交向内推进r.模板题 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <math.h> 5 const double eps = 1e-10 ; 6 7 using namespace std ; 8 9 struct node 10 { 11 do

POJ 3525 Most Distant Point from the Sea 二分+半平面交

题目大意:给出一个岛的海岸线的轮廓,求这个岛上的所有点到海岸的最长距离是多少. 思路:求多边形内切圆的问题要用二分+半平面交解决.二分半径的长度,然后将所有的边向左侧移动这个二分的长度,然后利用半平面交来判断是否能够满足条件.如果满足条件就提高下界,否则减小上界. 我的移动的方法是这样的,首先每条边都要用点向量式来表示,就是边上任意一点和这条边的方向向量.这样做以后的操作会方便很多.然后将每个直线的向左的法向量求出来(比如l的向量是v(x,y),那么它向左侧的法向量是(-y,x)),然后将法向量

poj 3525Most Distant Point from the Sea【二分+半平面交】

相当于多边形内最大圆,二分半径r,然后把每条边内收r,求是否有半平面交(即是否合法) #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> using namespace std; const int N=205; const double eps=1e-6; int n; struct dian { double x,y; dian(double X=0,double

UVA 3890 Most Distant Point from the Sea(二分法+半平面交)

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=11358 [思路] 二分法+半平面交 二分与海边的的距离,由法向量可以得到平移后的各边,半平面交在特定精度判断是否有交集. [代码] 1 #include<cmath> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace s

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

pro:给定凸多边形,求凸多边形内的点到最近边界的最远距离. sol:显然是二分一个圆,使得圆和凸多边形不相交,但是这样很难实现. 由于是凸多边形,我们可以把二分圆转化为二分凸多边形的移动. 如果每一边向左移动Mid后,任然存在“核”,则表示存在一点合法. 直线移动:移动起点即可,方向不变. #include<bits/stdc++.h> #define ll long long #define rep(i,a,b) for(int i=a;i<=b;i++) using namespa

POJ 3130 How I Mathematician Wonder What You Are!(半平面交求多边形的核)

题目链接 题意 : 给你一个多边形,问你该多边形中是否存在一个点使得该点与该多边形任意一点的连线都在多边形之内. 思路 : 与3335一样,不过要注意方向变化一下. 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <math.h> 5 6 using namespace std ; 7 8 struct node 9 { 10 double x; 11 d