c++ - 确定点是否在 boost::geometry::linear_ring 会计方向内

标签 c++ boost boost-geometry

我想用 boost::geometry 确定点是否在多边形内.

我使用函数 boost::geometry::within然后输入 boost::geometry::linear_ring<boost::geometry::point_2d>指定轮廓。

如果我不需要考虑轮廓的方向,一切都很好。
但就我而言,我想考虑方向。我的意思是,如果特定轮廓的内部区域被认为受其边界限制并且是有限的,那么倒置轮廓的内部区域应该是无限的——与初始轮廓的区域互补。

是否可以在 within 中考虑轮廓的方向功能?

可以用下面的代码来表达:

// Create contour which defines square
boost::geometry::linear_ring<boost::geometry::point_2d> contour;
contour.push_back(boost::geometry::point_2d(4, 2));
contour.push_back(boost::geometry::point_2d(2, 2));
contour.push_back(boost::geometry::point_2d(2, 4));
contour.push_back(boost::geometry::point_2d(4, 4));
contour.push_back(boost::geometry::point_2d(4, 2));

// Create contour which defines square with opposite direction.
boost::geometry::linear_ring<boost::geometry::point_2d> contourInverted = contour;
std::reverse(contourInverted.begin(), contourInverted.end());

// Specify point inside square
boost::geometry::point_2d testPoint(3, 3);

// Perform tests
bool ret1 = boost::geometry::within(testPoint, contour);
bool ret2 = boost::geometry::within(testPoint, contourInverted);

执行上述代码后ret1ret2都是true .但我会 ret1 != ret2 .

通常我需要在 ret1 != ret2 时获得功能对于任何 testPoint (当点正好在边界上或多边形退化等时,我在这里不考虑边界情况......)

我试过了 different strategies传递给 boost::geometry::within , 但我还没有得到我需要的东西。

似乎我需要或类似的功能在 boost::geometry 的某处实现了,因为 documetation for within有带孔的多边形示例。但我还没有意识到如何将它用于我的案例。

还有一个非常简单的解决方法。我只需要编写一个代码来确定轮廓的方向。然后我就否定或不否定 within 的结果功能取决于轮廓方向。但是如果 boost::geometry已经实现了,我不想重复它。

最佳答案

据我所知,Boost.Geometry 和 Boost.Polygon 都不能处理您定义的“无限”区域。他们确实使用带孔的多边形甚至一组这样的多边形。

您可以考虑添加一个大矩形来限制您的宇宙。然后您可以将倒置轮廓定义为此类矩形中的孔。

顺便说一句,在许多情况下,可以避免将框转换为轮廓。 Boost.Geometry 提供了一个适配器“box_view”,它允许像使用(正向)轮廓一样使用框。

至于任意轮廓的方向,可能最简单的方法是计算它的面积。另一方面,对于结构良好的环,方向在编译时已知,并由元函数 traits::point_order 提供,请参阅详细信息 here

关于c++ - 确定点是否在 boost::geometry::linear_ring 会计方向内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20013289/

相关文章:

C++ 从非右值对象访问右值引用对象

c++ - 声明 unsigned int 和数组(将具有不同大小)的 unordered_map?

C++ boost 图 : How to customize callback function vf2_print_callback in vf2_subgrap_iso. hpp

c++ - 通过哈希值和谓词搜索 std::unordered_set

c++ - 处理套接字断开连接。 boost/Winsock

c++ - boost.Geometry 中面积的三角测量

c++ - GetModuleHandle 在 Visual C++ 中如何工作

c++ - boost::geometry: 使用圆的最近邻

c++ - Boost.几何 : How to create simple array of polygons and save tham as svg image?

c++ - 如何正确替换全局new和delete运算符