Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 4370 | Accepted: 1728 |
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 integersxi 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
2 4 0 0 0 1 1 1 1 0 8 0 0 0 2 1 2 1 1 2 1 2 2 3 2 3 0
Sample Output
YES NO
Source
#include<stdio.h>
#include<math.h>
#define eps 1e-8
struct point
{
double x,y;
} point[103],temp[103],tp[103];
int n,m,t;
double a,b,c;
void getline(struct point p1,struct point p2)
{
a=p1.y-p2.y;
b=p2.x-p1.x;
c=p1.x*p2.y-p2.x*p1.y;
}
void getinter(struct point p1,struct point p2)
{
double a2=p2.y-p1.y;
double b2=p1.x-p2.x;
double c2=p2.x*p1.y-p1.x*p2.y;
double tmd=a*b2-a2*b;
temp[102].x=(b*c2-b2*c)/tmd;
temp[102].y=(a2*c-a*c2)/tmd;
}
void make_clockwise()
{
int i;
double area=0;
for(i=2;i<=n;i++)
area+=(point[i].x-point[1].x)*(point[i+1].y-point[1].y)-(point[i].y-point[1].y)*(point[i+1].x-point[1].x);
if(area>eps)
{
//printf("cc\n");
for(i=0;i<=n+1-i;i++)
{
point[102]=point[i];
point[i]=point[n+1-i];
point[n+1-i]=point[102];
}
}
}
void input()
{
int i;
scanf("%d",&n);
for(i=1; i<=n; i++) scanf("%lf%lf",&point[i].x,&point[i].y);
point[0]=point[n];
point[n+1]=point[1];
make_clockwise();
for(i=0; i<=n+1; i++) temp[i]=point[i];
m=n;
}
void cut()
{
int cou=0,i;
for(i=1;i<=m;i++)
{
if(a*temp[i].x+b*temp[i].y+c<=0) tp[++cou]=temp[i];
else
{
if(a*temp[i-1].x+b*temp[i-1].y+c<0)
{
getinter(temp[i-1],temp[i]);
tp[++cou]=temp[102];
}
if(a*temp[i+1].x+b*temp[i+1].y+c<0)
{
getinter(temp[i+1],temp[i]);
tp[++cou]=temp[102];
}
}
}
tp[0]=tp[cou];
tp[cou+1]=tp[1];
for(i=0;i<=cou+1;i++) temp[i]=tp[i];
m=cou;
//printf(" %d\n",cou);
}
void solve()
{
int i;
for(i=1;i<=n;i++)
{
getline(point[i],point[i+1]);
cut();
}
if(m>0) printf("YES\n");
else printf("NO\n");
}
int main()
{
//freopen("t.txt","r",stdin);
scanf("%d",&t);
while(t--)
{
input();
solve();
}
return 0;
}