2016-07-14 167 views
0

我正在使用Clipper並想確定兩個(多)多邊形是否相交。如何確定兩個多邊形是否使用Clipper相交?

我的期望是,圖書館會有一個很好的,抽象的方式來提出這個問題,但似乎並沒有。

我認爲Area()方法可能是有用的,但它僅適用於PathExecute()方法返回Paths

我已經建立了以下M(幾乎)我們證明了問題:

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

回答

1

它好像這樣做的最簡單的方法是計算在被返回的Paths對象的路徑數Execute()方法。 Paths是一個簡單的向量,所以,如果它有size()==0,那就沒有交集。

#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); 
} 
相關問題