我有一大堆点,它们定义了一些(不一定是凸的)形状的边界。然后我有一些查询点 (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
:
within
的行为 w.r.t.边缘“取决于”,因此请注意文档中的细微差别。
关于c++ - 如何确定 (x, y) 点是否在由边界点列表定义的多边形内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10639540/