Skip to content

micycle1/Clipper2-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

128 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Clipper2-java

A Java port of Clipper2, an open source freeware library that performs robust 2D line and polygon clipping, and offsetting.

What is Clipper2-java?

Clipper2-java is a fast planar geometry library for:

  • Boolean clipping of polygons and polylines: Intersection, Union, Difference, Xor
  • Offsetting / buffering (inflate/deflate) of closed polygons and open polylines (ClipperOffset / Clipper.InflatePaths)
  • Rectangle clipping for polygons and polylines (RectClip, RectClipLines)
  • Minkowski sum/difference
  • Utilities: area, bounds, orientation, simplification (RDP), trimming duplicates/collinear vertices

Usage

Overview

The interface of Clipper2-java aims to match the original C# version closely.

For simple use cases, the static methods in clipper2.Clipper are sufficient.

For advanced scenarios (open paths, PolyTree nesting, reusing engines), use Clipper64 / ClipperD directly.

Coordinates & precision

Note Clipper2’s core algorithms operate on integer coordinates for numerical robustness.

  • Use Path64 / Paths64 (with long coordinates) for best performance and robustness.
  • Use PathD / PathsD (with double coordinates) for convenience. These are scaled and rounded to integers internally using a user-specified precision.

Example

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.Union(subj, clip, FillRule.NonZero);
solution.get(0).forEach(p -> System.out.println(p.toString()));

Installation (Maven / Gradle)

Clipper2-java is available as a Maven/Gradle artifact via Jitpack.

Port Info

  • tangiblesoftwaresolutions' C# to Java Converter did the heavy lifting (but then a lot of manual work was required).
  • Wrapper objects are used to replicate C# ref (pass-by-reference) behaviour. This isn't very Java-esque but avoids an unmanageable refactoring effort.
  • Code passes all tests: polygon, line and polytree.
  • Uses lower-case (x, y) for point coordinates.
  • Private local variables have been renamed to their camelCase variant but public methods (i.e. those of Clipper.class) retain their C# PascalCase names (for now...).
  • Benchmarks can be run by including jmh:benchmark to the chosen maven goal.
  • scanlineList from ClipperBase uses Java TreeSet (variable renamed to scanlineSet).

Benchmark

lightbringer's Java port of Clipper1 is benchmarked against this project in the benchmarks. Clipper2-java is faster, which becomes more pronounced input size grows.

Benchmark               (edgeCount)  Mode  Cnt  Score   Error  Units
Clipper1.Intersection         1000  avgt    2  0.209           s/op
Clipper1.Intersection         2000  avgt    2  1.123           s/op
Clipper1.Intersection         4000  avgt    2  9.691           s/op
Clipper2.Intersection         1000  avgt    2  0.130           s/op
Clipper2.Intersection         2000  avgt    2  0.852           s/op
Clipper2.Intersection         4000  avgt    2  3.465           s/op

About

Java port of Clipper2, a Polygon Clipping and Offsetting Library

Topics

Resources

License

Stars

Watchers

Forks

Contributors 6

Languages