【POJ 3130】How I Mathematician Wonder What You Are! ,Japan 2006(半平面交)

本文探讨了如何判断一个多边形内部是否存在一个点C,对于多边形内的任意点P,线段CP都在多边形内部的问题。通过转换为求多边形各边的半平面交来解决,并提供了完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

问给定的多边形中是否存在一点CC,使得对于多边形内任意一点P都有线段CPCP在多边形内。

Solution

发现题目的实质是求该多边形的各边是否存在半平面交,上板子即可。

Source

/****************************
 * Au: Hany01
 * Prob: POJ3130 How I Mathematician Wonder What You Are!
 * Date: Feb 13th, 2018
 * Email: hany01@foxmail.com
****************************/

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<list>
#include<cassert>
#include<map>
#include<queue>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define rep(i , j) for (int i = 0 , i##_end_ = j; i < i##_end_ ; ++ i)
#define For(i , j , k) for (int i = (j) , i##_end_ = (k) ; i <= i##_end_ ; ++ i)
#define Fordown(i , j , k) for (int i = (j) , i##_end_ = (k) ; i >= i##_end_ ; -- i)
#define Set(a , b) memset(a , b , sizeof(a))
#define SZ(a) ((int)(a.size()))
#define ALL(a) a.begin(), a.end()
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define y1 wozenmezhemecaia 
#ifdef hany01
#define debug(...) fprintf(stderr , __VA_ARGS__)
#else
#define debug(...)
#endif

inline void File() {
#ifdef hany01 
    freopen("poj3130.in" , "r" , stdin);
    freopen("poj3130.out" , "w" , stdout);
#endif
}

template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read() {
    register char c_; register int _ , __;
    for (_ = 0 , __ = 1 , c_ = getchar() ; !isdigit(c_) ; c_ = getchar()) if (c_ == '-')  __ = -1;
    for ( ; isdigit(c_) ; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const double eps = 1e-9;

inline int dcmp(double x) { if (fabs(x) < eps) return 0; return x < 0 ? -1 : 1; }

struct Point
{
    double x, y;
    Point(double x = 0, double y = 0): x(x), y(y) {}
};
typedef Point Vector;
Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
Vector operator - (Vector A, Vector 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); }
Vector operator / (Vector A, double p) { return Vector(A.x / p, A.y / p); }
double Cross(Vector A, Vector B) { return A.x * B.y - A.y * B.x; }
double Angle(Vector v) { return atan2(v.y, v.x); }
struct Line
{
    Point P; Vector v; double ang;
    Line(Point P = Point(0, 0), Vector v = Vector(0, 0)): P(P), v(v) { ang = Angle(v); }
};
bool operator < (const Line& A, const Line& B) { return A.ang < B.ang; }

const int maxn = 55;

Point p[maxn];
Line L[maxn];
int n;

inline bool OnLeft(Line L, Point P) { return dcmp(Cross(L.v, P - L.P)) > 0; }

inline Point LineIntersection(Line A, Line B) {
    double t = Cross(B.v, A.P - B.P) / Cross(A.v, B.v);
    return A.P + A.v * t;
}

inline int HalfplaneIntersection(Line* L, int n)
{
    sort(L + 1, L + 1 + n);
    int head, tail;
    static Point p[maxn];
    static Line q[maxn];
    q[head = tail = 1] = L[1];
    For(i, 2, n) {
        while (head < tail && !OnLeft(L[i], p[tail - 1])) -- tail;
        while (head < tail && !OnLeft(L[i], p[head])) ++ head;
        q[++ tail] = L[i];
        if (!dcmp(Cross(q[tail].v, q[tail - 1].v))) {
            -- tail;
            if (OnLeft(q[tail], L[i].P)) q[tail] = L[i];
        }
        if (head < tail) p[tail - 1] = LineIntersection(q[tail - 1], q[tail]);
    }
    while (head < tail && !OnLeft(q[head], p[tail - 1])) -- tail;
    if (head + 1 >= tail) return 0;
    p[tail] = LineIntersection(q[head], q[tail]);
    return 1;
}

int main()
{
    File();
    while (scanf("%d", &n) != EOF && n) {
        For(i, 1, n) p[i].x = read(), p[i].y = read();
        p[n + 1] = p[1];
        For(i, 1, n) L[i] = Line(p[i], p[i + 1] - p[i]);
        if (HalfplaneIntersection(L, n)) puts("1"); else puts("0");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值