POJ 3335 (计算几何+半平面交)

来源:互联网 发布:java多用户商城源码 编辑:程序博客网 时间:2024/05/17 02:18

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 formn 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
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

题意:简单的判断多边形有木有核

代码:
 
#include<cstdio>#include<iostream>#include<cstdlib>#include<cstring>#include<algorithm>#include<functional>#include<vector>#include <map>#include<cmath>using namespace std;typedef long long LL;const LL MOD=1e9+7;const double eps=1e-9;const int MAXN=100+23;#define point pair<double,double>#define x first#define y secondvoid getline(double &a,double &b,double &c,point x1,point x2){    a=(x1.y-x2.y);    b=(x2.x-x1.x);    c=-a*x1.x-b*x1.y;}point intersect(double a,double b,double c,point x1,point x2){    double u = fabs(a * x1.x + b * x1.y + c);    double v = fabs(a * x2.x + b * x2.y + c);    return point( (x1.x * v + x2.x * u) / (u + v) , (x1.y * v + x2.y * u) / (u + v) );}int n,last,now;point p1[MAXN],p2[MAXN],p3[MAXN];void cut(double a,double b,double c){    now=0;    for(int i=1;i<=last;i++)    {        if(a*p2[i].x+b*p2[i].y+c<=0)        {            p3[++now]=p2[i];        }        else        {            if(a*p2[i-1].x+b*p2[i-1].y+c<=0)            {                p3[++now]=intersect(a,b,c,p2[i],p2[i-1]);            }            if(a*p2[i+1].x+b*p2[i+1].y+c<=0)            {                p3[++now]=intersect(a,b,c,p2[i],p2[i+1]);            }        }    }    //printf("now=%d\n",last);    last=now;    for(int i=1;i<=last;i++)p2[i]=p3[i];    p2[last+1]=p3[1];p2[0]=p3[last];}int main(){    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        for(int i=1;i<=n;i++)scanf("%lf%lf",&p1[i].x,&p1[i].y),p2[i]=p1[i];        p2[n+1]=p1[1];p2[0]=p1[n];        p1[n+1]=p1[1];        last=n;        now=0;        for(int i=1;i<=n;i++)        {            double a,b,c;            getline(a,b,c,p1[i],p1[i+1]);            //printf("%f %f %f\n",a,b,c);            cut(a,b,c);        }        if(last>0)puts("YES");        else puts("NO");    }    return 0;}
本人蒟蒻,如有错误,还望指正




阅读全文
0 0
原创粉丝点击