Dijkstra überarbeitet

master
Schoko 4 years ago
parent ad4e93c284
commit 5ace7839a0
  1. 57
      board.js
  2. 138
      dijkstra.js
  3. 3
      index.html

@ -9,14 +9,18 @@ var tilesColor = [water,beach,grass]
var board_numbers = []; var board_numbers = [];
var cycle = 50; var cycle = 50;
var tile_width=16; var tile_width=32;
var tile_height=16; var tile_height=32;
var width = Math.floor(1600 / tile_width); var width = Math.floor(640 / tile_width);
var height = Math.floor(800 / tile_height); var height = Math.floor(640 / tile_height);
var current_noise = []; var current_noise = [];
var currentBoardArray = []; var currentBoardArray = [];
var clickCounter = 0; var clickCounter = 0;
var startx = 0;
var starty = 0;
var endx = 0;
var endy = 0;
function findHeighestNumber(array){ function findHeighestNumber(array){
@ -100,7 +104,7 @@ function createBoard(height, width, array){
var board = document.getElementById("can"); var board = document.getElementById("can");
var context = board.getContext("2d"); var context = board.getContext("2d");
console.log(array); ////console.log(array);
for(var i = 0; i < height; i++){ for(var i = 0; i < height; i++){
for(var j = 0; j < width; j++){ for(var j = 0; j < width; j++){
@ -137,7 +141,7 @@ function generate_perlin_noise(rounds,height,width){
for(var r = 0; r < rounds; r++){ for(var r = 0; r < rounds; r++){
var tmp_array = []; var tmp_array = [];
//console.log("------------------ Step "+r+" --------------"); ////console.log("------------------ Step "+r+" --------------");
@ -148,11 +152,11 @@ function generate_perlin_noise(rounds,height,width){
var startValue = Math.random() * (maxValue - minValue) + minValue; //Stupid, but i always forgot, how to set the boundries var startValue = Math.random() * (maxValue - minValue) + minValue; //Stupid, but i always forgot, how to set the boundries
var counter = 0; var counter = 0;
for(var j = 0; j < width; j++){ for(var j = 0; j < width; j++){
//console.log(counter); ////console.log(counter);
//console.log("Step "+i+"+"+j+":"); ////console.log("Step "+i+"+"+j+":");
//console.log("counter: "+counter); ////console.log("counter: "+counter);
if (j % (points_width - 1) === 0 && !(j === 0) || j === (width - 1)){ if (j % (points_width - 1) === 0 && !(j === 0) || j === (width - 1)){
//console.log(counter); ////console.log(counter);
let endValue = Math.random() * (maxValue - minValue) + minValue; //Stupid, but i always forgot, how to set the boundries let endValue = Math.random() * (maxValue - minValue) + minValue; //Stupid, but i always forgot, how to set the boundries
interpolate(startValue, endValue, counter); interpolate(startValue, endValue, counter);
row = row.concat(interpolate(startValue, endValue, counter)); row = row.concat(interpolate(startValue, endValue, counter));
@ -162,7 +166,7 @@ function generate_perlin_noise(rounds,height,width){
counter++; counter++;
} }
} }
//console.log(row); ////console.log(row);
tmp_array.push(row); tmp_array.push(row);
} }
@ -192,7 +196,7 @@ function generate_perlin_noise(rounds,height,width){
points_width = Math.floor(points_width / 2)+1; points_width = Math.floor(points_width / 2)+1;
points_height = Math.floor(points_height / 2)+1; points_height = Math.floor(points_height / 2)+1;
} }
console.log(result); //console.log(result);
//return result; //return result;
@ -206,8 +210,8 @@ function generate_perlin_noise(rounds,height,width){
combi.push(rows); combi.push(rows);
} }
console.log("------------- Ergebnisse ----------- Ergebnisse -------------"); //console.log("------------- Ergebnisse ----------- Ergebnisse -------------");
console.log(combi); //console.log(combi);
// adding all array to one array // adding all array to one array
for(var r = 0; r < rounds; r++){ for(var r = 0; r < rounds; r++){
@ -230,8 +234,8 @@ function generate_perlin_noise(rounds,height,width){
// ???? Anomalie ???? // ???? Anomalie ????
console.log(result); //console.log(result);
console.log(combi); //console.log(combi);
return combi; return combi;
} }
@ -242,11 +246,12 @@ function generate_perlin_noise(rounds,height,width){
* */ * */
function run_perlin_noise(){ function run_perlin_noise(){
var noise_array = generate_perlin_noise(cycle,width,height); var noise_array = generate_perlin_noise(cycle,width,height);
console.log(noise_array); //console.log(noise_array);
current_noise = noise_array; current_noise = noise_array;
currentBoardArray = createBoardArray(current_noise, height, width); currentBoardArray = createBoardArray(current_noise, height, width);
createBoard(height,width,currentBoardArray); createBoard(height,width,currentBoardArray);
//currentBoardArray = createBoardArray(current_noise, height, width);
//dijkstra(currentBoardArray,startx,15,15,30,30);
} }
@ -283,16 +288,26 @@ function moveShip(canvas, e){
var y = Math.floor(e.clientY - rect.top); var y = Math.floor(e.clientY - rect.top);
var ship = new Ship(x,y,5); var ship = new Ship(x,y,5);
ship.draw(canvas.getContext("2d")); ship.draw(canvas.getContext("2d"));
console.log("x: "+x+" y: "+y); //console.log("x: "+x+" y: "+y);
} }
window.onload = (function(){ window.onload = (function(){
var can = document.getElementById("can"); var can = document.getElementById("can");
can.addEventListener('mousedown', function(e){ rect = can.getBoundingClientRect();
can.addEventListener('mousedown', function(e){
var x = Math.floor((e.clientX - rect.left)/tile_width);
var y = Math.floor((e.clientY - rect.top )/tile_height);
if(clickCounter === 2){ if(clickCounter === 2){
clickcounter = 1; clickcounter = 1;
endx = x;
endy = y;
console.log(startx+" "+starty+" : "+endx+" "+endy);
currentBoardArray = createBoardArray(current_noise, height, width);
dijkstra(currentBoardArray, startx, starty,endx,endy);
} else { } else {
clickCounter++; clickCounter++;
startx = x;
starty = y;
} }
createBoard(height,width,currentBoardArray); createBoard(height,width,currentBoardArray);
moveShip(can, e); moveShip(can, e);

@ -1,45 +1,47 @@
//implemtantion of dijkstra algorithm //implemtantion of dijkstra algorithm
var max = Number.MAX_SAFE_INTEGER; var max = Number.MAX_SAFE_INTEGER-100;
function findNodeWithMinDist(list){ function findNodeWithMinDistAndInQ(list, Q){
var result = ""; var result = "NaN";
var dist = max; var dist = max;
for(var i = 0; i < list.length; i++){ for(var i = 0; i < Q.length; i++){
for(var j = 0; j < list[i].length; j++){ //console.log(Q[i]);
if(dist < list[i][j]){ if(list.get(Q[i]) < dist){
dist = list[i][j]; dist = list.get(Q[i]);
result = i+"$"+j; result = Q[i];
}
} }
} }
//console.log("result: "+result);
return result; return result;
} }
function getNeighbours(array, i,j){ function getNeighbours(array, i,j){
var result = []; var result = [];
if(i - 1 > -1 && i+1 < array.length){ if((i-1) > -1 && (i+1) < array.length && (j-1) > -1 && j+1 < array[i].length){
if(j-1 > -1 && j+1 < array.length){ for(var k = -1; k < 2; k++){
for(var k = -1; k < 2; k++){ for(var l = -1; l < 2; l++){
for(var l = -1; l < 2; l){ var row =i+k;
if(array[i][j].accessible){ var coloumn=j+l;
result.push(i+"$"+j); if(array[row][coloumn].accessible){
} result.push(row+"$"+coloumn);
} }
} }
} }
} else { } else {
for(var k = -1; k < 2; k++){ for(var k = -1; k < 2; k++){
for(var l = -1; l < 2; l){ for(var l = -1; l < 2; l++){
var row =i+k;
var coloumn=j+l;
try{ try{
if(array[i][j].accessible){ if(array[row][coloumn].accessible){
result.push(i+"$"+j); result.push(row+"$"+coloumn);
} }
} catch(e){ } catch(e){
console.log("zugriff auserhalb des feldes"); ////console.log("zugriff auserhalb des feldes");
} }
} }
} }
@ -53,40 +55,96 @@ function getNeighbours(array, i,j){
* *
*/ */
function dijkstra(array, start_i, start_j, end_i,end_j){ function dijkstra(array, start_i, start_j, end_i,end_j){
var Q = new Set();
var distance = []; var Q = [];
var pre = []; var distance = new Map();
var pre = new Map();
for(var i = 0; i < array.length; i++){ for(var i = 0; i < array.length; i++){
for(var j = 0; j < array[i].length; j++){ for(var j = 0; j < array[i].length; j++){
distance[i][j] = max; var tmp = i+"$"+j;
pre[i][j] = null; distance.set(tmp, max);
Q.add(i+"$"+j); pre.set(tmp, null);
Q.push(tmp);
} }
} }
distance[start_i][start_j] = 0;
while !(Q.size === 0){ distance.set(start_i+"$"+start_j, 0);
var node = findNodeWithMinDist(distance); var t = 0;
Q.delete(node); while (Q.length !== 0){
var node = findNodeWithMinDistAndInQ(distance,Q);
if(node === "NaN"){
console.log(Q);
console.log(distance);
return null;
}
Q.splice(Q.indexOf(node),1);
var indices = node.split("$"); var indices = node.split("$");
var neighbours = getNeighbours(array,indicies[0],indices[1]); indices[0] = parseInt(10, indices[0]);
for(var i = 0; i < indices.length; i++){ indices[1] = parseInt(10, indices[1]);
var alt = dist[indices[0]][indices[1]]+1;
if(Q.has(neighbours[i])){ var neighbours = getNeighbours(array,indices[0],indices[1]);
var indices_neighbour = neighbour[i].split("$"); //console.log(node+" === "+end_i+"$"+end_j);
if(alt < dist[indices_neighbour[i]][indices_neighbour[j]]){
dist[indicies_neighbour[i]][indices_neighbour[j]] = alt; if(node.localeCompare(end_i+"$"+end_j) === 0){
pre[indices_neighbour[i]][indices_neighbour[j]] = node;
console.log("YAY");
console.log(t);
console.log(distance);
console.log(pre);
return reverse(end_i+"$"+end_j,start_i+"$"+start_j,pre);
}
for(var i = 0; i < neighbours.length; i++){
var alt = distance.get(node)+1;
if(Q.includes(neighbours[i])){
if(alt < distance.get(neighbours[i])){
distance.set(neighbours[i], alt);
pre.set(neighbours[i], node);
} }
} }
} }
//console.log(t);
t++;
}//
console.log(t);
console.log(distance);
console.log(pre);
return null;
}
function reverse(target, source, pre){
var path = [];
var u = target;
console.log("target: "+target);
console.log("source: "+source);
if(pre.get(u) != null || u === source){
console.log("hallo");
while(u != null){
console.log("hallo");
u = pre.get(u);
path = [u].concat(path);
}
} }
path.push(target);
path.shift();
console.log("!!!!")
console.log(path);
return path;
return {dist: distance[], pre: pre[]};
} }

@ -2,11 +2,12 @@
<html> <html>
<head> <head>
<title>Perlin Noise</title> <title>Perlin Noise</title>
<script src="dijkstra.js"></script>
<script src="board.js"></script> <script src="board.js"></script>
</head> </head>
<body> <body>
<h1> Perlin Noise</h1> <h1> Perlin Noise</h1>
<canvas id="can" width="1600" height="800" style="border:1px solid #000000;"></canvas> <canvas id="can" width="640" height="640" style="border:1px solid #000000;"></canvas>
<button id="new_btn"> Neu </button> <button id="new_btn"> Neu </button>
</body> </body>
</html> </html>

Loading…
Cancel
Save