$.deepClone = function (obj) {
return JSON.parse(JSON.stringify(obj))
}
$.getNodePoint = function (node, position) {
var x = node.nodestyle.x;
var y = node.nodestyle.y;
var width = node.nodeWidth;
var height = node.nodeHeight;
var point = null;
switch (position) {
case "top":
point = {
x: x + (width / 2), y: y - 20
}
break;
case "left":
point = {
x: x - 20, y: y + (height / 2)
}
break;
case "right":
point = {
x: x + width + 20, y: y + (height / 2)
}
break;
case "bottom":
point = {
x: x + (width / 2), y: y + height + 20
}
break;
default:
break;
}
return point;
}
$.getTwoNodePointList = function (beginNode, endNode) {
var pointList = [];
var bx = beginNode.nodestyle.x;
var by = beginNode.nodestyle.y;
var ex = endNode.nodestyle.x;
var ey = endNode.nodestyle.y;
var xList = [bx - 20, bx + beginNode.nodeWidth + 20, ex - 20, ex + endNode.nodeWidth + 20];
var yList = [by - 20, by + beginNode.nodeHeight + 20, ey - 20, ey + endNode.nodeHeight + 20];
xList = $.unique(xList);
yList = $.unique(yList);
$(xList).each(function (xIndex, xItem) {
$(yList).each(function (yIndex, yItem) {
pointList.push({
x: xItem, y: yItem
});
});
});
return pointList;
}
$.generateManhattanPath = function (beginPoint, endPoint, pointList) {
//曼彻斯特路由算法,求出路径
var pathList = [];
var currentPoint = $.deepClone(beginPoint);
var endX = endPoint.x;
var endY = endPoint.y;
pathList.push($.deepClone(beginPoint));
while (currentPoint.x != endPoint.x && currentPoint.y != endPoint.y) {
if (currentPoint.x != endPoint.x && currentPoint.y != endPoint.y) {
if (currentPoint.x < endX) {
//水平向右边
var returnPoint = $.getHorizontalPointList(endX, currentPoint.y, pointList, "desc");
if (returnPoint != null && returnPoint != undefined) {
currentPoint = returnPoint;
pathList.push($.deepClone(returnPoint));
}
} else if (currentPoint.x > endX) {
//水平向左边
var returnPoint = $.getHorizontalPointList(endX, currentPoint.y, pointList, "asc");
if (returnPoint != null && returnPoint != undefined) {
currentPoint = returnPoint;
pathList.push($.deepClone(returnPoint));
}
}
}
if (currentPoint.x != endPoint.x && currentPoint.y != endPoint.y) {
if (currentPoint.y < endY) {
//向下
var returnPoint = $.getVerticalPointList(endY, currentPoint.x, pointList, "desc");
if (returnPoint != null && returnPoint != undefined) {
currentPoint = returnPoint;
pathList.push($.deepClone(returnPoint));
}
} else if (currentPoint.y > endY) {
//向上
var returnPoint = $.getVerticalPointList(endY, currentPoint.x, pointList, "asc");
if (returnPoint != null && returnPoint != undefined) {
currentPoint = returnPoint;
pathList.push($.deepClone(returnPoint));
}
}
}
}
pathList.push($.deepClone(endPoint));
return pathList;
}
$.getHorizontalPointList = function (endx, y, pointList, order) {
var newPointList = pointList.filter(function (point) {
if (order == "desc") {
//水平向右边
return point.y == y && point.x <= endx;
} else {
//水平向左边
return point.y == y && point.x >= endx;
}
}).sort(function (a, b) {
if (order == "desc") {
return b.x - a.x;
} else {
return a.x - b.x;
}
});
return newPointList[0];
}
$.getVerticalPointList = function (endy, x, pointList, order) {
var newPointList = pointList.filter(function (point) {
if (order == "desc") {
//向下
return point.x == x && point.y <= endy;
} else {
//向上
return point.x == x && point.y >= endy;
}
}).sort(function (a, b) {
if (order == "desc") {
return b.y - a.y;
} else {
return a.y - b.y;
}
});
return newPointList[0];
}
改良版曼哈顿路由算法
于 2024-07-15 21:26:12 首次发布