• Stars
    star
    607
  • Rank 71,110 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 12 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

JavaScript k-d Tree Implementation

k-d Tree JavaScript Library

A basic but super fast JavaScript implementation of the k-dimensional tree data structure.

As of version 1.01, the library is defined as an UMD module (based on https://github.com/umdjs/umd/blob/master/commonjsStrict.js).

In computer science, a k-d tree (short for k-dimensional tree) is a space-partitioning data structure for organizing points in a k-dimensional space. k-d trees are a useful data structure for several applications, such as searches involving a multidimensional search key (e.g. range searches and nearest neighbor searches). k-d trees are a special case of binary space partitioning trees.

Demos

  • Spiders - animated multiple nearest neighbour search
  • Google Map - show nearest 20 out of 3000 markers on mouse move
  • Colors - search color names based on color space distance
  • Mutable - dynamically add and remove nodes

Usage

Using global exports

When you include the kd-tree script via HTML, the global variables kdTree and BinaryHeap will be exported.

// Create a new tree from a list of points, a distance function, and a
// list of dimensions.
var tree = new kdTree(points, distance, dimensions);

// Query the nearest *count* neighbours to a point, with an optional
// maximal search distance.
// Result is an array with *count* elements.
// Each element is an array with two components: the searched point and
// the distance to it.
tree.nearest(point, count, [maxDistance]);

// Insert a new point into the tree. Must be consistent with previous
// contents.
tree.insert(point);

// Remove a point from the tree by reference.
tree.remove(point);

// Get an approximation of how unbalanced the tree is.
// The higher this number, the worse query performance will be.
// It indicates how many times worse it is than the optimal tree.
// Minimum is 1. Unreliable for small trees.
tree.balanceFactor();

Using RequireJS

requirejs(['path/to/kdTree.js'], function (ubilabs) {
	// Create a new tree from a list of points, a distance function, and a
	// list of dimensions.
	var tree = new ubilabs.kdTree(points, distance, dimensions);

	// Query the nearest *count* neighbours to a point, with an optional
	// maximal search distance.
	// Result is an array with *count* elements.
	// Each element is an array with two components: the searched point and
	// the distance to it.
	tree.nearest(point, count, [maxDistance]);

	// Insert a new point into the tree. Must be consistent with previous
	// contents.
	tree.insert(point);

	// Remove a point from the tree by reference.
	tree.remove(point);

	// Get an approximation of how unbalanced the tree is.
	// The higher this number, the worse query performance will be.
	// It indicates how many times worse it is than the optimal tree.
	// Minimum is 1. Unreliable for small trees.
	tree.balanceFactor();
});

Example

var points = [
  {x: 1, y: 2},
  {x: 3, y: 4},
  {x: 5, y: 6},
  {x: 7, y: 8}
];

var distance = function(a, b){
  return Math.pow(a.x - b.x, 2) +  Math.pow(a.y - b.y, 2);
}

var tree = new kdTree(points, distance, ["x", "y"]);

var nearest = tree.nearest({ x: 5, y: 5 }, 2);

console.log(nearest);

About

Developed at Ubilabs. Released under the MIT Licence.

More Repositories

1

geocomplete

jQuery Geocoding and Places Autocomplete Plugin
JavaScript
1,227
star
2

react-geosuggest

A React autosuggest for the Google Maps Places API.
TypeScript
1,021
star
3

axidraw

Use JavaScript to draw on any flat surface with the friendly AxiDraw robot
JavaScript
161
star
4

google-maps-api-threejs-layer

Google Maps API layer that uses Three.js to for super fast animation
JavaScript
159
star
5

google-maps-react-hooks

The JavaScript library to easily implement a Google Maps map into your react application. It comes with a collection of React hooks to access the map instance or different Maps JavaScript Services.
TypeScript
73
star
6

threejs-overlay-view

A wrapper for the Google Maps WebglOverlayView that takes care of the integration between three.js and the Google Maps JavaScript API. It lets you create a Google Map overlays directly with three.js.
TypeScript
55
star
7

google.maps.polyline.edit

Enables Google Maps API V3 Polyline Editing
JavaScript
33
star
8

mobile-range-slider

A lightweight JavaScript range slider that works on mobile devices such as iOS or Android.
JavaScript
20
star
9

google-maps-api-svg-overlay

SVG Overlay for the Google Maps JavaScript API v3
JavaScript
16
star
10

google-maps-deckgl-overlay

An example for using a Google Map with Deck.gl
JavaScript
13
star
11

geolocation-remote

Geolocation Remote - Control the Location of Your Mobile Device
JavaScript
11
star
12

node-geobatch

Batch geocode addresses from multiple sources.
JavaScript
10
star
13

esa-climate-from-space

Climate from Space application for ESA's CCI+ program.
TypeScript
9
star
14

node-stagger

Execute a stack with a given "request-per-seconds" and "max" rate.
JavaScript
9
star
15

project-template

The project creation tooling used at Ubilabs
JavaScript
9
star
16

refinery-custom-page-parts

Custom page parts for refinery
Ruby
8
star
17

soccer-debs-challenge

The ACM DEBS 2013 Grand Challenge
JavaScript
8
star
18

webpack-node-modules-list

Exports all used node modules of a webpack bundle to a file.
JavaScript
8
star
19

node-batch-geocoder

Node.js Batch Geocoder for the Google Maps API
JavaScript
7
star
20

node-image-saver

Saves an Image Data URI Back to the File System
JavaScript
7
star
21

sunzi-recipes

Our server provisioning recipes for sunzi.
Shell
6
star
22

node-parallel-transform-stream

A NodeJS transform stream which runs transformations in parallel and preserves input order.
JavaScript
6
star
23

flickr_geocoding_bookmarklet

A Bookmarklet for Better Geocoding within Flickr using Google Maps
JavaScript
6
star
24

grunt-gcloud

A wrapper for the google-gcloud node module.
JavaScript
5
star
25

icons_generator

Multiple Icons Generator using Ruby and ImageMagick
Ruby
5
star
26

touchstates

jQuery Touch State Plugin
JavaScript
4
star
27

kirby-object-storage-stream-wrappers

Prototype to run a Kirby CMS instance with data on Object Storage (GCS)
4
star
28

google-map-bounds-limit

Limits zoom and panning of a google map
JavaScript
4
star
29

cookbooks

Our Fancy Chef Cookbooks
Ruby
3
star
30

fastbillr

Ruby Api wrapper for the fastbill.com API
Ruby
3
star
31

stackenblochen

A grid system for rectanglers.
CSS
2
star
32

jquery-touchevents

jQuery Plugin to Proxy Touch Events
JavaScript
2
star
33

esa-webgl-globe

TypeScript
2
star
34

google-maps-visualrefresh

Comparing the new and old Google Maps styles.
1
star
35

node-google-maps-api-stream

A streaming, rate-limited, and caching interface to Google Maps APIs.
JavaScript
1
star
36

template

HTML, CSS and JavaScript Templates
JavaScript
1
star
37

nagios-plugins

A collection of custom Nagios plugins that we use @ubilabs.
Shell
1
star
38

image-performance

Test Image Rendering Performance on Various Browsers
1
star
39

fusion_wiki

1
star
40

storycamp

Basecamp Story Card Printer for Google Chrome
JavaScript
1
star
41

retrobox

Ruby
1
star
42

basic_server_stack

Ruby
1
star
43

gdd

1
star
44

node-api-stream

Create streaming, rate-limited APIs with ease.
JavaScript
1
star
45

ubilabs.github.com

1
star
46

binary-view.js

Binary schemes for JavaScript. Let's you define (write and load) binary formats
JavaScript
1
star
47

drone_elixir_example

Example to show some examples on drone.io. This one is for elixir
Elixir
1
star
48

babylonian

A Mixed Languages Map Type
1
star