问题描述
我想检查一个点是否在带孔的多边形内或外。具体来说,我想知道给定的点是否会在带有孔的多边形的“填充区域”内;如果该点位于一个孔内,我将认为该点在具有孔的多边形之外。
我知道有一个Cgal函数check_inside
可以检查点是否位于多边形内(无孔)。还有一个Cgal函数connect_holes
,该函数绘制从带有孔的多边形中每个孔的最高顶点到多边形本身的路径。我可以看到使用这些函数实现目标的两种解决方法,但是我想知道是否有Cgal函数可以直接完成此任务。
解决方法
请参见CGAL :: General_polygon_set_2 的oriented_side()成员函数。显然,还有一个免费(重载)功能(由于某些原因未记录)。
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Boolean_set_operations_2.h>
typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;
typedef Kernel::Point_2 Point_2;
typedef CGAL::Polygon_2<Kernel> Polygon_2;
typedef CGAL::Polygon_with_holes_2<Kernel> Polygon_with_holes_2;
# define nice(os) ((os == CGAL::ON_ORIENTED_BOUNDARY) ? "on boundary" : \
(os == CGAL::POSITIVE) ? "inside" : "outside")
int main() {
Polygon_2 hole;
hole.push_back(Point_2(1,1));
hole.push_back(Point_2(1,2));
hole.push_back(Point_2(2,1));
Polygon_2 out;
out.push_back(Point_2(0,0));
out.push_back(Point_2(3,3));
out.push_back(Point_2(0,3));
Polygon_with_holes_2 pwh(out,&hole,&hole+1);
std::cout << pwh << std::endl;
auto os = CGAL::oriented_side(Point_2(0,0),pwh);
std::cout << "(0,0) is : " << nice(os) << std::endl;
os = CGAL::oriented_side(Point_2(0.5,0.5),0) is : " << nice(os) << std::endl;
os = CGAL::oriented_side(Point_2(1,1),0) is : " << nice(os) << std::endl;
os = CGAL::oriented_side(Point_2(2.5,2.5),0) is : " << nice(os) << std::endl;
os = CGAL::oriented_side(Point_2(3,3),0) is : " << nice(os) << std::endl;
os = CGAL::oriented_side(Point_2(3.5,3.5),0) is : " << nice(os) << std::endl;
return 0;
}