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

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

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

代码:
 
#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 second
void 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值