首页 > 题解 > poj 3525 Most Distant Point from the Sea

poj 3525 Most Distant Point from the Sea

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 (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 <cstdio>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#define N 1000
#define INF 0x3f3f3f3f
using namespace std;int n;double Max;

const double eps=1e-7;
int dcmp(double x)
{
	if (fabs(x)<eps) return 0;
	else
		if (x>0)
			return 1;
		else
			return -1;
}

const double pi=acos(-1.0);

struct Vector
{
	double x,y;
	Vector (double a=0,double b=0)
	{
		x=a,y=b;
	}
	bool operator < (const Vector &a) const
	{
		return x<a.x||(x==a.x&&y<a.y);
	}
};
typedef Vector Point;

struct Line
{
	Point p;
	Vector v;
	Line(Point P=Point(0,0),Vector V=Vector(0,0))
	{
		p=P,v=V;
	}
};

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 b) {return Vector(a.x*b  ,a.y*b  );}
Vector operator / (Vector a,double b) {return Vector(a.x/b  ,a.y/b  );}
bool   operator ==(Vector a,Vector b) {return !dcmp(a.x-b.x)&&!dcmp(a.y-b.y);}

Vector Rotate(Vector a,double rad)
{
	return Vector(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));
}

double Dot(Vector a,Vector b)
{
    return a.x*b.x+a.y*b.y;
}

double Len(Vector a){return sqrt(Dot(a,a));}

double Angle(Vector a,Vector b){return acos(Dot(a,b)/Len(a)/Len(b));}

double Cross(Vector a,Vector b)
{
    return a.x*b.y-a.y*b.x;
}

Vector Normal(Vector a)
{
    return Vector(-a.y/Len(a),a.x/Len(a));
}

Point Line_cross(Line a,Line b)
{
	Vector u=a.p-b.p;
	double t=Cross(b.v,u)/Cross(a.v,b.v);
	return a.p+a.v*t;
}

double Dis_To_Line(Point p,Point a,Point b)
{
	return fabs(Cross(a-b,p-b)/Len(a-b));
}

double Dis_To_Seg(Point p,Point a,Point b)
{
	if (a==b) return Len(p-a);
	Vector v=b-a,w=p-a,u=p-b;
	if (dcmp(Dot(v,w)<0))	return Len(w);
	else if (dcmp(Dot(v,u))>0)	return Len(v);
	else return fabs(Cross(v,w)/Len(v));
}

bool Line_Seg_Cross(Point l1,Point l2,Point a,Point b)
{
	Vector v=l1-l2,w=a-l1,u=b-l2;
	return dcmp(Cross(v,w))!=dcmp(Cross(v,u));
}

bool Seg_Cross(Point a1,Point a2,Point b1,Point b2)
{
	double c1=Cross(a2-a1,b1-b1),c2=Cross(a2-a1,b2-a1),
	       c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
	return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
}

Point Line_Pro(Point p,Point a,Point b)
{
	Vector v=b-a;
	return a+v*(Dot(v,p-a)/Dot(v,v));
}

double Area_T(Point a, Point b,Point c)
{
	return Cross(b-a,c-a)*0.5;
}

Point TC(Point a,Point b,Point c)
{
	Point p=(a+b)/2,q=(a+c)/2;
	Vector v=Rotate(b-a,pi/2),w=Rotate(c-a,pi/2);
	if (!dcmp(Cross(v,w)))
	{
        if (dcmp(Len(a-b)+Len(b-c)-Len(a-c))==0)
            return (a+c)/2;
        if (dcmp(Len(a-c)+Len(b-c)-Len(a-b))==0)
            return (a+b)/2;
        if (dcmp(Len(a-b)+Len(a-c)-Len(b-c))==0)
            return (b+c)/2;
    }
    return Line_cross(Line(p,v),Line(q,w));
}

Point TG(Point a,Point b,Point c)
{
    return (a+b+c)/3;
}

double Area_P(Point ploy[],int num)
{
	double area=0;
	for (int i=2;i<num;i++)
		area+=Cross(ploy[i]-ploy[1],ploy[i+1]-ploy[1]);
	return area/2;
}

bool Point_In_Ploy(Point p,Point poly[],int num)
{
	int ans=0,k,d1,d2;
	for (int i=1;i<=num;i++)
	{
		if (!dcmp(Dis_To_Seg(p,poly[i],poly[i%num+1]))) return 0;
		k=dcmp(Cross(poly[i%num+1]-poly[i],p-poly[i]));
		d1=dcmp(poly[i].y-p.y);
		d2=dcmp(poly[i%num+1].y-p.y);
		if (k>0&&d1<=0&&d2>0) ++ans;
		if (k<0&&d1>=0&&d2<0) --ans;
	}
	if (ans) return 1;
	return 0;
}

Point stack[N];int top;
void Graham(Point p[],int num)
{
	memset(stack,0,sizeof stack);top=0;
	sort(p+1,p+num+1);
	for (int i=1;i<=num;i++)
	{
		while (top>1 && dcmp(Cross(stack[top]-stack[top-1],p[i]-stack[top-1]))<=0)
			top--;
		stack[++top]=p[i];
	}
	int k=top;
	for (int i=num-1;i>=1;i--)
	{
		while (top>k && dcmp(Cross(stack[top]-stack[top-1],p[i]-stack[top-1]))<=0)
			top--;
		stack[++top]=p[i];
	}
	if (num>1) top--;
}

double rotating()
{
    if (top==1) return 0;
    if (top==2) return Len(stack[2]-stack[1]);
    int now=1;
    double ans=0;
    for (int i=1;i<=top;++i)
    {
        while (dcmp(Dis_To_Line(stack[now],stack[i],stack[i%top+1])-Dis_To_Line(stack[now%top+1],stack[i],stack[i%top+1]))<=0)
            now=now%top+1;
        ans=max(ans,Len(stack[now]-stack[i]));
        ans=max(ans,Len(stack[now]-stack[i%top+1]));
    }
    return ans;
}

Point poly[N],npoly[N],p[N];int cnt,ncnt;
void Crate_Square()
{
	cnt=0;
	poly[++cnt]=Point(INF,INF);
	poly[++cnt]=Point(INF,-INF);
	poly[++cnt]=Point(-INF,-INF);
	poly[++cnt]=Point(-INF,INF);
}
void Cut_Ploy(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 (Line_Seg_Cross(a,b,c,d))
			npoly[++ncnt]=Line_cross(Line(a,b-a),Line(c,d-c));
	}
	cnt=ncnt;
	for (int i=1;i<=ncnt;i++)
		poly[i]=npoly[i];
}

bool check(double mid)
{
	Point A,B,C,D;
	Vector v,w;
	Crate_Square();
	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;
		Cut_Ploy(C,D);
	}
	if (cnt) return 1;
	return 0;
}

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;
}


main()
{
	double x,y,ans;
	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);
    }
}