-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
484 lines (390 loc) · 16.4 KB
/
Copy pathmain.cpp
File metadata and controls
484 lines (390 loc) · 16.4 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
/******************************************************************
Este projeto foi feito com o objetivo de aprender a mexer com a
livraria gráfica do C++ chamada SFML.
Em caso de algum erro ou melhoria contactar:
jpsg.joao.gaspar@gmail.com
This project was made with purpose of learn how to use c++
graphical library SFML
if you find any mistake or improement contact:
jpsg.joao.gaspar@gmail.com
*******************************************************************/
/* Includes gerais */
#include <iostream>
/* Includes para utilização de gráficos, sons, etc. */
#include <SFML/Graphics.hpp>
/* Definições gerais da aplicação */
#define VECTOR_SIZE 100
#define WINDOW_HEIGHT 1450
#define WINDOW_WIDTH 2500
#define WINDOW_PROPORTION_MENU 0.25
#define WINDOW_TITLE "SORTVISUALIZER_1.0"
#define BAR_SPACE 2
#define NONE_SORT -1
#define SELECTION_SORT 0
#define QUICK_SORT 1
#define BUBBLE_SORT 2
#define INSERTION_SORT 3
#define ORGINE_TRADE 0
#define NEW_TRADE 1
#define TIME 75
#define SHOW_RESULTS true
#define HIDE_RESULTS false
/* EStutura com os dados */
typedef struct dataType{
float height;
} DATA;
/* Funções */
void renderGUIWindow(sf::RenderWindow& window, int sortAlgorithm, bool results);
void drawGrid(sf::RenderWindow& window);
void drawTitle(sf::RenderWindow& window);
bool loadTextFont();
void drawMenu(sf::RenderWindow& window);
void arrayWindowDraw(sf::RenderWindow& window, DATA vectorData[VECTOR_SIZE], int selected, int actual);
void vectorInitialization(DATA vectorData[VECTOR_SIZE]);
sf::Vector2f barPostionGetter(int i, float barHeight, float barWidth);
float barGetWidth();
void selectionSortRun(sf::RenderWindow& window, DATA vectorData[VECTOR_SIZE]);
int findSmallerNumber(DATA vectorData[VECTOR_SIZE], int actual);
void quickSortRun(sf::RenderWindow& window, DATA vectorData[VECTOR_SIZE], int first, int last);
int partition (sf::RenderWindow& window, DATA vectorData[VECTOR_SIZE], int first, int last);
void swap(sf::RenderWindow& window, DATA vectorData[VECTOR_SIZE], int x, int y, int sortAlgorithm);
void drawSortSelected(sf::RenderWindow& window, int sortSelected);
void bubbleSortRun(sf::RenderWindow& window, DATA vectorData[VECTOR_SIZE]);
void insertionSortRun(sf::RenderWindow& window, DATA vectorData[VECTOR_SIZE]);
void renderResults(sf::RenderWindow& window, bool results);
bool isUnordered(DATA vectorData[VECTOR_SIZE]);
/* Variáveis globais */
sf::Font textFont; // Fonte para o texto
sf::Clock timeCounter; // starts the clock
sf::Time timeDurantion; // Variável que guarda o tempo decorrido
float timeFixed = 0.f; // Tempo decorrido convertido em float
int swapNumber = 0; // Número de trocas feitas pelo algoritmo
/* Função main */
int main()
{
/* Janela da aplicação */
sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), WINDOW_TITLE, sf::Style::Close | sf::Style::Titlebar);
/* Limitação da atualização da janela */
window.setFramerateLimit(60);
/* Vetor com os dados a organizar*/
DATA vectorData[VECTOR_SIZE];
/* Variável que guarda os eventos ocorridos na janela */
sf::Event event;
/* Variaável que guarda a informação se o algoritmo está a correr */
bool sortRun = false;
/* Qual o algoritmo a correr ou que terá de ser corrido */
int sortAlgorithm = -1;
/* Carrega a fonte para o texto - cajo haja erro a aplicação fecha */
if (!loadTextFont()){window.close();exit(0);}
/* Inicialização do vetor com os dados */
vectorInitialization(vectorData);
/* Enquando a janela estiver aberta correr o programa */
while (window.isOpen())
{
/* Detecta os eventos da janela */
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed){ /* Evento de fecho da janela */
window.close();
}
if (event.type == sf::Event::KeyPressed) /* Evento de tecla premida */
{
switch (event.key.code)
{
case sf::Keyboard::Escape: /* Tecala premida escape */
window.close(); /* fecha a janela */
break;
case sf::Keyboard::Num0: /* Tecala premida 0 */
sortAlgorithm = SELECTION_SORT; /* define o selection sort */
break;
case sf::Keyboard::Num1: /* Tecala premida 1*/
sortAlgorithm = QUICK_SORT; /* define o quick sort */
break;
case sf::Keyboard::Num2: /* Tecala premida 2 */
sortAlgorithm = BUBBLE_SORT; /* define o bubble sort */
break;
case sf::Keyboard::Num3: /* Tecala premida 3 */
sortAlgorithm = INSERTION_SORT; /* defin o insertion sort */
break;
case sf::Keyboard::Space: /* Tecala premida espaço */
if(sortAlgorithm != -1){ /* caso haja algoritmo definido */
sortRun = true; /* inicia o algotitmo */
}
break;
case sf::Keyboard::R: /* Tecala premida R */
sortRun = false; /* Para o algoritmo*/
sortAlgorithm = NONE_SORT; /* Limpa o tipo de algoritmo */
timeFixed = 0.f; /* reset o tempo decorrido */
swapNumber = 0; /* reset o número de trocas */
vectorInitialization(vectorData); /* insere novos dados no array */
break;
default:
break;
}
}
}
if(sortRun){ /* se verdadeiro prepara-se para correr o algoritmo */
if(!isUnordered(vectorData)){ /* verifica se o array ja está ordenado */
sortRun = false; /* em caso afirmativo para de correr o algoritmo */
continue;
}else{ /* caso não esteja ordenado */
switch (sortAlgorithm) /* correr o algoritmo escolhido*/
{ /* em todos os caso reinicia o relógio */
case SELECTION_SORT:
timeCounter.restart();
selectionSortRun(window, vectorData);
break;
case QUICK_SORT:
quickSortRun(window, vectorData, 0, VECTOR_SIZE-1);
break;
case BUBBLE_SORT:
timeCounter.restart();
bubbleSortRun(window, vectorData);
break;
case INSERTION_SORT:
timeCounter.restart();
insertionSortRun(window, vectorData);
break;
default:
break;
}
timeDurantion = timeCounter.getElapsedTime(); /* ao fim de correr o algoritmo le o tempo */
timeFixed = timeDurantion.asSeconds(); /* converte o tempo para um float e guarda-o */
sortRun = false; /* para o algoritmo */
}
}
else{
window.clear(sf::Color::Black);
renderGUIWindow(window, sortAlgorithm, HIDE_RESULTS);
arrayWindowDraw(window, vectorData, -1, -1);
window.display();
sf::sleep(sf::milliseconds(TIME));
}
}
return 0;
}
bool isUnordered(DATA vectorData[VECTOR_SIZE]){
for(int i=0; i<VECTOR_SIZE-1; i++){
if(vectorData[i].height > vectorData[i+1].height){
return true;
}
}
return false;
}
void swap(sf::RenderWindow& window, DATA vectorData[VECTOR_SIZE], int x, int y, int sortAlgorithm){
float aux=vectorData[x].height;
vectorData[x].height = vectorData[y].height;
vectorData[y].height = aux;
window.clear(sf::Color::Black);
renderGUIWindow(window, sortAlgorithm, SHOW_RESULTS);
arrayWindowDraw(window, vectorData, x, y);
window.display();
swapNumber++;
sf::sleep(sf::milliseconds(TIME));
}
/* Funções para o INSERTION SORT */
void insertionSortRun(sf::RenderWindow& window, DATA vectorData[VECTOR_SIZE]){
int i, key, j;
for (i = 1; i < VECTOR_SIZE; i++)
{
key = vectorData[i].height;
j = i - 1;
while (j >= 0 && vectorData[j].height > key)
{
vectorData[j + 1].height = vectorData[j].height;
j = j - 1;
}
vectorData[j + 1].height = key;
swapNumber++;
window.clear(sf::Color::Black);
renderGUIWindow(window, INSERTION_SORT, SHOW_RESULTS);
arrayWindowDraw(window, vectorData, i, j+1);
window.display();
sf::sleep(sf::milliseconds(TIME));
}
}
/* Funções para o BUBBLE SORT */
void bubbleSortRun(sf::RenderWindow& window, DATA vectorData[VECTOR_SIZE])
{
int i, j;
for (i = 0; i < VECTOR_SIZE-1; i++)
for (j = 0; j < VECTOR_SIZE-i-1; j++)
if (vectorData[j].height > vectorData[j+1].height)
swap(window, vectorData, j, j+1, BUBBLE_SORT);
}
/* Funções para o QUICK SORT */
int partition (sf::RenderWindow& window, DATA vectorData[VECTOR_SIZE], int first, int last){
int pivot = vectorData[last].height; // pivot
int i = (first - 1); // Index of smaller element
for (int j = first; j <= last- 1; j++){
if (vectorData[j].height <= pivot)
{
i++;
swap(window, vectorData, i, j, QUICK_SORT);
}
}
swap(window, vectorData, i+1, last, QUICK_SORT);
return (i + 1);
}
void quickSortRun(sf::RenderWindow& window, DATA vectorData[VECTOR_SIZE], int first, int last){
if (first < last){
int index = partition(window, vectorData, first, last);
quickSortRun(window, vectorData, first, index - 1);
quickSortRun(window, vectorData, index + 1, last);
}
}
/* Funções para o SELECTION SORT */
void selectionSortRun(sf::RenderWindow& window, DATA vectorData[VECTOR_SIZE]){
int actual = 0;
int selected = 0;
while(actual != (VECTOR_SIZE-1)){
selected = findSmallerNumber(vectorData, actual);
swap(window, vectorData, actual, selected, SELECTION_SORT);
actual++;
}
}
int findSmallerNumber(DATA vectorData[VECTOR_SIZE], int actual){
int small = actual;
int smallAux = vectorData[actual].height;
for(int i = actual + 1; i < VECTOR_SIZE; i++){
if(smallAux > vectorData[i].height){
small = i;
smallAux = vectorData[i].height;
}
}
return small;
}
/***********************************************/
void arrayWindowDraw(sf::RenderWindow& window, DATA vectorData[VECTOR_SIZE], int selected, int actual){
float barWidth = barGetWidth();
for(int i = 0; i < VECTOR_SIZE; i++){
sf::RectangleShape rectangle(sf::Vector2f(barWidth, vectorData[i].height));
rectangle.setPosition(barPostionGetter(i, vectorData[i].height, barWidth));
if(i == selected){
rectangle.setFillColor(sf::Color::Magenta);
}
else if(i == actual){
rectangle.setFillColor(sf::Color::Green);
}
else{
rectangle.setFillColor(sf::Color::White);
}
window.draw(rectangle);
}
}
float barGetWidth(){
return (WINDOW_WIDTH*(1-WINDOW_PROPORTION_MENU)-(VECTOR_SIZE*BAR_SPACE))/VECTOR_SIZE;
}
sf::Vector2f barPostionGetter(int i, float barHeight, float barWidth){
float x, y;
x = i * (barWidth+BAR_SPACE);
y = WINDOW_HEIGHT-barHeight;
return sf::Vector2f(x, y);
}
void vectorInitialization(DATA vectorData[VECTOR_SIZE]){
for(int i = 0; i < VECTOR_SIZE; i++){
vectorData[i].height = (float) rand()/RAND_MAX*WINDOW_HEIGHT;
}
}
/*********************************************************************/
void renderGUIWindow(sf::RenderWindow& window, int sortAlgorithm, bool results){
drawGrid(window);
drawTitle(window);
drawSortSelected(window, sortAlgorithm);
drawMenu(window);
renderResults(window, results);
}
void renderResults(sf::RenderWindow& window, bool results){
sf::Text text;
text.setFont(textFont);
text.setCharacterSize(30);
text.setFillColor(sf::Color::Black);
text.setStyle(sf::Text::Bold);
text.setPosition(WINDOW_WIDTH-WINDOW_WIDTH*WINDOW_PROPORTION_MENU +130 , 1000);
if(results){
timeDurantion = timeCounter.getElapsedTime();
text.setString("Time duration: " + std::to_string(timeDurantion.asSeconds())+ " seconds");
}
else{
text.setString("Time duration: " + std::to_string(timeFixed) + " seconds");
}
window.draw(text);
text.setPosition(WINDOW_WIDTH-WINDOW_WIDTH*WINDOW_PROPORTION_MENU +130 , 1060);
text.setString("Swaps: " + std::to_string(swapNumber));
window.draw(text);
}
void drawSortSelected(sf::RenderWindow& window, int sortSelected){
sf::Text text;
text.setFont(textFont);
text.setCharacterSize(60);
text.setFillColor(sf::Color::Black);
text.setStyle(sf::Text::Bold);
text.setPosition(WINDOW_WIDTH-WINDOW_WIDTH*WINDOW_PROPORTION_MENU +130 , 820);
text.setString("Selected sort:");
window.draw(text);
text.setCharacterSize(45);
text.setPosition(WINDOW_WIDTH-WINDOW_WIDTH*WINDOW_PROPORTION_MENU +130 , 900);
if(sortSelected == NONE_SORT){
text.setString("Choose one sort");
}
else if(sortSelected == SELECTION_SORT){
text.setString("SELECTION SORT");
}
else if(sortSelected == QUICK_SORT){
text.setString("QUICK SORT");
}
else if(sortSelected == BUBBLE_SORT){
text.setString("BUBBLE SORT");
}
else if(sortSelected == INSERTION_SORT){
text.setString("INSERTION SORT");
}
window.draw(text);
}
void drawMenu(sf::RenderWindow& window){
sf::Text text;
text.setFont(textFont);
text.setCharacterSize(30);
text.setFillColor(sf::Color::Black);
text.setStyle(sf::Text::Bold);
text.setPosition(WINDOW_WIDTH-WINDOW_WIDTH*WINDOW_PROPORTION_MENU +130 , 150);
text.setString("Selection Sort - 0");
window.draw(text);
text.setPosition(WINDOW_WIDTH-WINDOW_WIDTH*WINDOW_PROPORTION_MENU +130 , 210);
text.setString("Quick Sort - 1");
window.draw(text);
text.setPosition(WINDOW_WIDTH-WINDOW_WIDTH*WINDOW_PROPORTION_MENU +130 , 270);
text.setString("Bubble Sort - 2");
window.draw(text);
text.setPosition(WINDOW_WIDTH-WINDOW_WIDTH*WINDOW_PROPORTION_MENU +130 , 330);
text.setString("Insertion Sort - 3");
window.draw(text);
text.setPosition(WINDOW_WIDTH-WINDOW_WIDTH*WINDOW_PROPORTION_MENU +130 , 450);
text.setString("Reset - R");
window.draw(text);
text.setPosition(WINDOW_WIDTH-WINDOW_WIDTH*WINDOW_PROPORTION_MENU +130 , 510);
text.setString("Start - Space");
window.draw(text);
text.setPosition(WINDOW_WIDTH-WINDOW_WIDTH*WINDOW_PROPORTION_MENU +130 , 570);
text.setString("Sair - ESC");
window.draw(text);
}
void drawTitle(sf::RenderWindow& window){
sf::Text text;
text.setFont(textFont);
text.setString("Sort Visualizer");
text.setCharacterSize(60);
text.setFillColor(sf::Color::Red);
text.setStyle(sf::Text::Bold);
text.setPosition(WINDOW_WIDTH-WINDOW_WIDTH*WINDOW_PROPORTION_MENU +130 , 5);
window.draw(text);
}
void drawGrid(sf::RenderWindow& window){
sf::RectangleShape sideMenu(sf::Vector2f(WINDOW_WIDTH*WINDOW_PROPORTION_MENU,WINDOW_HEIGHT));
sideMenu.setPosition(WINDOW_WIDTH-WINDOW_WIDTH*WINDOW_PROPORTION_MENU, 0);
sideMenu.setFillColor(sf::Color::Cyan);
window.draw(sideMenu);
}
bool loadTextFont(){
return textFont.loadFromFile("font/RobotoCondensed-Bold.ttf");
}