首页 > 代码库 > poj3335 半交平面,多边形内核
poj3335 半交平面,多边形内核
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 5300 | Accepted: 2112 |
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
Source
1 #include <iostream> 2 #include <algorithm> 3 #include <cmath> 4 #include <stdio.h> 5 using namespace std; 6 #define exp 1e-10 7 8 struct node 9 { 10 double x; 11 double y; 12 }; 13 14 node point[105];//记录最开始的多边形 15 node q[105]; //临时保存新切割的多边形 16 node p[105]; //保存新切割出的多边形 17 int n,m;//n的原先的点数,m是新切割出的多边形的点数 18 double a,b,c; 19 20 void getline(node x,node y) //获取直线ax+by+c==0 21 { 22 a=y.y-x.y; 23 b=x.x-y.x; 24 c=y.x*x.y-x.x*y.y; 25 } 26 27 node intersect(node x,node y) //获取直线ax+by+c==0 和点x和y所连直线的交点 28 { 29 double u=fabs(a*x.x+b*x.y+c); 30 double v=fabs(a*y.x+b*y.y+c); 31 node ans; 32 ans.x=(x.x*v+y.x*u)/(u+v); 33 ans.y=(x.y*v+y.y*u)/(u+v); 34 return ans; 35 } 36 37 void cut() //用直线ax+by+c==0切割多边形 38 { 39 int cutm=0,i; 40 for(i=1;i<=m;i++) 41 { 42 if(a*p[i].x+b*p[i].y+c>=0) //题目是顺时钟给出点的 43 { //所以一个点在直线右边的话,那么带入值就会大于等于0 44 q[++cutm]=p[i]; //说明这个点还在切割后的多边形内,将其保留 45 } 46 else 47 { 48 if(a*p[i-1].x+b*p[i-1].y+c>0) //该点不在多边形内,但是它和它相邻的点构成直线与 49 { //ax+by+c==0所构成的交点可能在新切割出的多边形内, 50 q[++cutm]=intersect(p[i-1],p[i]); //所以保留交点 51 } 52 if(a*p[i+1].x+b*p[i+1].y+c>0) 53 { 54 q[++cutm]=intersect(p[i+1],p[i]); 55 } 56 } 57 } 58 for(i=1;i<=cutm;i++) 59 { 60 p[i]=q[i]; 61 } 62 p[cutm+1]=q[1]; 63 p[0]=q[cutm]; 64 m=cutm; 65 } 66 67 void solve() 68 { 69 int i; 70 for(i=1;i<=n;i++) 71 { 72 p[i]=point[i]; 73 } 74 point[n+1]=point[1]; 75 p[n+1]=p[1]; 76 p[0]=p[n]; 77 m=n; 78 for(i=1;i<=n;i++) 79 { 80 getline(point[i],point[i+1]); //根据point[i]和point[i+1]确定直线ax+by+c==0 81 cut(); //用直线ax+by+c==0切割多边形 82 } 83 } 84 85 int main() 86 { 87 int cas,i; 88 //freopen("D:\\in.txt","r",stdin); 89 scanf("%d",&cas); 90 while(cas--) 91 { 92 scanf("%d",&n); 93 for(i=1;i<=n;i++) 94 { 95 scanf("%lf%lf",&point[i].x,&point[i].y); 96 } 97 solve(); 98 if(m==0) 99 {100 printf("NO\n");101 }102 else103 {104 printf("YES\n");105 }106 }107 return 0;108 }
poj3335 半交平面,多边形内核