Skip to content

Commit

Permalink
code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
timohausmann committed Mar 3, 2020
1 parent 5ab2f4d commit 7f78143
Showing 1 changed file with 186 additions and 186 deletions.
372 changes: 186 additions & 186 deletions quadtree.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,206 +30,206 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

;(function() {
/*
* Quadtree Constructor
* @param Object bounds bounds of the node { x, y, width, height }
* @param Integer max_objects (optional) max objects a node can hold before splitting into 4 subnodes (default: 10)
* @param Integer max_levels (optional) total max levels inside root Quadtree (default: 4)
* @param Integer level (optional) deepth level, required for subnodes (default: 0)
*/
function Quadtree( bounds, max_objects, max_levels, level ) {
this.max_objects = max_objects || 10;
this.max_levels = max_levels || 4;
this.level = level || 0;
this.bounds = bounds;
this.objects = [];
this.nodes = [];
};
/*
* Split the node into 4 subnodes
*/
Quadtree.prototype.split = function() {
var nextLevel = this.level + 1,
subWidth = this.bounds.width/2,
subHeight = this.bounds.height/2,
x = this.bounds.x,
y = this.bounds.y;
//top right node
this.nodes[0] = new Quadtree({
x : x + subWidth,
y : y,
width : subWidth,
height : subHeight
}, this.max_objects, this.max_levels, nextLevel);
//top left node
this.nodes[1] = new Quadtree({
x : x,
y : y,
width : subWidth,
height : subHeight
}, this.max_objects, this.max_levels, nextLevel);
//bottom left node
this.nodes[2] = new Quadtree({
x : x,
y : y + subHeight,
width : subWidth,
height : subHeight
}, this.max_objects, this.max_levels, nextLevel);
//bottom right node
this.nodes[3] = new Quadtree({
x : x + subWidth,
y : y + subHeight,
width : subWidth,
height : subHeight
}, this.max_objects, this.max_levels, nextLevel);
};
/*
* Determine which node the object belongs to
* @param Object pRect bounds of the area to be checked, with x, y, width, height
* @return Array an array of indexes of the intersecting subnodes
* (0-3 = top-right, top-left, bottom-left, bottom-right / ne, nw, sw, se)
*/
Quadtree.prototype.getIndex = function( pRect ) {
var indexes = [],
verticalMidpoint = this.bounds.x + (this.bounds.width / 2),
horizontalMidpoint = this.bounds.y + (this.bounds.height / 2);
/*
* Quadtree Constructor
* @param Object bounds bounds of the node { x, y, width, height }
* @param Integer max_objects (optional) max objects a node can hold before splitting into 4 subnodes (default: 10)
* @param Integer max_levels (optional) total max levels inside root Quadtree (default: 4)
* @param Integer level (optional) deepth level, required for subnodes (default: 0)
*/
function Quadtree(bounds, max_objects, max_levels, level) {
this.max_objects = max_objects || 10;
this.max_levels = max_levels || 4;
this.level = level || 0;
this.bounds = bounds;
this.objects = [];
this.nodes = [];
};
/*
* Split the node into 4 subnodes
*/
Quadtree.prototype.split = function() {
var nextLevel = this.level + 1,
subWidth = this.bounds.width/2,
subHeight = this.bounds.height/2,
x = this.bounds.x,
y = this.bounds.y;
//top right node
this.nodes[0] = new Quadtree({
x : x + subWidth,
y : y,
width : subWidth,
height : subHeight
}, this.max_objects, this.max_levels, nextLevel);
//top left node
this.nodes[1] = new Quadtree({
x : x,
y : y,
width : subWidth,
height : subHeight
}, this.max_objects, this.max_levels, nextLevel);
//bottom left node
this.nodes[2] = new Quadtree({
x : x,
y : y + subHeight,
width : subWidth,
height : subHeight
}, this.max_objects, this.max_levels, nextLevel);
//bottom right node
this.nodes[3] = new Quadtree({
x : x + subWidth,
y : y + subHeight,
width : subWidth,
height : subHeight
}, this.max_objects, this.max_levels, nextLevel);
};
/*
* Determine which node the object belongs to
* @param Object pRect bounds of the area to be checked, with x, y, width, height
* @return Array an array of indexes of the intersecting subnodes
* (0-3 = top-right, top-left, bottom-left, bottom-right / ne, nw, sw, se)
*/
Quadtree.prototype.getIndex = function(pRect) {
var indexes = [],
verticalMidpoint = this.bounds.x + (this.bounds.width/2),
horizontalMidpoint = this.bounds.y + (this.bounds.height/2);

var startIsNorth = pRect.y < horizontalMidpoint,
startIsWest = pRect.x < verticalMidpoint,
endIsEast = pRect.x + pRect.width > verticalMidpoint,
endIsSouth = pRect.y + pRect.height > horizontalMidpoint;
endIsSouth = pRect.y + pRect.height > horizontalMidpoint;

//top-right quad
if(startIsNorth && endIsEast) {
indexes.push(0);
//top-right quad
if(startIsNorth && endIsEast) {
indexes.push(0);
}

//top-left quad
if(startIsWest && startIsNorth) {
indexes.push(1);
}
//top-left quad
if(startIsWest && startIsNorth) {
indexes.push(1);
}

//bottom-left quad
if(startIsWest && endIsSouth) {
indexes.push(2);
}
//bottom-left quad
if(startIsWest && endIsSouth) {
indexes.push(2);
}

//bottom-right quad
if(endIsEast && endIsSouth) {
indexes.push(3);
}
return indexes;
};
/*
* Insert the object into the node. If the node
* exceeds the capacity, it will split and add all
* objects to their corresponding subnodes.
* @param Object pRect bounds of the object to be added { x, y, width, height }
*/
Quadtree.prototype.insert = function( pRect ) {
var i = 0,
indexes;
//if we have subnodes, call insert on matching subnodes
if(this.nodes.length) {
indexes = this.getIndex( pRect );
for(i=0; i<indexes.length;i++) {
this.nodes[indexes[i]].insert( pRect );
}
return;
}
//otherwise, store object here
this.objects.push( pRect );
//bottom-right quad
if(endIsEast && endIsSouth) {
indexes.push(3);
}
return indexes;
};
/*
* Insert the object into the node. If the node
* exceeds the capacity, it will split and add all
* objects to their corresponding subnodes.
* @param Object pRect bounds of the object to be added { x, y, width, height }
*/
Quadtree.prototype.insert = function(pRect) {
var i = 0,
indexes;
//if we have subnodes, call insert on matching subnodes
if(this.nodes.length) {
indexes = this.getIndex(pRect);
for(i=0; i<indexes.length; i++) {
this.nodes[indexes[i]].insert(pRect);
}
return;
}
//otherwise, store object here
this.objects.push(pRect);

//max_objects reached
if( this.objects.length > this.max_objects && this.level < this.max_levels ) {
//max_objects reached
if(this.objects.length > this.max_objects && this.level < this.max_levels) {

//split if we don't already have subnodes
if(!this.nodes.length) {
this.split();
}
//add all objects to their corresponding subnode
for(i=0; i<this.objects.length; i++) {
indexes = this.getIndex( this.objects[ i ] );
for(var k=0; k<indexes.length;k++) {
this.nodes[indexes[k]].insert( this.objects[i] );
}
}
//split if we don't already have subnodes
if(!this.nodes.length) {
this.split();
}
//add all objects to their corresponding subnode
for(i=0; i<this.objects.length; i++) {
indexes = this.getIndex(this.objects[i]);
for(var k=0; k<indexes.length; k++) {
this.nodes[indexes[k]].insert(this.objects[i]);
}
}

//clean up this node
this.objects = [];
}
};
/*
* Return all objects that could collide with the given object
* @param Object pRect bounds of the object to be checked { x, y, width, height }
* @Return Array array with all detected objects
*/
Quadtree.prototype.retrieve = function( pRect ) {
var indexes = this.getIndex( pRect ),
returnObjects = this.objects;
//if we have subnodes, retrieve their objects
if(this.nodes.length) {
for(var i=0; i<indexes.length;i++) {
returnObjects = returnObjects.concat( this.nodes[indexes[i]].retrieve( pRect ) );
}
}
//clean up this node
this.objects = [];
}
};
/*
* Return all objects that could collide with the given object
* @param Object pRect bounds of the object to be checked { x, y, width, height }
* @Return Array array with all detected objects
*/
Quadtree.prototype.retrieve = function(pRect) {
var indexes = this.getIndex(pRect),
returnObjects = this.objects;
//if we have subnodes, retrieve their objects
if(this.nodes.length) {
for(var i=0; i<indexes.length; i++) {
returnObjects = returnObjects.concat(this.nodes[indexes[i]].retrieve(pRect));
}
}

//remove duplicates
returnObjects = returnObjects.filter(function(item, index) {
return returnObjects.indexOf(item) >= index;
});
return returnObjects;
};
/*
* Clear the quadtree
*/
Quadtree.prototype.clear = function() {
this.objects = [];
for( var i=0; i < this.nodes.length; i=i+1 ) {
if(this.nodes.length) {
this.nodes[i].clear();
}
}
//remove duplicates
returnObjects = returnObjects.filter(function(item, index) {
return returnObjects.indexOf(item) >= index;
});
return returnObjects;
};
/*
* Clear the quadtree
*/
Quadtree.prototype.clear = function() {
this.objects = [];
for(var i=0; i < this.nodes.length; i++) {
if(this.nodes.length) {
this.nodes[i].clear();
}
}

this.nodes = [];
};
this.nodes = [];
};

//export for commonJS or browser
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = Quadtree;
} else {
window.Quadtree = Quadtree;
}
//export for commonJS or browser
if(typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = Quadtree;
} else {
window.Quadtree = Quadtree;
}

})();

0 comments on commit 7f78143

Please sign in to comment.