-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.js
193 lines (180 loc) · 5.25 KB
/
draw.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
function draw() {
if (initialised) {
//algorithm for Dijkstra
if (method == "Dijkstra") {
if (openSet.length > 0) {
current = lowestDVal(); //returns node with least d value
//No path from the source to destination node with a finite distance
if (current.d === Infinity) {
console.log("no solution");
noLoop();
return;
}
//Found the destination
if (current === dest) {
noLoop();
console.log("We're Done!");
}
//Remove the "current" vertex from openSet and add it to closedSet
var removeIndex = openSet
.map(function (item) {
return item;
})
.indexOf(current);
openSet.splice(removeIndex, 1);
closedSet.push(current);
for (neighbor of current.neighbors) {
//checking if the node is valid
if (!neighbor.obstacle) {
dScore = current.d + 1;
if (dScore < neighbor.d) {
neighbor.d = dScore;
neighbor.parent = current;
}
}
}
}
}
// algorithm for A* Search
if (method == "A* Search") {
if (openSet.length > 0) {
current = lowestFVal();
if (current == dest) {
noLoop();
console.log("We're Done!");
}
//removing the "current" vertex from openSet and adding it to closedSet
var removeIndex = openSet
.map(function (item) {
return item;
})
.indexOf(current);
openSet.splice(removeIndex, 1);
closedSet.push(current);
for (neighbor of current.neighbors) {
//checking to see if the node is valid
if (!closedSet.includes(neighbor) && !neighbor.obstacle) {
gScore = current.g + heuristic(neighbor, current);
let isGbetter = false;
if (openSet.includes(neighbor)) {
if (gScore < neighbor.g) {
neighbor.g = gScore;
isGbetter = true;
}
} else {
neighbor.g = gScore;
isGbetter = true;
openSet.push(neighbor);
}
if (isGbetter) {
neighbor.h = heuristic(neighbor, dest);
neighbor.f = neighbor.g + neighbor.h;
neighbor.parent = current;
}
}
}
} else {
console.log("no solution");
noLoop();
return;
}
}
//algorithm for Breadth First Search
if (method == "Breadth First Search") {
if (openSet.length > 0) {
current = openSet[0];
if (current == dest) {
noLoop();
console.log("We're Done!");
}
//removing the "current" vertex from openSet and adding it to closedSet
var removeIndex = openSet
.map(function (item) {
return item;
})
.indexOf(current);
openSet.splice(removeIndex, 1);
console.log(openSet);
for (neighbor of current.neighbors) {
if (!closedSet.includes(neighbor) && !neighbor.obstacle) {
openSet.push(neighbor);
closedSet.push(neighbor);
neighbor.parent = current;
}
}
} else {
console.log("no solution");
noLoop();
return;
}
}
//algorithm for Depth First Search
if (method == "Depth First Search") {
if (openSet.length > 0) {
console.log(openSet);
current = openSet[openSet.length - 1];
if (current == dest) {
noLoop();
console.log("We're Done!");
}
//removing the "current" vertex from openSet and adding it to closedSet
var removeIndex = openSet
.map(function (item) {
return item;
})
.indexOf(current);
openSet.splice(removeIndex, 1);
console.log(openSet);
for (neighbor of current.neighbors) {
if (!closedSet.includes(neighbor) && !neighbor.obstacle) {
openSet.push(neighbor);
closedSet.push(neighbor);
neighbor.parent = current;
}
}
} else {
console.log("no solution");
noLoop();
return;
}
}
background(255);
//displaying the grid on monitor
for (let i = 0; i < col; i++) {
for (let j = 0; j < row; j++) {
grid[i][j].show(255);
}
}
//coloring the visited, unvisited nodes and the shortest path
for (node of openSet) {
if (method === "Dijkstra") {
if (node.d != Infinity) {
node.show(color(45, 196, 129));
}
} else {
node.show(color(45, 196, 129));
}
}
for (node of closedSet) {
node.show(color(255, 0, 0, 50));
}
//initializing shortestPath array
shortestPath = [];
let temp = current;
shortestPath.push(temp);
while (temp.parent) {
shortestPath.push(temp.parent);
temp = temp.parent;
}
noFill();
stroke(255, 0, 200);
strokeWeight(4);
beginShape();
for (path of shortestPath) {
vertex(path.i * res + res / 2, path.j * res + res / 2);
}
endShape();
src.show(color(87, 50, 168));
dest.show(color(140, 68, 20));
}
}