poj 3335(半平面交)
来源:互联网 发布:java会议室预定系统 编辑:程序博客网 时间:2024/04/30 02:19
题意:顺时针给出n个点的凸多边形,问凸多边形的内核。
题解:半平面交模板题,有点坑,用大白上的模板时,判断点是否在有向直线的左边,线上的也算。。
#include <cstdio>#include <cstring>#include <cmath>#include <vector>#include <algorithm>using namespace std;const double eps = 1e-9;const double PI = acos(-1);double Sqr(double x) { return x * x; }int dcmp(double x) { if (fabs(x) < eps) return 0; return x > 0 ? 1 : -1;}struct Point { double x, y; Point(double a = 0, double b = 0): x(a), y(b) {}};typedef Point Vector;typedef vector<Point> Polygon;Vector operator + (const Vector& a, const Vector& b) { return Vector(a.x + b.x, a.y + b.y); }Vector operator - (const Vector& a, const Vector& b) { return Vector(a.x - b.x, a.y - b.y); } Vector operator * (const Vector& a, double b) { return Vector(a.x * b, a.y * b); } Vector operator / (const Vector& a, double b) { return Vector(a.x / b, a.y / b); } bool operator == (const Vector& a, const Vector& b) { return !dcmp(a.x - b.x) && !dcmp(a.y - b.y); } bool operator < (const Vector& a, const Vector& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); } double Dot(const Vector& a, const Vector& b) { return a.x * b.x + a.y * b.y; } double Length(const Vector& a) { return sqrt(Dot(a, a)); } double Cross(const Vector& a, const Vector& b) { return a.x * b.y - a.y * b.x; } double Angle(const Vector& a, const Vector& b) { return acos(Dot(a, b) / Length(a) / Length(b)); } Vector Rotate(Vector A, double rad) { return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad)); } //默认逆时针,rad前加负号是顺时针double angle(Vector v) { return atan2(v.y, v.x); } struct Line { Point p; Vector v; double ang; Line() {} Line(Point a, Vector b): p(a), v(b) { ang = atan2(b.y, b.x); } bool operator < (const Line& L) const { return ang < L.ang; } Point point(double a) { return p + v * a; }};//点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(vector<Line>& L) { vector<Point> poly; int n = L.size(); sort(L.begin(), L.end()); int first = 0, rear = 0; vector<Point> p(n); vector<Line> q(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]); for (int i = first; i <= rear; i++) poly.push_back(p[i]); return poly.size(); }Point P[105];vector<Line> L;int n;int main() { int t; scanf("%d", &t); while (t--) { L.clear(); scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%lf%lf", &P[i].x, &P[i].y); for (int i = 0; i < n; i++) L.push_back(Line(P[i], P[i] - P[(i + 1) % n])); int res = HalfplaneIntersection(L); if (res) printf("YES\n"); else printf("NO\n"); } return 0;}
0 0
- poj 3335(半平面交)
- poj 3335 Rotating Scoreboard - 半平面交
- POJ 3335 Rotating Scoreboard (半平面交)
- Rotating Scoreboard - POJ 3335 半平面交
- POJ 3335 Rotating Scoreboard 半平面交
- POJ 3335 Rotating Scoreboard 半平面交
- poj 3335(半平面交)
- poj 3335 Rotating Scoreboard(半平面交)
- POJ 3335 (计算几何+半平面交)
- 半平面交入门 poj 3335 poj 1474 poj 1279 半平面交模板
- 半平面交 poj 3335(判断是否有内核) poj 3525 (半平面交+二分)
- POJ 3525 半平面交
- POJ 3384 半平面交
- POJ 1755 半平面交
- POJ 2540 半平面交
- 【poj 2451】半平面交
- POJ 3525 半平面交
- 半平面交 (POJ 1279
- 区间求和 I
- POJ 1426 好玩的打表题(bfs+大整数取模)
- String类
- web用户退出清空网页缓存
- UIWebView基本使用
- poj 3335(半平面交)
- Matlab畫地圖
- android 透明度计算
- 一种基于 HTTP 长连接的“服务器推”技术在web端的应用
- html横向树目录
- PS进阶点滴积累
- 彻底解决Android 拍照 内存溢出 Out of Memory的问题
- Android中的权限
- redis命令学习笔记