5

我正在尝试使用 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,结果将包含边界边缘,但对我来说这返回错误。我对上面的期望如下,绿色代表solutions路径。

如何使此功能正常工作(使用 Clipper 库)?

4

1 回答 1

0

您示例中的多边形不相交,因此函数 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;
}

这仅适用于一个多边形不完全在另一个多边形内的情况。

于 2020-03-17T23:36:28.657 回答