All Projects → CorentinTh → Quadtree Js

CorentinTh / Quadtree Js

Licence: mit
A simple quadtree implementation for javascript and typescript (nodejs or browser).

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects
js
455 projects

Projects that are alternatives of or similar to Quadtree Js

Pg pathman
Partitioning tool for PostgreSQL
Stars: ✭ 509 (+1313.89%)
Mutual labels:  range
Depth clustering
🚕 Fast and robust clustering of point clouds generated with a Velodyne sensor.
Stars: ✭ 657 (+1725%)
Mutual labels:  range
Docx Builder
NPM Module for creating or merging .docx files
Stars: ✭ 11 (-69.44%)
Mutual labels:  node-module
Solidarity
Solidarity is an environment checker for project dependencies across multiple machines.
Stars: ✭ 540 (+1400%)
Mutual labels:  node-module
React Slider
Accessible, CSS agnostic, slider component for React.
Stars: ✭ 627 (+1641.67%)
Mutual labels:  range
Shopify Api Node
Official Node Shopify connector sponsored by MONEI.net
Stars: ✭ 695 (+1830.56%)
Mutual labels:  node-module
Translation.js
集成了谷歌翻译、有道翻译与百度翻译的网页翻译接口,可在 Node.js 与 Chrome 扩展 / 应用中使用。
Stars: ✭ 493 (+1269.44%)
Mutual labels:  node-module
Ad Bs Converter
A javascript implementation to convert bikram samvat to anno domini and vice-versa
Stars: ✭ 20 (-44.44%)
Mutual labels:  node-module
Rando.js
The world's easiest, most powerful random function.
Stars: ✭ 659 (+1730.56%)
Mutual labels:  node-module
Singledateandtimepicker
You can now select a date and a time with only one widget !
Stars: ✭ 921 (+2458.33%)
Mutual labels:  range
React Range
🎚️Range input with a slider. Accessible. Bring your own styles and markup.
Stars: ✭ 545 (+1413.89%)
Mutual labels:  range
Period
PHP's time range API
Stars: ✭ 616 (+1611.11%)
Mutual labels:  range
Vue Ctk Date Time Picker
VueJS component to select dates & time, including a range mode
Stars: ✭ 707 (+1863.89%)
Mutual labels:  range
Ion2 Calendar
📅 A date picker components for ionic2 /ionic3 / ionic4
Stars: ✭ 537 (+1391.67%)
Mutual labels:  range
Cluster Dispatch
解决Node.js在Cluster模式下的连接/资源复用问题
Stars: ✭ 13 (-63.89%)
Mutual labels:  node-module
Nsfwjs
NSFW detection on the client-side via TensorFlow.js
Stars: ✭ 5,223 (+14408.33%)
Mutual labels:  node-module
Node Module Boilerplate
Boilerplate to kickstart creating a Node.js module
Stars: ✭ 668 (+1755.56%)
Mutual labels:  node-module
Angular Datetime Range
📅 Angular directive for datetime range input
Stars: ✭ 27 (-25%)
Mutual labels:  range
Flexsearch
Next-Generation full text search library for Browser and Node.js
Stars: ✭ 8,108 (+22422.22%)
Mutual labels:  node-module
Conf
Simple config handling for your app or module
Stars: ✭ 707 (+1863.89%)
Mutual labels:  node-module

logo

Weekly Downloads GitHub Workflow Status Coverage Status npm bundle size GitHub package.json version Dependencies Licence Badge

A powerful quadtree implementation in javascript. It can be used for nodejs or directly in the browser.

Installation

Node JS

Quadtree-js can be installed using yarn or npm.

npm install js-quadtree
# or
yarn add js-quadtree

And import :

// EMAScript import
import {QuadTree, Box, Point, Circle} from 'js-quadtree';
// Or Common JS:
const {QuadTree, Box, Point, Circle} = require('js-quadtree');

const quadtree = new QuadTree(new Box(0, 0, 1000, 1000));

quadtree.insert(new Point(100, 200, {custom: 'data'}));

const results = quadtree.query(new Circle(150, 150, 100));

Browser

You can use the CDN:

<script src="https://unpkg.com/js-quadtree"></script>

And everything is globally accessible and prefixed with QT:

const quadtree = new QT.QuadTree(new QT.Box(0, 0, 1000, 1000));

quadtree.insert(new QT.Point(100, 200, {custom: 'data'}));

const results = quadtree.query(new QT.Circle(150, 150, 100));

Usage

Creation

// Create the bounding area of the quadtree (x, y, width, height)
const boundingArea = new Box(0, 0, 1000, 1000);

// Instantiate  the new quadtree
const quadtree = new QuadTree(boundingArea);

You can also specify the following optional parameters:

// Create the bounding area of the quadtree (x, y, width, height)
const boundingArea = new Box(0, 0, 1000, 1000);

const config = {
    capacity: 10,            // Specify the maximum amount of point per node (default: 4)
    removeEmptyNodes: true,  // Specify if the quadtree has to remove subnodes if they are empty (default: false).
    maximumDepth: 5,         // Specify the maximum depth of the quadtree. -1 for no limit (default: -1).
    // Specify a custom method to compare point for removal (default: (point1, point2) => point1.x === point2.x && point1.y === point2.y).
    arePointsEqual: (point1, point2) => point1.data.foo === point2.data.foo      
};

// An array of point to insert directly (same as quadtree.insert(points) )
const points = [new Point(10, 10), new Point(52, 64)];

const quadtree = new QuadTree(boundingArea, config, points);

Insert

You can insert a Point element, an array of Point element, your own element as long as it has an x and a y property or an array of custom element.

const point = new Point(10, 25);

const pointArray = [
    new Point(45, 22),
    new Point(30, 60),
    new Point(14, 12)
];

const customPoint = {
    x: 94,
    y: 23,
    customField:{}
};

quadtree.insert(point);
quadtree.insert(pointArray);
quadtree.insert(customPoint);

You can add your data in a Point element:

const myData = {
    foo: 'bar'
};

const point = new Point(50, 50, myData);

console.log(point.data.foo); // 'bar'

Remove

As the insert method, you can remove a Point element, an array of Point element, your own element as long as it has an x and a y property or an array of custom element.

By default, points having the same x and y values will be removed. To override this behavior, add a method under arePointsEqual in the config of the quadtree that takes two points in parameters and return a boolean if the points are equal. Example: const quadtree = new QuadTree(boundingArea, {arePointsEqual: (point1, point2) => point1.data.foo === point2.data.foo});

const point = new Point(10, 25);

const pointArray = [
    new Point(45, 22),
    new Point(30, 60),
    new Point(14, 12)
];

const customPoint = {
    x: 94,
    y: 23
};

quadtree.remove(point);
quadtree.remove(pointArray);
quadtree.remove(customPoint);

Note: it doesn't have to be the same object, the test is done with the coordinates.

Query

Use the query method to get all the point within a range.

// This will return all the points in the given Box (x, y, width, height)
const points = quadtree.query(new Box(10, 10, 100, 100));
// This will return all the points in the given Circle (x, y, radius)
const points = quadtree.query(new Circle(10, 10, 100));

You can use a Box or a Circle as a range or even your own range element as long as it has the following methods:

  • contains: return true if a point is within this range, false otherwise.
  • intersects: return true if a Box intersects with this range, false otherwise. See the Box definition for a good example.

Get all the point

If want to retrieve all the point, you can use this method:

const points = quadtree.getAllPoints();

Note: you may want to store your points in a side array since, it have to look trough all the child nodes.

Get Tree

You can get the amount of points by nodes with the getTree() method.

const qt = new QuadTree(new Box(0, 0, 10, 10));

qt.insert(new Point(5, 5));
qt.insert(new Point(6, 5));
qt.insert(new Point(4, 5));
qt.insert(new Point(3, 5));
qt.insert(new Point(2, 5));
qt.insert(new Point(1, 5));

console.log(qt.getTree());

// {
//     ne: 2, 
//     nw: {
//         ne: 0, 
//         nw: 0, 
//         se: 3, 
//         sw: 2
//     }, 
//     se: 2,
//     sw: 3
// }

Clear

Use this method to clear the quadtree. It remove all the points and sub-nodes.

quadtree.clear();

Contribute

Pull requests are welcome ! Feel free to contribute.

Credits

Coded with ❤️ by Corentin Thomasset.

License

This project is under the MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].