Clipper - an open source freeware polygon clipping library.

The Clipper library primarily performs the boolean clipping operations - intersection, union, difference & xor - on 2D polygons. It also performs polygon offsetting.
The library handles complex (self-intersecting) polygons, polygons with holes and polygons with overlapping co-linear edges.
Input polygons for clipping can use EvenOdd, NonZero, Positive and Negative filling modes.
The clipping code is based on the Vatti clipping algorithm, and out performs other clipping libraries.

The package contains the library's full source code (written in Delphi, C++, and C#), numerous demos, a help file and links to third party Perl, Ruby and Haskell modules.

Version: 4.6
Last updated: 29 October 2011
Freeware for both open source and commercial applications (Boost Software License).
Copyright © 2010-2011 Angus Johnson











Feature comparison with 2 well known polygon clipping libraries:
  Speed tests* ... Features ... Cost
  174239 vertices¹ Random complex polygons
looping 1000 times²
All boolean
operations supported
Supports Holes Supports
complex polygons
Supports multiple polygon fill types Free for commercial use
General Polygon Clipper 11.4 seconds 10.2 seconds
(40 errors³)
Yes Yes Yes No No
PolyBoolean 2.66 seconds N/A Yes Yes No No No
Clipper 0.21 secs 7.6 seconds
(0 errors)
Yes Yes Yes EvenOdd, NonZero, Positive, Negative Yes

* Speed tests were performed using the latest versions of all software (as of 2 June 2010) as published on the respective websites. The benchmark test utility with full source code (created using C++Builder 2009) can be downloaded here. Tests were performed on a PC with 2.53 gigahertz Intel Core2 Duo P8700 processor with 4GB RAM running Window 7 Professional.
¹ The '174239 vertices' test uses a test data sample from the PolyBoolean website here.
² The 'random complex polygon' test consists of intersecting 1000 randomly generated subject and clip polygons, where both polygons have 100 vertices. All vertex coordinates are rounded to 10 to "stress-test" the algorithms by substantially increasing the number of complex (multi-edge) intersections.
³ The errors in GPC's random polygon test are unhandled exceptions that result in a failure of the clipping operation.

An independant test of 5 polygon clipping libraries can be found here:
http://rogue-modron.blogspot.com/2011/04/polygon-clipping-wrapper-benchmark.html


Code snippet in C++ showing how to use the Clipper library ...
	#include "clipper.hpp"
	
	//from clipper.hpp ...
	//typedef signed long long long64;
	//struct IntPoint {long64 X; long64 Y;};
	//typedef std::vector<IntPoint> Polygon;
	//typedef std::vector<Polygon> Polygons;
	...
	using namespace ClipperLib;
	
	Polygons subj(2), clip(1), solution;
	
	subj[0].push_back(IntPoint(180,200));
	subj[0].push_back(IntPoint(260,200));
	subj[0].push_back(IntPoint(260,150));
	subj[0].push_back(IntPoint(180,150));
	
	subj[1].push_back(IntPoint(215,160));
	subj[1].push_back(IntPoint(230,190));
	subj[1].push_back(IntPoint(200,190));
	
	clip[0].push_back(IntPoint(190,210));
	clip[0].push_back(IntPoint(240,210));
	clip[0].push_back(IntPoint(240,130));
	clip[0].push_back(IntPoint(190,130));
	
	DrawPolygons(subj, 0x160000FF, 0x600000FF);
	DrawPolygons(clip, 0x20FFFF00, 0x30FF0000);
	
	Clipper c;
	c.AddPolygons(subject, ptSubject);
	c.AddPolygons(clip, ptClip);
	if (c.Execute(ctIntersection, solution)
	  DrawPolygons(solution, 0x3000FF00, 0xFF006600);


Code snippet in Delphi showing how to use the Clipper library ...
	uses Clipper;
	...
	var
	  i: integer;
	  subj, clip, solution: TPolygons;
	begin
	  setlength(subj, 2);
	  setlength(subj[0], 4);
	  subj[0][0] := IntPoint(180,200);
	  subj[0][1] := IntPoint(260,200);
	  subj[0][2] := IntPoint(260,150);
	  subj[0][3] := IntPoint(180,150);
	  
	  setlength(subj[1], 3);
	  subj[1][0] := IntPoint(215,160);
	  subj[1][1] := IntPoint(230,190);
	  subj[1][2] := IntPoint(200,190);

	  setlength(clip, 1);
	  setlength(clip[0], 4);
	  clip[0][0] := IntPoint(190,210);
	  clip[0][1] := IntPoint(240,210);
	  clip[0][2] := IntPoint(240,130);
	  clip[0][3] := IntPoint(190,130);
	  
	  DrawPolygons(subj, $160000FF, $600000FF);
	  DrawPolygons(clip, $20FFFF00, $30FF0000);
		
	  with TClipper.Create do
	  try
	    AddPolygons(subj, ptSubject);
	    AddPolygons(clip, ptClip);
	    if Execute(ctIntersection, solution) then
	      DrawPolygons(solution, $3000FF00, $FF006600);
	  finally
	    free;
	  end;
	  
	end;


Code snippet in C# showing how to use the Clipper library ...
	using ClipperLib;
	
	using Polygon = List<IntPoint>;
	using Polygons = List<List<IntPoint>>;
	...
	Polygons subj = new Polygons(2);
	subj.Add (new Polygon(4));
	subj[0].Add(new IntPoint(180, 200));
	subj[0].Add(new IntPoint(260, 200));	
	subj[0].Add(new IntPoint(260, 150));
	subj[0].Add(new IntPoint(180, 150));

	subj.Add(new Polygon(3));
	subj[1].Add(new IntPoint(215, 160));
	subj[1].Add(new IntPoint(230, 190));	
	subj[1].Add(new IntPoint(200, 190));

	Polygons clip = new Polygons(1);
	clip.Add(new Polygon(4));
	clip[0].Add(new IntPoint(190, 210));
	clip[0].Add(new IntPoint(240, 210));	
	clip[0].Add(new IntPoint(240, 130));
	clip[0].Add(new IntPoint(190, 130));

	DrawPolygons(subj, Color.FromArgb(0x16, 0, 0, 0xFF), 
	  Color.FromArgb(0x60, 0, 0, 0xFF));
	DrawPolygons(clip, Color.FromArgb(0x20, 0xFF, 0xFF, 0), 
	  Color.FromArgb(0x30, 0xFF, 0, 0));

	Polygons solution = new Polygons();

	Clipper c = new Clipper();
	c.AddPolygons(subj, PolyType.ptSubject);
	c.AddPolygons(clip, PolyType.ptClip);
	if (c.Execute(ClipType.ctIntersection, solution, 
	  PolyFillType.pftEvenOdd, PolyFillType.pftEvenOdd))
	    DrawPolygons(solution, Color.FromArgb(0x30, 0, 0xFF, 0), 
	  		Color.FromArgb(0xFF, 0, 0x66, 0));
	


Download the latest version of Clipper from Sourceforge.



Back to my Delphi Page « » Back to my Home Page