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

标签 c++ clipperlib

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

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);

关于c++ - 使用剪刀库的线和多边形之间的交点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58335363/

相关文章:

c++ - 如何解决c++中mysql_init期间的mysql错误 "insufficient memory"?

c++ - 为什么 Vector3D 类中的同一个操作有两种不同类型的运算符重载?

c# - 我的代码有什么问题 :"clipping polygon(ellipse points) with line segments(subject) with clipper library in c#"

javascript - 使线条跟随形状的轮廓

python - pyclipper : Crash on trivial case ("terminate called throwing an exception")

C++ 比较两个具有相同持续时间的 std::chrono::time_points 失败

c++ - GCC 不喜欢带有空格的 C++ 风格转换

c++ - 两步到一步初始化和错误处理

c++ - 将多边形坐标从 Double 转换为 Long 以用于 Clipper 库

c++ - 内存地址输出而不是值