Art Gallery
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 7218 | Accepted: 2908 |
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
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
#define mes(a,x,s) memset(a,x,(s)*sizeof a[0])
#define mem(a,x) memset(a,x,sizeof a)
#define ysk(x) (1<<(x))
typedef long long ll;
typedef pair<int, int> pii;
const int INF =0x3f3f3f3f;
const int maxn=1500 ;
const double PI=cos(-1.0);
const double eps=1e-10;
int dcmp(double x)
{
if(fabs(x)<eps) return 0;
else return x<0?-1:1;
}
struct Point
{
double x,y;
Point(double x=0,double y=0):x(x),y(y) {};
bool operator ==(const Point B)const {return dcmp(x-B.x)==0&&dcmp(y-B.y)==0;}
bool operator<(const Point& b)const
{
return dcmp(x-b.x)<0|| dcmp(x-b.x)==0 &&dcmp(y-b.y)<0;
}
};
typedef Point Vector;
Vector operator -(Vector A,Vector B) {return Vector(A.x-B.x,A.y-B.y); }
double Cross(Vector A,Vector B)//叉乘
{
return A.x*B.y-A.y*B.x;
}
double Dot(Vector A,Vector B)//点乘
{
return 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 p) {return Vector(A.x*p,A.y*p); }
Vector operator /(Vector A,double p) {return Vector(A.x/p,A.y/p); }
Vector operator -(Vector A) {return Vector(-A.x,-A.y);}
struct Line
{
Point p,p2;
Vector v;
double ang;
Line(){}
Line(Point a,Vector v):p(a),v(v){ang=atan2(v.y,v.x); }//点线式
void twoPointIntial(Point p,Point p2)//两点式
{
this->p=p;
this->p2=p2;
v= p2-p;
ang=atan2(v.y,v.x);
}
Point point(double t)//参数方程求参数t对应点
{
return p+v*t;
}
bool operator<(const Line & L)const
{
return ang<L.ang;
}
};
Point GetIntersection(Line a,Line b)
{
Vector u=a.p-b.p;
double t=Cross(b.v,u)/Cross(a.v,b.v);
return a.p+a.v*t;
}
bool OnRight(Line L,Point p)
{
return dcmp(Cross(L.v,p-L.p))<0;
}
int HalfplaneIntersection(Line* L,int n,Point * poly)
{
sort(L,L+n);
int first,last;
Point *p=new Point[n];
Line *q=new Line[n];
q[first=last=0]=L[0];
for(int i=1;i<n;i++)
{
while(first<last&&OnRight(L[i],p[last-1] ) ) last--;
while(first<last&&OnRight(L[i],p[first])) first++;
q[++last]=L[i];
if(fabs(Cross(q[last].v,q[last-1].v) )<eps )
{
last--;
if(!OnRight(q[last],L[i].p) ) q[last]=L[i];
}
if(first<last) p[last-1]=GetIntersection(q[last-1],q[last] );
}
while(first<last&&OnRight(q[first],p[last-1])) last--;
if(last-first<=1) return 0;//小于等于3个,不存在
p[last]=GetIntersection(q[last],q[first]);
int m=0;
for(int i=first;i<=last;i++) poly[m++]=p[i];
return m;
}
double PolygonArea(Point *p,int n)
{
double area=0;
for(int i=1;i<n-1;i++)
{
area+=Cross(p[i]-p[0],p[i+1]-p[0]);
}
return area/2;
}
int n;
Point poly[maxn+10];
Line L[maxn+10];
void anticlockwise()
{
if(dcmp(PolygonArea(poly, n)<0) ) reverse(poly,poly+n);
}
int main()
{
std::ios::sync_with_stdio(false);
int T;cin>>T;
while(T--)
{
cin>>n;
for0(i,n)
{
cin>>poly[i].x>>poly[i].y;
}
anticlockwise();
for0(i,n)
{
L[i]=Line(poly[i],poly[ (i+1)%n ]-poly[i] );
}
double ans;
int m=HalfplaneIntersection(L,n,poly);
if(!m ) ans=0;
else ans=PolygonArea(poly, m);
if( ans<=eps) puts("0.00");
else printf("%.2f\n",ans);
}
return 0;
}