1.0.3 • Published 8 years ago

yy-spatialmap v1.0.3

Weekly downloads
3
License
MIT
Repository
github
Last release
8 years ago

spatialMap

2D Spatial Map for javascript

Live Example with Source

https://davidfig.github.io/spatialmap/

Installation

npm install yy-spatialmap

API Reference

SpatialMap

Kind: global class

new SpatialMap(cellSize, width, height, options)

2D spatial map

ParamTypeDescription
cellSizenumberused to create map
widthnumberof world
heightnumberof world
optionsobject
options.updatebooleankeep a second list of elements to enable update

Example

var map = new SpatialMap();

// use your own 2D graphics libraries--I like PIXI.js
var circle = new Circle(10, 10, 5);

// each object must have an AABB bounding box [x1, y1, x2, y2],
// see yy-intersects (github:davidfig/intersects) for a library that provides this with various shapes
circle.AABB = [5, 5, 10, 10];
map.insert(circle);

// returns the circle
var results = map.query([0, 0, 10, 10]);

// or iterate over the results to avoid creating new arrays
map.query([0, 0, 10, 10],
 function(object)
 {
     object.draw();
 }
);

spatialMap.insert(object, AABB)

inserts an object into the map (also removes object from last insertion) side effect: adds object.spatial to track last insertion

Kind: instance method of SpatialMap

ParamTypeDescription
objectobject
AABBArray.<number>bounding box x1, y1, x2, y2

spatialMap.remove(object)

removes existing object from the map

Kind: instance method of SpatialMap

ParamType
objectobject

spatialMap.query(AABB) ⇒ Array.<object>

returns an array of objects contained within bounding box NOTE: this may include duplicates

Kind: instance method of SpatialMap
Returns: Array.<object> - search results

ParamTypeDescription
AABBArray.<number>bounding box to search x1, y1, x2, y2

spatialMap.queryCallbackArticle(AABB, callback) ⇒ boolean

iterates through objects in the same buckets as article stops iterating if the callback returns true

Kind: instance method of SpatialMap
Returns: boolean - true if callback returned early

ParamTypeDescription
AABBArray.<number>bounding box to search x1, y1, x2, y2
callbackfunction

spatialMap.queryCallback(AABB, callback) ⇒ boolean

iterates through objects contained within bounding box stops iterating if the callback returns true

Kind: instance method of SpatialMap
Returns: boolean - true if callback returned early

ParamTypeDescription
AABBArray.<number>bounding box to search x1, y1, x2, y2
callbackfunction

spatialMap.getAverageSize() ⇒ number

helper function to evaluate proper cell size

Kind: instance method of SpatialMap
Returns: number - the average number of entries in each bucket

spatialMap.getLargest() ⇒ number

helper function to evaluate proper cell size

Kind: instance method of SpatialMap
Returns: number - the largest sized bucket

spatialMap.getBuckets() ⇒ Array.<object>

helper function to evaluate SpatialMap

Kind: instance method of SpatialMap
Returns: Array.<object> - an array of buckets in the form of x1, y1, x2, y2


Copyright (c) 2016 YOPEY YOPEY LLC - MIT License - Documented by jsdoc-to-markdown

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago