JS uses canvas to draw the interface.
Define two classes: node save point and F=g+h value in a * algorithm;
/**
* node *
P : node corresponding coordinates
* PN : node's parent
* g : a*-g
* H : a*-h/
function node (p, PN, G, h) {
if (!) ( P instanceof Point) | | isNaN (g) | | isNaN (h)) {
throw new Error ("illegal parameter");
}
THIS.G = g;
This.h = h;
THIS.F = g + H;
THIS.P = p;
THIS.PN = PN;
This.equals = function (obj) {
if (!) ( obj instanceof Node)) {return
false;
}
Return This.p.equals (OBJ.P);
}
/**
* dot
/function point (x, y) {
if (isNaN (x) | | | isNaN (y)) {
throw new Error ("illegal parameter");
}
this.x = x;
This.y = y;
This.equals = function (obj) {
if (!) ( obj instanceof Point)] {return
false;
}
return this.x = = obj.x && This.y = = obj.y;
}
Algorithm implementation: Explore whether the new node-> is accessed. Skip: (Has been logged.) (Whether an update is required.) Update: "" To join the Open list): "" Until the end of the search to the end or the map says a bit is accessed by the loop.
Seek while
(true) {
if (openlist.length = 0) {break
;
}
var node = Openlist.shift ();
if (Node.p.equals (constants). End_point)) {
rsnode = node;
break;
var Nodearr = utils.explore (node, constants. Map_array, constants. End_point);
Closelist.push (node);
Determine if the node being explored has been accessed if it has not been accessed, add it to the Open list for
(var i in Nodearr) {
var n = nodearr[i];
Determine if the open list already exists whether the node needs to be updated if
(Utils.inarray (n, closelist) = = 1) {
var off = Utils.inarray (n, openlist);
if (off = = 1) {
openlist.push (n);
} Else{
var u = openlist[off];
This node has been explored and there is now a better path
if (U.f > N.f) {
openlist.splice (off, 0);
Openlist.push (n);
}}} Openlist.sort (Utils.sortnode);
}
Source: http://download.csdn.net/detail/lj745280746/7007935
Reference: Http://wenku.baidu.com/link?url= 0qhr1lg5ngtqast6e4j12zenvc4a5wiybpd3mrfkznvp7z7nukljagegnpdumy-il7dedpjjwbehkjmjiogde5emrulbyhycfd7rw0ubpsc