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

spatialhash-2d

v1.0.31

Published

A node.js implementation of a spatial hash

Downloads

8

Readme

npm package

SpatialHash

A node.js implementation of a 2D spatial hash.

Documentation

Item requirements

Example of an item:

var item = {
    range: {
        x: 0,
        y: 0,
        w: 100,
        h: 150
    },
    __b: undefined
};
  • Item must contain a .range object. The .range object also must have x, y, w and h variables/properties.
  • Item must not have a .__b item defined.
  • Functionality is not guaranteed if requirements are not met.

Constructor

const SpatialHash = require('spatialhash-2d');
var hash = new SpatialHash(rangeObj, bucketSize);

rangeObj

An object containing x, y, w and h variables/properties. The same functionality as item.range object.

{
    x: /* X center */,
    y: /* Y center */,
    w: /* Half-width */,
    h: /* Half-height */
}

bucketSize

A number that represents the size of a bucket.

bucketSize is used to create the spatial hashes. The hashes are in a dimension of a square and have a half-width of bucketSize.

Variables

.bucketSize

The second argument passed in the constructor. Changing it will create problems unless proceeded with a .init() call.

.itemCount

The amount of items currently in the map.

The maximum item count is 900 000 000 000 000.

.hashes

The map. See remarks for more info.

.horizontalBuckets

Amount of horizontal buckets in the map. See remarks for more info.

.verticalBuckets

Amount of vertical buckets in the map. See remarks for more info.

.bucketCount

Amount of buckets in the map. Basically .horizontalBuckets * .verticalBuckets.

Functions

.init()

Initializes the spatial hash arrays. Automatically called in the constructor.

Can be called to remove all items.

.insert(item)

Inserts an item.

See item requirements that need to be met. Functionality is not guaranteed if requirements are not met.

The maximum item count is 900 000 000 000 000.

The .insert(item) function creates a .__b object in the item.

.remove(item)

Removes an item.

The .remove(item) function will not remove items that do not have a .__b object inside them.

.update(item)

Removes then inserts the item.

Items that are moving need to be updated.

Calls .remove(item) and .insert(item).

.query(rangeObj, [selector])

Searches the map for items in a specified rangeObj then returns an array of found items.

If any item is found to be intersecting the rangeObj, the class will call selector(item) if selector is provided.

If selector(item) returns false the array will not include the item.

.any(rangeObj)

Searches the map for items in a specified rangeObj.

Returns a boolean whether an item was found or not.

If any item is found to be intersecting the rangeObj, it will immediately return true. Otherwise returns false.

.find(rangeObj, [callback])

Searches the map for items in a specified rangeObj.

If any item is found to be intersecting the rangeObj, the class will call callback(item) if callback is provided.

Remarks

This implementation's map is a two-dimensional array-like object.

var hash = new SpatialHash({
    x: 0,
    y: 0,
    w: 200,
    h: 200
}, 100);
console.log(hash.hashes);

returns

{ '0': { '0': [], '1': [], '2': [], '-2': [], '-1': [] },
  '1': { '0': [], '1': [], '2': [], '-2': [], '-1': [] },
  '2': { '0': [], '1': [], '2': [], '-2': [], '-1': [] },
  '-2': { '0': [], '1': [], '2': [], '-2': [], '-1': [] },
  '-1': { '0': [], '1': [], '2': [], '-2': [], '-1': [] } }

The map goes horizontally then vertically, and at the end is an array that will contain objects.

.insert(item) can insert an item into the map multiple times.

This is because an item can be larger than the bucket size and will sometimes not be found when searching.

This requires for an ID, and that's why there is a maximum item count, 900 000 000 000 000, to ensure integer stability.

item.__b.id number may overlap with some other id.

This is due to the class just adding 1 to the next number.

This is fixable but very performance-costly.

Also this problem will appear only if you add 1 800 000 000 000 000 or more objects into the map and then remove it.

Which will take a few hours to come to.