多边形之间的交点作为线剪

h4x*_*x04 5 geometry clipperlib

我正在尝试使用 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;
}
Run Code Online (Sandbox Code Playgroud)

我认为当测试两个边界多边形时ctIntersection,结果将包含边界边缘,但对我来说这返回 false。我对上面的期望如下,绿色代表solutions路径。

我如何让这个函数工作(使用 Clipper 库)?

Las*_*zlo 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;
}
Run Code Online (Sandbox Code Playgroud)

这只适用于一个多边形不完全位于另一个多边形内部的情况。