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<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<functional>
#include<vector>
#include <map>
#include<cmath>
using namespace std;
typedef long long LL;
const LL MOD=1e9+7;
const double eps=1e-9;
const int MAXN=1500+23;
#define point pair<double,double>
#define x first
#define y second
double cross(point o,point a,point b)
{
return (a.x-o.x)*(b.y-o.y)-(a.y-o.y)*(b.x-o.x);
}
double getarea(point p[],int n)
{
double area=0;
for(int i=2;i<n;i++)
{
area+=(p[i].x-p[1].x)*(p[i+1].y-p[1].y)-(p[i].y-p[1].y)*(p[i+1].x-p[1].x);
}
return area/2.0;
}
void getline(double &a,double &b,double &c,point x1,point x2)
{
a=(x1.y-x2.y);
b=(x2.x-x1.x);
c=-a*x1.x-b*x1.y;
}
point intersect(double a,double b,double c,point x1,point x2)
{
double u = fabs(a * x1.x + b * x1.y + c);
double v = fabs(a * x2.x + b * x2.y + c);
return point( (x1.x * v + x2.x * u) / (u + v) , (x1.y * v + x2.y * u) / (u + v) );
}
int n,last,now;
point p1[MAXN],p2[MAXN],p3[MAXN];
void cut(double a,double b,double c)
{
now=0;
for(int i=1;i<=last;i++)
{
if(a*p2[i].x+b*p2[i].y+c<=eps)
{
p3[++now]=p2[i];
}
else
{
if(a*p2[i-1].x+b*p2[i-1].y+c<=eps)
{
p3[++now]=intersect(a,b,c,p2[i],p2[i-1]);
}
if(a*p2[i+1].x+b*p2[i+1].y+c<=eps)
{
p3[++now]=intersect(a,b,c,p2[i],p2[i+1]);
}
}
}
//printf("now=%d\n",last);
last=now;
for(int i=1;i<=last;i++)p2[i]=p3[i];
p2[last+1]=p3[1];p2[0]=p3[last];
}
inline void GuiZhengHua()
{
//规整化方向,逆时针变顺时针,顺时针变逆时针
for(int i = 1; i <=(n)/2; i++)
swap(p1[i], p1[n-i]);//头文件加iostream
}
int main()
{
int T,cas=0;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%lf%lf",&p1[i].x,&p1[i].y);
double area=getarea(p1,n);
//printf("s=%.0f\n",area);
if(area>0)GuiZhengHua();//通过面积的正负调整方向,注意求面积的写法
p1[0]=p1[n];p1[n+1]=p1[1];
for(int i=0;i<=n+1;i++)p2[i]=p1[i];
last=n;
now=0;
for(int i=1;i<=n;i++)
{
double a,b,c;
getline(a,b,c,p1[i],p1[i+1]);
cut(a,b,c);
}
//printf("last=%d\n",last);
area=0;
if(last<=2)printf("%.2f\n",area);
else
{
area=getarea(p2,last);
printf("%.2f\n",fabs(area));
}
}
return 0;
}
本人蒟蒻,如有错误,还望指正