题目:
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 yisequence 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 <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn = 105;
const double eps = 1e-8;
const double PI = acos(-1.0);
const int inf = 0x3f3f3f3f;
//对于浮点数的,><=0的判断。
int sgn(double x)
{
if(fabs(x)<eps) return 0;
if(x<0) return -1;
else return 1;
}
struct Point
{
double x, y;
Point() {}
Point(double _x, double _y)
{
x = _x;
y = _y;
}
Point operator -(const Point &b)const
{
return Point(x-b.x,y-b.y);
}
Point operator +(const Point &b)const
{
return Point(x+b.x,y+b.y);
}
//叉积
double operator ^(const Point &b)const
{
return x*b.y-y*b.x;
}
//点积
double operator *(const Point &b)const
{
return x*b.x+y*b.y;
}
//绕原点旋转角度B(弧度制)后,x,y的变化。
void transXY(double B)
{
double tx = x, ty = y;
x = tx*cos(B)-ty*sin(B);
y = tx*sin(B)+ty*cos(B);
}
};
double dist(Point a, Point b)
{
//勾股定理,使用点积
return sqrt((a-b)*(a-b));
}
struct Line{
Point s,e;
double k;
Line(){}
Line(Point _s, Point _e){
s = _s;
e = _e;
k = atan2(e.y-s.y,e.x-s.x);
}
//两直线求交点
//第一个值为0表示直线重合,为1表示平行,为2表示相交
//只有第一个值为2时,交点才有意义。
pair<int,Point> operator &(const Line &b) const{
Point res = s;
if(sgn((s-e)^(b.s-b.e))==0)//两直线叉积为0
{
if(sgn((s-b.e)^(b.s-b.e))==0)
return make_pair(0,res); //重合
else return make_pair(1,res); //平行
}
double t = ((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));//线段长的比值等于两个面积比值
res.x+=(e.x-s.x)*t;
res.y+=(e.y-s.y)*t;
return make_pair(2,res);
}
};
//半平面交,直线左边区域代表有效区域
bool HPIcmp(Line a, Line b)
{
if(fabs(a.k-b.k)>eps) return a.k<b.k; //如果a和b不共线,那么线段排序是从第三象限逆时针到第二象限
return ((a.s-b.s)^(b.e-b.s))<0; //如果两个向量平行,那么在上面的向量在前面
}
Line line[maxn],Q[maxn];
Point res[maxn];
int n,resn;
void HPI()
{
int tot = n;
sort(line,line+n,HPIcmp);
tot = 1;
for(int i = 1; i < n; i++)
{
if(fabs(line[i].k-line[i-1].k) > eps) //两向量不平行
line[tot++] = line[i];
}
int head = 0, tail = 1;
Q[0] = line[0];
Q[1] = line[1];
resn = 0;
for(int i = 2; i < tot; i++)
{
if(fabs((Q[tail].e-Q[tail].s)^(Q[tail-1].e-Q[tail-1].s))<eps||fabs((Q[head].e-Q[head].s)^(Q[head+1].e-Q[head+1].s))<eps) return ;
while(head < tail&&((((Q[tail]&Q[tail-1]).second-line[i].s)^(line[i].e-line[i].s))>eps)) tail--; //尾部的向量无效
while(head < tail&&((((Q[head]&Q[head+1]).second-line[i].s)^(line[i].e-line[i].s)))>eps) head++; //头部的向量无效
Q[++tail] = line[i];
}
while(head < tail&&((((Q[tail]&Q[tail-1]).second-line[head].s)^(line[head].e-line[head].s)))>eps) tail--; //尾部的向量无效
while(head < tail&&((((Q[head]&Q[head+1]).second-line[tail].s)^(line[tail].e-line[tail].s)))>eps) head++; //头部的向量无效
if(tail <= head+1) return ; //如果只有两个以下的向量,那就没必要在做什么了
for(int i = head; i < tail; i++)
{
res[resn++]=(Q[i]&Q[i+1]).second;
}
if(head<tail-1)
{
res[resn++]=(Q[head]&Q[tail]).second;
}
}
Point p[maxn];
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
scanf("%lf%lf", &p[i].x, &p[i].y);
}
for(int i = 1; i < n; i++)
{
line[i-1] = Line(p[i+1],p[i]);
}
line[n-1] = Line(p[1],p[n]);
resn = 0;
HPI();
if(resn == 0)
printf("NO\n");
else
printf("YES\n");
}
return 0;
}