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; }
相关文章推荐
- POJ 1279 Art Gallery 半平面交 + 多边形面积
- POJ 1279 Art Gallery [半平面交]
- POJ 1279 Art Gallery(半平面交)
- POJ 1279 Art Gallery 半平面交求多边形核
- POJ 1279 Art Gallery(半平面交)
- POJ 1279 Art Gallery 半平面交 + 多边形面积
- [POJ1279]Art Gallery(半平面交)
- zoj 1369 || poj 1279 Art Gallery(半平面交第一题~~嘿嘿~)
- POJ 1279 Art Gallery (半平面交求内核面积)
- POJ 1279 Art Gallery(半平面交求多边形核的面积)
- POJ-1279 Art Gallery(求多边形内核面积,半平面交)
- POJ 1279 || Art Gallery(半平面交求核面积
- poj 1279 Art Gallery(利用极角计算半平面交)
- poj 1279 -- Art Gallery (半平面交)
- poj 1279 Art Gallery(半平面交求多边形核面积)
- POJ 1279 Art Gallery(半平面交求多边形核)
- POJ 1279 Art Gallery 半平面交+求多边形核的面积
- poj 1279 Art Gallery (半平面交)
- poj1279-Art Gallery 直线围成的区域的面积(半平面交模板题)
- POJ 1279 Art Gallery(半平面交)