2016-07-14 32 views
0

Ich benutze Clipper und möchte feststellen, ob zwei (Multi-) Polygone sich schneiden.Wie kann ich feststellen, ob sich zwei Polygone mit Clipper überschneiden?

Meine Erwartung war, dass die Bibliothek eine schöne, abstrakte Art hätte, diese Frage zu stellen, aber das scheint sie nicht zu tun.

Ich dachte, dass die Area() Verfahren nützlich sein könnte, aber es funktioniert nur auf Path und die Execute() Methode gibt Paths.

Ich habe die folgende M (fast) bauten wir das Problem demonstriert:

#include <iostream> 
#include "clipper.hpp" 
using namespace ClipperLib; 

Paths MakeBox(int xmin, int xmax, int ymin, int ymax){ 
    Paths temp(1); 
    temp[0] << IntPoint(xmin,ymin) << IntPoint(xmax,ymin) << IntPoint(xmax,ymax) << IntPoint(xmin,ymax); 
    return temp; 
} 

bool Intersects(const Paths &subj, const Paths &clip){ 
    ClipperLib::Clipper c; 

    c.AddPaths(subj, ClipperLib::ptSubject, true); 
    c.AddPaths(clip, ClipperLib::ptClip, true); 

    ClipperLib::Paths solution; 
    c.Execute(ClipperLib::ctIntersection, solution, ClipperLib::pftNonZero, ClipperLib::pftNonZero); 

    return Area(solution); 
} 

int main(){ 
    Paths subj = MakeBox(0,10,0,10); 
    Paths clip1 = MakeBox(1,2,1,2); 
    Paths clip2 = MakeBox(15,20,15,20); 

    Intersects(subj,clip1); 
    Intersects(subj,clip2); 
} 

Antwort

1

Es scheint, als ob der einfachste Weg, dies zu tun, um die Anzahl der Pfade in der Paths Objekt durch die zurück zählen ist Execute() Methode. Paths ist ein einfacher Vektor, also, wenn es size()==0 hat, gibt es keine Schnittmenge.

#include <iostream> 
#include "clipper.hpp" 
using namespace ClipperLib; 

Paths MakeBox(int xmin, int xmax, int ymin, int ymax){ 
    Paths temp(1); 
    temp[0] << IntPoint(xmin,ymin) << IntPoint(xmax,ymin) << IntPoint(xmax,ymax) << IntPoint(xmin,ymax); 
    return temp; 
} 

bool Intersects(const Paths &subj, const Paths &clip){ 
    ClipperLib::Clipper c; 

    c.AddPaths(subj, ClipperLib::ptSubject, true); 
    c.AddPaths(clip, ClipperLib::ptClip, true); 

    ClipperLib::Paths solution; 
    c.Execute(ClipperLib::ctIntersection, solution, ClipperLib::pftNonZero, ClipperLib::pftNonZero); 

    return solution.size()!=0; 
} 

int main(){ 
    Paths subj = MakeBox(0,10,0,10); 
    Paths clip1 = MakeBox(1,2,1,2); 
    Paths clip2 = MakeBox(15,20,15,20); 

    Intersects(subj,clip1); 
    Intersects(subj,clip2); 
}