题意:求多边形的核是否存在。
半平面交模板题。
代码:
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
#define MAXN 110
#define eps 1e-10
struct Point
{
double x, y;
Point(){}
Point(double x, double y):x(x),y(y){}
}p[MAXN], s[MAXN], q[MAXN];
struct Line
{
double a, b, c;
Line(){}
Line(double a, double b, double c):a(a),b(b),c(c){}
};
int n, size, si;
//通过a,b两点得到一条线
Line getline(Point a, Point b)
{
return Line ( b.y-a.y, a.x-b.x, b.x*a.y-a.x*b.y );
}
int dcmp(double x)
{
return (x<-eps)?-1:(x>eps);
}
//初始化
void init()
{
for (int i=1; i<=n; i++)
p[i] = s[i];
p[n+1] = p[1];
p[0] = p[n];
size = n;
}
//线段与直线的交点
Point intersect(Point a, Point b, Line L)
{
double u = fabs( L.a*a.x+L.b*a.y+L.c );
double v = fabs( L.a*b.x+L.b*b.y+L.c );
return Point( (a.x*v+b.x*u)/(u+v), (a.y*v+b.y*u)/(u+v) );
}
//直线L对多边形的每一条边进行切割
void cut(Line L)
{
si = 0;
for (int i=1; i<=size; i++)
{
if ( dcmp(L.a*p[i].x+L.b*p[i].y+L.c) >= 0)
q[++si] = p[i];
else
{
if ( dcmp(L.a*p[i-1].x+L.b*p[i-1].y+L.c) > 0)
q[++si] = intersect(p[i], p[i-1], L);
if ( dcmp(L.a*p[i+1].x+L.b*p[i+1].y+L.c) > 0)
q[++si] = intersect(p[i], p[i+1], L);
}
}
for (int i=1; i<=si; i++)//更新整个多边形的核
p[i] = q[i];
p[si+1] = p[1];
p[0] = p[si];
size = si;
}
void solve()
{
for (int i=1; i<=n; i++)
{
Line L = getline( s[i], s[i+1] );
cut(L);
}
printf("%s\n", size>0?"YES":"NO");
}
int main()
{
int ncase;
scanf("%d", &ncase);
while (ncase--)
{
scanf("%d", &n);
for (int i=1; i<=n; i++)
scanf("%lf%lf", &s[i].x, &s[i].y);
s[n+1] = s[1];
s[0] = s[n];
init();
solve();
}
return 0;
}