题意:有一个n个点的凸包多边形,在这个多边形内找一点到多边形边界的距离最远,输出最远距离。
题解:利用半平面交求多边形的内核,二分出距离,多边形的每条边都按这个距离向内推进,如果有交点就继续推进。 这里的半平面交讲的不错。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const double PI = acos(-1);
const int N = 105;
const double INF = 1e9;
const double eps = 1e-7;
struct Point {
double x, y;
Point(double x = 0, double y = 0): x(x), y(y) {}
}P[N], poly[N], v1[N], v2[N];
typedef Point Vector;
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;
}
}L[N];
int n;
double Sqr(double x) {
return x * x;
}
Point operator + (Point A, Point B) {
return Point(A.x + B.x, A.y + B.y);
}
Point operator - (Point A, Point B) {
return Point(A.x - B.x, A.y - B.y);
}
Point operator * (Point A, double p) {
return Point(A.x * p, A.y * p);
}
Point operator / (Point A, double p) {
return Point(A.x / p, A.y / p);
}
//计算点积的正负 负值夹角为钝角
int dcmp(double x) {
if (fabs(x) < 1e-9)
return 0;
return x < 0 ? -1 : 1;
}
bool operator < (const Point& a, const Point& b) {
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
bool operator == (const Point& a, const Point& b) {
return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}
//计算点积
double Dot(Point A, Point B) {
return A.x * B.x + A.y * B.y;
}
//计算叉积,也就是数量积
double Cross(Point A, Point B) {
return A.x * B.y - A.y * B.x;
}
//计算向量长度
double Length(Point A) {
return sqrt(Dot(A, A));
}
Vector Normal(Vector A) {
double L = Length(A);
return Vector(-A.y / L, A.x / L);
}
//向量A旋转rad弧度,rad负值为顺时针旋转
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 torad(double deg) {
return deg / 180.0 * PI;
}
//点p在有向直线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 = 0, rear = 0;
Point *p = new Point[n];
Line *q = new Line[n];
q[first] = L[0];
for (int i = 1; i < n; i++) {
while (first < rear && !OnLeft(L[i], p[rear - 1]))
rear--;
while (first < rear && !OnLeft(L[i], p[first]))
first++;
q[++rear] = L[i];
if (fabs(Cross(q[rear].v, q[rear - 1].v)) < eps) {
rear--;
if (OnLeft(q[rear], L[i].p))
q[rear] = L[i];
}
if (first < rear)
p[rear - 1] = GetIntersection(q[rear - 1], q[rear]);
}
while (first < rear && !OnLeft(q[first], p[rear - 1]))
rear--;
if (rear - first <= 1)
return 0;
p[rear] = GetIntersection(q[rear], q[first]);
int m = 0;
for (int i = first; i <= rear; i++)
poly[m++] = p[i];
return m;
}
int main() {
while (scanf("%d", &n) == 1 && n) {
for (int i = 0; i < n; i++)
scanf("%lf%lf", &P[i].x, &P[i].y);
for (int i = 0; i < n; i++) {
v1[i] = P[(i + 1) % n] - P[i];
v2[i] = Normal(v1[i]);
}
double left = 0, right = 20000;
while (right - left > eps) {
double mid = (left + right) / 2;
for (int i = 0; i < n; i++)
L[i] = Line(P[i] + v2[i] * mid, v1[i]);
int m = HalfplaneIntersection(L, n, poly);
if (!m)
right = mid;
else
left = mid;
}
printf("%lf\n", left);
}
return 0;
}