POJ 3525(计算几何+凸多边形最大内切圆)
问题描述:
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 (xi, yi)–(xi+1, yi+1) (1 ≤ i ≤ n − 1) and the line segment (xn, yn)–(x1, y1) 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<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const double eps=1e-8;
const double inf=1e9;
const int MAXN=210;
int m;//保存多边形的点数
double r;//保存内移距离
int cCnt,curCnt;//此时cCnt为最终切割得到的多边形的顶点数、暂存顶点个数
struct point
{
double x,y;
};
point points[MAXN],p[MAXN],q[MAXN];//读入的多边形的顶点(顺时针)、p为存放最终切割得到的多边形顶点的数组、暂存核的顶点
void getline(point x,point y,double &a,double &b,double &c) //两点x、y确定一条直线a、b、c为其系数
{
a = y.y - x.y;
b = x.x - y.x;
c = y.x * x.y - x.x * y.y;
}
void initial()
{
for(int i = 1; i <= m; ++i)p[i] = points[i];
p[m+1] = p[1];
p[0] = p[m];
cCnt = m;
}
point intersect(point x,point y,double a,double b,double c)
{
double u = fabs(a * x.x + b * x.y + c);
double v = fabs(a * y.x + b * y.y + c);
point pt;
pt.x=(x.x * v + y.x * u) / (u + v);
pt.y=(x.y * v + y.y * u) / (u + v);
return pt;
}
void cut(double a,double b ,double c)
{
curCnt = 0;
for(int i = 1; i <= cCnt; ++i)
{
if(a*p[i].x + b*p[i].y + c >= 0)q[++curCnt] = p[i];
else
{
if(a*p[i-1].x + b*p[i-1].y + c > 0)
{
q[++curCnt] = intersect(p[i],p[i-1],a,b,c);
}
if(a*p[i+1].x + b*p[i+1].y + c > 0)
{
q[++curCnt] = intersect(p[i],p[i+1],a,b,c);
}
}
}
for(int i = 1; i <= curCnt; ++i)p[i] = q[i];
p[curCnt+1] = q[1];
p[0] = p[curCnt];
cCnt = curCnt;
}
int dcmp(double x)
{
if(fabs(x)<eps) return 0;
else return x<0?-1:1;
}
void solve()
{
initial();
for(int i = 1; i <= m; ++i){
point ta, tb, tt;
tt.x = points[i+1].y - points[i].y;
tt.y = points[i].x - points[i+1].x;
double k = r / sqrt(tt.x * tt.x + tt.y * tt.y);
tt.x = tt.x * k;
tt.y = tt.y * k;
ta.x = points[i].x + tt.x;
ta.y = points[i].y + tt.y;
tb.x = points[i+1].x + tt.x;
tb.y = points[i+1].y + tt.y;
double a,b,c;
getline(ta,tb,a,b,c);
cut(a,b,c);
}
}
void GuiZhengHua(){ //规整化方向,逆时针变顺时针,顺时针变逆时针
for(int i = 1; i < (m+1)/2; i ++)
swap(points[i], points[m-i]);
}
int main()
{
while (scanf("%d",&m)!=EOF){
if (m==0) break;
for (int i=1;i<=m;i++)
scanf("%lf%lf",&points[i].x,&points[i].y);
GuiZhengHua();
points[m+1]=points[1];
double left=0,right=inf,mid;
while ((right-left)>=eps) {//二分求半径
mid=(left+right)/2.0;
// cout<<1<<endl;
r=mid;
solve();
if (cCnt<=0) right=mid;
else left=mid;
}
printf("%.6f\n",left);
}
return 0;
}
还没有评论,来说两句吧...