k-a-v
k-a-v

Reputation: 347

Intersection between polygons as line Clipper

I'm trying to use the Clipper C++ library to implement an is_bordering function, as shown below.

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

I thought that when two bordering polygons were tested with ctIntersection the result would contain the bordering edges, but for me this returns false. What I expect from the above would be the following, with green representing the solutions Paths.

How do I get this function working (with the Clipper library)?

Upvotes: 5

Views: 1082

Answers (1)

Laszlo
Laszlo

Reputation: 801

The polygons in your example are not intersecting, so the function is_bordering() is returning 0 as expected. The union of adjacent polygons will be a single polygon, so you could test for that too:

#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;
}

This works only for the case when one polygon is not entirely inside the other.

Upvotes: 0

Related Questions