poj3525Most Distant Point from the Sea(半平面交)

链接

求凸多边形内一点距离边最远。

做法:二分+半平面交判定。

二分距离,每次让每条边向内推进d,用半平面交判定一下是否有核。

本想自己写一个向内推进。。仔细一看发现自己的平面交模板上自带。。

  1 #include <iostream>

  2 #include<cstdio>

  3 #include<cstring>

  4 #include<algorithm>

  5 #include<stdlib.h>

  6 #include<vector>

  7 #include<cmath>

  8 #include<queue>

  9 #include<set>

 10 using namespace std;

 11 #define N 100000

 12 #define LL long long

 13 #define INF 0xfffffff

 14 const double eps = 1e-8;

 15 const double pi = acos(-1.0);

 16 const double inf = ~0u>>2;

 17 const int MAXN=1550;

 18 int m;

 19 double r;

 20 int cCnt,curCnt;//此时cCnt为最终切割得到的多边形的顶点数、暂存顶点个数

 21 struct point

 22 {

 23     double x,y;

 24     point(double x=0,double y=0):x(x),y(y){}

 25 };

 26 point points[MAXN],p[MAXN],q[MAXN];//读入的多边形的顶点(顺时针)、p为存放最终切割得到的多边形顶点的数组、暂存核的顶点

 27 void getline(point x,point y,double &a,double &b,double   &c) //两点x、y确定一条直线a、b、c为其系数

 28 {

 29     a = y.y - x.y;

 30     b = x.x - y.x;

 31     c = y.x * x.y - x.x * y.y;

 32 }

 33 double dis(point a,point b)

 34 {

 35     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));

 36 }

 37 void initial()

 38 {

 39     for(int i = 1; i <= m; ++i)p[i] = points[i];

 40     p[m+1] = p[1];

 41     p[0] = p[m];

 42     cCnt = m;//cCnt为最终切割得到的多边形的顶点数,将其初始化为多边形的顶点的个数

 43 }

 44 point intersect(point x,point y,double a,double b,double c) //求x、y形成的直线与已知直线a、b、c、的交点

 45 {

 46     double u = fabs(a * x.x + b * x.y + c);

 47     double v = fabs(a * y.x + b * y.y + c);

 48     point pt;

 49     pt.x=(x.x * v + y.x * u) / (u + v);

 50     pt.y=(x.y * v + y.y * u) / (u + v);

 51     return  pt;

 52 }

 53 void cut(double a,double b ,double c)

 54 {

 55     curCnt = 0;

 56     for(int i = 1; i <= cCnt; ++i)

 57     {

 58         if(a*p[i].x + b*p[i].y + c >= 0)q[++curCnt] = p[i];// c由于精度问题,可能会偏小,所以有些点本应在右侧而没在,

 59         //故应该接着判断

 60         else

 61         {

 62             if(a*p[i-1].x + b*p[i-1].y + c > 0) //如果p[i-1]在直线的右侧的话,

 63             {

 64                 //则将p[i],p[i-1]形成的直线与已知直线的交点作为核的一个顶点(这样的话,由于精度的问题,核的面积可能会有所减少)

 65                 q[++curCnt] = intersect(p[i],p[i-1],a,b,c);

 66             }

 67             if(a*p[i+1].x + b*p[i+1].y + c > 0) //原理同上

 68             {

 69                 q[++curCnt] = intersect(p[i],p[i+1],a,b,c);

 70             }

 71         }

 72     }

 73     for(int i = 1; i <= curCnt; ++i)p[i] = q[i];//将q中暂存的核的顶点转移到p中

 74     p[curCnt+1] = q[1];

 75     p[0] = p[curCnt];

 76     cCnt = curCnt;

 77 }

 78 int solve(double r)

 79 {

 80     //注意:默认点是顺时针,如果题目不是顺时针,规整化方向

 81     initial();

 82 //    for(int i = 1; i <= m; ++i)

 83 //    {

 84 //        double a,b,c;

 85 //        getline(points[i],points[i+1],a,b,c);

 86 //        cut(a,b,c);

 87 //    }

 88 

 89       //如果要向内推进r,用该部分代替上个函数

 90       for(int i = 1; i <= m; ++i){

 91           point ta, tb, tt;

 92           tt.x = points[i+1].y - points[i].y;

 93           tt.y = points[i].x - points[i+1].x;

 94           double k = r / sqrt(tt.x * tt.x + tt.y * tt.y);

 95           tt.x = tt.x * k;

 96           tt.y = tt.y * k;

 97           ta.x = points[i].x + tt.x;

 98           ta.y = points[i].y + tt.y;

 99           tb.x = points[i+1].x + tt.x;

100           tb.y = points[i+1].y + tt.y;

101           double a,b,c;

102           getline(ta,tb,a,b,c);

103           cut(a,b,c);

104       }

105     //多边形核的面积

106 //    double area = 0;

107 //    for(int i = 1; i <= curCnt; ++i)

108 //        area += p[i].x * p[i + 1].y - p[i + 1].x * p[i].y;

109 //    area = fabs(area / 2.0);

110 //    printf("%.2f\n",area);

111     if(curCnt) return 1;

112     return 0;

113 

114 }

115 void GuiZhengHua(){

116      //规整化方向,逆时针变顺时针,顺时针变逆时针

117     for(int i = 1; i < (m+1)/2; i ++)

118       swap(points[i], points[m-i]);

119 }

120 //void change(double d)

121 //{

122 //    int i;

123 //    for(i = 1; i <= m ;i++)

124 //    {

125 //        double len = dis(p[i],points[i+1]);

126 //        double a = points[i+1].y-points[i].y;

127 //        double b = points[i].x-points[i+1].x;

128 //        double cos = a/len;

129 //        double sin = b/len;

130 //        points[i] = point(points[i].x+cos*d,points[i].y+sin*d);

131 //        points[i+1] = point(points[i+1].x+cos*d,points[i+1].y+sin*d);

132 //    }

133 //}

134 int main()

135 {

136     int i;

137     while(scanf("%d",&m)&&m)

138     {

139         for(i = 1 ; i<=m; i++)

140         scanf("%lf%lf",&points[i].x,&points[i].y);

141         GuiZhengHua();

142         points[m+1] = points[1];

143         double rig = INF,lef = 0,mid;

144         while(rig-lef>eps)

145         {

146             mid = (rig+lef)/2.0;

147             //change(mid);

148             if(solve(mid))

149             lef = mid;

150             else rig = mid;

151         }

152         printf("%.6f\n",lef);

153     }

154     return 0;

155 }
View Code

 

你可能感兴趣的:(ant)