您的位置:首页 > 其它

POJ 1279 Art Gallery 半平面交求面积

2016-01-10 15:38 441 查看
和CQOI 2006 凸多边形就多组数据的区别。。

#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 1502;
struct Point {
double x, y;
Point(){}
Point(double _x, double _y) : x(_x), y(_y) {}
Point operator +(const Point &b) const { return Point(x + b.x, y + b.y); }
Point operator -(const Point &b) const { return Point(x - b.x, y - b.y); }
double operator *(const Point &b) const { return x * b.y - y * b.x; }
Point operator *(double b) const { return Point(b * x, b * y); }
} poly
;

struct Line {
Point x, v;
double ang;
Line(){}
Line(const Point &_x, const Point &_v) : x(_x), v(_v), ang(atan2(_v.y, _v.x)) { }

bool operator <(const Line &b) const {
if (ang == b.ang) return v * (b.x - x) < 0; // 极角相等时,这么算的叉积表示a在b的左边时返回Line a < b,unique的时候就会选择a而丢掉b。
return ang < b.ang;
}

bool operator ==(const Line &b) const { return ang == b.ang; }

Point intersection(const Line &b) {
Point u = x - b.x;
double t = (b.v * u) / (v * b.v); // 画了图才知道这么写的意思。。
return x + v * t;
}
} lines
;

bool right(const Point &p, const Line &l) {
return l.v * (p - l.x) < 0;
}

int half_plane_intersection(Line l[], int n, Point poly[]) {
static Line q
;
sort(l + 1, l + n + 1);
n = unique(l + 1, l + n + 1) - (l + 1); // 极角相等时,挑出最靠近“凸多边形中心”的那个,见Line::operator<。
int f = 1, r = 0, m = 0;
q[++r] = l[1]; q[++r] = l[2];
for (int i = 3; i <= n; i++) { // 逐个添加并删除半平面
while (f < r && right(q[r].intersection(q[r - 1]), l[i])) r--;
while (f < r && right(q[f].intersection(q[f + 1]), l[i])) f++;
q[++r] = l[i];
}
while (f < r && right(q[r].intersection(q[r - 1]), q[f])) r--; // 还要处理首尾
while (f < r && right(q[f].intersection(q[f + 1]), q[r])) f++;
q[r + 1] = q[f];
for (int i = f; i <= r; i++)
poly[++m] = q[i].intersection(q[i + 1]);
return m;
}

double calc_area(Point poly[], int m) {
double ans = 0;
if (m <= 2) return ans;
poly[m + 1] = poly[1];
for (int i = 1; i <= m; i++)
ans += poly[i] * poly[i + 1];
return fabs(ans) / 2;
}

int main() {
int t, n, i, j, line_num;
scanf("%d", &t);
for (i = 1; i <= t; i++) {
line_num = 0;
scanf("%d", &n);
for (j = 1; j <= n; j++)
scanf("%lf%lf", &poly[j].x, &poly[j].y);
poly[n + 1] = poly[1];
for (j = 1; j <= n; j++)
lines[++line_num] = Line(poly[j], poly[j] - poly[j + 1]);
int m = half_plane_intersection(lines, line_num, poly);
printf("%.2lf\n", calc_area(poly, m));
}
return 0;
}

Art Gallery

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 6415 Accepted: 2652
Description

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
1
7
0 0
4 4
4 7
9 7
13 -1
8 -6
4 -4

Sample Output
80.00
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: