From 351c0e80978ddd1557e3f037cefa7f0e28dbdc2f Mon Sep 17 00:00:00 2001 From: Lee Williams Date: Tue, 20 Mar 2012 00:55:14 -0700 Subject: [PATCH 1/3] Add HTML5 geolocation, helper function for showing arbitrary lat/lon --- templates/game.html | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/templates/game.html b/templates/game.html index d9e0af8..8bc5afc 100644 --- a/templates/game.html +++ b/templates/game.html @@ -715,6 +715,16 @@ return ret; } + function showLocation(lat, lon) { + ll=lon - (portwidth / 2.0); + rr=ll+portwidth; + bb=lat - (portheight / 2.0); + tt=bb+portheight; + apply_transform(); + fetchTiles(); + redraw(); + } + $(document).ready( function() { $("#routemode").data("mode","route").css("background-color","AFE6AC"); @@ -783,6 +793,18 @@ event.returnValue = false; } + function setPosition(position) { + showLocation(position.coords.latitude, position.coords.longitude); + } + + if (navigator.geolocation) { + navigator.geolocation.getCurrentPosition(setPosition, function() { + console.log("geolocation error"); + }); + } else { + error('not supported'); + } + canvas = $("#canvas")[0] ctx = canvas.getContext("2d"); ctx.lineCap="round"; @@ -1006,13 +1028,14 @@

A warning!

This thing is a memory hog. Panning in particular will seriously crash your browser after a few minutes. I'll see what I can do.

Some places to go

Fancy information

From 067f421374635ee5bdf1e64e82e6cabfb4921a15 Mon Sep 17 00:00:00 2001 From: Lee Williams Date: Tue, 20 Mar 2012 02:55:00 -0700 Subject: [PATCH 2/3] Implement queue.extract_min using a binary heap --- templates/game.html | 151 +++++++++++++++++++++++++++++++++----------- 1 file changed, 113 insertions(+), 38 deletions(-) diff --git a/templates/game.html b/templates/game.html index 8bc5afc..39f454b 100644 --- a/templates/game.html +++ b/templates/game.html @@ -231,7 +231,7 @@ // insert into queue this.queue.insert( {'orig':best_edge_dest, 'dest':cand_dest_node,'way':cand_edge_data, 'edgeweight':cand_edge_weight}, cand_dest_node, best_edge_weight+cand_edge_weight ); - document.getElementById("queuesize").innerHTML=this.queue.size; + document.getElementById("queuesize").innerHTML=this.queue.size(); } } @@ -373,68 +373,143 @@ } - function Queue(){ - this.data = {}; - this.size=0; + // Binary MinHeap + function PriQueue() { + this.data = []; - this.insert = function(item,label,prio){ - this.size += 1; + this.insert = function(item, prio) { + this.data.push([item, prio]); + this.bubbleUp(this.data.length - 1); + } - if( this.data[label] !== undefined ) { - if(this.data[label][1]>prio){ - this.data[label] = [item,prio]; + this.getPriority = function(item) { + for (var i = 0; i < this.data.length; i++) { + if (this.data[i][0] == item) { + return this.data[i][1]; } - } else { - this.data[label] = [item,prio]; } - } + return null; + } - this.min = function() { - var min=null; - var minweight=10000000; - for( var item in this.data ){ - if ( this.data[item][1] < minweight ) { - minweight = this.data[item][1]; - min=item; + this.decreasePriority = function(item, prio) { + for (var i = 0; i < this.data.length; i++) { + if (this.data[i][0] == item) { + if(this.data[i][1] < prio) { + // you're not decreasing priority. + return; + } + this.data[i][1] = prio; + this.bubbleUp(i); + return; } } - return min; } - this.max = function() { - var max=null; - var maxweight=-10000000; - for( var item in this.data ){ - if ( this.data[item][1] > maxweight ) { - maxweight = this.data[item][1]; - min=item; + this.extract_min = function() { + var result = this.data[0]; + var end = this.data.pop(); + if (this.data.length > 0) { + this.data[0] = end; + this.sinkDown(0); + } + return result; + } + + this.size = function() { + return this.data.length; + } + + this.empty = function() { + return 0 === this.data.length; + } + + this.bubbleUp = function(offset) { + var el = this.data[offset]; + while(offset > 0) { + var parentOffset = Math.floor((offset + 1) / 2) - 1; + var parent = this.data[parentOffset]; + if(el[1] < parent[1]) { + this.data[parentOffset] = el; + this.data[offset] = parent; + offset = parentOffset; + } + else { + break; + } + } + } + + this.sinkDown = function(offset) { + var el = this.data[offset]; + while(true) { + // Compute the indices of the child elements. + var child2N = (offset + 1) * 2; + var child1N = child2N - 1; + + var swap = null; + + // If the first child exists (is inside the array)... + if (child1N < this.data.length) { + var child1 = this.data[child1N]; + if (child1[1] < el[1]) { + swap = child1N; + } + } + // Do the same checks for the other child. + if (child2N < this.data.length) { + var child2 = this.data[child2N]; + if (child2[1] < (swap == null ? el[1] : child1[1])) { + swap = child2N; + } + } + + // If the element needs to be moved, swap it, and continue. + if (swap != null) { + this.data[offset] = this.data[swap]; + this.data[swap] = el; + offset = swap; + } + // Otherwise, we are done. + else { + break; + } + } + } + } + + function Queue(){ + this.data = {}; + this.priQueue = new PriQueue(); + + this.insert = function(item,key,prio){ + if( this.data[key] !== undefined ) { + if(this.data[key][1] > prio) { + this.data[key] = [item,prio]; + this.priQueue.decreasePriority(key, prio); } + } else { + this.data[key] = [item,prio]; + this.priQueue.insert(key, prio); } - return min; } this.extract_min = function() { - this.size -= 1; - var minkey = this.min(); + var minkey = this.priQueue.extract_min()[0]; var ret = this.data[minkey]; delete this.data[minkey]; return ret; } - this.extract_max = function() { - this.size -= 1; - var maxkey = this.max(); - var ret = this.data[maxkey]; - delete this.data[maxkey]; - return ret; - } - this.empty = function() { for( var item in this.data ){ return false; } return true; } + + this.size = function() { + return this.priQueue.size(); + } } From cc85ef601ace7dc2cf4f80ce7a16c9056be190c4 Mon Sep 17 00:00:00 2001 From: Lee Williams Date: Tue, 20 Mar 2012 03:53:37 -0700 Subject: [PATCH 3/3] Small tweaks to attempt to improve rendering performance. --- templates/game.html | 107 +++++++++++++++++++++++--------------------- 1 file changed, 57 insertions(+), 50 deletions(-) diff --git a/templates/game.html b/templates/game.html index 39f454b..6b98760 100644 --- a/templates/game.html +++ b/templates/game.html @@ -5,6 +5,7 @@ +