我想用boost::geometry确定点是否在多边形内。
我使用函数boost::geometry::within和 typeboost::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);
上面的代码执行后ret1,ret2都是true. 但我会的ret1 != ret2。
一般来说,我需要在ret1 != ret2任何时候获得功能testPoint(当点恰好在边界上或多边形退化等时,我不考虑边界情况......)
我尝试了不同的策略来传递给boost::geometry::within,但我没有得到我需要的东西。
似乎我需要或类似的功能在 中的某处实现boost::geometry,因为inside的文档具有带孔的多边形示例。但我还没有意识到如何将它用于我的案例。
还有一个非常简单的解决方法。我只需要编写一个代码来确定轮廓的方向。然后我只是within根据轮廓方向否定或不否定函数的结果。但是如果boost::geometry已经实现了,我不想复制它。