polygon-clipping vs clipper-lib vs earcut
Geometric Clipping Libraries
polygon-clippingclipper-libearcutSimilar Packages:

Geometric Clipping Libraries

These libraries provide functionalities for geometric operations such as clipping, triangulation, and polygon manipulation. They are essential in applications involving graphics rendering, spatial analysis, and geographic information systems (GIS). Each library has its unique strengths and use cases, making them suitable for different types of geometric computations and visualizations.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
polygon-clipping410,564619350 kB452 years agoMIT
clipper-lib48,029210215 kB7-BSL
earcut02,43157.3 kB258 months agoISC

Feature Comparison: polygon-clipping vs clipper-lib vs earcut

Clipping Operations

  • polygon-clipping:

    polygon-clipping provides basic clipping operations such as intersection and union. It is suitable for applications that require simple geometric manipulations without the complexity of handling intricate shapes.

  • clipper-lib:

    clipper-lib supports a wide range of clipping operations including union, intersection, difference, and XOR. It can handle complex polygons with holes and overlapping shapes, making it ideal for applications that require precise geometric manipulations.

  • earcut:

    earcut does not perform clipping operations; instead, it focuses solely on triangulating polygons. It efficiently converts a polygon into a set of triangles, which is essential for rendering in graphics applications but does not handle clipping.

Performance

  • polygon-clipping:

    polygon-clipping is generally slower than clipper-lib and earcut due to its simpler algorithms. It is best suited for applications where performance is not the primary concern and where ease of use is more important.

  • clipper-lib:

    clipper-lib is optimized for performance, especially when dealing with complex polygons. It uses an efficient algorithm that minimizes the number of operations required for clipping, making it suitable for high-performance applications.

  • earcut:

    earcut is known for its exceptional speed in triangulating polygons. It is designed to be lightweight and fast, making it an excellent choice for real-time applications where performance is critical, such as in gaming or interactive graphics.

Ease of Use

  • polygon-clipping:

    polygon-clipping offers a user-friendly API that is easy to understand and implement. It is suitable for developers who need basic polygon operations without the overhead of complex configurations.

  • clipper-lib:

    clipper-lib has a steeper learning curve due to its comprehensive feature set and complexity. It requires a good understanding of geometric concepts to utilize effectively, making it less beginner-friendly.

  • earcut:

    earcut is very easy to use, with a straightforward API that allows developers to quickly implement polygon triangulation. It is designed for simplicity and can be integrated with minimal effort into existing projects.

Library Size

  • polygon-clipping:

    polygon-clipping is also lightweight, but it may not be as optimized as earcut. It strikes a balance between functionality and size, making it suitable for applications that need basic clipping without significant overhead.

  • clipper-lib:

    clipper-lib is relatively large due to its extensive functionality and support for various geometric operations. This may impact the overall bundle size of applications that include it.

  • earcut:

    earcut is lightweight and has a small footprint, making it an excellent choice for performance-sensitive applications where minimizing bundle size is crucial.

Use Cases

  • polygon-clipping:

    polygon-clipping is suitable for simpler applications that need basic geometric operations, such as web mapping tools or simple graphic editors.

  • clipper-lib:

    clipper-lib is ideal for applications requiring complex polygon operations, such as CAD software, GIS applications, and any scenario where precise geometric calculations are necessary.

  • earcut:

    earcut is perfect for real-time graphics applications, such as games and interactive visualizations, where fast triangulation of polygons is needed for rendering.

How to Choose: polygon-clipping vs clipper-lib vs earcut

  • polygon-clipping:

    Choose polygon-clipping if you require a straightforward library for basic polygon operations like intersection, union, and difference. It is user-friendly and ideal for simpler applications where advanced features are not necessary.

  • clipper-lib:

    Choose clipper-lib if you need a robust solution for polygon clipping and boolean operations. It excels in handling complex shapes and is suitable for applications requiring precise geometric calculations, such as CAD software or GIS applications.

  • earcut:

    Choose earcut if your primary focus is on fast and efficient polygon triangulation. It is particularly useful for rendering 2D shapes in WebGL or canvas, where performance is critical, and you need a lightweight solution for converting polygons into triangles.

README for polygon-clipping

polygon-clipping

Apply boolean Polygon clipping operations (intersection, union, difference, xor) to your Polygons & MultiPolygons.

CI codecov License: MIT npm

Quickstart

const polygonClipping = require('polygon-clipping')

const poly1 = [[[0,0],[2,0],[0,2],[0,0]]]
const poly2 = [[[-1,0],[1,0],[0,1],[-1,0]]]

polygonClipping.union       (poly1, poly2 /* , poly3, ... */)
polygonClipping.intersection(poly1, poly2 /* , poly3, ... */)
polygonClipping.xor         (poly1, poly2 /* , poly3, ... */)
polygonClipping.difference  (poly1, poly2 /* , poly3, ... */)

API

/* All functions take one or more [multi]polygon(s) as input */

polygonClipping.union       (<geom>, ...<geoms>)
polygonClipping.intersection(<geom>, ...<geoms>)
polygonClipping.xor         (<geom>, ...<geoms>)

/* The clipGeoms will be subtracted from the subjectGeom */
polygonClipping.difference(<subjectGeom>, ...<clipGeoms>)

Input

Each positional argument (<geom>) may be either a Polygon or a MultiPolygon. The GeoJSON spec is followed, with the following notes/modifications:

  • MultiPolygons may contain touching or overlapping Polygons.
  • rings are not required to be self-closing.
  • rings may contain repeated points, which are ignored.
  • rings may be self-touching and/or self-crossing. Self-crossing rings will be interpreted using the non-zero rule.
  • winding order of rings does not matter.
  • inner rings may extend outside their outer ring. The portion of inner rings outside their outer ring is dropped.
  • inner rings may touch or overlap each other.

Output

For non-empty results, output will always be a MultiPolygon containing one or more non-overlapping, non-edge-sharing Polygons. The GeoJSON spec is followed, with the following notes/modifications:

  • outer rings will be wound counter-clockwise, and inner rings clockwise.
  • inner rings will not extend outside their outer ring.
  • rings will not overlap, nor share an edge with each other.
  • rings will be self-closing.
  • rings will not contain repeated points.
  • rings will not contain superfluous points (intermediate points along a straight line).
  • rings will not be self-touching nor self-crossing.
  • rings may touch each other, but may not cross each other.

In the event that the result of the operation is the empty set, output will be a MultiPolygon with no Polygons: [].

Correctness

Run: npm test

The tests are broken up into unit tests and end-to-end tests. The end-to-end tests are organized as GeoJSON files, to make them easy to visualize thanks to GitHub's helpful rendering of GeoJSON files. Browse those tests here.

Performance

The Martinez-Rueda-Feito polygon clipping algorithm is used to compute the result in O((n+k)*log(n)) time, where n is the total number of edges in all polygons involved and k is the number of intersections between edges.

Settings

Global settings are set via environment variables.

  • POLYGON_CLIPPING_MAX_QUEUE_SIZE and POLYGON_CLIPPING_MAX_SWEEPLINE_SEGMENTS: Aims to prevent infinite loops - usually caused by floating-point math round-off errors. Defaults are 1,000,000.

Changelog

This project adheres to Semantic Versioning.

The full changelog is available at CHANGELOG.md.

Authors

Sponsors

Please contact Mike Fogel if you or your company is interested in sponsoring work on specific bug fixes or feature requests.

Based on