POJ3335Rotating Scoreboard【半平面交判断多边形是否存在内核】

来源:互联网 发布:gid软件下载 编辑:程序博客网 时间:2024/06/07 11:38

Language:
Rotating Scoreboard
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 5988 Accepted: 2373

Description

This year, ACM/ICPC World finals will be held in a hall in form of a simple polygon. The coaches and spectators are seated along the edges of the polygon. We want to place a rotating scoreboard somewhere in the hall such that a spectator sitting anywhere on the boundary of the hall can view the scoreboard (i.e., his line of sight is not blocked by a wall). Note that if the line of sight of a spectator is tangent to the polygon boundary (either in a vertex or in an edge), he can still view the scoreboard. You may view spectator's seats as points along the boundary of the simple polygon, and consider the scoreboard as a point as well. Your program is given the corners of the hall (the vertices of the polygon), and must check if there is a location for the scoreboard (a point inside the polygon) such that the scoreboard can be viewed from any point on the edges of the polygon.

Input

The first number in the input line, T is the number of test cases. Each test case is specified on a single line of input in the form n x1 y1 x2 y2 ... xn yn where n (3 ≤ n ≤ 100) is the number of vertices in the polygon, and the pair of integers xi yi sequence specify the vertices of the polygon sorted in order.

Output

The output contains T lines, each corresponding to an input test case in that order. The output line contains either YES or NO depending on whether the scoreboard can be placed inside the hall conforming to the problem conditions.

Sample Input

24 0 0 0 1 1 1 1 08 0 0  0 2  1 2  1 1  2 1  2 2  3 2  3 0

Sample Output

YESNO

题意:给定多边形的顶点坐标判断是否存在内核;

第一道半平面交参考博客:http://blog.csdn.net/accry/article/details/6070621

#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>using namespace std;struct point{double x,y;}A[105],p[105],q[105];int N;int endcnt,tempcnt; double r;void getline(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-p2.y*p1.x;}void init(){for(int i=1;i<=N;++i){p[i]=A[i];}p[N+1]=p[1];p[0]=p[N];endcnt=N;}point intersect(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 temp;temp.x=(p1.x*v+p2.x*u)/(u+v);temp.y=(p1.y*v+p2.y*u)/(u+v);return temp;}void cut(double a,double b,double c){tempcnt=0;for(int i=1;i<=endcnt;++i){if(a*p[i].x+b*p[i].y+c>=0)q[++tempcnt]=p[i];else {if(a*p[i-1].x+b*p[i-1].y+c>0)q[++tempcnt]=intersect(p[i],p[i-1],a,b,c);if(a*p[i+1].x+b*p[i+1].y+c>0)q[++tempcnt]=intersect(p[i],p[i+1],a,b,c);}}for(int i=1;i<=tempcnt;++i){p[i]=q[i];}p[tempcnt+1]=q[1];p[0]=p[tempcnt];endcnt=tempcnt;}void guizheng(){int i;for(i=1;i<=N;++i){q[i]=A[N-i+1];}for(i=1;i<=N;++i){A[i]=q[i];}}void solve(){//guizheng();init();for(int i=1;i<=N;++i){double a,b,c;getline(A[i],A[i+1],a,b,c);cut(a,b,c);}}int main(){int t,i,j,k;scanf("%d",&t);while(t--){scanf("%d",&N);for(i=1;i<=N;++i){scanf("%lf%lf",&A[i].x,&A[i].y);}A[N+1]=A[1];solve();if(endcnt>0)printf("YES\n");else printf("NO\n");}return 0;}


0 0
原创粉丝点击