使用剪刀库的线和多边形之间的交点

Intersection between line and Polygon using clipper library

我尝试了所有的可能性。但是我没有在线和多边形之间的交点。

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() ;
  1. 您对开放路径交叉点使用了错误的 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]

  2. 你应该用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);