Как определить, пересекаются ли два полигона с помощью Clipper?

Я использую Clipper и хочу определить, пересекаются ли два (много) полигона.

Я ожидал, что у библиотеки будет хороший, абстрактный способ задать этот вопрос, но это не так.

Я думал, что Area() Метод может быть полезным, но он работает только на Path и Execute() метод возвращает Paths,

Я построил следующее М (почти) МЫ, демонстрирующее проблему:

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

Другие решения

Других решений пока нет …