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

本文介绍了一个基于半平面交算法求解多边形内核问题的基础实现方案,通过具体的输入输出示例展示了如何计算从多边形地板上的特定区域观察到墙面每个点的可视范围面积。

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值