HDU 1756 (计算几何 点在多边形内)

多边形可能是凹的所以不能用to_left_tset测试。

直接用板子。

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
#define maxn 111

struct point {
    double x, y;
    point (double _x = 0, double _y = 0) : x(_x), y(_y) {}
    point operator - (point a) const {
        return point (x-a.x, y-a.y);
    }
    bool operator < (const point &a) const {
        return x < a.x || (x == a.x && y < a.y);
    }
}p[maxn];

const double eps = 1e-10;
int dcmp (double x) {
    if (fabs (x) < eps)
        return 0;
    else return x < 0 ? -1 : 1;
}
double cross (point a, point b) {
    return a.x*b.y-a.y*b.x;
}
double dot (point a, point b) {
    return a.x*b.x + a.y*b.y;
}
bool PointOnLine (point a1, point a2, point p) {//判断点p是不是在线段a1a2上
    if (dcmp (cross (a1-p, a2-p)) == 0 && dcmp (dot (a1-p, a2-p)) <= 0)
        return 1;
    return 0;
}

int n, m;

bool PointInPolygon (point a, point *b) {
    int w = 0;
    for (int i = 0; i < n; i++) {
        if (PointOnLine (b[i], b[(i+1)%n], a))
            return 0;
        int k = dcmp (cross (b[(i+1)%n]-b[i], a-b[i]));
        int d1 = dcmp (b[i].y - a.y);
        int d2 = dcmp (b[(i+1)%n].y - a.y);
        if (k > 0 && d1 <= 0 && d2 > 0)
            w++;
        if (k < 0 && d2 <= 0 && d1 > 0)
            w--;
    }
    if (w != 0)
        return 1;
    return 0;
}

int main () {
    //freopen ("in", "r", stdin);
    ios::sync_with_stdio(0);
    while (cin >> n) {
        for (int i = 0; i < n; i++) {
            cin >> p[i].x >> p[i].y;
        }
        cin >> m;
        while (m--) {
            point tmp;
            cin >> tmp.x >> tmp.y;
            if (PointInPolygon (tmp, p)) {
                cout << "Yes" << endl;
            }
            else cout << "No" << endl;
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值