-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
347 lines (334 loc) · 12.7 KB
/
main.cpp
File metadata and controls
347 lines (334 loc) · 12.7 KB
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
#include<SFML/Graphics.hpp>
#include<SFML/Audio.hpp>
#include<SFML/Window.hpp>
#include<iostream>
#include<functional>
#include<math.h>
#include<float.h>
#include<vector>
#include<set>
#include<string.h>
#include<string>
#include<sstream>
using namespace std;
using namespace sf;
#define num 60
vector<pair<int,int> > pathD;
float dist[num][num];
bool sptSet[num][num];
int weight=10;
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){
sf::sleep(milliseconds(10));
cout << "Visiting x = " << previous[dest_x][dest_y].first << " " << "and y = " << previous[dest_x][dest_y].second << endl;
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];
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));
int possibleX[] = {0, 0, 1, -1, 1, -1, -1, 1};
int possibleY[] = {1, -1, 0, 0, 1, 1, -1, -1};
for(int i = 0; i < 8; ++i) {
int newRow = min_x + possibleX[i];
int newCol = min_y + possibleY[i];
if( grid[newRow][newCol]==weight && sptSet[newRow][newCol]==false && dist[newRow][newCol]>dist[min_x][min_y]+1.0){
dist[newRow][newCol]=weight+dist[min_x][min_y];
previous[newRow][newCol]=make_pair(min_x,min_y);
}
else if(grid[newRow][newCol]==1 && sptSet[newRow][newCol]==false && dist[newRow][newCol]>dist[min_x][min_y]+1.0){
dist[newRow][newCol]=dist[min_x][min_y]+1.0;
previous[newRow][newCol]=make_pair(min_x,min_y);
}
}
}
}
if(found==1){
findpath(previous,dist,dest_x,dest_y,source_x,source_y);
}
else{
cout<<"Cell Not Found"<<endl;
}
return;
}
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);
}
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));
cout << "Visiting x = " << i << " " << "and y = " << j << endl;
pathA.push_back(make_pair(i,j));
int temp_i=i;
int temp_j=j;
i=cellDetails[temp_i][temp_j].parent_x;
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;
int possibleX[] = {0, 0, 1, -1, 1, -1, -1, 1};
int possibleY[] = {1, -1, 0, 0, 1, 1, -1, -1};
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));
if(isDestination(i,j,dest)==true){
cout<<"Destination Found\n";
destFound=true;
break;
}
for(int k = 0; k < 8; ++k) {
int newRow = i + possibleX[k];
int newCol = j + possibleY[k];
if(grid[newRow][newCol]==1 && closedList[newRow][newCol]==false){
cell successor;
successor.g=cellDetails[i][j].g+1.0;
successor.h=calculateHvalue(newRow,newCol,dest);
successor.f=successor.g+successor.h;
successor.parent_x=i;
successor.parent_y=j;
if(cellDetails[newRow][newCol].g>successor.g){
cellDetails[newRow][newCol]=successor;
openList.insert(make_pair(successor.f,make_pair(newRow,newCol)));
}
}
if(grid[newRow][newCol]==weight && closedList[newRow][newCol]==false){
cell successor;
successor.g=cellDetails[i][j].g+weight;
successor.h=calculateHvalue(newRow,newCol,dest);
successor.f=successor.g+successor.h;
successor.parent_x=i;
successor.parent_y=j;
if(cellDetails[newRow][newCol].g>successor.g){
cellDetails[newRow][newCol]=successor;
openList.insert(make_pair(successor.f,make_pair(newRow,newCol)));
}
}
}
}
if(destFound==false)
cout<<"Destination cell not found.\n";
else
tracePath(source,dest,cellDetails);
}
/* main program */
int main(){
int filled[num][num];
int grid[60][60];
for(int i=0;i<60;i++)
for(int j=0;j<60;j++){
if(i==0||i==59||j==0||j==59)
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;
filled[i][j]=0;
}
memset(closedList,false,sizeof(closedList));
int source_x,source_y,dest_x,dest_y;
cout<<"Enter source index:";
cin>>source_x>>source_y;
cout<<"Enter destination index:";
cin>>dest_x>>dest_y;
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");
sf::Font font;
font.loadFromFile("arial.ttf");
sf::Text text1("DIJKSTRA",font,15);
sf::Text text2("A*",font,24);
RectangleShape buttonStartD(Vector2f(75,25));
buttonStartD.setFillColor(Color::Green);
RectangleShape buttonStartA(Vector2f(75,25));
buttonStartA.setFillColor(Color::Magenta);
RectangleShape rectangle(Vector2f(10,10));
rectangle.setFillColor(Color::White);
RectangleShape brectangle(Vector2f(10,10));
brectangle.setFillColor(Color::Black);
RectangleShape grectangle(Vector2f(10,10));
grectangle.setFillColor(Color::Green);
grectangle.setOutlineThickness(2);
grectangle.setOutlineColor(Color::Red);
RectangleShape mrectangle(Vector2f(10,10));
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::Black);
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);
RectangleShape weighted(Vector2f(10,10));
weighted.setFillColor(Color::Cyan);
// 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;
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;
if(row<60&col<60)cout<<"Cell "<<row<<" , "<<col<<" state is: "<<grid[row][col]<<endl;
if(X>600&&X<675&&Y>0&&Y<25){
threadD.launch();
}
if(X>600&&X<675&&Y>75&&Y<100){
threadA.launch();
}
}
if(event.type==Event::MouseButtonPressed && event.mouseButton.button==Mouse::Right){
int X=event.mouseButton.x;
int Y=event.mouseButton.y;
int row=Y/10;
int col=X/10;
if(grid[row][col]==1 &&row<60&&col<60)
grid[row][col]=weight;
else if(grid[row][col]==weight && row<60&&col<60)
grid[row][col]=1;
}
}
window.clear();
buttonStartD.setPosition(600,0);
window.draw(buttonStartD);
buttonStartA.setPosition(600,75);
window.draw(buttonStartA);
text1.setPosition(600,0);
text2.setPosition(630,75);
window.draw(text1);
window.draw(text2);
stringstream ss1,ss2;
ss1<<pathD.size();
ss2<<pathA.size();
if(!pathA.empty()){
for(int i=0;i<int(pathA.size());i++){
mrectangle.setPosition(pathA[i].second*10,pathA[i].first*10);
window.draw(mrectangle);
filled[pathA[i].first][pathA[i].second]=1;
}
}
if(!pathD.empty()){
for(int i=0;i<int(pathD.size());i++){
grectangle.setPosition(pathD[i].second*10,pathD[i].first*10);
window.draw(grectangle);
filled[pathD[i].first][pathD[i].second]=1;
}
}
blueRectangle.setPosition(source_y*10,source_x*10);
window.draw(blueRectangle);
filled[source_x][source_y]=1;
rrectangle.setPosition(dest_y*10,dest_x*10);
window.draw(rrectangle);
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]==weight){
weighted.setOutlineThickness(2);
weighted.setOutlineColor(Color::Red);
weighted.setPosition(j,i);
window.draw(weighted);
}
if(grid[i/10][j/10]==0){
brectangle.setOutlineThickness(2);
brectangle.setOutlineColor(Color::Red);
brectangle.setPosition(j,i);
window.draw(brectangle);
}
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);
}
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);
}
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){
rectangle.setOutlineThickness(2);
rectangle.setOutlineColor(Color::Red);
rectangle.setPosition(j,i);
window.draw(rectangle);
}
}
window.display();
}
return 0;
}