Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

simple-quadtree

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-quadtree

quadtree data structure

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

qtree - a simple JavaScript quadtree

qtree is a minimal quadtree implementation that supports simple put and get 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. qtree has no dependencies.

Usage

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

var QuadTree = require('qtree');
var qt = QuadTree(0, 0, 100, 100);

You can also give QuadTree some options, the only option currently 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

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'});

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 until all of them have been iterated, or the callback function returns false.

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.

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

License

(The MIT License)

Copyright (c) 2013 Antti Saarinen <antti.p.saarinen@gmail.com>

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.

Keywords

FAQs

Package last updated on 23 Aug 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc