O(logn)判断点在凸多边形内

学习了优秀的算法。。。大概是找到这个点在哪个三角形块内,所以二分很优秀~

这个里面没有凸包,一般还要加个凸包

来自 http://blog.csdn.net/codeforces_sphinx/article/details/7200301

#include <iostream>
#include <functional>
#include <algorithm>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <utility>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
using namespace std;

#define LOWBIT(x) ( (x) & ( (x) ^ ( (x) - 1 ) ) )
#define CLR(x, k) memset((x), (k), sizeof(x))
#define CPY(t, s) memcpy((t), (s), sizeof(s))
#define SC(t, s) static_cast<t>(s)
#define LEN(s) static_cast<int>( strlen((s)) )
#define SZ(s) static_cast<int>( (s).size() )

typedef double LF;
typedef __int64 LL;        //VC
typedef unsigned __int64 ULL;

typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<double, double> PDD;

typedef vector<int> VI;
typedef vector<char> VC;
typedef vector<double> VF;
typedef vector<string> VS;

template <typename T>
T sqa(const T & x)
{
    return x * x;
}
template <typename T>
T gcd(T a, T b)
{
    if (!a || !b)
    {
        return max(a, b);
    }
    T t;
    while (t = a % b)
    {
        a = b;
        b = t;
    }
    return b;
};

const int INF_INT = 0x3f3f3f3f;
const LL INF_LL = 0x7fffffffffffffffLL;        //15f
const double oo = 10e9;
const double eps = 10e-7;
const double PI = acos(-1.0);

#define  ONLINE_JUDGE

const int MAXN = 100004;

int n, m, k;
struct Point
{
    int x, y;
}pnt[MAXN];
struct Segment
{
    int vx, vy;
    Segment() {}
    Segment(Point po, Point pt)
    {
        vx = pt.x - po.x;
        vy = pt.y - po.y;
    }
    LL operator * (const Segment & rhs) const
    {
        return LL(vx) * LL(rhs.vx) + LL(vy) * LL(rhs.vy);
    }
    LL operator ^ (const Segment & rhs) const
    {
        return LL(vx) * LL(rhs.vy) - LL(vy) * LL(rhs.vx);
    }
};

bool cmpXY(const Point & lhs, const Point & rhs)
{
    return lhs.y != rhs.y ? lhs.y < rhs.y : lhs.x < rhs.x;
}
bool cmpPK(const Point & lhs, const Point & rhs)
{
    return (Segment(pnt[0], lhs) ^ Segment(pnt[0], rhs)) > 0;    //逆时针的极角排序,>改<就是顺时针的了
}
bool inPolygon(const Point & pt)
{
    int low = 1, high = n - 2, mid = (low + high) / 2, res = -1;
    bool sign = false;
    int token;
    LL flag[2];
    while (low <= high)
    {
        mid = (low + high) / 2;
        flag[0] = (Segment(pnt[0], pt) ^ Segment(pnt[0], pnt[mid]));
        if (0 == flag[0])
        {
            sign = true;
            token = mid;
            break ;
        }
        flag[1] = (Segment(pnt[0], pt) ^ Segment(pnt[0], pnt[mid + 1]));
        if (0 == flag[1])
        {
            sign = true;
            token = mid + 1;
            break ;
        }
        //if (flag[0] * flag[1] < 0)    //NX的数据点,在这WA了两次
        if (flag[0] < 0 && flag[1] > 0 || flag[0] > 0 && flag[1] < 0)
        {
            res = mid;
            break ;
        }
        if (flag[0] < 0 && flag[1] < 0)
        {
            low = mid + 1;
        }
        else
        {
            high = mid - 1;
        }
    }
    if (sign)
    {
        if ((Segment(pnt[0], pt) * Segment(pnt[token], pt)) <= 0)
        {
            return true;
        }
        return false;
    }
    if (-1 == res)
    {
        return false;
    }
    return (Segment(pnt[mid], pt) ^ Segment(pnt[mid], pnt[mid + 1])) <= 0;
}
void ace()
{
    Point pt;
    int cnt;
    while (scanf("%d %d %d", &n, &m, &k) != EOF)
    {
        for (int i = 0; i < n; ++i)
        {
            scanf("%d %d", &pnt[i].x, &pnt[i].y);
        }
        sort(pnt, pnt + n, cmpXY);
        sort(pnt + 1, pnt + n, cmpPK);
        pnt[n] = pnt[0];
        cnt = 0;
        while (m--)
        {
            scanf("%d %d", &pt.x, &pt.y);
            if (inPolygon(pt))
            {
                ++cnt;
            }
        }
        puts(cnt >= k ? "YES" : "NO");
    }
    return ;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    ace();
    return 0;
}
/*******************************************************************************
Test Data...
5 4 2
1 -1
1 2
0 4
-1 2
-1 -1
-2 -1
1 -1
0 1
2 3
*******************************************************************************/

时间: 2024-08-05 02:36:19

O(logn)判断点在凸多边形内的相关文章

A Round Peg in a Ground Hole - POJ 1584 (判断凸多边形&amp;判断点在多边形内&amp;判断圆在多边形内)

题目大意:首先给一个圆的半径和圆心,然后给一个多边形的所有点(多边形按照顺时针或者逆时针给的),求,这个多边形是否是凸多边形,如果是凸多边形在判断这个圆是否在这个凸多边形内. 分析:判断凸多边形可以使用相邻的三个点叉积判断,因为不知道顺时针还是逆时针,所以叉积如果有有整数和负数,那么一定不是凸多边形(注意允许多多点在一条线段上).判断圆在凸多边形首先要判断圆心是否在多边形内,如果在多边形内,再次判断圆心到达到变形每条边的最短距离,如果小于半径就是不合法.ps:一道好题,通过这个题学会了不少东西.

poj 1584 A Round Peg in a Ground Hole 判断多边形是否为凸多边形 + 圆心是否在凸多边形内 + 圆是否在凸多边形内部

题目来源: http://poj.org/problem?id=1584 题意: 给一个多边形, 一个圆心以及半径. 首先判断是否为凸多边形. 如果是凸多边形, 再判断,圆是否在凸多边形内部. 分析: 1) 先判断是否为凸多边形 ,题目给出的顶点是有序的, 即顺时针或是 逆时针.用叉积方向判断. 2) 判断圆在多边形内, 首先判断 圆心是否在多边形内部, 是的话,然后再 判断 圆心到多边形 所有边的 距离d >= r , 即可. 代码如下: const int Max_N = 1005; con

判断点是否在凸多边形内

判断点是否在凸多边形内的方法很多,此处仅给出使用向量叉积判断点是否在凸多边形内的方法. 以下图为例说明问题: 原则: 1. 将多边形的第i条边的第一个顶点指向点P得到向量 v1,然后将从第一个顶点指向第二个顶点得到向量v2,叉乘这两个向量. 2.如果叉乘结果与上一条边的叉乘结果的乘积大于0则继续执行,如果乘积小于0,表示点P不在凸多边形内,直接返回即可. 要点:要求凸多边形的点以固定的顺序给出,例如固定为逆时针或顺时针. 实现的代码如下: struct Point { float x; floa

利用window.navigator.userAgent判断当前是否微信内置浏览器

<!DOCTYPE HTML> <html lang="en"> <head> <meta charset="utf-8"/> <title>判断是否是微信内置浏览器</title> </head> <body> <h1>如果用微信浏览器打开可以看到下面的文字</h1> <p></p> </body> </

canvas路径剪切和判断是否在路径内

1.剪切路径 clip() var ctx=mycanvas.getContext('2d'); ctx.beginPath(); // 建一个矩形路径 ctx.moveTo(20,10) ctx.lineTo(100,10) ctx.lineTo(100,100) ctx.lineTo(20,100) ctx.closePath(); // 剪切 ctx.clip(); ctx.fillRect(0,0,50,50); 这句话的作用是,在这个画布上绘图时,只在clip()的这块路径区域内才st

POJ 1410 Intersection(线段相交&amp;&amp;判断点在矩形内&amp;&amp;坑爹)

Intersection 大意:给你一条线段,给你一个矩形,问是否相交. 相交:线段完全在矩形内部算相交:线段与矩形任意一条边不规范相交算相交. 思路:知道具体的相交规则之后题其实是不难的,但是还有个坑点就是题目里明明说给的是矩形左上角跟右下角的点,但实际上不是,需要重新判断一下...真坑. 1 struct Point 2 { 3 double x, y; 4 } A, B, C, D; 5 struct Line 6 { 7 Point a, b; 8 } L; 9 10 int n; 11

ZOJ 1081 Points Within | 判断点在多边形内

题目: 给个n个点的多边形,n个点按顺序给出,给个点m,判断m在不在多边形内部 题解: 网上有两种方法,这里写一种:射线法 大体的思想是:以这个点为端点,做一条平行与x轴的射线(代码中射线指向x轴正方向) 如果交点个数为奇数的话就在内部,如果为偶数(包括0)就在外部 #include<cstdio> #include<algorithm> #include<cstring> #define N 105 using namespace std; int n,m; stru

logN判点是否在凸多边形内 HRBUSTOJ1429

就是利用叉积的性质,如果向量A1到向量A2是顺时针则叉积为负反之为正. 然后我们可以二分的判断找到一个点恰被两条射线夹在一起. 然后我们再判断是否l,r这两个点所连直线与点的关系. 具体资料可以参照这个BLOG https://www.cnblogs.com/yym2013/p/3673616.html 代码 By:大奕哥 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=100005; 4 int x[N],y[N]

【原创】判断点在多边形内

一.应用场景:地图应用中判断一个位置是否在一个区域内.我曾经应用在百度地图上,代码为js实现.据我了解,目前百度地图api已经提供该功能. 二.概要: 1.行政区划边界是多边形: 2.多边形分为凸多边形和凹多边形: 3.应用:产生随机数据(即一个平面坐标)在制定的行政区划边界以内(即多边形内),在正式情况下不需要从图形的层面处理数据,数据本身就有在那个区划下的属性. 三.假定: 1.多边形的点都是不重合的点: 2.给定的多边形边界坐标集就是时针顺序的,即要么符合逆时针顺序要么符合顺时针顺序,不存