1080. Art Gallery
限制条件
时间限制: 1 秒, 内存限制: 32 兆
题目描述
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.

输入格式
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.
输出格式
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).
样例输入
1 7 0 0 4 4 4 7 9 7 13 -1 8 -6 4 -4
样例输出
80.00
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <complex>
using namespace std;
const double inf=1e5;
const int maxn=2000;
const double eps=1e-10;
struct point{
double x;
double y;
point(){}
point(double a,double b):x(a),y(b){}
friend point operator - (const point &p,const point &q){
/*point temp;
temp.x=p.x-q.x;
temp.y=p.y-q.y;
return temp;*/
return point(p.x-q.x,p.y-q.y);
}
};
double det(point p,point q){
return p.x*q.y-q.x*p.y;
}
struct YBS{
double a,b,c;
YBS(point p,point q){
a=q.y-p.y;
b=p.x-q.x;
c=det(q,p);
}
};
struct DBX{
int n;
point a[maxn];
};
struct TDBX{
vector<point> P;
};
double calc(YBS &L,point &a) {
return a.x*L.a + a.y*L.b + L.c;
}
point JD(point &a,point &b,YBS &L){
point res;
double t1=calc(L,a);
double t2=calc(L,b);
res.x=(t2*a.x-t1*b.x)/(t2-t1);
res.y=(t2*a.y-t1*b.y)/(t2-t1);
return res;
}
TDBX cut(TDBX &S,YBS &L){
int n=S.P.size();
int i,j;
TDBX res;
for(i=0;i<n;i++){
if(calc(L,S.P[i])<-eps){
res.P.push_back(S.P[i]);
}
else{
j=(i-1);
if(j<0)j=n-1;
if(calc(L,S.P[j])<-eps){
res.P.push_back(JD(S.P[i],S.P[j],L));
}
j=(i+1);
if(j==n)j=0;
if(calc(L,S.P[j])<-eps){
res.P.push_back(JD(S.P[i],S.P[j],L));
}
}
}
return res;
}
TDBX core(DBX &a){
TDBX res;
res.P.clear();
int n=a.n;
int i;
res.P.push_back(point(-inf,-inf));
res.P.push_back(point(-inf,inf));
res.P.push_back(point(inf,inf));
res.P.push_back(point(inf,-inf));
if(n==2){
YBS L(a.a[0],a.a[1]);
return res=cut(res,L);
}
for(i=0;i<n;i++){
YBS L(a.a[i],a.a[(i+1)%n]);
res=cut(res,L);
}
return res;
}
DBX init(DBX &a){
int n=a.n;
double area=0;
for(int i=1;i<n-1;i++){
area+=det(a.a[i]-a.a[0],a.a[i+1]-a.a[0]);
}
if(area<0){
DBX temp;
temp.n=n;
temp.a[0]=a.a[0];
for(int i=n-1;i>=1;i--){
temp.a[n-i]=a.a[i];
}
return temp;
}
return a;
}
double BC(point &a,point &b){
return sqrt(pow((a.x-b.x),2)+pow((a.y-b.y),2));
}
double SofA(double a,double b,double c){
double p=(a+b+c)/2;
double s=sqrt(p*(p-a)*(p-b)*(p-c));
return s;
}
double getArea(TDBX &a){
double area=0;
int i;
int n=a.P.size();
for(i=2;i<n;i++){
area+=SofA(BC(a.P[0],a.P[i-1]),BC(a.P[0],a.P[i]),BC(a.P[i-1],a.P[i]));
}
return area;
}
int main()
{
DBX a;
int T;
int n;
point temp;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
a.n=n;
for(int i=0;i<n;i++){
scanf("%lf%lf",&temp.x,&temp.y);
a.a[i]=temp;
}
a=init(a);
TDBX ans=core(a);
double result=getArea(ans);
printf("%.2lf\n",result);
}
return 0;
}

浙公网安备 33010602011771号