-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph.cpp
762 lines (680 loc) · 22.1 KB
/
graph.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
#include <GL/glut.h>
#include <iostream>
#include <vector>
#include <queue>
#include <string>
using namespace std;
const int n = 40;
int INF = 9999;
#define SOURCE 1024
#define DEST 2024
#define WALL 3024
#define EMPTY 4024
#define CHECKED 5024
#define FOUND 6024
#define NOTFOUND 7024
#define PATH 8024
float screenWidth = 750;
float screenHeight = 750;
int clickCount = 0;
int prevX, prevY;
int isMouseLeftDown = 0;
int isMouseRightDown = 0;
int startEndNodes = 0;
double hVDistance = 1.0;
double dDistance = 1.4;
const int TIME_SECS = 1000;
const int SCREEN_FPS = 60;
bool canAcceptInput = true;
int flag = 0;
bool isOnStartPage = true;
class SPoint {
public:
int x=NULL;
int y=NULL;
SPoint(void) {
}
SPoint(int x, int y) {
this->x = x;
this->y = y;
}
};
class Node {
public:
int gridx=NULL;
int gridy=NULL;
double distance=INF;
int type=NULL;
bool visited=false;
SPoint parent = SPoint(NULL, NULL);
Node(void) {
}
Node(int x, int y) {
this->gridx = x;
this->gridy = y;
}
Node(int x, int y, int type, bool visited, int distance = INF) {
this->gridx = x;
this->gridy = y;
this->type = type;
this->distance = distance;
this->visited = visited;
}
int getType() {
return this->type;
}
int getGridPositionX() {
return this->gridx;
}
int getGridPositionY() {
return this->gridy;
}
int getDistance() {
return this->distance;
}
bool isEqual(Node s, Node d) {
if (s.getGridPositionX() == d.getGridPositionX() && s.getGridPositionY() == d.getGridPositionY()) {
return true;
}
return false;
}
};
bool operator<(const Node& n1, const Node& n2) {
return n1.distance > n2.distance;
}
vector<Node> nodes;
priority_queue<Node> nQueue;
Node mapn[n][n];
Node *start;
Node *endn;
vector<SPoint> path;
bool gridHasSource() {
for (int i = 0; i < n; i++) {
int type = nodes.at(i).getType();
if (type == SOURCE) {
return true;
break;
}
}
return false;
}
bool gridHasDest() {
for (int i = 0; i < n; i++) {
int type = nodes.at(i).getType();
if (type == DEST) {
return true;
break;
}
}
return false;
}
void clicked(int i, int j, int type) {
if (type != EMPTY) {
float dif = 0.1;
if (type == SOURCE)
{
glColor3f(0.258, 0.031, 0.388);
dif = -0.1;
}
else if (type == DEST) {
glColor3f(0.772, 0.447, 1);
dif = -0.1;
}
else if (type == WALL) {
glColor3f(0.047, 0.207, 0.278);
dif = 0.05;
}
else if (type == CHECKED) {
glColor3f(0.250, 0.807, 0.890);
dif = 0.08;
}
else if (type == PATH) {
glColor3f(1, 0.996, 0.415);
dif = 0.0;
}
glBegin(GL_POLYGON);
glVertex2f(i + dif, j + dif);
glVertex2f(i + dif, j - dif + 1);
glVertex2f(i - dif + 1, j - dif + 1);
glVertex2f(i - dif + 1, j + dif);
glEnd();
}
}
void reDrawPoints() {
for (size_t i = 0; i < nodes.size(); i++) {
int x = nodes[i].getGridPositionX();
int y = nodes[i].getGridPositionY();
//i == 0 ? clicked(x, y, i + 1) : clicked(x, y, i + 1);
clicked(x, y, nodes[i].getType());
}
}
bool hasNodePlaced(Node n) {
int type = n.getType();
if (type == PATH) {
return true;
}
if (type == SOURCE || type == DEST || type == WALL || type == CHECKED) {
return true;
}
return false;
}
void addPoints(int x, int y) {
int posx = x / (screenWidth / n);
int posy = y / (screenHeight / n);
for (size_t i = 0; i < nodes.size(); i++) {
int x = nodes.at(i).getGridPositionX();
int y = nodes.at(i).getGridPositionY();
if ((x == posx && y == posy) && !hasNodePlaced(nodes.at(i))) {
if (startEndNodes == 0) {
Node node = Node(posx, posy, SOURCE, true, 0);
nodes.at(i) = node;
startEndNodes = 1;
}
else if (startEndNodes == 1) {
Node node = Node(posx, posy, DEST, false, INF);
nodes.at(i) = node;
startEndNodes = 2;
}
else if (startEndNodes >= 2) {
Node node = Node(posx, posy, WALL, false, INF);
nodes.at(i) = node;
startEndNodes++;
}
break;
}
}
}
void removePoints(int x, int y) {
int posx = x / (screenWidth / n);
int posy = y / (screenHeight / n);
for (size_t i = 2; i < nodes.size(); i++) {
int x = nodes.at(i).getGridPositionX();
int y = nodes.at(i).getGridPositionY();
if (x == posx && y == posy && hasNodePlaced(nodes.at(i))) {
Node node = Node(posx, posy, EMPTY, false, INF);
nodes.at(i) = node;
startEndNodes--;
break;
}
}
}
void handleMouse(int button, int state, int x, int y) {
if (canAcceptInput) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
isMouseLeftDown = 0;
////cout << "Mouse Left Clicked at : x=" << x / 50 + 1 << " y=" << y / 50 + 1 << endl;
addPoints(x, y);
}
else if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
isMouseLeftDown = 1;
}
else if (button == GLUT_RIGHT_BUTTON && state == GLUT_UP) {
isMouseRightDown = 0;
removePoints(x, y);
}
else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
isMouseRightDown = 1;
}
glutPostRedisplay();
}
}
void makeReady() {
//for testing
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int x = nodes.at(j * n + i).getGridPositionX();
int y = nodes.at(j * n + i).getGridPositionY();
////cout << "(" << x << "," << y << ")" << " ";
}
////cout << endl;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int type = nodes.at(j * n + i).getType();
if (type == SOURCE) {
////cout << " " << "S" << " ";
start = &nodes.at(j * n + i);
}
else if (type == DEST) {
////cout << " " << "D" << " ";
endn = &nodes.at(j * n + i);
}
else if (type == WALL) {
////cout << " " << "W" << " ";
}
else if (type == EMPTY) {
////cout << " " << "I" << " ";
}
}
////cout << endl;
}
//Convert to 2d array
memset(mapn, 0, sizeof(mapn[0][0]) * n * n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
//int x = nodes.at(j * n + i).getGridPositionX();
//int y = nodes.at(j * n + i).getGridPositionY();
mapn[j][i] = nodes.at(j * n + i);
}
}
////cout << "Distmap is:" << endl;
//for (int i = 0; i < n; i++) {
// for (int j = 0; j < n; j++) {
// try {
// ////cout << "(" << mapn[i][j].getGridPositionX() << "," << mapn[i][j].getGridPositionY() << ")" << " ";
// }
// catch (const out_of_range& e) {
// }
// }
// ////cout << endl;
//}
////cout << "distance" << endl;
//for (int i = 0; i < n; i++) {
// for (int j = 0; j < n; j++) {
// try {
// ////cout << mapn[i][j].distance << " ";
// }
// catch (const out_of_range& e) {
// }
// }
// ////cout << endl;
//}
nQueue.push(*start);
////cout << "start node" << start.getGridPositionX() << "," << start.getGridPositionY() << endl;
}
void showPath(int value) {
//cout << " Path is : " << endl;
if (mapn[endn->getGridPositionX()][endn->getGridPositionY()].type != INF) {
Node current = mapn[endn->getGridPositionX()][endn->getGridPositionY()];
while (current.type != SOURCE && current.parent.x != NULL && current.parent.y != NULL) {
//cout << "Current node at end : " << current.parent.x << "," << current.parent.y << endl;
//path.push_back(current.parent);
current = mapn[current.parent.x][current.parent.y];
//nodes.at(current.getGridPositionX()* n + current.getGridPositionY()).type = PATH;
if (current.type != start->type) {
nodes.at(current.getGridPositionX() * n + current.getGridPositionY()).type = PATH;
}
}
glutPostRedisplay();
}
}
void drive(int value) {
if (value > 0 && !nQueue.empty()) {
Node current;
if (!nQueue.empty()) {
current = nQueue.top();
}
/////cout <<"Size Queue: "<<nQueue.size() << endl;/
//nQueue.pop();
Node t;
/*////cout << "End Node" << endn.getGridPositionX() << "," << endn.getGridPositionY() << endl;
////cout << "Current Node" << current.getGridPositionX() << "," << current.getGridPositionY() << endl;*/
if (current.type == DEST) {
////cout << "End Found" << endl;
mapn[endn->getGridPositionX()][endn->getGridPositionY()].distance = FOUND;
flag = 1;
}
//top
if (current.getGridPositionX() - 1 >= 0) {
t = mapn[current.getGridPositionX() - 1][current.getGridPositionY()];
//top top
if (!t.visited && t.getType() != WALL && t.distance > current.distance + hVDistance) {
/*////cout << "Top Top temp is:" << t.getType() << endl;
////cout << "Top Top type pos:" << t.getGridPositionX() << "," << t.getGridPositionY() << endl;*/
t.distance = current.distance + hVDistance;
mapn[current.getGridPositionX() - 1][current.getGridPositionY()].distance = current.distance + hVDistance;
t.parent.x = current.getGridPositionX();
t.parent.y = current.getGridPositionY();
mapn[current.getGridPositionX() - 1][current.getGridPositionY()].parent.x = current.getGridPositionX();
mapn[current.getGridPositionX() - 1][current.getGridPositionY()].parent.y = current.getGridPositionY();
//mapn[current.getGridPositionX() - 1][current.getGridPositionY()].visited = true;
nQueue.push(t);
}
//top left
if (current.getGridPositionY() - 1 > 0) {
t = mapn[current.getGridPositionX() - 1][current.getGridPositionY() - 1];
if (!t.visited && t.getType() != WALL && t.distance > current.distance + dDistance) {
/*////cout << "Top left temp is:" << t.getType() << endl;
////cout << "Top left type pos:" << t.getGridPositionX() << "," << t.getGridPositionY() << endl;*/
t.distance = current.distance + dDistance;
mapn[current.getGridPositionX() - 1][current.getGridPositionY() - 1].distance = current.distance + dDistance;
t.parent.x = current.getGridPositionX();
t.parent.y = current.getGridPositionY();
mapn[current.getGridPositionX() - 1][current.getGridPositionY() - 1].parent.x = current.getGridPositionX();
mapn[current.getGridPositionX() - 1][current.getGridPositionY() - 1].parent.y = current.getGridPositionY();
//mapn[current.getGridPositionX() - 1][current.getGridPositionY() - 1].visited = true;
nQueue.push(t);
}
}
//top right
if (current.getGridPositionY() + 1 < n) {
t = mapn[current.getGridPositionX() - 1][current.getGridPositionY() + 1];
if (!t.visited && t.getType() != WALL && t.distance > current.distance + dDistance) {
/*////cout << "Top right temp is:" << t.getType() << endl;
////cout << "Top right type pos:" << t.getGridPositionX() << "," << t.getGridPositionY() << endl;*/
t.distance = current.distance + dDistance;
mapn[current.getGridPositionX() - 1][current.getGridPositionY() + 1].distance = current.distance + dDistance;
t.parent.x = current.getGridPositionX();
t.parent.y = current.getGridPositionY();
mapn[current.getGridPositionX() - 1][current.getGridPositionY() + 1].parent.x = current.getGridPositionX();
mapn[current.getGridPositionX() - 1][current.getGridPositionY() + 1].parent.y = current.getGridPositionY();
//mapn[current.getGridPositionX() - 1][current.getGridPositionY() + 1].visited = true;
nQueue.push(t);
}
}
}
//left
if (current.getGridPositionY() - 1 > 0) {
t = mapn[current.getGridPositionX()][current.getGridPositionY() - 1];
if (!t.visited && t.getType() != WALL && t.distance > current.distance + hVDistance) {
/*////cout << "left temp is:" << t.getType() << endl;
////cout << "left type pos:" << t.getGridPositionX() << "," << t.getGridPositionY() << endl;*/
t.distance = current.distance + hVDistance;
mapn[current.getGridPositionX()][current.getGridPositionY() - 1].distance = current.distance + hVDistance;
t.parent.x = current.getGridPositionX();
t.parent.y = current.getGridPositionY();
mapn[current.getGridPositionX()][current.getGridPositionY() - 1].parent.x = current.getGridPositionX();
mapn[current.getGridPositionX()][current.getGridPositionY() - 1].parent.y = current.getGridPositionY();
//mapn[current.getGridPositionX()][current.getGridPositionY() - 1].visited = true;
nQueue.push(t);
}
}
//right
if (current.getGridPositionY() + 1 < n) {
t = mapn[current.getGridPositionX()][current.getGridPositionY() + 1];
if (!t.visited && t.getType() != WALL && t.distance > current.distance + hVDistance) {
/*////cout << "right temp is:" << t.getType() << endl;
////cout << "right type pos:" << t.getGridPositionX() << "," << t.getGridPositionY() << endl;*/
t.distance = current.distance + hVDistance;
mapn[current.getGridPositionX()][current.getGridPositionY() + 1].distance = current.distance + hVDistance;
t.parent.x = current.getGridPositionX();
t.parent.y = current.getGridPositionY();
mapn[current.getGridPositionX()][current.getGridPositionY() + 1].parent.x = current.getGridPositionX();
mapn[current.getGridPositionX()][current.getGridPositionY() + 1].parent.y = current.getGridPositionY();
//mapn[current.getGridPositionX()][current.getGridPositionY() + 1].visited = true;
nQueue.push(t);
}
}
//down
if (current.getGridPositionX() + 1 < n) {
//down down
t = mapn[current.getGridPositionX() + 1][current.getGridPositionY()];
if (!t.visited && t.getType() != WALL && t.distance > current.distance + hVDistance) {
/*////cout << "down temp is:" << t.getType() << endl;
////cout << "down type pos:" << t.getGridPositionX() << "," << t.getGridPositionY() << endl;*/
t.distance = current.distance + hVDistance;
mapn[current.getGridPositionX() + 1][current.getGridPositionY()].distance = current.distance + hVDistance;
t.parent.x = current.getGridPositionX();
t.parent.y = current.getGridPositionY();
mapn[current.getGridPositionX() + 1][current.getGridPositionY()].parent.x = current.getGridPositionX();
mapn[current.getGridPositionX() + 1][current.getGridPositionY()].parent.y = current.getGridPositionY();
//mapn[current.getGridPositionX() + 1][current.getGridPositionY()].visited = true;
nQueue.push(t);
}
//down left
if (current.getGridPositionY() - 1 >= 0) {
t = mapn[current.getGridPositionX() + 1][current.getGridPositionY() - 1];
if (!t.visited && t.getType() != WALL && t.distance > current.distance + dDistance) {
/*////cout << "down left temp is:" << t.getType() << endl;
////cout << "down left type pos:" << t.getGridPositionX() << "," << t.getGridPositionY() << endl;*/
t.distance = current.distance + dDistance;
mapn[current.getGridPositionX() + 1][current.getGridPositionY() - 1].distance = current.distance + dDistance;
t.parent.x = current.getGridPositionX();
t.parent.y = current.getGridPositionY();
mapn[current.getGridPositionX() + 1][current.getGridPositionY() - 1].parent.x = current.getGridPositionX();
mapn[current.getGridPositionX() + 1][current.getGridPositionY() - 1].parent.y = current.getGridPositionY();
//mapn[current.getGridPositionX() + 1][current.getGridPositionY() - 1].visited = true;
nQueue.push(t);
}
}
//down right
if (current.getGridPositionY() + 1 < n) {
t = mapn[current.getGridPositionX() + 1][current.getGridPositionY() + 1];
if (!t.visited && t.getType() != WALL && t.distance > current.distance + dDistance) {
/*////cout << "down right temp is:" << t.getType() << endl;
////cout << "down right type pos:" << t.getGridPositionX() << "," << t.getGridPositionY() << endl;*/
t.distance = current.distance + dDistance;
mapn[current.getGridPositionX() + 1][current.getGridPositionY() + 1].distance = current.distance + dDistance;
t.parent.x = current.getGridPositionX();
t.parent.y = current.getGridPositionY();
mapn[current.getGridPositionX() + 1][current.getGridPositionY() + 1].parent.x = current.getGridPositionX();
mapn[current.getGridPositionX() + 1][current.getGridPositionY() + 1].parent.y = current.getGridPositionY();
//mapn[current.getGridPositionX() + 1][current.getGridPositionY() + 1].visited = true;
nQueue.push(t);
}
}
}
current.visited = true;
if (current.visited && !hasNodePlaced(current)) {
nodes.at(current.getGridPositionX() * n + current.getGridPositionY()).type = CHECKED;
}
glutPostRedisplay();
//mapn[current.getGridPositionX()][current.getGridPositionY()].visited = true;
////cout << endl;
//for (int i = 0; i < n; i++) {
// for (int j = 0; j < n; j++) {
// try {
// if (mapn[i][j].distance != INF && !hasNodePlaced(mapn[i][j])) {
// //nodes.at(j * n + i).type = CHECKED;
// }
// //cout << mapn[i][j].distance << "\t";
// }
// catch (const out_of_range& e) {
// }
// }
// //cout << endl;
//}
}
int prevSize = nQueue.size();
if (!nQueue.empty()) {
nQueue.pop();
}
else {
//cout << "Destination Not Found" << endl;
canAcceptInput = true;
}
int currSize = nQueue.size();
if (prevSize - currSize && flag == 0) {
glutTimerFunc(TIME_SECS / SCREEN_FPS, drive, nQueue.size());
}
else if (flag == 1) {
showPath(1);
canAcceptInput = true;
}
}
void findPath() {
canAcceptInput = false;
makeReady();
drive(nQueue.size());
//while (nQueue.size() > 0) {
//}
////cout << "Path is:" << endl;
//if (path.size() > 0) {
// for (int i = 0; i < path.size(); i++) {
// ////cout << "(" << path.at(i).getGridPositionX() << "," << path.at(i).getGridPositionY() << ")" << " ";
// }
//}
}
void drawString(float x, float y, float z, char* string) {
glRasterPos3f(x, y, z);
for (char* c = string; *c != '\0'; c++) {
glColor3f(1, 1, 1);
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *c); // Updates the position
}
}
float findBitmapMidPoint(const char* str) {
return ((n / 2) - (strlen(str) / 2) * 8.76 / 31);
}
void display(void) {
////cout << "Node Size is : " << nodes.size() << endl;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (isOnStartPage) {
//Starting page
glClearColor(0.117, 0.423, 0.713, 1.0);
char collegeName[64] = "BLDEA's V P Dr PG Halakatti College of Engineering & Technology";
char dept[31] = "Department of Computer Science";
char proj[21] = "OpengGL Mini Project";
char title[23] = "Pathfinding Visualizer";
char instrc[43] = "Instructions";
char text1[21] = "Press Enter To Start";
char text2[28] = "Press 'r' or 'R' to Restart";
char text3[38] = "Press 's' or 'S' to Start Visualizing";
char text4[64] = "Press Left Mouse to place Nodes and Right Mouse to remove Nodes";
char text5[35] = "Press <ESC KEY> to quit the window";
char text6[13] = "Created By:";
char text7[23] = "Ayush Naik, Arjun Naik";
char text8[23] = "2BL18CS012, 2BL18CS010";
drawString(findBitmapMidPoint(collegeName), 3, 0.0, collegeName);
drawString(findBitmapMidPoint(dept), 6, 0.0, dept);
drawString(findBitmapMidPoint(proj), 8, 0.0, proj);
drawString(findBitmapMidPoint(title), 13, 0.0, title);
drawString(findBitmapMidPoint(instrc), 16, 0.0, instrc);
drawString(findBitmapMidPoint(text1), 18, 0.0, text1);
drawString(findBitmapMidPoint(text2), 19, 0.0, text2);
drawString(findBitmapMidPoint(text3), 20, 0.0, text3);
drawString(findBitmapMidPoint(text4), 21, 0.0, text4);
drawString(findBitmapMidPoint(text5), 22, 0.0, text5);
drawString(n-10, n-5, 0.0, text6);
drawString(n - 10, n - 3, 0.0, text7);
drawString(n - 10, n - 2, 0.0, text8);
}
else {
//gird page
glClearColor(0.964, 0.964, 0.964, 1.0);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
glLineWidth(1);
glColor3f(0.686, 0.847, 0.972);
glBegin(GL_LINE_LOOP);
glVertex2f(i, j);
glVertex2f(i, j + 1);
glVertex2f(i + 1, j + 1);
glVertex2f(i + 1, j);
glEnd();
}
}
reDrawPoints();
}
glutSwapBuffers();
glutPostRedisplay();
}
void onMouseMovement(int x, int y) {
if (canAcceptInput) {
if (isMouseLeftDown == 1 && nodes.size() > 1) {
addPoints(x, y);
glutPostRedisplay();
}
if (isMouseRightDown == 1 && nodes.size() > 1) {
removePoints(x, y);
glutPostRedisplay();
}
}
}
void resize(int w, int h) {
screenWidth = w;
screenHeight = h;
int ratio = 1.0f * w / h;
glViewport(0, 0, w, h);
//glClearColor(1.0, 1.0, 1.0, 1.0); // black background
glMatrixMode(GL_PROJECTION); // setup viewing projection
glLoadIdentity(); // start with identity matrix
glOrtho(0.0, n, n, 0.0, -1.0, 1.0); // setup a 10x10x2 viewing world
}
void reset() {
flag = 0;
memset(mapn, NULL, sizeof(mapn[0][0])*n*n);
while (!nQueue.empty()) {
nQueue.pop();
}
path.clear();
vector<SPoint>().swap(path);
path.shrink_to_fit();
//cout << "size and capacity" << nodes.size() << " " << nodes.capacity() << endl;
nodes.clear();
vector<Node>().swap(nodes);
//cout << "size and capacity" << nodes.size() << " " << nodes.capacity() << endl;
nodes.shrink_to_fit();
//cout << "size and capacity" << nodes.size() << " " << nodes.capacity() << endl;
//delete &start;
//delete &endn;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Node node = Node(i, j, EMPTY, false, INF);
nodes.push_back(node);
}
}
startEndNodes = 0;
glutPostRedisplay();
}
bool hasSourceAndDest() {
int nodeCount=0;
if (nodes.size() > 0) {
for (size_t i = 0; i < nodes.size(); i++) {
if (nodes.at(i).type == SOURCE || nodes.at(i).type == DEST) {
++nodeCount;
if (nodeCount == 1) {
continue;
}
else if (nodeCount == 2) {
break;
}
}
}
}
if (nodeCount == 2) {
return true;
}
return false;
}
void onKeyPress(unsigned char key, int x, int y) {
switch (key) {
case 27:
exit(0);
break;
case 115:
case 'S':
if (canAcceptInput) {
if (hasSourceAndDest()) {
findPath(); //starts algorithm
}
}
break;
case 114:
case 'R':
reset(); //clears all points
break;
case 13:
isOnStartPage = false;
break;
}
glutPostRedisplay();
}
int main(int argc, char** argv) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Node node = Node(i, j, EMPTY, false, INF);
nodes.push_back(node);
}
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(50, 50);
glutCreateWindow("square");
glutFullScreen();
//glutInitWindowSize(screenWidth, screenHeight);
//glClearColor(0.964, 0.964, 0.964, 1.0);
glMatrixMode(GL_PROJECTION); // setup viewing projection
glLoadIdentity(); // start with identity matrix
glOrtho(0.0, n, n, 0.0, -1.0, 1.0); // setup a 10x10x2 viewing world
/*glEnable(GL_LINE_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);*/
glutDisplayFunc(display);
glutMouseFunc(handleMouse);
glutReshapeFunc(resize);
glutMotionFunc(onMouseMovement);
glutKeyboardFunc(onKeyPress);
glutMainLoop();
return 0;
}