题目描述
传送门
题意:求多边形的核可行区域的面积
题解
用半平面交求出凸壳了之后求面积
都是基本操作
代码
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define N 10005
const double inf=1e9;
const double eps=1e-9;
int dcmp(double x)
{
if (x<=eps&&x>=-eps) return 0;
return (x>0)?1:-1;
}
struct Vector
{
double x,y;
Vector(double X=0,double Y=0)
{
x=X,y=Y;
}
};
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 p) {return Vector(a.x*p,a.y*p);}
int T,n,cnt,ncnt;
double x,y,ans;
Point p[N],poly[N],npoly[N];
double Cross(Vector A,Vector B)
{
return A.x*B.y-A.y*B.x;
}
bool insLS(Point A,Point B,Point C,Point D)
{
Vector v=B-A,w=C-A,u=D-A;
return dcmp(Cross(v,w))!=dcmp(Cross(v,u));
}
Point GLI(Point P,Vector v,Point Q,Vector w)
{
Vector u=P-Q;
double t=Cross(w,u)/Cross(v,w);
return P+v*t;
}
void init()
{
cnt=0;
poly[++cnt]=Point(inf,inf);
poly[++cnt]=Point(inf,-inf);
poly[++cnt]=Point(-inf,-inf);
poly[++cnt]=Point(-inf,inf);
}
void halfp(Point A,Point B)
{
ncnt=0;
Point C,D;
for (int i=1;i<=cnt;++i)
{
C=poly[i%cnt+1];
D=poly[(i+1)%cnt+1];
if (dcmp(Cross(B-A,C-A))<=0)
npoly[++ncnt]=C;
if (insLS(A,B,C,D))
npoly[++ncnt]=GLI(A,B-A,C,D-C);
}
cnt=ncnt;
for (int i=1;i<=cnt;++i)
poly[i]=npoly[i];
}
double Area()
{
double area=0;
for (int i=2;i<cnt;++i)
area+=Cross(poly[i]-poly[1],poly[i+1]-poly[1]);
return fabs(area/2.0);
}
int main()
{
scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
for (int i=1;i<=n;++i)
{
scanf("%lf%lf",&x,&y);
p[i]=Point(x,y);
}
init();
for (int i=1;i<=n;++i)
halfp(p[i%n+1],p[(i+1)%n+1]);
ans=Area();
printf("%.2lf\n",ans);
}
}