Skip to content

1 - subdivide bug, 2 - node.js compatibility, 3 - retrieveInBounds #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 104 additions & 5 deletions JavaScript/QuadTree/src/QuadTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,67 @@ QuadTree.prototype.retrieve = function(item)
{
//get a copy of the array of items
var out = this.root.retrieve(item).slice(0);
//return QuadTree._filterResults(out, {x:item.x, y:item.y, width:0, height:0});
return out;
}

QuadTree.prototype.retrieveInBounds = function (bounds)
{
var treeResult = this.root.retrieveInBounds(bounds);
return QuadTree._filterResults(treeResult, bounds);
}

QuadTree._filterResults = function(treeResult, bounds)
{
var filteredResult = [];

if(this.root instanceof BoundsNode)
{
for (var i=0; i < treeResult.length; i++)
{
var node = treeResult[i];
if (QuadTree._isBoundOverlappingBound(node, bounds))
{
filteredResult.push(node);
}
}
}
else
{
for (var i=0; i < treeResult.length; i++)
{
var node = treeResult[i];
if(QuadTree._isPointInsideBounds(node, bounds))
{
filteredResult.push(node);
}
}
}

return filteredResult;
}

QuadTree._isPointInsideBounds = function (point, bounds)
{
return (
(point.x >= bounds.x) &&
(point.x <= bounds.x + bounds.width) &&
(point.y >= bounds.y) &&
(point.y <= bounds.y + bounds.height)
);
}


QuadTree._isBoundOverlappingBound = function (b1, b2)
{
return !(
b1.x > (b2.x + b2.width) ||
b2.x > (b1.x + b1.width) ||
b1.y > (b2.y + b2.height) ||
b2.y > (b1.y + b1.height)
);
}

/************** Node ********************/


Expand Down Expand Up @@ -199,6 +257,47 @@ Node.prototype.retrieve = function(item)
return this.children;
}

Node.prototype.retrieveInBounds = function(bounds)
{
var result = [];

if(this.collidesWith(bounds))
{
result = result.concat(this._stuckChildren);

if(this.children.length)
{
result = result.concat(this.children);
}
else
{
if(this.nodes.length)
{
for (var i = 0; i < this.nodes.length; i++)
{
result = result.concat(this.nodes[i].retrieveInBounds(bounds));
}
}
}
}

return result;
}


Node.prototype.collidesWith = function (bounds)
{
var b1 = this._bounds;
var b2 = bounds;

return !(
b1.x > (b2.x + b2.width) ||
b2.x > (b1.x + b1.width) ||
b1.y > (b2.y + b2.height) ||
b2.y > (b1.y + b1.height)
);
}

Node.prototype._findIndex = function(item)
{
var b = this._bounds;
Expand Down Expand Up @@ -255,7 +354,7 @@ Node.prototype.subdivide = function()
width:b_w_h,
height:b_h_h
},
depth);
depth, this._maxDepth, this._maxChildren);

//top right
this.nodes[Node.TOP_RIGHT] = new this._classConstructor({
Expand All @@ -264,7 +363,7 @@ Node.prototype.subdivide = function()
width:b_w_h,
height:b_h_h
},
depth);
depth, this._maxDepth, this._maxChildren);

//bottom left
this.nodes[Node.BOTTOM_LEFT] = new this._classConstructor({
Expand All @@ -273,7 +372,7 @@ Node.prototype.subdivide = function()
width:b_w_h,
height:b_h_h
},
depth);
depth, this._maxDepth, this._maxChildren);


//bottom right
Expand All @@ -283,7 +382,7 @@ Node.prototype.subdivide = function()
width:b_w_h,
height:b_h_h
},
depth);
depth, this._maxDepth, this._maxChildren);
}

Node.prototype.clear = function()
Expand Down Expand Up @@ -428,4 +527,4 @@ if ( typeof Object.getPrototypeOf !== "function" ) {
}
*/

}(window));
}(this));