-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
151 lines (120 loc) · 2.64 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
var canvas = document.querySelector("canvas");
canvas.height= window.innerHeight;
canvas.width= 1000;
//console.log("size of canvas= "+canvas.width+", "+canvas.height);
var c = canvas.getContext('2d');
var rows,cols;
var size=20;
var fontSize="2px Arial";
var textXpostion=1;
var arr=[];
var mouseDown=false;
var mouseUp=true;
var mouseMove=false;
var blockedPath=[];
var mX=0;
var mY=0;
var startpoint=0;
var start_point=false;
var end_point=false;
var block_point=false;
var startFindingPath=false;
var visitedCell=[];
var unvisitedCell=[];
var minValue=0;
var minValueIndex=0;
var pathIndex=undefined;
//current Cell
var currentCell={
x:undefined,
y:undefined,
index:undefined
};
var startCell={
x:undefined,
y:undefined,
index:undefined
};
var endCell={
x:undefined,
y:undefined,
index:undefined
};
//conrolling grid here
var Grid=function(size){
rows=Math.floor(canvas.height/size);
cols=Math.floor(canvas.width/size);
//console.log(cols+" "+rows);
var i=0;
for(var x=0;x<rows;x++){
for(var y=0;y<cols;y++){
// var cell= new Cell(x,y);
arr.push(new Cell(x,y,i));
i++;
}
}
}
var grid= new Grid(size);
render();
function startPoint(){
start_point=true;
end_point=false;
block_point=false;
startFindingPath=false;
console.log("startpoint");
}
function blockPoint(){
block_point=true;
start_point=false;
end_point=false;
startFindingPath=false;
console.log("blockPoint");
}
function endPoint(){
end_point=true;
block_point=false;
start_point=false;
startFindingPath=false;
console.log("endpoint");
}
function findPath(){
end_point=false;
block_point=false;
start_point=false;
startFindingPath=true;
animate();
console.log("startFindingPath");
}
function animate(){
if(startFindingPath==true){
dijktraPath();
}
}
function dijktraPath(){
if(arr.length!=0){
requestAnimationFrame(animate);
if(visitedCell.includes(endCell.index)){
//do nothing
console.log("found it");
arr[pathIndex].smallestWeight();
pathIndex=arr[pathIndex].previous;
}
else{
// console.log("current cell index: "+startpoint);
var nextCell=nextNeig(arr[startpoint].X,arr[startpoint].Y,startpoint);
visitedCell.push(startpoint);
arr[startpoint].visitedCellColor();
document.getElementById('pathWeight').innerHTML=arr[startpoint].currentWeight;
unvisitedCell.splice(startpoint,1);
startpoint=nextCell;
}
}
}
function render(){
//generating the grid
for(var i=0;i<arr.length;i++){
arr[i].draw();
unvisitedCell.push(i);
//console.log(arr[i].X);
}
}