关闭

poj 1279 Art Gallery (半平面交)

90人阅读 评论(0) 收藏 举报
分类:

Art Gallery
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7280   Accepted: 2923

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

Source

[Submit]   [Go Back]   [Status]   [Discuss]


题目大意:求多边形内核的面积

题解:半平面交+叉积求面积

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define  N 2000
#define eps 1e-8
#define inf 1e9
using namespace std;
struct vector {
	double x,y;
	vector (double X=0,double Y=0){
		x=X,y=Y;
	}
}a[N],p[N],tmp[N];
typedef vector point;
vector operator -(vector a,vector b){
	return vector (a.x-b.x,a.y-b.y);
}
vector operator +(vector a,vector b){
	return vector (a.x+b.x,a.y+b.y);
}
vector operator *(vector a,double t){
	return vector (a.x*t,a.y*t);
}
int n,m;
double cross(vector a,vector b)
{
	return a.x*b.y-a.y*b.x;
}
point glt(point a,point a1,point b,point b1)
{
	vector v=a1-a; vector w=b1-b;
	vector u=a-b;
	double t=cross(w,u)/cross(v,w);
	return a+v*t;
}
void init()
{
   m=0;
   p[m++]=point(inf,inf);
   p[m++]=point(-inf,inf);
   p[m++]=point(-inf,-inf);
   p[m++]=point(inf,-inf);
}
void cut(point a,point b)
{
	int cnt=0;
	memset(tmp,0,sizeof(tmp));
	for (int i=0;i<m;i++){
		double c=cross(b-a,p[i]-a);
		double d=cross(b-a,p[(i+1)%m]-a);
		if (c>=0) tmp[cnt++]=p[i];
		if (c*d<0) 
		 tmp[cnt++]=glt(a,b,p[i],p[(i+1)%m]);
	}
	m=cnt;
	for (int i=0;i<cnt;i++) p[i]=tmp[i];
}
int main()
{
	freopen("a.in","r",stdin);
	int T;
	scanf("%d",&T);
	while (T--) {
		scanf("%d",&n);
		if (!n) break;
		for (int i=1;i<=n;i++) scanf("%lf%lf",&a[i].x,&a[i].y);
		a[n+1]=a[1];
		init();
		for (int i=2;i<=n+1;i++){
			cut(a[i],a[i-1]);
			if (!m) break;
		}
		double area=0;
		p[m]=p[0];
		for (int i=1;i<m;i++)
		  area+=cross(p[i]-p[0],p[i+1]-p[0]);
		printf("%.2lf\n",fabs(area)/2);
	}
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:232451次
    • 积分:14390
    • 等级:
    • 排名:第822名
    • 原创:1265篇
    • 转载:12篇
    • 译文:0篇
    • 评论:65条
    欢迎交流
    QQ:1184204539
    最新评论