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

simple-quadtree

v0.1.3

Published

quadtree data structure

Downloads

1,871

Readme

simple-quadtree

simple-quadtree is a minimal quadtree implementation that supports simple put, get, remove and clear operations on objects having a x, y position and w, h dimension.

Installation

npm install simple-quadtree

Should also work in all browsers as is. simple-quadtree has no dependencies.

Usage

Create a quadtree by giving it some bounds, e.g. x, y, width and height

var QuadTree = require('simple-quadtree');
var qt = QuadTree(0, 0, 100, 100);

You can also give QuadTree some options; currently, the only option is the maximum number of children in a quadtree node until it is subdivided:

var qt = QuadTree(0, 0, 100, 100, { maxchildren: 25 }); // defaults to 25

Putting objects

Put objects into the quadtree by using put. The objects can be anything as long as they have x, y, w, h properties as numbers indicating the bounding area of that object:

qt.put({x: 5, y: 5, w: 0, h: 0, string: 'test'});

Each w and h property must be nonnegative.

Getting objects

Iterate over the objects by giving an area and a callback:

qt.get({x:0, y: 0, w: 10, h: 10}, function(obj) {
    // obj == {x: 5, y: 5, w: 0, h: 0, string: 'test'}
});

Iterating over objects continues as long as there are remaining objects and the callback function returns true. If the callback does not return true, the iteration is interrupted.

Alternatively you can omit the callback to return an array of all matching objects:

var result = qt.get({x:0, y: 0, w: 10, h: 10});
    // result == [{x: 5, y: 5, w: 0, h: 0, string: 'test'}]

You can also give a buffer threshold, indicating that you want to iterate over all objects in the area expanded by the threshold to all directions:

qt.get({x:0, y: 0, w: 4, h: 4}, 2, function(obj) {
    // obj == {x: 5, y: 5, w: 0, h: 0, string: 'test'}
});	     

Alternatively, you can also iterate over all objects that are close to a line segment. The line segment is defined by having x, y, dx, dy, dist properties:

qt.get({x: 0, y: 0, dx: 1, dy: 1}, 1, function(obj) {
    // obj == {x: 5, y: 5, w: 0, h: 0, string: 'test'}
});

Please note it is assumed that dx, dy are a 2-d vector normalized to the length of 1. The dist property of the object tells the length of the line segment to this direction. If dist is undefined or negative, it is assumed to be an infinite line instead of a line segment.

Please also note that getting objects close to a line segment will guarantee that all objects close to the line will be iterated over, but other non-overlapping objects may also be returned.

You can also use a buffer threshold when iterating based on a line segment.

Modifying objects

In order to remove or update an object already in the quadtree, you have to identify the object. By default, update and remove affect all objects with x, y, w, h identical to the passed object.

If you want to remove only a specific object, you can should pass the name of the uniquely identifying property to update and remove.

Updating objects

If the coordinates of an object already put into the quadtree change, you should call update to make sure it is still indexed in the right location:

var obj = { x: 5, y: 5, w: 0, h: 0, string: 'test', id: 4233 };
qt.put(obj);

assert(obj.x == 5);
qt.update(obj, 'id', { x: 10 }); // change obj.x to 10
assert(obj.x == 10); 

Call to update also modifies the coordinates of the object to be updated, and returns true if the object was correctly updated and false if the object to be updated was not found.

Please note that despite passing 'id' as the identifying attribute, the object passed to update must still have the same x, y, w, h properties as the original inserted object. update traverses the tree similarly as put, so if these properties are not the same, it is possible that the object to be updated is not found.

Removing objects

To remove objects, call remove with the original object that was passed to put:

var obj = { x: 5, y: 5, w: 0, h: 0, string: 'test', id: 4233 };
qt.put(obj);
qt.remove(obj); // remove all objects with matching coordinates

var obj1 = { x: 5, y: 5, w: 0, h: 0, string: 'test', id: 4233 };
var obj2 = { x: 5, y: 5, w: 0, h: 0, string: 'test', id: 4234 };

qt.put(obj1);
qt.put(obj2);

// remove by uniquely identifying attribute
qt.remove(obj1, 'id'); // only obj1 removed
qt.remove(obj1); // obj2 removed

Please note that despite passing 'id' as the identifying attribute, the object passed to put must still have the same x, y, w, h properties as the object to be removed. remove traverses the tree similarly as put, so if these properties are not the same, it is possible that the object to be removed is not found.

Clearing the quadtree

To clear the quadtree call clear:

qt.clear();

This creates a new root node effectively putting the existing one up for garbage collection.

License

(The MIT License)

Copyright (c) 2013 Antti Saarinen <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.