Skip to content

Latest commit

 

History

History
58 lines (41 loc) · 2.66 KB

File metadata and controls

58 lines (41 loc) · 2.66 KB

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 Clipper class 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.

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