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 🙏

© 2024 – Pkg Stats / Ryan Hefner

polyclip-js

v1.0.1

Published

A JavaScript port of the polyclip library

Downloads

6

Readme

Introduction

This is a TypeScript implementation of "polyclip" by the authors of Clipping simple polygons with degenerate intersections: Erich L. Foster, Kai Hormann, and Romeo Traian Popa.

Polyclip was a c++ implementation of their extension of the Greiner-Hormann clipping algorithm, which computes the intersection (or union) of two non-self-intersecting complex polygons, with possibly multiple and nested components, even in case of degenerate intersections (vertex on edge, overlapping edges, etc.).

Installation

npm install polyclip-js

# or

yarn add polyclip-js

Usage

The library exports two functions: clipXY and clipArray. They both produce the same output but take different input types.

clipXY

Accepts two arrays of polygons, where each polygon is an array of points, and each point is an object with x and y properties. Returns a new polygon in the same format that describes the intersection of the two input polygons.

import { clipXY } from 'polyclip-js'

clipXY(
	[
		[
			{ x: 0, y: 0 },
			{ x: 10, y: 0 },
			{ x: 10, y: 10 },
			{ x: 0, y: 10 },
		],
	],
	[
		[
			{ x: 5, y: 5 },
			{ x: 15, y: 5 },
			{ x: 15, y: 15 },
			{ x: 5, y: 15 },
		],
	]
)

// returns
// [
//   [
//     { x: 5, y: 5 },
//     { x: 10, y: 5 },
//     { x: 10, y: 10 },
//     { x: 5, y: 10 },
//   ],
// ]

clipArray

Accepts two arrays of polygons, where each polygon is an array of points, and each point is an object with x and y properties. Returns a new polygon in the same format that describes the intersection of the two input polygons.

import { clipArray } from 'polyclip-js'

clipArray(
	[
		[
			[0, 0],
			[10, 0],
			[10, 10],
			[0, 10],
		],
	],
	[
		[
			[5, 5],
			[15, 5],
			[15, 15],
			[5, 15],
		],
	]
)

// returns
// [
//   [
//     [5, 5],
//     [10, 5],
//     [10, 0],
//     [5, 0],
//   ],
// ]

Test File Format

The library's tests rely on the original .poly files from the paper's supplementary material.

The "*.poly" file must have the following structure. Each line contains two numbers (int or double), the x and the y coordinates of a vertex, followed by a "," or a ";", where the "," is used to separate the vertices of a polygon component and ";" marks the end of the component. For example, the following 7 lines:

0 0, 1 0, 0 1; -0.5 -0.5, 1.5 -0.5, 1.5 1.5, -0.5 1.5;

describe a polygon with 2 components, a right triangle inside a square. All vertices in one file must be different from each other.

Admitted Input

The following features are allowed in the input polygons:

  • the vertex order in each component can be CW or CCW
  • components can be nested (AKA holes)
  • the two input polygons are allowed to have degenerate intersections (vertex on edge, overlapping edges, etc.) with each other

The following features are not allowed in the input polygons:

  • the polygons should not self-intersect (including degenerate self-intersections like vertex on vertex, vertex on edge), although the result will be correct as long as the self- intersection does not lie on the other polygon

Robustness

The implementation is based on floating point numbers with double precision and therefore not robust. The EPSILON parameter (set to 0.000000001) is used as a tolerance for equality checks, and two numbers are considered equal if their difference is less than EPSILON.

License

This library is offered under the MIT license. The original was offered under the CC BY 4.0 license.