Skip to content

Commit

Permalink
added solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
peterkhayes committed Jul 29, 2014
1 parent 6a8d681 commit 546a4c7
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
71 changes: 71 additions & 0 deletions Quadtree/solutions/official solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
var Quadtree = function(box) {
this.box = box;
this.point = null;
this.SW = null;
this.SE = null;
this.NW = null;
this.NE = null;
};

Quadtree.prototype.insert = function(point) {
if (!this.point) {
this.point = point;
} else {
var quadrant = this.box.findQuadrantForPoint(point);
if (!this[quadrant]) {
this[quadrant] = new Quadtree(this.box.getQuadrant(quadrant));
}
this[quadrant].insert(point);
}
};

Quadtree.prototype.findPointsWithin = function(searchBox) {
var points = [];
if (this.point && searchBox.contains(this.point)) {
points.push(this.point);
}

if (this.SW && searchBox.overlaps(this.SW.box)) {
points = points.concat(this.SW.findPointsWithin(searchBox));
}
if (this.SE && searchBox.overlaps(this.SE.box)) {
points = points.concat(this.SE.findPointsWithin(searchBox));
}
if (this.NW && searchBox.overlaps(this.NW.box)) {
points = points.concat(this.NW.findPointsWithin(searchBox));
}
if (this.NE && searchBox.overlaps(this.NE.box)) {
points = points.concat(this.NE.findPointsWithin(searchBox));
}

return points;
};

Quadtree.prototype.findNearestPointTo = function(target) {
if (!this.point) {
return null;
}

var xDist = 1;
var yDist = 1;

var searchBox = new Box(target.x - xDist, target.y - yDist, target.x + xDist, target.y + yDist);

var points = this.findPointsWithin(searchBox);
while (points.length === 0) {
searchBox.expand();
points = this.findPointsWithin(searchBox);
}

var best = null;
var bestDist = Infinity;
for (var i = 0; i < points.length; i++) {
var point = points[i];
var dist = point.distanceTo(target);
if (dist < bestDist) {
bestDist = dist;
best = point;
}
}
return best;
};
4 changes: 4 additions & 0 deletions Quadtree/spec/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ describe("Quadtree tests", function() {
});

it("Works on large trees. Properly searches 360000 points, finding 3000.", function() {
var startTime = Date.now();
var points = bigFilledQuadtree.findPointsWithin(new Box(220, 280, 319, 309));
expect(points.length).to.be(3000);
expect(Date.now() - startTime).to.be.below(100);
});
});

Expand Down Expand Up @@ -203,12 +205,14 @@ describe("Quadtree tests", function() {
});

it("Works quickly. Performs 100 queries for random points (should be under 1000 ms)", function() {
var startTime = Date.now();
for (var i = 0; i < 100; i++) {
var x = (_.random(1000, 5000))/10 - 0.05;
var y = (_.random(1000, 5000))/10 - 0.05;
var point = bigFilledQuadtree.findNearestPointTo(new Point(x, y));
expectPoint(point, Math.round(x), Math.round(y));
}
expect(Date.now() - startTime).to.be.below(1000);
});
});

Expand Down

0 comments on commit 546a4c7

Please sign in to comment.