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

polygon-intersection

v2.0.1

Published

A library for finding the intersection of two polygons

Downloads

14

Readme

Polygon intersection helper

This module has an algorithm to check if two simple polygons intersect, and another to find the intersection polygons.

Definition of intersection

For an intersection to exist, the intersection area must be > 0. In other words, the polygon interiors must intersect. For example, the orange and blue polygons do not intersect in the first case, but intersect in the second:

⠀ | ⠀ | |:-------------------------:|:-------------------------:| Intersection example | Intersection example

For more examples, check below the table of tests both algortihms were submitted to.

How the algorithms work

The algorithm to check if the intersection exists was based on the observation that an intersection occurs if one of the following criteria is fulfilled:

  1. An edge of one polygon crosses an edge of the other;
  2. A point of one polygon lies inside the other;
  3. A point of one polygon lies on an edge of the other, in such a way that the interior of the point's corner intersects the interior of the other polygon.

The algorithm to find the intersection polygons was then designed to apply these three tests in an iterative fashion and save the points it considers to be part of the intersection polygon.

How to use them

To use each algorithm, both polygons must be arrays of 2D point coordinates, ordered counter-clockise. Then, they may be called like:

const polygon1 = [[0,0],[1,0],[1,1],[0,1]];
const polygon2 = [[0,-1],[2,0.5],[0,2]];
const polygonsIntersect = checkIfPolygonsIntersect(polygon1, polygon2);
const intersectionPolygons = findIntersectionBetweenPolygons(polygon1, polygon2);

Validation tests

⠀ | ⠀ | ⠀ | ⠀ | |:-------------------------:|:-------------------------:|:-------------------------:|:-------------------------:| Intersection example Test 01. Intersection: true | Intersection example Test 02. Intersection: true | Intersection example Test 03. Intersection: false | Intersection example Test 04. Intersection: true Intersection example Test 05. Intersection: true | Intersection example Test 06. Intersection: false | Intersection example Test 07. Intersection: false | Intersection example Test 08. Intersection: true Intersection example Test 09. Intersection: false | Intersection example Test 10. Intersection: true | Intersection example Test 11. Intersection: false | Intersection example Test 12. Intersection: true Intersection example Test 13. Intersection: true | Intersection example Test 14. Intersection: true | Intersection example Test 15. Intersection: true | Intersection example Test 16. Intersection: true Intersection example Test 17. Intersection: true | Intersection example Test 18. Intersection: true