您的位置:首页 > 其它

poj 1279 Art Gallery(半平面交)

2017-04-19 14:36 471 查看
计算的时候我是假设点输入的顺序是按顺时针输入的,ac了,然而题目并没有说点是顺时针输入。。。。。直接上模板

#include <cstdio>
#include <cstring>
#include <cmath>

const int MAXN = 1510;

struct Point
{
double x,y;
Point() {}
Point(double _x,double _y)
{
x = _x;
y = _y;
}
Point operator -(const Point &b)const
{
return Point(x - b.x, y - b.y);
}
double operator ^(const Point &b)const
{
return x*b.y - y*b.x;
}
double operator *(const Point &b)const
{
return x*b.x + y*b.y;
}
};

void 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;
}

int n,m;
Point p[MAXN];
Point tp[MAXN];
Point np[MAXN];

void Cut(double a,double b,double c)
{
int tmp = 0;
for(int i = 1; i <= m; i++)
{
if(a*np[i].x + b*np[i].y + c >= 0)
tp[++tmp] = np[i];
else
{
if(a*np[i-1].x + b*np[i-1].y + c > 0)
tp[++tmp] = Intersection(np[i-1],np[i],a,b,c);
if(a*np[i+1].x + b*np[i+1].y + c > 0)
tp[++tmp] = Intersection(np[i],np[i+1],a,b,c);
}
}
for(int i = 1; i <= tmp; i++)
np[i] = tp[i];
np[0] = np[tmp];
np[tmp+1] = np[1];
m = tmp;
}

double CalcArea(int n)
{
double res = 0;
for(int i = 1; i <= n; i++)
res += (np[i]^np[i+1])/2;
return fabs(res);
}

double solve()
{
double a,b,c;
for(int i = 1; i <= n; ++i)
np[i] = p[i];
p[n+1] = p[1];
np[n+1] = np[1];
np[0] = np
;
m = n;
for(int i = 1; i <= n; ++i)
{
Get_equation(p[i],p[i+1],a,b,c);
Cut(a,b,c);
}
return CalcArea(m);
}

int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i = 1; i <= n; ++i)
scanf("%lf %lf",&p[i].x,&p[i].y);
printf("%.2f\n",solve());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: