半平面交 Half-plane Intersection 模板

2017-07-06  本文已影响0人  evilgiven

reference: 半平面交的新算法及其实用价值 - 朱泽园


poj3335

Rotating Scoreboard

Time Limit: 2000MS Memory Limit: 65536K

Description

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

Source

Tehran 2006 Preliminary

按顺时针方向依次给出多边形上的点(边), 求多边形的核

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

const double eps = 1e-8;
int tol, pn, dq[20005], top, bot;

struct Point{
    double x, y;

    Point(double x = 0.0, double y = 0.0) : x(x), y(y) {}
};

struct Line{
    Point a, b;
    double angle;

    Line& operator = (const Line& other){
        this->a.x   = other.a.x, this->a.y   = other.a.y;
        this->b.x   = other.b.x, this->b.y   = other.b.y;
        this->angle = other.angle;
        return *this;
    }
};

Point p[20005];
Line  l[20005];

inline int dbcmp(double k){
    return fabs(k) < eps ? 0 : k > 0 ? 1 : -1;
}

inline double multi(Point O, Point A, Point B){
    return (A.x - O.x) * (B.y - O.y) - (A.y - O.y) * (B.x - O.x);
}

inline bool cmp(const Line& l1, const Line& l2){
    int d = dbcmp(l1.angle - l2.angle);
    return d ? d < 0 : dbcmp(multi(l1.a, l2.a, l2.b)) > 0;
}

void addLine(Line& l, double x1, double y1, double x2, double y2){
    l.a.x = x1, l.a.y = y1;
    l.b.x = x2, l.b.y = y2;
    l.angle = atan2(y2 - y1, x2 - x1);
}

Point getIntersect(Line l1, Line l2){
    double A1 = l1.b.y - l1.a.y;
    double B1 = l1.a.x - l1.b.x;
    double C1 = (l1.b.x - l1.a.x) * l1.a.y - (l1.b.y - l1.a.y) * l1.a.x;
    double A2 = l2.b.y - l2.a.y;
    double B2 = l2.a.x - l2.b.x;
    double C2 = (l2.b.x - l2.a.x) * l2.a.y - (l2.b.y - l2.a.y) * l2.a.x;
    Point p((C2 * B1 - C1 * B2) / (A1 * B2 - A2 * B1), (C1 * A2 - C2 * A1) / (A1 * B2 - A2 * B1));
    return p;
}

bool judge(Line l0, Line l1, Line l2){
     Point p = getIntersect(l1, l2);
     return dbcmp(multi(p, l0.a, l0.b)) < 0;
}

void HalfPlaneIntersect(){
    int i, j;

    sort(l, l + tol, cmp);
    for(i = 0, j = 0; i < tol; i++) if(dbcmp(l[i].angle - l[j].angle) > 0) l[++j] = l[i];
    tol = j + 1, dq[0] = 0, dq[1] = 1, top = 1, bot = 0;
    for(i = 2; i < tol; i++){
        while(top > bot && judge(l[i], l[dq[top]], l[dq[top - 1]])) top--;
        while(top > bot && judge(l[i], l[dq[bot]], l[dq[bot + 1]])) bot++;
        dq[++top] = i;
    }
    while(top > bot && judge(l[dq[bot]], l[dq[top]], l[dq[top - 1]])) top--;
    while(top > bot && judge(l[dq[top]], l[dq[bot]], l[dq[bot + 1]])) bot++;
    dq[++top] = dq[bot];
    for(pn = 0, i = bot; i < top; i++, pn++) p[pn] = getIntersect(l[dq[i + 1]], l[dq[i]]);
}


double getArea(){
    if(pn < 3) return 0;
    double area = 0;
    for(int i = 1; i < pn - 1; i++) area += multi(p[0], p[i], p[i + 1]);
    if(area < 0) area = -area;
    return area / 2;
}

int main()
{
    int T, n, x0, y0, x1, y1, x2, y2;

    scanf("%d", &T);
    for(int cas = 1; cas <= T; cas++){
        tol = 0;
        scanf("%d", &n);
        scanf("%d %d", &x1, &y1);
        x0 = x1, y0 = y1;
        for(int i = 1; i < n; i++){
            scanf("%d %d", &x2, &y2);
            addLine(l[tol++], x2, y2, x1, y1);
            x1 = x2, y1 = y2;
        }
        addLine(l[tol++], x0, y0, x1, y1);
        HalfPlaneIntersect();
        printf("%s\n", pn < 3 ? "NO" : "YES");
    }
    return 0;
}

addLine(l[tol++], x1, y1, x2, y2) 为例, 半平面取点 (x1, y1) 指向 (x2, y2) 的向量的左边.
HalfPlaneIntersect() 之后再 getArea() 可取得给出的 tol 个半平面相交的面积, 本题只需要判断多边形的核因此只需要判断 pn 大于等于3, 以上半平面交集不是空集即可.

上一篇下一篇

猜你喜欢

热点阅读