polygon-crop

Cut a polygon to a bounding box

Usage no npm install needed!

<script type="module">
  import polygonCrop from 'https://cdn.skypack.dev/polygon-crop';
</script>

README

polygon-crop

Cut a polygon to a bounding box

Similar to https://github.com/mapbox/lineclip but without the apparent bug(s), and with only closed path (polygon) support.

Turns out this is a pretty hard problem, perhaps mostly due to the discrete nature of computing.

Thus powered by the impressive and widely ported clipper-lib , which will do much more for you than crop polygons, if you so desire. Its interface is slightly more difficult to use, namely it only supports integer coordinates, including integers that far surpass Number.MAX_SAFE_INTEGER with a ~2X performance hit. polygon-crop keeps everything inside the fast performance range, well under Number.MAX_SAFE_INTEGER.

Usage

yarn add polygon-crop
const polygonCrop = require('polygon-crop');

const subPolygons = polygonCrop(
    [[x0, y0], [x1, y1], [x2, y2]/* , ... */],
    [cropLeft, cropTop, cropRight, cropBottom]
);