半平面交 Half-plane Intersection 模板 nlogn

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, 以上半平面交集不是空集即可.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容