<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>A-Star</title>
<script type="text/javascript">
/*
蛮好玩的东西。。。
严重感谢winter。。。
*/
var AStar = function () {
this.tmin = -1; //上部搜索最小值(不包括)
this.rmax = -1; //右部搜索最大值(不包括)
this.bmax = -1; //下部搜索最大值(不包括)
this.lmin = -1; //左部搜索最小值(不包括)
this.initialize();
};
AStar.prototype = {
constructor : AStar
, pointSource : function () {
//获取点对象构造的引用
return this.constructor.Point;
}
, euclidean : function (a, b) {
with (Math) { return round(10 * sqrt(pow(a.X - b.X, 2) + pow(a.Y - b.Y, 2))); }
}
, manhattan : function (a, b) {
with (Math) { return abs(a.X - b.X) + abs(a.Y - b.Y); }
}
, initialize : function () {
//初始化参数
this.open = []; //open表
this.close = []; //close表
this.maps = {}; //已经发现点组
}
, makeID : function (x, y, limit) {
//创造ID
return x + y * limit;
}
, getMinNode : function () {
//获取最佳节点
var i = 1, o = this.open, l = o.length, min = i - 1, max = o[0].F, t = null;
for (; i < l ; i ++) {
t = o[i];
if (t.F < max) { max = t.F, min = i; }
}
t = o[min], o[min] = o[l - 1], o.pop();
return t;
}
, getNodes : function (node) {
//获取子节点
var map = this.Map, tmin = this.tmin, rmax = this.rmax, bmax = this.bmax, lmin = this.lmin, nodes = []
, x = node.X, y = node.Y, t = y - 1, r = x + 1, b = y + 1, l = x - 1
, _t = t > tmin && (map[t][x] === 0)
, _r = r < rmax && (map[y][r] === 0)
, _b = b < bmax && (map[b][x] === 0)
, _l = l > lmin && (map[y][l] === 0), i = 0;
if (_t) {
nodes[i++] = [x, t];
if (_l && (map[t][l] === 0)) nodes[i++] = [l, t];
if (_r && (map[t][r] === 0)) nodes[i++] = [r, t];
}
if (_l) nodes[i++] = [l, y];
if (_b) {
nodes[i++] = [x, b];
if (_l && (map[b][l] === 0)) nodes[i++] = [l, b]