POJ3525-Most distant point from the sea (Binary + semi-plane intersection)

Source: Internet
Author: User
Most distant point from the sea
Time limit:5000 Ms   Memory limit:65536 K
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 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.

NIn the first line is the number of vertices of the polygon, satisfying 3 ≤N≤ 100. Subsequent n lines areX-AndY-Coordinates ofNVertices. line segments (XI,Yi)-(XI+ 1,YI+ 1) (1 ≤IN? 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 ctions. 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 shoshould be output. an output line shoshould not contain extra characters such as spaces. the answer shocould 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

40 010000 010000 100000 1000030 010000 07000 100060 40100 20250 40250 70100 900 7030 010000 100005000 50010

Sample output

5000.000000494.23364134.5429480.353553
Ask you to find the maximum distance between the points in the convex bag and the side of the convex bag. Train of Thought: You can divide the answer into two parts and calculate the half plane intersection.
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <vector>#include <algorithm>using namespace std;#define REP(_,a,b) for(int _ = (a); _ < (b); _++)#define sz(s)  (int)((s).size())typedef long long ll;const double eps = 1e-9;const int maxn = 100*2+10;struct Point{double x,y;Point(double x=0.0,double y = 0.0):x(x),y(y){}};vector<Point> vP;typedef Point Vector;Point poly[maxn];vector<Vector> vV1,vV2;struct Line {Point P;Vector v;double ang;Line(){}Line(Point P,Vector v):P(P),v(v){ang = atan2(v.y,v.x);}bool operator <(const Line&L) const{return ang < L.ang;}};Line L[maxn];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 p){return Vector(A.x*p,A.y*p);}Vector operator / (Vector A,double p){return Vector(A.x/p,A.y/p);}bool operator < (const Point &a,const Point &b){return a.x < b.x || (a.x==a.y && a.y < b.y);}int dcmp(double x){if(fabs(x) < eps) return 0;else return x < 0? -1:1;}bool operator == (const Point &a,const Point &b){return dcmp(a.x-b.x)==0&& dcmp(a.y-b.y)==0;}double Dot(Vector A,Vector B) {return A.x*B.x+A.y*B.y;}double Length(Vector A) {return sqrt(Dot(A,A));}double Angle(Vector A,Vector B) {return acos(Dot(A,B)/Length(A)/Length(B));}double Cross(Vector A,Vector B) {return A.x*B.y-A.y*B.x;}Vector Rotate(Vector A,double rad) {return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad)); }Vector Normal(Vector A) {double L = Length(A);return Vector(-A.y/L,A.x/L);}bool OnLeft(Line L,Point p){return Cross(L.v,p-L.P) > 0;}Point GetIntersection(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;}int HalfplaneIntersection(Line* L,int n,Point* poly){sort(L,L+n);int first,last;Point *p = new Point[n];Line *q = new Line[n];q[first=last=0] = L[0];for(int i = 1; i < n; i++){while(first < last && !OnLeft(L[i],p[last-1])) last--;while(first < last && !OnLeft(L[i],p[first])) first++;q[++last] = L[i];if(fabs(Cross(q[last].v,q[last-1].v))<eps) {last--;if(OnLeft(q[last],L[i].P)) q[last] = L[i];}if(first<last) p[last-1] = GetIntersection(q[last-1],q[last]);}while(first < last && !OnLeft(q[first],p[last-1])) last--;if(last - first <=1) return 0;p[last] = GetIntersection(q[last],q[first]);int m = 0;for(int i = first; i <= last; i++) poly[m++] = p[i];return m;}int n;void init(){vP.clear();vV1.clear();vV2.clear();}void input(){REP(_,0,n){double x,y;scanf("%lf%lf",&x,&y);vP.push_back(Point(x,y));}#define next(i) ((i)+1)%nREP(_,0,n){vV1.push_back(vP[next(_)]-vP[(_)]);vV2.push_back(Normal(vP[next(_)]-vP[(_)]));}}void solve(){double l = 0,r = 40000;while(r-l > eps){double mid = (l+r)/2;REP(_,0,n) {L[_] = Line(vP[_]+vV2[_]*mid ,vV1[_]);}int m = HalfplaneIntersection(L,n,poly);if(m==0){r = mid;}else{l = mid;}}printf("%.7f\n",r);}int main(){while(~scanf("%d",&n) && n){init();input();solve();}return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.