POJ 3525(计算几何+凸多边形最大内切圆)

约定不等于承诺〃 2022-06-07 22:43 273阅读 0赞

问题描述:

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

  1. 4
  2. 0 0
  3. 10000 0
  4. 10000 10000
  5. 0 10000
  6. 3
  7. 0 0
  8. 10000 0
  9. 7000 1000
  10. 6
  11. 0 40
  12. 100 20
  13. 250 40
  14. 250 70
  15. 100 90
  16. 0 70
  17. 3
  18. 0 0
  19. 10000 10000
  20. 5000 5001
  21. 0

Sample Output

  1. 5000.000000
  2. 494.233641
  3. 34.542948
  4. 0.353553

题目题意:题目给我们一个凸多边形,让我们求出最大内切圆的半径。

题目分析:凸多边形可以看出凸多边形的边(直线)的半平面的交,其实只要这个半平面的交存在,那么这个内切圆就存在,当半平面的交不存在的时候,内切圆就不存在了。(最后变成一个点)

因为是凸多边形的原因,我们可以通过判断凸多边形的核是否存在来判断。

我们平移凸多边形的各个边,直到核不存在了,平移的距离就是最大内切圆的半径。

代码如下:(半平面交的代码是模板代码)

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. using namespace std;
  6. const double eps=1e-8;
  7. const double inf=1e9;
  8. const int MAXN=210;
  9. int m;//保存多边形的点数
  10. double r;//保存内移距离
  11. int cCnt,curCnt;//此时cCnt为最终切割得到的多边形的顶点数、暂存顶点个数
  12. struct point
  13. {
  14. double x,y;
  15. };
  16. point points[MAXN],p[MAXN],q[MAXN];//读入的多边形的顶点(顺时针)、p为存放最终切割得到的多边形顶点的数组、暂存核的顶点
  17. void getline(point x,point y,double &a,double &b,double &c) //两点x、y确定一条直线a、b、c为其系数
  18. {
  19. a = y.y - x.y;
  20. b = x.x - y.x;
  21. c = y.x * x.y - x.x * y.y;
  22. }
  23. void initial()
  24. {
  25. for(int i = 1; i <= m; ++i)p[i] = points[i];
  26. p[m+1] = p[1];
  27. p[0] = p[m];
  28. cCnt = m;
  29. }
  30. point intersect(point x,point y,double a,double b,double c)
  31. {
  32. double u = fabs(a * x.x + b * x.y + c);
  33. double v = fabs(a * y.x + b * y.y + c);
  34. point pt;
  35. pt.x=(x.x * v + y.x * u) / (u + v);
  36. pt.y=(x.y * v + y.y * u) / (u + v);
  37. return pt;
  38. }
  39. void cut(double a,double b ,double c)
  40. {
  41. curCnt = 0;
  42. for(int i = 1; i <= cCnt; ++i)
  43. {
  44. if(a*p[i].x + b*p[i].y + c >= 0)q[++curCnt] = p[i];
  45. else
  46. {
  47. if(a*p[i-1].x + b*p[i-1].y + c > 0)
  48. {
  49. q[++curCnt] = intersect(p[i],p[i-1],a,b,c);
  50. }
  51. if(a*p[i+1].x + b*p[i+1].y + c > 0)
  52. {
  53. q[++curCnt] = intersect(p[i],p[i+1],a,b,c);
  54. }
  55. }
  56. }
  57. for(int i = 1; i <= curCnt; ++i)p[i] = q[i];
  58. p[curCnt+1] = q[1];
  59. p[0] = p[curCnt];
  60. cCnt = curCnt;
  61. }
  62. int dcmp(double x)
  63. {
  64. if(fabs(x)<eps) return 0;
  65. else return x<0?-1:1;
  66. }
  67. void solve()
  68. {
  69. initial();
  70. for(int i = 1; i <= m; ++i){
  71. point ta, tb, tt;
  72. tt.x = points[i+1].y - points[i].y;
  73. tt.y = points[i].x - points[i+1].x;
  74. double k = r / sqrt(tt.x * tt.x + tt.y * tt.y);
  75. tt.x = tt.x * k;
  76. tt.y = tt.y * k;
  77. ta.x = points[i].x + tt.x;
  78. ta.y = points[i].y + tt.y;
  79. tb.x = points[i+1].x + tt.x;
  80. tb.y = points[i+1].y + tt.y;
  81. double a,b,c;
  82. getline(ta,tb,a,b,c);
  83. cut(a,b,c);
  84. }
  85. }
  86. void GuiZhengHua(){ //规整化方向,逆时针变顺时针,顺时针变逆时针
  87. for(int i = 1; i < (m+1)/2; i ++)
  88. swap(points[i], points[m-i]);
  89. }
  90. int main()
  91. {
  92. while (scanf("%d",&m)!=EOF){
  93. if (m==0) break;
  94. for (int i=1;i<=m;i++)
  95. scanf("%lf%lf",&points[i].x,&points[i].y);
  96. GuiZhengHua();
  97. points[m+1]=points[1];
  98. double left=0,right=inf,mid;
  99. while ((right-left)>=eps) {//二分求半径
  100. mid=(left+right)/2.0;
  101. // cout<<1<<endl;
  102. r=mid;
  103. solve();
  104. if (cCnt<=0) right=mid;
  105. else left=mid;
  106. }
  107. printf("%.6f\n",left);
  108. }
  109. return 0;
  110. }

发表评论

表情:
评论列表 (有 0 条评论,273人围观)

还没有评论,来说两句吧...

相关阅读

    相关 java 寻找凸多边形

    最近再次用到的一段代码,在此记录一下。 网上找了一圈,实在是找不到原文了。 算法大概是:先定位最左边并且最上面的点,计算这个点与其他点的夹角,找出顺时针转角最小的。按此规