计算几何中半平面交的一个重要用法既是确定多边形的核。
平面简单多边形的核是该多边形内部的一个点集,该点集中任意一点与多边形边界上一点的连线都处于这个多边形内部。就是一个在一个房子里面放一个摄像 头,能将所有的地方监视到的放摄像头的地点的集合即为多边形的核。
原多边形称为vera,切割后的多变形称为verb。
顺序给出一个多边形各个顶点的点集, 首先另verb=vera。
我们从第一个点开始,选取两个连续的点(这两个点都是vera上的点)做一条直线ax+by+c=0切分多边形verb,如是将verb切成两半,我们选取能使的ax+by+c>=0的那一半而舍弃掉另一半,从而构成一个新的verb,再继续切分,切完一轮,若verb任然存在,即verb是vera的核。
代码如下:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
struct Point
{
double x, y;
}point[150], temp[150], temp_temp[150];
int num_temp, cut_num;
int n;
double a, b, c;
void getline(Point p2, Point p1)//可以轻松证明出该结论。
{
a = p1.y - p2.y;
b = p2.x - p1.x;
c = p2.y*p1.x - p1.y*p2.x;
}
Point intersect(Point x, Point y) //获取直线ax+by+c==0 和点x和y所连直线的交点
{
double u = fabs(a*x.x + b*x.y + c);
double v = fabs(a*y.x + b*y.y + c);
Point ans;
ans.x = (x.x*v + y.x*u) / (u + v);
ans.y = (x.y*v + y.y*u) / (u + v);
return ans;
}
void cut()
{
cut_num = 0;
for (int i = 1; i <= num_temp; i++)
{
Point p = temp[i];
Point p1 = temp[i - 1];
Point p2 = temp[i + 1];
if (a*p.x + b*p.y + c >= 0)
{
temp_temp[++cut_num] = p;
}
else
{
if (a*p1.x + b*p1.y + c > 0)
{
temp_temp[++cut_num] = intersect(temp[i - 1], temp[i]);
}
if (a*p2.x + b*p2.y + c>0)
{
temp_temp[++cut_num] = intersect(temp[i + 1], temp[i]);
}
}
}
for (int i = 1; i <= cut_num; i++)
{
temp[i] = temp_temp[i];
}
temp[cut_num+1] = temp_temp[1];
temp[0] = temp_temp[cut_num];
num_temp = cut_num;
}
bool solve()
{
for (int i = 0; i <= n + 1; i++)
{
temp[i] = point[i];
}
num_temp = n;
for (int i = 1; i <= n; i++)
{
getline(point[i], point[i + 1]);
cut();
}
if (num_temp == 0)
return false;
return true;
}
int main()
{
int t;
#ifdef glx
freopen("in.txt", "r", stdin);
#endif
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%lf%lf", &point[i].x, &point[i].y);
}
point[0] = point[n];
point[n + 1] = point[1];
if (solve())
printf("YES\n");
else
printf("NO\n");
//cout << num_temp << endl;
}
return 0;
}