c++ - 如何确定 (x, y) 点是否在由边界点列表定义的多边形内

标签 c++ boost geometry computational-geometry boost-geometry

我有一大堆点,它们定义了一些(不一定是凸的)形状的边界。然后我有一些查询点 (x, y) 并且我想确定 (x, y) 是否在我的点边界定义的区域内。

所以,很简单的问题。如何确定查询点是否在我的边界点形成的形状内?是否有一个很好的 boost 模块?我正在查看 boost::geometry 但还没有找到任何东西..

最佳答案

您似乎在寻找 within,不是吗?

http://www.boost.org/libs/geometry/doc/html/geometry/reference/algorithms/within/within_2.html

他们在页面上给出的示例实际上是多边形中的点:

#include <iostream>
#include <list>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>

#include <boost/geometry/domains/gis/io/wkt/wkt.hpp>


int main()
{
    typedef boost::geometry::model::d2::point_xy<double> point_type;
    typedef boost::geometry::model::polygon<point_type> polygon_type;

    polygon_type poly;
    boost::geometry::read_wkt(
        "POLYGON((2 1.3,2.4 1.7,2.8 1.8,3.4 1.2,3.7 1.6,3.4 2,4.1 3,5.3 2.6,5.4 1.2,4.9 0.8,2.9 0.7,2 1.3)"
            "(4.0 2.0, 4.2 1.4, 4.8 1.9, 4.4 2.2, 4.0 2.0))", poly);

    point_type p(4, 1);

    std::cout << "within: " << (boost::geometry::within(p, poly) ? "yes" : "no") << std::endl;

    return 0;
}

更新:正如@ildjarn 指出的那样,如果您希望位于多边形边缘本身的点可以计数,您可能更愿意使用covered_by:

http://www.boost.org/libs/geometry/doc/html/geometry/reference/algorithms/covered_by/covered_by_2.html

within 的行为 w.r.t.边缘“取决于”,因此请注意文档中的细微差别。

关于c++ - 如何确定 (x, y) 点是否在由边界点列表定义的多边形内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10639540/

相关文章:

c++ - 如何在 Solaris 8 SPARC 上启用 _GLIBCXX_USE_C99?

c++ - 使用用户定义的构造函数初始化 C++ 结构

c++ std::string name 和 std::string &name 之间的区别

c++ - 如何有效地将按位运算应用于(大)压缩位 vector ?

python - 在给定索引和半径的 numpy 数组中绘制一个圆,无需外部库

java - 检测图中的所有圆圈

algorithm - 将平行线与正交二维网格相交

c++ - 更多关于中介者模式和 OO 设计

c++ - 如何使用 z3 中的 arg() 函数?

c++ - 寻求内存泄漏方面的帮助 - 拥有多线程队列、字符缓冲区和结构