npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

greiner-hormann

v1.5.0

Published

Greiner-Hormann clipping algorithm

Readme

License

Greiner-Hormann polygon clipping

  • Does AND, OR, difference (intersection, union, difference, if you're human)
  • Plays nicely with Leaflet, comes with an adaptor for it
  • Handles non-convex polygons and multiple clipping areas
  • ~4kb compressed, no dependencies
  • Written in TypeScript with full type definitions

Demo and documentation

Note: If you are looking for something more powerful, take a look at the Martinez polygon clipping implementation.

Install

npm install greiner-hormann

Usage

ES Modules (Recommended)

import { intersection, union, diff } from 'greiner-hormann';

const polygon1 = [
  [0, 0],
  [100, 0],
  [100, 100],
  [0, 100],
  [0, 0]
];

const polygon2 = [
  [50, 50],
  [150, 50],
  [150, 150],
  [50, 150],
  [50, 50]
];

// Calculate intersection
const result = intersection(polygon1, polygon2);

// Calculate union
const combined = union(polygon1, polygon2);

// Calculate difference
const difference = diff(polygon1, polygon2);

CommonJS

const { intersection, union, diff } = require('greiner-hormann');

const result = intersection(polygon1, polygon2);

Browser (UMD)

<script src="path/to/greiner-hormann.umd.js"></script>
<script>
  const result = greinerHormann.intersection(polygon1, polygon2);
</script>

Leaflet Integration

The library includes a Leaflet adaptor that works directly with Leaflet polygon objects:

import { intersection, union, diff } from 'greiner-hormann/leaflet';
import L from 'leaflet';

// Create Leaflet polygons
const polygon1 = L.polygon([
  [51.509, -0.08],
  [51.503, -0.06],
  [51.51, -0.047]
]);

const polygon2 = L.polygon([
  [51.508, -0.075],
  [51.502, -0.055],
  [51.515, -0.05]
]);

// Calculate intersection with Leaflet polygons
const result = intersection(polygon1, polygon2);

// Add result to map
if (result) {
  if (Array.isArray(result[0])) {
    // Single polygon result
    L.polygon(result, { color: 'red' }).addTo(map);
  } else {
    // Multiple polygons result
    result.forEach(poly => {
      L.polygon(poly, { color: 'red' }).addTo(map);
    });
  }
}

Named Imports for Leaflet

You can use named imports to avoid conflicts with the main library:

import { intersection, union, diff } from 'greiner-hormann';
import { 
  intersection as leafletIntersection, 
  union as leafletUnion, 
  diff as leafletDiff 
} from 'greiner-hormann/leaflet';

// Use with coordinate arrays
const coordResult = intersection(coordPolygon1, coordPolygon2);

// Use with Leaflet polygons
const leafletResult = leafletIntersection(leafletPolygon1, leafletPolygon2);

Coordinate Format

Input and output can be {x: number, y: number} objects or [x, y] arrays. The library will output points in the same format you provide:

// Array format
const arrayPolygon = [
  [0, 0],
  [100, 0],
  [100, 100],
  [0, 100]
];

// Object format
const objectPolygon = [
  { x: 0, y: 0 },
  { x: 100, y: 0 },
  { x: 100, y: 100 },
  { x: 0, y: 100 }
];

// Both work - output format matches input format
const result1 = intersection(arrayPolygon, arrayPolygon);   // Returns arrays
const result2 = intersection(objectPolygon, objectPolygon); // Returns objects

TypeScript Support

The library is written in TypeScript and includes full type definitions:

import { intersection, union, diff } from 'greiner-hormann';

type Point = [number, number] | { x: number; y: number };
type Polygon = Point[];

const polygon1: Polygon = [[0, 0], [10, 0], [10, 10], [0, 10]];
const polygon2: Polygon = [[5, 5], [15, 5], [15, 15], [5, 15]];

const result: Polygon[] | null = intersection(polygon1, polygon2);

API

intersection(polygon1, polygon2)

Returns the intersection of two polygons, or null if they don't intersect.

union(polygon1, polygon2)

Returns the union of two polygons.

diff(polygon1, polygon2)

Returns the difference of two polygons (polygon1 - polygon2).