Implementation of the Perlin Noise Algorithm
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
1.7 KiB

//implemtantion of dijkstra algorithm
var max = Number.MAX_SAFE_INTEGER;
function findNodeWithMinDist(list){
var result = "";
var dist = max;
for(var i = 0; i < list.length; i++){
for(var j = 0; j < list[i].length; j++){
if(dist < list[i][j]){
dist = list[i][j];
result = i+"$"+j;
}
}
}
return result;
}
function getNeighbours(array, i,j){
var result = [];
if(i - 1 > -1 && i+1 < array.length){
if(j-1 > -1 && j+1 < array.length){
for(var k = -1; k < 2; k++){
for(var l = -1; l < 2; l){
if(array[i][j].accessible){
result.push(i+"$"+j);
}
}
}
}
} else {
for(var k = -1; k < 2; k++){
for(var l = -1; l < 2; l){
try{
if(array[i][j].accessible){
result.push(i+"$"+j);
}
} catch(e){
console.log("zugriff auserhalb des feldes");
}
}
}
}
return result;
}
/**
*
*/
function dijkstra(array, start_i, start_j, end_i,end_j){
var Q = new Set();
var distance = [];
var pre = [];
for(var i = 0; i < array.length; i++){
for(var j = 0; j < array[i].length; j++){
distance[i][j] = max;
pre[i][j] = null;
Q.add(i+"$"+j);
}
}
distance[start_i][start_j] = 0;
while !(Q.size === 0){
var node = findNodeWithMinDist(distance);
Q.delete(node);
var indices = node.split("$");
var neighbours = getNeighbours(array,indicies[0],indices[1]);
for(var i = 0; i < indices.length; i++){
var alt = dist[indices[0]][indices[1]]+1;
if(Q.has(neighbours[i])){
var indices_neighbour = neighbour[i].split("$");
if(alt < dist[indices_neighbour[i]][indices_neighbour[j]]){
dist[indicies_neighbour[i]][indices_neighbour[j]] = alt;
pre[indices_neighbour[i]][indices_neighbour[j]] = node;
}
}
}
}
return {dist: distance[], pre: pre[]};
}