我正在尝试使用 Clipper C++ 库来实现 is_bordering
功能,如下图。
bool is_bordering(Path p1, Path p2) {
Paths solutions;
Clipper c;
// execute intersection on paths
c.AddPath(p1, ptSubject, true);
c.AddPath(p2, ptClip, true);
c.Execute(ctIntersection, solutions, pftNonZero);
return (solutions.size() > 0); // the paths share edges
}
int main() {
Path p1, p2;
p1 << IntPoint(0,0) << IntPoint(1,0) << IntPoint(0,1) << IntPoint(0,0);
p2 << IntPoint(1,0) << IntPoint(1,1) << IntPoint(0,1) << IntPoint(1,0);
cout << is_bordering(p1, p2) << endl;
}
我认为当使用
ctIntersection
测试两个边界多边形时结果将包含边界边缘,但对我来说这返回 false。我对上述内容的期望如下,绿色代表 solutions
路径。我如何让这个函数工作(使用 Clipper 库)?
最佳答案
您示例中的多边形不相交,因此函数 is_bordering() 按预期返回 0。相邻多边形的并集将是单个多边形,因此您也可以对此进行测试:
#include "clipper.hpp"
#include <iostream>
using namespace std;
using namespace ClipperLib;
bool is_bordering(Path p1, Path p2) {
Paths _intersection, _union;
Clipper c;
c.AddPath(p1, ptSubject, true);
c.AddPath(p2, ptClip, true);
c.Execute(ctIntersection, _intersection, pftNonZero );
c.Execute(ctUnion, _union, pftNonZero);
return (_intersection.size() > 0 || _union.size() < 2);
}
int main() {
Path p1, p2;
cInt I = 10;
p1 << IntPoint(0, 0) << IntPoint(I, 0) << IntPoint(0, I) << IntPoint(0, 0);
p2 << IntPoint(I, 0) << IntPoint(I, I) << IntPoint(0, I) << IntPoint(I, 0);
cout << is_bordering(p1, p2) << endl;
}
这仅适用于一个多边形不完全在另一个多边形内的情况。
关于geometry - 多边形之间的交点作为线剪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60480546/