-
Notifications
You must be signed in to change notification settings - Fork 2
/
GUI.cpp
executable file
·445 lines (442 loc) · 19.6 KB
/
GUI.cpp
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#include<SFML/Graphics.hpp>
#include<SFML/Audio.hpp>
#include<SFML/Window.hpp>
#include<iostream>
#include<functional> //for std::bind
#include<math.h> //Astar libraries
#include<float.h>
#include<vector>
#include<set>
#include<string.h>
#include<string>
#include<sstream>
using namespace std;
using namespace sf;
#define num 60 //number of cells in a row
//--------dijkstra--------
vector<pair<int,int>> pathD; //Shortest pathD
bool sptSet[num][num]; //explored cells
void findmin(float dist[num][num],int& min_x,int& min_y){
float mini=FLT_MAX;
for(int i=0;i<num;i++)
for(int j=0;j<num;j++)
if(sptSet[i][j]==false && dist[i][j]<mini){
mini=dist[i][j];
min_x=i;
min_y=j;
}
}
void findpath(pair<int,int> previous[num][num],float dist[num][num],int dest_x,int dest_y,int source_x,int source_y){
cout<<"\nLength of Dijkstra path is: "<<dist[dest_x][dest_y]<<endl;
while(previous[dest_x][dest_y].first!=source_x || previous[dest_x][dest_y].second!=source_y){ // both simultaneously equal to source coordinates
sf::sleep(milliseconds(10)); //delay shortest pathD
cout<<"go to ->\n("<<previous[dest_x][dest_y].first<<","<<previous[dest_x][dest_y].second<<") ";
pathD.push_back(make_pair(previous[dest_x][dest_y].first,previous[dest_x][dest_y].second));
int save_x=dest_x,save_y=dest_y;
dest_x=previous[save_x][save_y].first;
dest_y=previous[save_x][save_y].second;
}
}
void dijkstra(int source_x,int source_y,int dest_x,int dest_y,int grid[num][num]){
pair<int,int> previous[num][num];
float dist[num][num];
for(int i=0;i<num;i++)
for(int j=0;j<num;j++)
dist[i][j]=FLT_MAX;
dist[source_x][source_y]=0.0;
int found=0;
for(int i=0;i<num && found==0;i++)
for(int j=0;j<num && found==0;j++){
int min_x=0,min_y=0;
findmin(dist,min_x,min_y);
sptSet[min_x][min_y]=true;
if(sptSet[dest_x][dest_y]==true){
found=1;
break;
}
sf::sleep(milliseconds(1)); //delay exploration
//north
if(grid[min_x-1][min_y]==1 && sptSet[min_x-1][min_y]==false && dist[min_x-1][min_y]>dist[min_x][min_y]+1.0){
dist[min_x-1][min_y]=dist[min_x][min_y]+1.0;
previous[min_x-1][min_y]=make_pair(min_x,min_y);
}
//south
if(grid[min_x+1][min_y]==1 && sptSet[min_x+1][min_y]==false && dist[min_x+1][min_y]>dist[min_x][min_y]+1.0){
dist[min_x+1][min_y]=dist[min_x][min_y]+1.0;
previous[min_x+1][min_y]=make_pair(min_x,min_y);
}
//west
if(grid[min_x][min_y-1]==1 && sptSet[min_x][min_y-1]==false && dist[min_x][min_y-1]>dist[min_x][min_y]+1.0){
dist[min_x][min_y-1]=dist[min_x][min_y]+1.0;
previous[min_x][min_y-1]=make_pair(min_x,min_y);
}
//east:i,j+1
if(grid[min_x][min_y+1]==1 && sptSet[min_x][min_y+1]==false && dist[min_x][min_y+1]>dist[min_x][min_y]+1.0){
dist[min_x][min_y+1]=dist[min_x][min_y]+1.0;
previous[min_x][min_y+1]=make_pair(min_x,min_y);
}
//north-east:i-1,j+1
if(grid[min_x-1][min_y+1]==1 && sptSet[min_x-1][min_y+1]==false &&
dist[min_x-1][min_y+1]>dist[min_x][min_y]+sqrt(2) && !(grid[min_x-1][min_y]==0 && grid[min_x][min_y+1]==0)){
dist[min_x-1][min_y+1]=dist[min_x][min_y]+sqrt(2);
previous[min_x-1][min_y+1]=make_pair(min_x,min_y);
}
//south-east:i+1,j+1
if(grid[min_x+1][min_y+1]==1 && sptSet[min_x+1][min_y+1]==false &&
dist[min_x+1][min_y+1]>dist[min_x][min_y]+sqrt(2) && !(grid[min_x+1][min_y]==0 && grid[min_x][min_y+1]==0)){
dist[min_x+1][min_y+1]=dist[min_x][min_y]+sqrt(2);
previous[min_x+1][min_y+1]=make_pair(min_x,min_y);
}
//south-west:i+1,j-1
if(grid[min_x+1][min_y-1]==1 && sptSet[min_x+1][min_y-1]==false &&
dist[min_x+1][min_y-1]>dist[min_x][min_y]+sqrt(2) && !(grid[min_x+1][min_y]==0 && grid[min_x][min_y-1]==0)){
dist[min_x+1][min_y-1]=dist[min_x][min_y]+sqrt(2);
previous[min_x+1][min_y-1]=make_pair(min_x,min_y);
}
//north-west:i-1,j-1
if(grid[min_x-1][min_y-1]==1 && sptSet[min_x-1][min_y-1]==false &&
dist[min_x-1][min_y-1]>dist[min_x][min_y]+sqrt(2) && !(grid[min_x-1][min_y]==0 && grid[min_x][min_y-1]==0)){
dist[min_x-1][min_y-1]=dist[min_x][min_y]+sqrt(2);
previous[min_x-1][min_y-1]=make_pair(min_x,min_y);
}
}
findpath(previous,dist,dest_x,dest_y,source_x,source_y);
}
//--------Astar--------
typedef pair<int,int> Pair;
typedef pair<float,pair<int,int>> Ppair;
bool closedList[num][num];
vector<Pair> pathA;
struct cell{
int parent_x,parent_y;
float f,g,h;
cell() : f(FLT_MAX),g(FLT_MAX),h(FLT_MAX),parent_x(-1),parent_y(-1) {};
};
bool isDestination(int row,int col,Pair dest){
if(row==dest.first && col==dest.second)
return true;
else
return false;
}
float calculateHvalue(int row,int col,Pair dest){
int dx=abs(dest.first-row);
int dy=abs(dest.second-col);
return abs(dx-dy)+sqrt(2)*min(dx,dy); //Diagonal D=1,D2=sqrt(2)
}
void tracePath(Pair source,Pair dest,cell cellDetails[][num]){
int i=cellDetails[dest.first][dest.second].parent_x,j=cellDetails[dest.first][dest.second].parent_y;
while(!(i==source.first && j==source.second)){
sf::sleep(milliseconds(10)); //delay shortest path
cout<<i<<","<<j<<" to -> \n";
pathA.push_back(make_pair(i,j));
//pathSum=pathSum+cellDetails[i][j].g;
int temp_i=i;
int temp_j=j;
i=cellDetails[temp_i][temp_j].parent_x; //Solved substitution bug
j=cellDetails[temp_i][temp_j].parent_y;
}
cout<<"\nLength of A* path(g) is: "<<cellDetails[dest.first][dest.second].g<<endl;
}
void Astar(Pair source,Pair dest,int grid[][num]){
set<Ppair> openList;
cell cellDetails[num][num];
int i= source.first,j=source.second;
cellDetails[i][j].f=0.0;
cellDetails[i][j].g=0.0;
cellDetails[i][j].h=0.0;
cellDetails[i][j].parent_x=i;
cellDetails[i][j].parent_y=j;
openList.insert(make_pair(0.0,make_pair(i,j)));
bool destFound=false;
while(!openList.empty()){
Ppair p=*openList.begin();
openList.erase(openList.begin());
int i=p.second.first,j=p.second.second;
closedList[i][j]=true;
sf::sleep(milliseconds(1)); //delay exploration
if(isDestination(i,j,dest)==true){
cout<<"Destination Found\n";
destFound=true;
break; //out of while loop
}
// North:i-1,j
if(grid[i-1][j]==1 && closedList[i-1][j]==false){
cell successor;
successor.g=cellDetails[i][j].g+1.0;
successor.h=calculateHvalue(i-1,j,dest);
successor.f=successor.g+successor.h;
successor.parent_x=i;
successor.parent_y=j;
if(cellDetails[i-1][j].g>successor.g){ //Not in openList or bigger 'g' in openList
cellDetails[i-1][j]=successor;
openList.insert(make_pair(successor.f,make_pair(i-1,j)));
}
}
// East:i,j+1
if(grid[i][j+1]==1 && closedList[i][j+1]==false){
cell successor;
successor.g=cellDetails[i][j].g+1.0;
successor.h=calculateHvalue(i,j+1,dest);
successor.f=successor.g+successor.h;
successor.parent_x=i;
successor.parent_y=j;
if(cellDetails[i][j+1].g>successor.g){ //Not in openList or bigger 'g' in openList
cellDetails[i][j+1]=successor;
openList.insert(make_pair(successor.f,make_pair(i,j+1)));
}
}
// South:i+1,j
if(grid[i+1][j]==1 && closedList[i+1][j]==false){
cell successor;
successor.g=cellDetails[i][j].g+1.0;
successor.h=calculateHvalue(i+1,j,dest);
successor.f=successor.g+successor.h;
successor.parent_x=i;
successor.parent_y=j;
if(cellDetails[i+1][j].g>successor.g){ //Not in openList or bigger 'g' in openList
cellDetails[i+1][j]=successor;
openList.insert(make_pair(successor.f,make_pair(i+1,j)));
}
}
// West:i,j-1
if(grid[i][j-1]==1 && closedList[i][j-1]==false){
cell successor;
successor.g=cellDetails[i][j].g+1.0;
successor.h=calculateHvalue(i,j-1,dest);
successor.f=successor.g+successor.h;
successor.parent_x=i;
successor.parent_y=j;
if(cellDetails[i][j-1].g>successor.g){ //Not in openList or bigger 'g' in openList
cellDetails[i][j-1]=successor;
openList.insert(make_pair(successor.f,make_pair(i,j-1)));
}
}
// North-East:i-1,j+1
if(grid[i-1][j+1]==1 && closedList[i-1][j+1]==false && !(grid[i-1][j]==0 && grid[i][j+1]==0)){ //not simultaneously blocked
cell successor;
successor.g=cellDetails[i][j].g+sqrt(2);
successor.h=calculateHvalue(i-1,j+1,dest);
successor.f=successor.g+successor.h;
successor.parent_x=i;
successor.parent_y=j;
if(cellDetails[i-1][j+1].g>successor.g){ //Not in openList or bigger 'g' in openList
cellDetails[i-1][j+1]=successor;
openList.insert(make_pair(successor.f,make_pair(i-1,j+1)));
}
}
// South-East:i+1,j+1
if(grid[i+1][j+1]==1 && closedList[i+1][j+1]==false && !(grid[i+1][j]==0 && grid[i][j+1]==0)){
cell successor;
successor.g=cellDetails[i][j].g+sqrt(2);
successor.h=calculateHvalue(i+1,j+1,dest);
successor.f=successor.g+successor.h;
successor.parent_x=i;
successor.parent_y=j;
if(cellDetails[i+1][j+1].g>successor.g){ //Not in openList or bigger 'g' in openList
cellDetails[i+1][j+1]=successor;
openList.insert(make_pair(successor.f,make_pair(i+1,j+1)));
}
}
// South-West:i+1,j-1
if(grid[i+1][j-1]==1 && closedList[i+1][j-1]==false && !(grid[i+1][j]==0 && grid[i][j-1]==0)){
cell successor;
successor.g=cellDetails[i][j].g+sqrt(2);
successor.h=calculateHvalue(i+1,j-1,dest);
successor.f=successor.g+successor.h;
successor.parent_x=i;
successor.parent_y=j;
if(cellDetails[i+1][j-1].g>successor.g){ //Not in openList or bigger 'g' in openList
cellDetails[i+1][j-1]=successor;
openList.insert(make_pair(successor.f,make_pair(i+1,j-1)));
}
}
// North-West:i-1,j-1
if(grid[i-1][j-1]==1 && closedList[i-1][j-1]==false && !(grid[i-1][j]==0 && grid[i][j-1]==0)){
cell successor;
successor.g=cellDetails[i][j].g+sqrt(2);
successor.h=calculateHvalue(i-1,j-1,dest);
successor.f=successor.g+successor.h;
successor.parent_x=i;
successor.parent_y=j;
if(cellDetails[i-1][j-1].g>successor.g){ //Not in openList or bigger 'g' in openList
cellDetails[i-1][j-1]=successor;
openList.insert(make_pair(successor.f,make_pair(i-1,j-1)));
}
}
}
if(destFound==false)
cout<<"Destination cell not found.\n";
else
tracePath(source,dest,cellDetails);
}
//--------main()--------
int main(){
int filled[num][num]; //whether cell is colored
int grid[60][60]; //map with obstacle
for(int i=0;i<60;i++)
for(int j=0;j<60;j++){
if(i==0||i==59||j==0||j==59) //walls
grid[i][j]=0;
else
grid[i][j]=1;
}
for(int i=0;i<num;i++)
for(int j=0;j<num;j++){
sptSet[i][j]=false; //dijkstra all unexplored
filled[i][j]=0; //all uncolored
}
memset(closedList,false,sizeof(closedList)); //Astar all unexplored
int source_x=2,source_y=2,dest_x=50,dest_y=56; //Origin(2,3)->Goal(50,56)
Thread threadD(std::bind(&dijkstra,source_x,source_y,dest_x,dest_y,grid));
Thread threadA(std::bind(&Astar,make_pair(source_x,source_y),make_pair(dest_x,dest_y),grid));
RenderWindow window(VideoMode(800,600),"Grid");
// Music
// sf::Music music;
// sf::Music music2;
// if(!music.openFromFile("music.ogg"))
// cout<<"Music loading failed!\n";
// if(!music2.openFromFile("move.ogg"))
// cout<<"Music move loading failed!\n";
// // Text
sf::Font font;
font.loadFromFile("arial.ttf");
sf::Text text1("DIJKSTRA",font,15);
sf::Text text2("A*",font,24);
sf::Text lengthD(" ",font,24);
sf::Text lengthA(" ",font,24);
// text1.setFillColor(Color::Black);
// text2.setFillColor(Color::Black);
// lengthD.setFillColor(Color::Black);
// lengthA.setFillColor(Color::Black);
// Shapes
RectangleShape buttonStartD(Vector2f(75,25)); //button dijkstra
buttonStartD.setFillColor(Color::Green);
RectangleShape buttonStartA(Vector2f(75,25)); //button Astar
buttonStartA.setFillColor(Color::Magenta);
RectangleShape displayD(Vector2f(75,25)); //Dijkstra display
displayD.setFillColor(Color::White);
RectangleShape displayA(Vector2f(75,25)); //Astar display
displayA.setFillColor(Color::White);
RectangleShape rectangle(Vector2f(10,10)); //default box :White
rectangle.setFillColor(Color::White);
RectangleShape brectangle(Vector2f(10,10)); //Black box
brectangle.setFillColor(Color::Black);
RectangleShape grectangle(Vector2f(10,10)); //Green
grectangle.setFillColor(Color::Green);
grectangle.setOutlineThickness(2);
grectangle.setOutlineColor(Color::Red);
RectangleShape mrectangle(Vector2f(10,10)); //Magenta
mrectangle.setFillColor(Color::Magenta);
mrectangle.setOutlineThickness(2);
mrectangle.setOutlineColor(Color::Red);
RectangleShape blueRectangle(Vector2f(10,10));
blueRectangle.setFillColor(Color::Blue);
blueRectangle.setOutlineThickness(2);
blueRectangle.setOutlineColor(Color::Red);
RectangleShape rrectangle(Vector2f(10,10));
rrectangle.setFillColor(Color::Red);
rrectangle.setOutlineThickness(2);
rrectangle.setOutlineColor(Color::Red);
RectangleShape yrectangle(Vector2f(10,10));
yrectangle.setFillColor(Color::Yellow);
// Display
while(window.isOpen()){
Event event;
while(window.pollEvent(event)){
if(event.type==Event::Closed)
window.close();
if(event.type==Event::KeyPressed&& event.key.code==Keyboard::Space)
window.close();
if(event.type==Event::MouseButtonPressed&&event.mouseButton.button==Mouse::Left){
int X=event.mouseButton.x;
int Y=event.mouseButton.y;
int row=Y/10; //Reversed notion of row & column
int col=X/10;
if(grid[row][col]==0&&row<60&&col<60)
grid[row][col]=1;
else if(row<60&&col<60)
grid[row][col]=0;
cout<<"Cell "<<row<<" , "<<col<<" state is: "<<grid[row][col]<<endl;
if(X>600&&X<675&&Y>0&&Y<25){
threadD.launch();
// music.play();
}
if(X>600&&X<675&&Y>75&&Y<100){
threadA.launch();
// music2.play();
}
}
}
window.clear();
buttonStartD.setPosition(600,0);
window.draw(buttonStartD); //Dijkstra launch
buttonStartA.setPosition(600,75);
window.draw(buttonStartA); //Astar launch
displayD.setPosition(725,0); //Dijkstra length Background
window.draw(displayD);
displayA.setPosition(725,75);
window.draw(displayA);
text1.setPosition(600,0); //Dijkstra text
text2.setPosition(630,75);
lengthD.setPosition(725,0); //Display Dijkstra length
lengthA.setPosition(725,75);
window.draw(text1);
window.draw(text2);
stringstream ss1,ss2;
ss1<<pathD.size(); //int2string
lengthD.setString(ss1.str());
ss2<<pathA.size();
lengthA.setString(ss2.str());
window.draw(lengthD);
window.draw(lengthA);
if(!pathA.empty()){
for(int i=0;i<pathA.size();i++){
mrectangle.setPosition(pathA[i].second*10,pathA[i].first*10); //Reversed notion of row & column
window.draw(mrectangle); //final pathA
filled[pathA[i].first][pathA[i].second]=1;
}
}
if(!pathD.empty()){
for(int i=0;i<pathD.size();i++){
grectangle.setPosition(pathD[i].second*10,pathD[i].first*10); //Reversed notion of row & column
window.draw(grectangle); //final pathD
filled[pathD[i].first][pathD[i].second]=1;
}
}
blueRectangle.setPosition(source_y*10,source_x*10);
window.draw(blueRectangle); //source
filled[source_x][source_y]=1;
rrectangle.setPosition(dest_y*10,dest_x*10);
window.draw(rrectangle); //destination
filled[dest_x][dest_y]=1;
for(int i=0;i<=590 ;i+=10)
for(int j=0;j<=590;j+=10){
if(grid[i/10][j/10]==0){
brectangle.setOutlineThickness(2);
brectangle.setOutlineColor(Color::Red);
brectangle.setPosition(j,i);
window.draw(brectangle); //User's obstacle
}
if(sptSet[i/10][j/10]==true && filled[i/10][j/10]==0){
yrectangle.setOutlineThickness(2);
yrectangle.setOutlineColor(Color::Red);
yrectangle.setPosition(j,i);
window.draw(yrectangle); // Explored Cells by dijkstra
}
if(closedList[i/10][j/10]==true && filled[i/10][j/10]==0){
yrectangle.setOutlineThickness(2);
yrectangle.setOutlineColor(Color::Red);
yrectangle.setPosition(j,i);
window.draw(yrectangle); // Explored Cells by A*
}
if(grid[i/10][j/10]==1 && sptSet[i/10][j/10]==false && closedList[i/10][j/10]==false && filled[i/10][j/10]==0){ //not in dijkstra & A*
rectangle.setOutlineThickness(2);
rectangle.setOutlineColor(Color::Red);
rectangle.setPosition(j,i);
window.draw(rectangle); //default white cells
}
}
window.display();
}
return 0;
}