uvalive 3890
来源:互联网 发布:数据库入门 编辑:程序博客网 时间:2024/06/04 00:50
题意:有一个n个点的凸包多边形,在这个多边形内找一点到多边形边界的距离最远,输出最远距离。
题解:利用半平面交求多边形的内核,二分出距离,多边形的每条边都按这个距离向内推进,如果有交点就继续推进。
http://blog.csdn.net/accry/article/details/6070621这里的半平面交讲的不错。
#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;}
0 0
- uvalive 3890
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- 今天微软正式推出Windows 10
- #197 (div.2) A. Helpful Maths
- 游戏服务端架构发展史(上)
- 玩转Android---组件篇---数据存储之SQLite
- 九度oj 1207
- uvalive 3890
- 玩转Android---事件监听篇---第1篇
- 求.net后台程序员 http://jwstudio.cn?s=9uH (麻烦帮忙,广而告之,谢谢)
- presentationLayer
- shell脚本学习知识点(二)
- 黑马程序员--java--两种设计模式
- 玩转Android---事件监听篇---第2篇
- IOS 地图位置定位
- ReactiveCocoa Tutorial – The Definitive Introduction: Part 1/2