我尝试了所有的可能性。但是我没有在线和多边形之间的交点。
Paths clip(1), soln , pol(1);
clip[0] << IntPoint(1,1) << IntPoint(30,30) ;
pol[0] << IntPoint(10,10) << IntPoint(20, 10) << IntPoint(20,20) <<
IntPoint(10, 20) << IntPoint(10, 10);
Path line= clip[0];
Path poly = pol[0];
Clipper c;
c.AddPath(line, ptSubject, true);
c.AddPath(poly, ptClip, true);
c.Execute(ctIntersection, soln, pftNonZero, pftNonZero);
std::cout << soln.size() ;
最佳答案
您使用了错误的
Execute
覆盖对于开放路径交叉点。当主题是一行时,解决方案需要是PolyTree
而不是Paths
。... when open paths are passed to a Clipper object, the user must use a PolyTree object as the solution parameter, otherwise an exception will be raised. [src]
您应该使用
AddPath
的第三个参数打开而不是关闭的行主题.The function will return false if the path is invalid for clipping. A path is invalid for clipping when it has 2 vertices but is not an open path. [src]
所以修改如下:
c.AddPath(line, ptSubject, false); // a line is open
c.AddPath(poly, ptClip, true); // a polygon is closed
PolyTree soln; // the solution is a tree
c.Execute(ctIntersection, soln);
关于c++ - 使用剪刀库的线和多边形之间的交点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58335363/