Skip to content
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

Formatting #27

Merged
merged 9 commits into from
Jun 16, 2014
Merged
Show file tree
Hide file tree
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
36 changes: 16 additions & 20 deletions astar.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,11 @@ var astar = {
astar.init(graph);

options = options || {};
var heuristic = options.heuristic || astar.heuristics.manhattan;
var closest = options.closest || false;
var heuristic = options.heuristic || astar.heuristics.manhattan,
closest = options.closest || false;

var openHeap = astar.heap();

// set the start node to be the closest if required
var closestNode = start;
var openHeap = astar.heap(),
closestNode = start; // set the start node to be the closest if required

start.h = heuristic(start, end);

Expand All @@ -84,7 +82,7 @@ var astar = {
// Find all neighbors for the current node.
var neighbors = graph.neighbors(currentNode);

for(var i=0, il = neighbors.length; i < il; i++) {
for(var i = 0, il = neighbors.length; i < il; ++i) {
var neighbor = neighbors[i];

if(neighbor.closed || neighbor.isWall()) {
Expand All @@ -94,8 +92,8 @@ var astar = {

// The g score is the shortest distance from start to current node.
// We need to check if the path we have arrived at this neighbor is the shortest one we have seen yet.
var gScore = currentNode.g + neighbor.getCost(currentNode);
var beenVisited = neighbor.visited;
var gScore = currentNode.g + neighbor.getCost(currentNode),
beenVisited = neighbor.visited;

if(!beenVisited || gScore < neighbor.g) {

Expand Down Expand Up @@ -136,15 +134,15 @@ var astar = {
// See list of heuristics: http://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html
heuristics: {
manhattan: function(pos0, pos1) {
var d1 = Math.abs (pos1.x - pos0.x);
var d2 = Math.abs (pos1.y - pos0.y);
var d1 = Math.abs(pos1.x - pos0.x);
var d2 = Math.abs(pos1.y - pos0.y);
return d1 + d2;
},
diagonal: function(pos0, pos1) {
var D = 1;
var D2 = Math.sqrt(2);
var d1 = Math.abs (pos1.x - pos0.x);
var d2 = Math.abs (pos1.y - pos0.y);
var d1 = Math.abs(pos1.x - pos0.x);
var d2 = Math.abs(pos1.y - pos0.y);
return (D * (d1 + d2)) + ((D2 - (2 * D)) * Math.min(d1, d2));
}
}
Expand Down Expand Up @@ -322,7 +320,6 @@ BinaryHeap.prototype = {
// Update 'n' to continue at the new position.
n = parentN;
}

// Found a parent that is less, no need to sink any further.
else {
break;
Expand All @@ -337,11 +334,11 @@ BinaryHeap.prototype = {

while(true) {
// Compute the indices of the child elements.
var child2N = (n + 1) << 1, child1N = child2N - 1;
// This is used to store the new position of the element,
// if any.
var swap = null;
var child1Score;
var child2N = (n + 1) << 1,
child1N = child2N - 1;
// This is used to store the new position of the element, if any.
var swap = null,
child1Score;
// If the first child exists (is inside the array)...
if (child1N < length) {
// Look it up and compute its score.
Expand Down Expand Up @@ -369,7 +366,6 @@ BinaryHeap.prototype = {
this.content[swap] = element;
n = swap;
}

// Otherwise, we are done.
else {
break;
Expand Down
20 changes: 10 additions & 10 deletions benchmark/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ $(function() {
}
running = true;

var graph = new Graph(grid);
var start = graph.grid[0][0];
var end = graph.grid[140][140];
var results = [];
var times = 0;
var graph = new Graph(grid),
start = graph.grid[0][0],
end = graph.grid[140][140],
results = [],
times = 0;

for (var i = 0; i < 1000; i++) {
var startTime = performance ? performance.now() : new Date().getTime();
var result = astar.search(graph, start, end);
var endTime = performance ? performance.now() : new Date().getTime();
var startTime = performance ? performance.now() : new Date().getTime(),
result = astar.search(graph, start, end),
endTime = performance ? performance.now() : new Date().getTime();
times = times + (endTime - startTime);

results.push(
'<li>Found path with ' + result.length + ' steps. ' +
'Took ' + (endTime - startTime).toFixed(2) + ' milliseconds.</li>'
Expand Down
Loading