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 (xiyi)–(xi+1yi+1) (1 ≤ i ≤ n − 1) and the line segment (xnyn)–(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

转载:http://blog.csdn.net/non_cease/article/details/7814970

题意:给定一个凸多边形,求多边形中距离边界最远的点到边界的距离。

思路 : 每次将凸多边形每条边往里平移d,判断是否存在核;二分d即可。

  1 #include <iostream>  
  2 #include <cstdio>  
  3 #include <algorithm>  
  4 #include <cmath>  
  5 using namespace std;  
  6   
  7 const double eps = 1e-10;  
  8 const int maxn = 105;  
  9   
 10 int dq[maxn], top, bot, pn, order[maxn], ln;  
 11 struct Point {  
 12     double x, y;  
 13 } p[maxn];  
 14   
 15 struct Line {  
 16     Point a, b;  
 17     double angle;  
 18 } l[maxn], tmp[maxn];  
 19   
 20 int dblcmp(double k) {  
 21     if (fabs(k) < eps) return 0;  
 22     return k > 0 ? 1 : -1;  
 23 }  
 24   
 25 double multi(Point p0, Point p1, Point p2) {  
 26     return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);  
 27 }  
 28   
 29 bool cmp(int u, int v) {  
 30     int d = dblcmp(l[u].angle-l[v].angle);  
 31     if (!d) return dblcmp(multi(l[u].a, l[v].a, l[v].b)) > 0;  
 32     return d < 0;  
 33 }  
 34   
 35 void getIntersect(Line l1, Line l2, Point& p) {  
 36     double dot1,dot2;  
 37     dot1 = multi(l2.a, l1.b, l1.a);  
 38     dot2 = multi(l1.b, l2.b, l1.a);  
 39     p.x = (l2.a.x * dot2 + l2.b.x * dot1) / (dot2 + dot1);  
 40     p.y = (l2.a.y * dot2 + l2.b.y * dot1) / (dot2 + dot1);  
 41 }  
 42   
 43   
 44 bool judge(Line l0, Line l1, Line l2) {  
 45     Point p;  
 46     getIntersect(l1, l2, p);  
 47     return dblcmp(multi(p, l0.a, l0.b)) < 0;  
 48 }  
 49   
 50 void addLine(double x1, double y1, double x2, double y2) {  
 51     l[ln].a.x = x1; l[ln].a.y = y1;  
 52     l[ln].b.x = x2; l[ln].b.y = y2;  
 53     l[ln].angle = atan2(y2-y1, x2-x1);  
 54     ln++;  
 55 }  
 56   
 57 bool halfPlaneIntersection(Line l[], int n) {  
 58     int i, j;  
 59     for (i = 0; i < n; i++) order[i] = i;  
 60     sort(order, order+n, cmp);  
 61     for (i = 1, j = 0; i < n; i++)  
 62         if (dblcmp(l[order[i]].angle-l[order[j]].angle) > 0)  
 63             order[++j] = order[i];  
 64     n = j + 1;  
 65     dq[0] = order[0];  
 66     dq[1] = order[1];  
 67     bot = 0;  
 68     top = 1;  
 69     for (i = 2; i < n; i++) {  
 70         while (bot < top && judge(l[order[i]], l[dq[top-1]], l[dq[top]])) top--;  
 71         while (bot < top && judge(l[order[i]], l[dq[bot+1]], l[dq[bot]])) bot++;  
 72         dq[++top] = order[i];  
 73     }  
 74     while (bot < top && judge(l[dq[bot]], l[dq[top-1]], l[dq[top]])) top--;  
 75     while (bot < top && judge(l[dq[top]], l[dq[bot+1]], l[dq[bot]])) bot++;  
 76     if (bot + 1 >= top) return false; //当dq中少于等于两条边时,说明半平面无交集  
 77     return true;  
 78 }  
 79   
 80 double getDis(Point a, Point b) {  
 81     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));  
 82 }  
 83   
 84 void changePolygon(double h) {  //每次将直线向里面平移距离h
 85     double len, dx, dy;  
 86     for (int i = 0; i < ln; i++) {  
 87         len = getDis(l[i].a, l[i].b);  
 88         dx = (l[i].a.y - l[i].b.y) / len * h;  
 89         dy = (l[i].b.x - l[i].a.x) / len * h;  
 90         tmp[i].a.x = l[i].a.x + dx;  
 91         tmp[i].a.y = l[i].a.y + dy;  
 92         tmp[i].b.x = l[i].b.x + dx;  
 93         tmp[i].b.y = l[i].b.y + dy;  
 94         tmp[i].angle = l[i].angle;  
 95     }  
 96 }  
 97   
 98 double BSearch() {  
 99     double l = 0, r = 20000, mid;  
100     while (l + eps < r) {  
101         mid = (l + r) / 2;  
102         changePolygon(mid);  
103         if (halfPlaneIntersection(tmp, ln))  
104             l = mid;  
105         else r = mid;  
106     }  
107     return l;  
108 }  
109   
110 int main()  
111 {  
112     int i;  
113   
114     while (scanf ("%d", &pn) && pn) {  
115         for (i = 0; i < pn; i++)  
116             scanf ("%lf%lf", &p[i].x, &p[i].y);  
117         for (i = ln = 0; i < pn-1; i++)  
118             addLine(p[i].x, p[i].y, p[i+1].x, p[i+1].y);  
119         addLine(p[i].x, p[i].y, p[0].x, p[0].y);  
120   
121         printf ("%.6lf\n", BSearch());  
122     }  
123     return 0;  
124 }  

 

posted @ 2017-10-13 11:05  抓不住Jerry的Tom  阅读(189)  评论(0编辑  收藏  举报