-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
635 lines (537 loc) · 26.8 KB
/
main.cpp
File metadata and controls
635 lines (537 loc) · 26.8 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
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
/*
Final Project: Graphs in SFML
Luis Barajas
Mauricio Rico
Data Structures
30/11/16
A01370934@itesm.mx
A01370874@itesm.mx
*/
#include <SFML/Graphics.hpp>
#include <iostream>
#include "Graph.hpp"
#include <math.h>
#define PI 3.141592
void configureSFML(sf::VideoMode *desktop, sf::RenderWindow *window);
void mainDraw(sf::RenderWindow *window, Graph<std::string, float> *graph);
void createVertex(sf::Vector2i & position, Graph<std::string, float> *graph, sf::RenderWindow *window, std::string & textUserInteraction);
void drawVertex(sf::RenderWindow *window, Vertex<std::string, float> * vertex, sf::Color color);
Vertex<std::string, float> * getClickedVertex(Graph<std::string, float> *graph, sf::Vector2i & position);
void createEdge(sf::RenderWindow *window, Graph<std::string, float> *graph, Vertex<std::string, float> * originVertex, Vertex<std::string, float> * destinationVertex, std::string & textUserInteraction);
void drawEdge(sf::RenderWindow *window, Vertex<std::string, float> * originVertex, Vertex<std::string, float> * destinationVertex, sf::Color color);
void createPath(sf::RenderWindow *window, Graph<std::string, float> *graph, Vertex<std::string, float> * originVertex, Vertex<std::string, float> * destinationVertex, std::string *textCountriesNumber, std::string *textUserInteraction);
void redrawScene(sf::RenderWindow *window, Graph<std::string, float> *graph);
int main(){
//Variables
Graph<std::string, float> graph;
sf::VideoMode desktop;
sf::RenderWindow window;
configureSFML(&desktop, &window);
mainDraw(&window, &graph);
return 0;
}
//Method for configuring the necessary elements to intializze SFML window
void configureSFML(sf::VideoMode *desktop, sf::RenderWindow *window){
*desktop = sf::VideoMode::getDesktopMode();
window->create(sf::VideoMode(1024, 768, desktop->bitsPerPixel), "Graphs");
}
void mainDraw(sf::RenderWindow *window, Graph<std::string, float> *graph){
//Configure the background
sf::Texture texture;
if (!texture.loadFromFile("world.jpg"))
{
std::cout << "Error: loading the background image!" << '\n';
}
sf::Sprite background(texture);
window->clear(sf::Color::White);
// Set background to image
window->draw(background);
//Configure the font
sf::Font font;
font.loadFromFile("Akashi.ttf");
//Rectangle for the background in the title
sf::RectangleShape rec(sf::Vector2f(1024,80));
rec.setPosition(0,0);
rec.setFillColor(sf::Color::White);
// Configure the countriesNumber object
std::string textCountriesNumber;
sf::Text countriesNumber;
countriesNumber.setFont(font);
countriesNumber.setCharacterSize(24);
countriesNumber.setColor(sf::Color::Black);
countriesNumber.setPosition(sf::Vector2f(20, 10));
// Configure the userInteraction object
std::string textUserInteraction = "";
sf::Text userInteraction;
userInteraction.setFont(font);
userInteraction.setCharacterSize(18);
userInteraction.setColor(sf::Color::Black);
userInteraction.setPosition(sf::Vector2f(20, 52));
//Configure button for path
//Background for the button
sf::RectangleShape pathBackground(sf::Vector2f(130,35));
pathBackground.setPosition(825,45);
pathBackground.setFillColor(sf::Color::Black);
//text for the button
sf::Text pathBtnTitle;
pathBtnTitle.setFont(font);
pathBtnTitle.setCharacterSize(16);
pathBtnTitle.setColor(sf::Color::White);
pathBtnTitle.setPosition(850,52);
pathBtnTitle.setString("New Path");
//Configure button for edge
//Background for the button
sf::RectangleShape edgeBackground(sf::Vector2f(130,35));
edgeBackground.setPosition(825,5);
edgeBackground.setFillColor(sf::Color::Black);
//text for the button
sf::Text edgeBtnTitle;
edgeBtnTitle.setFont(font);
edgeBtnTitle.setCharacterSize(16);
edgeBtnTitle.setColor(sf::Color::White);
edgeBtnTitle.setPosition(850,12);
edgeBtnTitle.setString("New Edge");
//Assgint the default values to the strings that are displayed in the header
textCountriesNumber = "There are " + std::to_string(graph->getVerticesLength()) + " countries.";
textUserInteraction = "Click wherever you want a new country or click any button";
//Variables for accepting & assigning the input text
bool createAVertex = false;
bool createAnEdge = false;
bool setEdgeCost = false;
bool createAPath = false;
bool waitingEsc = false;
bool createAnEdgeFromBtn = false;
bool setEdgeCostFromBtn = false;
//Variable for getting the position of the Mouse when clicked
sf::Vector2i position;
//Variable for saving the clicked Vertices
Vertex<std::string, float> * clickedVertex = nullptr;
Vertex<std::string, float> * originVertex = nullptr;
Vertex<std::string, float> * destinationVertex = nullptr;
while (window->isOpen())
{
//Assign the string of the title
countriesNumber.setString(textCountriesNumber);
userInteraction.setString(textUserInteraction);
sf::Event event;
while (window->pollEvent(event))
{
switch (event.type){
case sf::Event::Closed:
std::cout << "Window closed" << '\n';
window->close();
break;
case sf::Event::MouseButtonReleased:
{
if(createAnEdge){
//Position of the mouse in the window
position = sf::Mouse::getPosition(*window);
//Get the clicked vertex in the window
clickedVertex = getClickedVertex(graph, position);
//Only set the cost if there was a clicked vertex
if (clickedVertex != nullptr){
createAnEdge = false;
setEdgeCost = true;
// Change the text displayed
textCountriesNumber = "Enter the cost for the connection: ";
textUserInteraction = "";
}else{
// Change the text displayed
textCountriesNumber = "No country was selected, try again";
textUserInteraction = "";
}
}else if(createAnEdgeFromBtn){
if(originVertex == nullptr){
//Position of the mouse in the window
position = sf::Mouse::getPosition(*window);
//Get the clicked vertex in the window
originVertex = getClickedVertex(graph, position);
//Only request the destination if there was an origin selected
if (originVertex != nullptr){
// Change the text displayed
textCountriesNumber = "Select the destination";
textUserInteraction = "";
}else{
textCountriesNumber = "No country was selected, try again";
textUserInteraction = "";
}
}else{
position = sf::Mouse::getPosition(*window);
destinationVertex = getClickedVertex(graph, position);
createAnEdgeFromBtn = false;
//Only create a path if there was an origin and destination selected
if (destinationVertex != nullptr){
setEdgeCostFromBtn = true;
textCountriesNumber = "Enter the cost of the connection: ";
textUserInteraction = "";
}else{
textCountriesNumber = "No country was selected, try again";
textUserInteraction = "";
}
}
}else if (createAPath){
if(originVertex == nullptr){
//Position of the mouse in the window
position = sf::Mouse::getPosition(*window);
//Get the clicked vertex in the window
originVertex = getClickedVertex(graph, position);
//Only request the destination if there was an origin selected
if (originVertex != nullptr){
// Change the text displayed
textCountriesNumber = "Select the destination";
textUserInteraction = "";
}else{
textCountriesNumber = "No country was selected, try again";
textUserInteraction = "";
}
}else{
position = sf::Mouse::getPosition(*window);
destinationVertex = getClickedVertex(graph, position);
createAVertex = false;
createAnEdge = false;
setEdgeCost = false;
createAPath = false;
createAnEdgeFromBtn = false;
setEdgeCostFromBtn = false;
waitingEsc = true;
//Only create a path if there was an origin and destination selected
if (destinationVertex != nullptr){
createPath(window,graph,originVertex,destinationVertex,&textCountriesNumber,&textUserInteraction);
}else{
textCountriesNumber = "No country was selected, try again";
textUserInteraction = "";
}
}
}else if (!waitingEsc){
//Check if the click is in the title bar
if (sf::Mouse::getPosition(*window).y <= 80){
//Check if button path was clicked
if(sf::Mouse::getPosition(*window).x >= pathBackground.getPosition().x
&& sf::Mouse::getPosition(*window).x <= pathBackground.getPosition().x + pathBackground.getSize().x
&& sf::Mouse::getPosition(*window).y >= pathBackground.getPosition().y
&& sf::Mouse::getPosition(*window).y <= pathBackground.getPosition().y + pathBackground.getSize().y){
createAPath = true;
// Change the text displayed
textCountriesNumber = "Select the origin";
textUserInteraction = "";
}
if(sf::Mouse::getPosition(*window).x >= edgeBackground.getPosition().x
&& sf::Mouse::getPosition(*window).x <= edgeBackground.getPosition().x + edgeBackground.getSize().x
&& sf::Mouse::getPosition(*window).y >= edgeBackground.getPosition().y
&& sf::Mouse::getPosition(*window).y <= edgeBackground.getPosition().y + edgeBackground.getSize().y){
createAnEdgeFromBtn = true;
// Change the text displayed
textCountriesNumber = "Select the origin";
textUserInteraction = "";
}
}else{
// get the local mouse position (relative to window)
position = sf::Mouse::getPosition(*window);
// Change the text displayed
textCountriesNumber = "Enter the name for the country: ";
textUserInteraction = "";
// Toggle the boolean so it accepts input
createAVertex = true;
}
}
}
break;
case sf::Event::TextEntered:
if ( createAVertex ){
//Submit the entered text
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Return)){
if(textUserInteraction != ""){
createVertex(position, graph, window, textUserInteraction);
createAVertex = false;
if(graph->getVerticesLength() > 1){
//Change the displayed text
textCountriesNumber = "Select the countries to make a connection";
textUserInteraction = "Press escape when you are finished";
createAnEdge = true;
}else{
createAnEdge = false;
textCountriesNumber = "There are " + std::to_string(graph->getVerticesLength()) + " countries.";
textUserInteraction = "Click wherever you want a new country or click any button";
}
}else{
textCountriesNumber = "Please enter a name to the country";
textUserInteraction = "";
}
}
//Delete the last character
if (sf::Keyboard::isKeyPressed(sf::Keyboard::BackSpace)){
if(textUserInteraction.length() > 0){
textUserInteraction.pop_back();
}
}else
// it's a printable char
if ( event.text.unicode < 0x80 ) {
char letter = (char) event.text.unicode;
textUserInteraction += letter;
}
}else if(setEdgeCostFromBtn){
//Submit the entered text
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Return)){
if(textUserInteraction != ""){
createEdge(window, graph, originVertex, destinationVertex, textUserInteraction);
createAVertex = true;
//Set the vetices to null
originVertex = nullptr;
destinationVertex = nullptr;
setEdgeCostFromBtn = false;
textCountriesNumber = "There are " + std::to_string(graph->getVerticesLength()) + " countries.";
textUserInteraction = "Click wherever you want a new country or click any button";
}else{
textCountriesNumber = "Please enter a cost for the edge";
textUserInteraction = "";
}
}
//Delete the last character
if (sf::Keyboard::isKeyPressed(sf::Keyboard::BackSpace)){
if(textUserInteraction.length() > 0){
textUserInteraction.pop_back();
}
}else
// it's a printable char
if ( event.text.unicode < 0x80 ) {
char letter = (char) event.text.unicode;
textUserInteraction += letter;
}
}else if (createAnEdge){
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)){
createAnEdge = false;
textCountriesNumber = "There are " + std::to_string(graph->getVerticesLength()) + " countries.";
textUserInteraction = "Click wherever you want a new country or click any button";
}
}else if (setEdgeCost){
//Submit the entered text
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Return)){
if(textUserInteraction != ""){
originVertex = graph->getVerticesList()->getDataAtTail();
createEdge(window, graph, originVertex, clickedVertex, textUserInteraction);
originVertex = nullptr;
createAVertex = false;
setEdgeCost = false;
createAnEdge = true;
textCountriesNumber = "Select the countries to make a connection";
textUserInteraction = "Press escape when you are finished";
}else{
textCountriesNumber = "Please enter a cost for the edge";
textUserInteraction = "";
}
}
//Delete the last character
if (sf::Keyboard::isKeyPressed(sf::Keyboard::BackSpace)){
if(textUserInteraction.length() > 0){
textUserInteraction.pop_back();
}
}else
// it's a printable char
if ( event.text.unicode < 0x80 ) {
char letter = (char) event.text.unicode;
textUserInteraction += letter;
}
//Wait until the user click the key escape to reset the window and delete the path
}else if(waitingEsc){
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)){
waitingEsc = false;
textCountriesNumber = "There are " + std::to_string(graph->getVerticesLength()) + " countries.";
textUserInteraction = "Click wherever you want a new country or click any button";
//Restart the scene
window->clear(sf::Color::White);
window->draw(background);
redrawScene(window,graph);
//Set the vetices to null
originVertex = nullptr;
destinationVertex = nullptr;
}
}
break;
}
}
//Draw background of the header section
window->draw(rec);
//Draw the elements
window->draw(countriesNumber);
window->draw(userInteraction);
window->draw(pathBackground);
window->draw(pathBtnTitle);
window->draw(edgeBackground);
window->draw(edgeBtnTitle);
window->display();
}
}
//Method for creating a new vertex
void createVertex(sf::Vector2i & position, Graph<std::string, float> *graph, sf::RenderWindow *window, std::string & textUserInteraction){
//Get the position coordinates
int x = position.x - 10;
int y = position.y - 10;
// Add vertex ti graph
Vertex<std::string, float> * newVertex = new Vertex<std::string, float>(textUserInteraction);
newVertex->setX(x);
newVertex->setY(y);
//Add vertex to the graph object
graph->addVertex(newVertex);
//Call the metho to draw the vertex
drawVertex(window,newVertex,sf::Color::White);
}
//Method for drawing a vertex in SFML
void drawVertex(sf::RenderWindow *window, Vertex<std::string, float> * vertex, sf::Color color){
//Get the elements of the vertex
int x = vertex->getX();
int y = vertex->getY();
std::string data = vertex->getData();
//Draw the vertex in the place pressed
sf::CircleShape circle(10.f);
circle.setFillColor(color);
circle.setPosition(x,y);
window->draw(circle);
//Configure the font
sf::Font font;
font.loadFromFile("Akashi.ttf");
//Draw its name next to it
sf::Text name;
name.setFont(font);
name.setCharacterSize(12);
name.setColor(color);
name.setPosition(sf::Vector2f(x-5, y-15));
name.setString(data);
window->draw(name);
}
// Method for getting the clicked Vertex
Vertex<std::string, float> * getClickedVertex(Graph<std::string, float> *graph, sf::Vector2i & position){
LinkedList<Vertex<std::string, float> *> * vertices = graph->getVerticesList();
Node<Vertex<std::string, float> *> * vertexNode = vertices->getHead();
Vertex<std::string, float> * token;
//loop until the last element of the list
while (vertexNode != nullptr) {
//Get the vertex in the node
token = vertexNode->getData();
//Check if the position of the mouse and vertex merge
if(position.x >= token->getX()
&& position.x <= token->getX() + 20
&& position.y >= token->getY()
&& position.y <= token->getY() + 20){
//Return the vertex clicked
return token;
}
//Get the next element in the list
vertexNode = vertexNode->getNext();
}
//If there was no vertex clicked return nullptr
token = nullptr;
return token;
}
//Method for creating the new edge
void createEdge(sf::RenderWindow *window, Graph<std::string, float> *graph, Vertex<std::string, float> * originVertex, Vertex<std::string, float> * destinationVertex, std::string & textUserInteraction){
//transform the input from the user to float
float cost = std::stof(textUserInteraction);
//Add the edge to the graph object
graph->addEdge(originVertex,destinationVertex,cost);
//Call the method to draw the edge
drawEdge(window,originVertex,destinationVertex,sf::Color::White);
}
//Method for drawing an edge in SFML
void drawEdge(sf::RenderWindow *window, Vertex<std::string, float> * originVertex, Vertex<std::string, float> * destinationVertex, sf::Color color){
//Get the coordinates of the vertices
float x_o = originVertex->getX();
float y_o = originVertex->getY();
float x_i = destinationVertex->getX();
float y_i = destinationVertex->getY();
//get the delta in the coordinates for using trigonometry
int delta_x = x_i - x_o;
int delta_y = y_i - y_o;
float angleToRotate = 0;
//Get the angle to rotate the rectangle so it gets form the origin to the destination
angleToRotate = (atan2 (delta_y,delta_x) * 180) / PI;
//The length of the edge
float hypotenuse = delta_y / sin (angleToRotate*PI/180);
//Draw the edge
sf::RectangleShape line(sf::Vector2f(hypotenuse,5));
line.setPosition(x_o + 7.5,y_o + 12.5);
line.setFillColor(color);
line.setRotation(angleToRotate);
window->draw(line);
}
//Create a new path, and display it visually
void createPath(sf::RenderWindow *window, Graph<std::string, float> *graph, Vertex<std::string, float> * originVertex, Vertex<std::string, float> * destinationVertex, std::string *textCountriesNumber, std::string *textUserInteraction){
//Get the path with djikstra method in the graph class
LinkedList<Vertex<std::string, float> *> * path = graph->findPath(originVertex,destinationVertex);
Node<Vertex<std::string, float> *> * vertexNode = path->getHead();
Node<Vertex<std::string, float> *> * nextVertexNode = vertexNode->getNext();
//Exit the function if there was no path
if (nextVertexNode == nullptr){
//Change the displayed text
*textCountriesNumber = "There is no possible path from " + originVertex->getData() + " to " +
destinationVertex->getData();
*textUserInteraction = "Press escape to create a new path or create a new country";
delete path;
return;
}
Vertex<std::string, float> * vertex;
Vertex<std::string, float> * nextVertex;
int length = path->getLength();
int i = 0;
std::string pathInText = "";
std::string vertexData;
//Loop until the path the last element in path list
while(nextVertexNode != nullptr){
vertex = vertexNode->getData();
nextVertex = nextVertexNode->getData();
drawEdge(window,vertex,nextVertex,sf::Color(33,53,156));
if(i == 0){
//Origin Vertex
//Draw the vertex on top black
vertexData = vertex->getData();
drawVertex(window,vertex,sf::Color::Black);
pathInText += vertexData;
pathInText += "->";
}
vertexData = nextVertex->getData();
if(i < length - 2){
//Draw the vertex in blue
drawVertex(window,nextVertex,sf::Color(33,53,156));
pathInText += vertexData;
pathInText += "->";
}else{
//Destinaiton Vertex
//Draw the vertex in black
drawVertex(window,nextVertex,sf::Color::Black);
pathInText += vertexData;
}
vertexNode = vertexNode->getNext();
nextVertexNode = nextVertexNode->getNext();
i++;
}
//Change the displayed text
*textCountriesNumber = "The cheapest path to follow is: " + pathInText;
*textUserInteraction = "Press escape to create a new path or create a new country";
//free path space
delete path;
}
//Method for redrawing the scene without any paths
void redrawScene(sf::RenderWindow *window, Graph<std::string, float> *graph){
//Draw all the vertices & edges in white again
LinkedList<Vertex<std::string, float> *> * vertices = graph->getVerticesList();
int lengthVertices = vertices->getLength();
Node<Vertex<std::string, float> *> * vertexNode = vertices->getHead();
Vertex<std::string, float> * vertex;
while(vertexNode != nullptr){
vertex = vertexNode->getData();
drawVertex(window,vertex,sf::Color::White);
vertexNode = vertexNode->getNext();
}
LinkedList<Edge<std::string, float> *> * edges = graph->getEdgesList();
int lengthEdges = edges->getLength();
Node<Edge<std::string, float> *> * edgeNode = edges->getHead();
Edge<std::string, float> * edge;
Vertex<std::string, float> * origin;
Vertex<std::string, float> * destination;
while(edgeNode != nullptr){
edge = edgeNode->getData();
origin = edge->getOrigin();
destination = edge->getDestination();
drawEdge(window,origin,destination,sf::Color::White);
edgeNode = edgeNode->getNext();
}
}