Skip to content

AngusJohnson/Clipper2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Clipper2

A Polygon Clipping, Offsetting and Triangulation library (in C++, C# & Delphi)

GitHub Actions C++ status C# License Nuget documentation

The Clipper2 library performs intersection, union, difference and XOR boolean operations on both simple and complex polygons. It also performs polygon offsetting, and Constrained Delaunay Triangulation. This is a major update of my original Clipper library that was written over 10 years ago. That library I'm now calling Clipper1, and while it still works very well, Clipper2 is better in just about every way.

Compilers

Clipper2 can be compiled using either C++, or C#, or Delphi Pascal. The library can also be accessed from other programming languages by dynamically linking to exported functions in the C++ compiled Clipper2 library. (Since the C++ compiled code is measurably faster, C# and Delphi developers may also prefer this approach in applications where the library's performance is critical.)

Lang. Requirements
C++: Requires C++17, or
C#: The library uses Standard Library 2.0 but the sample code uses .NET6, or
Delphi: Compiles with any version of Delphi from version 7 to current.

Documentation

Extensive HTML documentation

Examples

Clipping

      //C++
      Paths64 subject, clip, solution;
      subject.push_back(MakePath({100, 50, 10, 79, 65, 2, 65, 98, 10, 21}));
      clip.push_back(MakePath({98, 63, 4, 68, 77, 8, 52, 100, 19, 12}));
      solution = Intersect(subject, clip, FillRule::NonZero);
      //C#
      Paths64 subj = new Paths64();
      Paths64 clip = new Paths64();
      subj.Add(Clipper.MakePath(new int[] { 100, 50, 10, 79, 65, 2, 65, 98, 10, 21 }));
      clip.Add(Clipper.MakePath(new int[] { 98, 63, 4, 68, 77, 8, 52, 100, 19, 12 }));
      Paths64 solution = Clipper.Intersect(subj, clip, FillRule.NonZero);
      //Delphi
      var 
        subject, clip, solution: TPaths64;
      begin
        SetLength(subject, 1);
        subject[0] := MakePath([100, 50, 10, 79, 65, 2, 65, 98, 10, 21]);
        SetLength(clip, 1);
        clip[0] := MakePath([98, 63, 4, 68, 77, 8, 52, 100, 19, 12]);
        solution := Intersect( subject, clip, frNonZero);

clipperB

Constrained Delaunay Triangulation

C++

subject = GetPathsFromSvgFile(folder + "coral3.svg");
Triangulate(subject, 0, sol, true);
DisplaySolution(".\\coral3t.svg", sol, multicolor);

C#

string TestFile = "coral3.svg";
srcFile = svgFolder + TestFile;
subject = GetPathsFromSvgFile(srcFile);
if (Clipper.Triangulate(subject, 0, out solution) == TriangulateResult.success)
Display(solution, tmpFolder + TestFile);

Delphi

inFilename := svgFolder + 'coral3.svg';
if not FileExists(inFilename) then Exit;
outFilename := 'coral3t.svg';
pp := GetPathsFromSvgFile(inFilename);
if Triangulate(pp, 0, sol, true) = trSuccess then
begin
SavePathsAsSvg(outFilename, pp, sol, rfOverwrite, false);
ShellExecute(0, nil, PChar(outFilename), nil, nil, 0);
end;

coral3 coral3t


Ports to other languages

lang. link
Java https://github.com/micycle1/Clipper2-java/
TypeScript https://github.com/countertype/clipper2-ts
Kotlin https://github.com/Monkey-Maestro/clipper2-kotlin
golang https://github.com/epit3d/goclipper2
Lua https://github.com/Ark223/Clipper2-Lua
WASM https://github.com/ErikSom/Clipper2-WASM/