POJ-1279 Art Gallery(求多边形内核面积,半平面交)

来源:互联网 发布:婚纱网络客服 编辑:程序博客网 时间:2024/06/03 16:01

                                   Art Gallery 


The art galleries of the new and very futuristic building of the Center for Balkan Cooperation have the form of polygons (not necessarily convex). When a big exhibition is organized, watching over all of the pictures is a big security concern. Your task is that for a given gallery to write a program which finds the surface of the area of the floor, from which each point on the walls of the gallery is visible. On the figure 1. a map of a gallery is given in some co-ordinate system. The area wanted is shaded on the figure 2. 
Input
The number of tasks T that your program have to solve will be on the first row of the input file. Input data for each task start with an integer N, 5 <= N <= 1500. Each of the next N rows of the input will contain the co-ordinates of a vertex of the polygon ? two integers that fit in 16-bit integer type, separated by a single space. Following the row with the co-ordinates of the last vertex for the task comes the line with the number of vertices for the next test and so on.
Output
For each test you must write on one line the required surface - a number with exactly two digits after the decimal point (the number should be rounded to the second digit after the decimal point).
Sample Input
170 04 44 79 713 -18 -64 -4
Sample Output
80.00

思路:这题就是一道最基础的半平面交,多边形求内核问题,直接套用模板就行了。

#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <cstdlib>#include <cmath>#include <vector>#include <queue>#include <map>#include <algorithm>#include <set>#include <functional>using namespace std;typedef long long LL;typedef unsigned long long ULL;const int INF = 1e9 + 5;const int MAXN = 3005;const int MOD = 1000000007;const double eps = 1e-8;const double PI = acos(-1.0);struct POINT{double x;double y;POINT(double a = 0, double b = 0) { x = a; y = b; } //constructor };/*半平面相交(直线切割多边形)(点标号从1开始)*/POINT points[MAXN], p[MAXN], q[MAXN];int n;double r;double area;int cCnt, curCnt;inline void getline(POINT x, POINT y, double &a, double &b, double &c) {a = y.y - x.y;b = x.x - y.x;c = y.x * x.y - x.x * y.y;}inline void initial() {for (int i = 1; i <= n; ++i)p[i] = points[i];p[n + 1] = p[1];p[0] = p[n];cCnt = n;}inline POINT intersect(POINT x, POINT y, double a, double b, double c) {double u = fabs(a * x.x + b * x.y + c);double v = fabs(a * y.x + b * y.y + c);return POINT((x.x * v + y.x * u) / (u + v), (x.y * v + y.y * u) / (u + v));}inline void cut(double a, double b, double c) {curCnt = 0;for (int i = 1; i <= cCnt; ++i) {if (a*p[i].x + b*p[i].y + c >= eps)q[++curCnt] = p[i];else {if (a*p[i - 1].x + b*p[i - 1].y + c > eps) {q[++curCnt] = intersect(p[i], p[i - 1], a, b, c);}if (a*p[i + 1].x + b*p[i + 1].y + c > eps) {q[++curCnt] = intersect(p[i], p[i + 1], a, b, c);}}}for (int i = 1; i <= curCnt; ++i)p[i] = q[i];p[curCnt + 1] = q[1]; p[0] = p[curCnt];cCnt = curCnt;}inline void solve() {//注意:默认点是顺时针,如果题目不是顺时针,规整化方向  initial();for (int i = 1; i <= n; ++i) {double a, b, c;getline(points[i], points[i + 1], a, b, c);cut(a, b, c);}/*如果要向内推进r,用该部分代替上个函数for(int i = 1; i <= n; ++i){Point ta, tb, tt;tt.x = points[i+1].y - points[i].y;tt.y = points[i].x - points[i+1].x;double k = r / sqrt(tt.x * tt.x + tt.y * tt.y);tt.x = tt.x * k;tt.y = tt.y * k;ta.x = points[i].x + tt.x;ta.y = points[i].y + tt.y;tb.x = points[i+1].x + tt.x;tb.y = points[i+1].y + tt.y;double a,b,c;getline(ta,tb,a,b,c);cut(a,b,c);}*///多边形核的面积  //double area = 0;for (int i = 1; i <= curCnt; ++i)area += p[i].x * p[i + 1].y - p[i + 1].x * p[i].y;area = fabs(area / 2.0);//此时cCnt为最终切割得到的多边形的顶点数,p为存放顶点的数组  }inline void GuiZhengHua() {//规整化方向,逆时针变顺时针,顺时针变逆时针  for (int i = 1; i < (n + 1) / 2; i++)swap(points[i], points[n - i]);//头文件加iostream  }inline void init() {for (int i = 1; i <= n; ++i)scanf("%lf%lf", &points[i].x, &points[i].y);points[n + 1] = points[1];area = 0;}int main(){int T;scanf("%d", &T);while (T--){scanf("%d", &n);init();solve();printf("%.2f\n", area+eps);}}



阅读全文
0 0
原创粉丝点击