JS implementation of a * Seek path algorithm __ static function

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.