uvalive 2512
来源:互联网 发布:海岛奇兵机械飞爪数据 编辑:程序博客网 时间:2024/06/06 11:48
题意:一个n边形的房间,在里面放一个监控,能看到整个房间,问有多大面积的区域可以放这个监控。
题解:直接半平面交并计算面积就可以了。
#include <cstdio>#include <cstring>#include <vector>#include <cmath>#include <algorithm>using namespace std;const double eps = 1e-9;const double PI = acos(-1);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)); }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; }};double PolygonArea(vector<Point>& res, int m) { double area = 0; for (int i = 1; i < m - 1; i++) area += Cross(res[i] - res[0], res[i + 1] - res[0]); return area / 2;}//点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();}const int N = 1505;int n;Point P[N];vector<Line> L;int main() { int t; scanf("%d", &t); while (t--) { scanf("%d", &n); for (int i = n - 1; i >= 0; i--) scanf("%lf%lf", &P[i].x, &P[i].y); L.clear(); for (int i = 0; i < n; i++) L.push_back(Line(P[i], P[(i + 1) % n] - P[i])); Polygon poly; int cnt = HalfplaneIntersection(L, poly); printf("%.2lf\n", PolygonArea(poly, cnt)); } return 0;}
0 0
- uvalive 2512
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- Servlet获取URL地址
- jquery combobox下拉及异步加载数据
- respondsToSelector的相关使用
- 17. Element dir 属性
- cocostudio 生成的ui用在 cocos2d js
- uvalive 2512
- PackageManagerService的常用方法
- OpenCV2:图像的几何变换,平移、镜像、缩放、旋转(1)
- 估算项目的时间进度
- 18. Element firstChild 属性
- ArcGIS for Android实现地图加载、放大缩小及定位功能
- Regionals 2014 Asia - Bangkok
- UVa11624 Fire!
- hdu2224The shortest path 经典问题之 旅行商问题