如何使用 Clipper 确定两个多边形是否相交?
How do I determine if two polygons intersect using Clipper?
我正在使用 Clipper 并想确定两个(多个)多边形是否相交。
我的期望是图书馆有一个很好的、抽象的方式来问这个问题,但它似乎没有。
我认为 Area()
方法可能有用,但它只适用于 Path
和 Execute()
方法 returns Paths
.
我已经构建了以下 M(almost)WE 来演示这个问题:
#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);
}
似乎最简单的方法是计算 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);
}
我正在使用 Clipper 并想确定两个(多个)多边形是否相交。
我的期望是图书馆有一个很好的、抽象的方式来问这个问题,但它似乎没有。
我认为 Area()
方法可能有用,但它只适用于 Path
和 Execute()
方法 returns Paths
.
我已经构建了以下 M(almost)WE 来演示这个问题:
#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);
}
似乎最简单的方法是计算 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);
}