poj3335——半平面交模板(方向不定)

题目链接:http://poj.org/problem?id=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

题目翻译:

今年,ACM/ICPC世界总决赛将以简单的多边形形式在大厅举行。教练和观众坐在多边形的边缘。我们希望在大厅的某处放置一个旋转记分牌,以便坐在大厅边界的任何地方的观众都能查看记分牌(即,他的视线不会被墙挡住)。请注意,如果旁观者的视线与面边界相切(在顶点或边中),他仍然可以查看记分牌。您可以将观众的座位视为沿简单多边形边界的点,并将记分牌也视为一个点。程序被赋予大厅的角(多边形的顶点),并且必须检查记分板(多边形内的点)是否有位置,以便记分板可以从多边形边缘的任何点查看。‎

‎输入‎

‎输入行中的第一个数字‎‎T‎‎是测试用例的数量。每个测试用例在一行输入上指定,格式‎‎为 n‎‎ ‎‎x‎‎1‎‎ ‎‎1‎‎ ‎‎x‎‎2‎‎ ‎‎y‎‎2‎‎ ...‎‎x‎‎n‎‎ ‎‎y‎‎ ‎‎ n ‎‎n‎‎ (3 = ‎‎n‎‎ = 100) 是多边形中的顶点数,而整数对‎‎x‎‎i‎‎ ‎‎ y ‎‎i‎‎序列指定按顺序排序的多边形的顶点。‎

‎输出‎

‎输出包含‎‎T‎‎行,每条线对应于按该顺序排列的输入测试用例。输出行包含"是"或"否",具体取决于记分牌是否可以放置在符合问题条件的大厅内。

 

题意理解:

给你n个点组成的凸多边形,判断是否存在能看到凸多边形所有区域的小凸多边形。

经典半平面交求核例题,几乎可以当模板用了。

关于半平面交的知识就不多说了。(抛一个讲的不错的链接:【详解】半平面交算法入门详解(计算几何)

这个题还有一个注意点,坐标不确定是逆时针还是顺时针读入的,所以需要对输入数据存储后,用叉乘算出凸多边形面积后判断是顺序还是逆序输入。

还有一个坑点(有的模板会出现):判断点是否在有向直线的左边。

//点p在有向直线L的左侧
bool OnLeft(Line L, Point p){
    return Cross(L.v, p - L.p) > 0;
}
要改为
bool OnLeft(Line L, Point p){
    return Cross(L.v, p - L.p) >= 0;
}
#include<iostream>
#include<cmath> 
#include<algorithm>
using namespace std;
const double eps = 1e-8;
const int inf=0x3f3f3f3f;
struct Point{
    double x, y;
    Point(double x = 0, double y = 0):x(x),y(y){}
}p[1005];
int n;
typedef Point Vector;
Vector operator + (Vector A, Vector B){
    return Vector(A.x+B.x, A.y+B.y);
}
Vector operator - (Point A, Point B){
    return Vector(A.x-B.x, A.y-B.y);
}
Vector operator * (Vector A, double p){
    return Vector(A.x*p, A.y*p);
}
int sgn(double x){
    if(fabs(x) < eps)
        return 0;
    if(x < 0)
        return -1;
    return 1;
}
double Dot(Vector A, Vector B){
    return A.x*B.x + A.y*B.y;
}
double Cross(Vector A, Vector B){
    return A.x*B.y-A.y*B.x;
}
double Length(Vector A){
    return sqrt(Dot(A, A));
}
Vector Normal(Vector A){//向量A左转90°的单位法向量
    double L = Length(A);
    return Vector(-A.y/L, A.x/L);
}
struct Line{
    Point p;//直线上任意一点
    Vector v;//方向向量,它的左边就是对应的半平面
    double ang;//极角,即从x轴正半轴旋转到向量v所需要的角(弧度)
    Line(){}
    Line(Point p, Vector v) : p(p), v(v){
        ang = atan2(v.y, v.x);
    }
    bool operator < (const Line& L) const {//排序用的比较运算符
        return ang < L.ang;
    }
}l[1005];
//点p在有向直线L的左侧
bool OnLeft(Line L, Point p){
    return Cross(L.v, p - L.p) >= 0;
}
//两直线交点。假定交点唯一存在
Point GetIntersection(Line a, Line b){
    Vector u = a.p - b.p;
    double t = Cross(b.v, u)/Cross(a.v, b.v);
    return a.p + a.v*t;
}
//半平面交的主过程
int HalfplaneIntersection(Line* L, int n, Point* poly){
    sort(L, L + n);//按照极角排序
    int fst = 0, lst = 0;//双端队列的第一个元素和最后一个元素
    Point *P = new Point[n];//p[i] 为 q[i]与q[i + 1]的交点
    Line *q = new Line[n];//双端队列
    q[fst = lst = 0] = L[0];//初始化为只有一个半平面L[0]
    for(int i = 1; i < n; ++i){
        while(fst < lst && !OnLeft(L[i], P[lst - 1])) --lst;
        while(fst < lst && !OnLeft(L[i], P[fst])) ++fst;
        q[++lst] = L[i];
        if(sgn(Cross(q[lst].v, q[lst - 1].v)) == 0){
            //两向量平行且同向,取内侧一个
            --lst;
            if(OnLeft(q[lst], L[i].p)) q[lst] = L[i];
        }
        if(fst < lst)
            P[lst - 1] = GetIntersection(q[lst - 1], q[lst]);
    }
    while(fst < lst && !OnLeft(q[fst], P[lst - 1])) --lst;
    //删除无用平面
    if(lst - fst <= 1) return 0;//空集
    P[lst] = GetIntersection(q[lst], q[fst]);//计算首尾两个半平面的交点
    //从deque复制到输出中
    int m = 0;
    for(int i = fst; i <= lst; ++i) poly[m++] = P[i];
    return m;
}
void init(Point *p, int n) {
	double s = 0;
	for(int i = 0; i < n; i++)
		s += Cross((p[(i+1)%n]-p[i]),(p[(i+2)%n]-p[(i+1)%n]));
	if(sgn(s) > 0) {
		for(int i = 0; i < n/2; i++)
			swap(p[i], p[n-i-1]);
	}
}
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		scanf("%d",&n);
		for(int i=0;i<n;i++)
			scanf("%lf%lf",&p[i].x,&p[i].y);
		init(p,n);
		l[0]=Line(p[0],p[n-1]-p[0]);
		for(int i=1;i<n;i++)
			l[i]=Line(p[i],p[i-1]-p[i]);
		int res=HalfplaneIntersection(l,n,p);
		if(!res) printf("NO\n");
		else printf("YES\n");
	}
	return 0;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值