poj1279-Art Gallery 直线围成的区域的面积(半平面交模板题)

Art Gallery
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 7997 Accepted: 3182

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


计算直线围成的面积,直接套用半平面交模板即可,半平面交知识请看blog.csdn.net/accry/article/details/6070621


ac代码如下:

#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<sstream>
#include<cassert>
#define LL long long
#define eps 1e-7
#define inf 1<<30
using namespace std;
struct Point {
	double x,y;
} p[1505],tp[1505],q[1505];
//叉积
double xmul(Point p0,Point p1,Point p2)
{
	return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
//通过两点,确定直线方程
double Get_equation(Point p1,Point p2,double &a,double &b,double &c)
{
	a=p2.y-p1.y;
	b=p1.x-p2.x;
	c=p2.x*p1.y-p1.x*p2.y;
}
//求交点
Point Intersection(Point p1,Point p2,double a,double b,double c)
{
	double u=fabs(a*p1.x+b*p1.y+c);
	double v=fabs(a*p2.x+b*p2.y+c);
	Point t;
	t.x=(p1.x*v+p2.x*u)/(u+v);
	t.y=(p1.y*v+p2.y*u)/(u+v);
	return t;
}
//求面积,正为顺时针,和叉积写法有关
double Get_area(Point p[],int n)
{
	double area=0;
	for(int i=2; i<n; i++)
		area+=xmul(p[1],p[i],p[i+1]);
	return -area/2.0;
}
//改变顺序
double Change_dir(Point p[],int n)
{
	for(int i=1; i<=n/2; i++)
		swap(p[i],p[n+1-i]);
}
//加入一条边,切割
void Cut(double a,double b,double c,Point p[],int &cnt)
{
	int tmp=0;
	for(int i=1; i<=cnt; i++) {
		//当前点就在右侧
		if(a*p[i].x+b*p[i].y+c>-eps) tp[++tmp]=p[i];
		else {
			//前一个点在右侧,产生交点
			if(a*p[i-1].x+b*p[i-1].y+c>eps)
				tp[++tmp]=Intersection(p[i-1],p[i],a,b,c);
			//同理
			if(a*p[i+1].x+b*p[i+1].y+c>eps)
				tp[++tmp]=Intersection(p[i],p[i+1],a,b,c);
		}
	}
	for(int i=1; i<=tmp; i++)
		p[i]=tp[i];
	p[0]=p[tmp];
	p[tmp+1]=p[1];
	cnt=tmp;
}
void slove(Point p[],int n)
{
	//默认顺时针,通过面积判断一下
	if(Get_area(p,n)<eps) Change_dir(p,n);
	p[0]=p[n];
	p[n+1]=p[1];
	//原来的点要备份一遍
	for(int i=0; i<=n+1; i++) q[i]=p[i];
	int cnt=n;
	for(int i=1; i<=n; i++) {
		double a,b,c;
		Get_equation(q[i],q[i+1],a,b,c);
		Cut(a,b,c,p,cnt);
	}
	printf("%.2f\n",fabs(Get_area(p,cnt)));
}
int main()
{
	int t,n;
	scanf("%d",&t);
	while(t--) {
		scanf("%d",&n);
		for(int i=1; i<=n; i++)
			scanf("%lf%lf",&p[i].x,&p[i].y);
		slove(p,n);
	}
	return 0;
}


题目链接: 点击打开链接poj.org/problem?id=1279

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值