RectClip

Delphifunction RectClip64(const rect: TRect64; const subjects: TPaths64): TPathsD;

Delphifunction RectClipD(const rect: TRectD; const subjects: TPathsD): TPathsD;


C++Paths64 RectClip64(const Rect64 rect, const Paths64& subjects);

C++PathsD RectClipD(const RectD rect, const PathsD& subjects);


C# public static Paths64 RectClip64(Rect64 rect, Paths64 subjects);

C# public static PathsD RectClipD(RectD rect, PathsD subjects);


As RectClip's name implies, clipping regions must be rectangular. And since these functions use a completely different clipping algorithm from the library's general purpose polygon clipper, they behave in a number of different ways too.

First, these functions are extremely fast when compared to the general purpose clipper (see below). However, RectClip only performs intersect clipping, not union or difference clipping. Subject polygons will only be clipped with the clipping boundary, so individual polygons will be unaffected by other polygons. And because RectClip preserves path orientation (winding direction), it is completely agnostic to filling rules. So whatever filling rule applies to the subject polygons will also apply to the solution.

Additional notes:


1. RectClip is agnostic to filling rules:

2. Performance:

RectClip is extremely fast when compared to the Library's general purpose clipper. Where the general purpose Intersect function has roughly O(n³) performance, RectClip has O(n) performance.


#include "clipper2/clipper.h"  
...
using namespace Clipper2Lib;

int main()
{
  srand (time(NULL));
  Path64 shapes[3]; // 3 shapes => +, T, C
  shapes[0] = MakePath({-10,-10, -10,-30, 10,-30, 10,-10, 
      30,-10, 30,10, 10,10, 10,30, -10,30, -10,10, -30,10, -30,-10});
  shapes[1] = MakePath({-10,30, -10,-10, -30,-10, -30,-30, 
      30,-30, 30,-10, 10,-10, 10,30});
  shapes[2] = MakePath({-30,-30, 30,-30, 30,-10, -10,-10, 
      -10,10, 30,10, 30,30, -30,30});
      
  const int shape_cnt = 10000;
  const int width = 400, height = 400, margin = 400 /6;
  Rect64 rect = Rect64(margin, margin, width - margin, height - margin);
  Paths64 subject;
  subject.reserve(shape_cnt);
  for (int i = 0; i < shape_cnt; ++i)
  {
    subject.push_back(translatePath(
      shapes[rand() % 3], 30 + rand(width -30), 
      30 + rand(height -30)));
  }
  Paths64 solution = RectClip(rect, subject);
}
      

See Also

Intersect, RectClipLines, Paths64, PathsD, Rect64, RectD