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

point-in-region

v1.0.0

Published

Quickly and robustly determines which region contains a given query point

Downloads

459

Readme

point-in-region

Locates a point in a collection of regions. Point location is exact, takes O(log(n)) time, and the data structure has a space requirement of O(n log(n)).

Example

//First create a list of vertices
var vertices = [
  [0, 0],
  [1, 0],
  [1, 1],
  [0, 1],
  [2, 0],
  [3, 0],
  [3, 1],
  [2, 1],
  [2.25, 0.25],
  [2.75, 0.25],
  [2.75, 0.75],
  [2.25, 0.75]
]

//Regions are defined by lists of loops of vertex indices
var regions = [
  //First region, just a square, one loop
  [ 
    [0, 1, 2, 3]
  ],
  //Second region, square with a hole in the middle
  [
    [4, 5, 6, 7],
    [11, 10, 9, 8]    //Note inner loop has opposite orientation
  ]
]

//Now we create the data structure
var classifyPoint = require("point-in-region")(vertices, regions)

//And we can use it to classify which region contains a given point
var assert = require("assert")
assert.equal(classifyPoint([0.5, 0.5]), 0)
assert.equal(classifyPoint([2.1, 0.1]), 1)
assert.equal(classifyPoint([100000, 10000]), -1)  //Outside points return -1
assert.equal(classifyPoint([2.5, 0.5]), -1)  //Point in center hole is outside region

Here is an in browser demo you can try out yourself:

Install

npm install point-in-region

API

var classify = require("point-in-region")(positions, regions)

Preprocesses a collection of regions to answer point location queries efficiently.

  • positions is a list of vertex positions for each of the regions
  • regions is a list of regions encoded as lists of clockwise oriented loops of indices

Returns A point membership classification function for the region set

Note The regions must obey certain topological and geometric properties for this classification to be correct. Specifically:

  • Any two edges are either disjoint, identical, or meet at exactly one common end point
  • The interior of each edge touches at most two regions

classify(point)

Returns the index of the region containing point

  • point is a 2D point encoded as a length 2 array

Returns The index of the region containing point or -1 if it is not in any region.

Credits

(c) 2014 Mikola Lysenko. MIT License