繁体   English   中英

如何使用Clipper确定两个多边形是否相交?

[英]How do I determine if two polygons intersect using Clipper?

我正在使用Clipper,想要确定两个(多)多边形是否相交。

我的期望是该图书馆将以一种很好的抽象方式来问这个问题,但事实并非如此。

我以为Area()方法可能有用,但是它仅适用于Path ,而Execute()方法返回Paths

我建立了以下M(几乎)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);
}

似乎最简单的方法是计算Execute()方法返回的Paths对象中的Paths数。 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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM