Example

The Clipper Library

Example

Delphi Code Sample:
  uses
    graphics32, clipper;
  
  function GetEllipsePoints(bounds: TIntRect): TPath;
  begin
    //code to create an elliptical polygon here
  end;
	
  procedure DrawPolygons(polys: TPaths; color: TColor32);
  begin
    //code to display the polygons here
  end;
	
  var
    sub, clp, sol: TPaths;
  begin

    //set up the subject and clip polygons ...
    setlength(sub, 3);
    sub[0] := GetEllipsePoints(IntRect(100,100,300,300));
    sub[1] := GetEllipsePoints(IntRect(125,130,275,180));
    sub[2] := GetEllipsePoints(IntRect(125,220,275,270));
	
    setlength(clp, 1);
    clp[0] := GetEllipsePoints(IntRect(140,70,220,320));

    //display the subject and clip polygons ...
    DrawPolygons(sub, 0x8033FFFF);
    DrawPolygons(clp, 0x80FFFF33);
    
    //get the intersection of the subject and clip polygons ...
    with TClipper.Create do
    try
      AddPaths(sub, ptSubject, true);
      AddPaths(clp, ptClip, true);
      Execute(ctIntersection, sol, pftEvenOdd, pftEvenOdd);
    finally
      free;
    end;
    
    //finally draw the intersection polygons ...
    DrawPolygons(sol, 0x40808080);
        
 
C++ Code Sample:
  #include "clipper.hpp"
  
  ...

  //from clipper.hpp ...
  //typedef long long cInt;
  //struct IntPoint {cInt X; cInt Y;};
  //typedef std::vector<IntPoint> Path;
  //typedef std::vector<Polygon> Paths;

  using namespace ClipperLib;

  void GetEllipsePoints(IntRect& bounds, Path& p)
  {/* ... */}
  
  void DrawPolygons(Paths& p, unsigned color)
  {/* ... */}
  
  int main()
  {
    //set up the subject and clip polygons ...
    Paths sub(3);
    GetEllipsePoints(IntRect(100,100,300,300), sub[0]);
    GetEllipsePoints(IntRect(125,130,275,180), sub[1]);
    GetEllipsePoints(IntRect(125,220,275,270), sub[2]);
    
    Paths clp(1);
    GetEllipsePoints(IntRect(140,70,220,320), clp[0]);
    
    //display the subject and clip polygons ...
    DrawPolygons(sub, 0x8033FFFF);
    DrawPolygons(clp, 0x80FFFF33);
    
    //get the intersection of the subject and clip polygons ...
    Clipper clpr;
    clpr.AddPaths(sub, ptSubject, true);
    clpr.AddPaths(clp, ptClip, true);
    Paths solution;
    clpr.Execute(ctIntersection, solution, pftEvenOdd, pftEvenOdd);
    
    //finally draw the intersection polygons ...
    DrawPolygons(solution, 0x40808080);
  }
        
 
C# Code Sample:
  ...
  using ClipperLib;
	
  ...
  using Path = List<IntPoint>;
  using Paths = List<List<IntPoint>>;
  
  static Path GetEllipsePoints(IntRect bounds)
  {/* ... */}
  
  static void DrawPolygons(Path p, uint color)
  {/* ... */}
  
  static void Main(string[] args)
  {
    Paths subjs = new Paths(3);
    subjs.Add(GetEllipsePoints(new IntRect(100,100,300,300)));
    subjs.Add(GetEllipsePoints(new IntRect(125,130,275,180)));
    subjs.Add(GetEllipsePoints(new IntRect(125,220,275,270)));
    
    Paths clips = new Paths(1);
    clips.Add(GetEllipsePoints(new IntRect(140,70,220,320)));
    
    DrawPolygons(subjs, 0x8033FFFF);
    DrawPolygons(clips, 0x80FFFF33);
    
    Paths solution = new Paths();
    Clipper c = new Clipper();
    c.AddPaths(subjs, PolyType.ptSubject, true);
    c.AddPaths(clips, PolyType.ptClip, true);
    c.Execute(ClipType.ctIntersection, solution);
    
    DrawPolygons(solution, 0x40808080);
  }