-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
114 lines (88 loc) · 2.73 KB
/
main.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
#include <SFML/Graphics.hpp>
#include <iostream>
#include <time.h>
#include "headers/sortingAlgos.h"
void generateArray(std::vector<int> &);
//Sorting algorithms
//I set the window as a global variable
sf::RenderWindow window(sf::VideoMode::getFullscreenModes()[0], "SORT visualisation");
void displayArray(std::vector<int> &,int);
void displayArrayEnd(std::vector<int> &,int);
#define elementNumber 1000
#define delay 0.1
int main()
{
//Initialisation of the array
std::vector <int> vect(elementNumber);
generateArray(vect);
//Main loop
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
//The call for the sorting algorithm with the display function inside
CocktailSort(vect);
for(auto &i:vect )std::cout <<i<<std::endl;
//The final display with the array sorted
for (int i = 0; i < vect.size(); i++)
displayArrayEnd(vect,i);
window.close();
}
return 0;
}
void generateArray(std::vector<int> &vect)
{
//We will generate elements in the array in the order
// -50 is for that the longest element in the array does not hit the up limit of the window
for(int i=1; i<elementNumber+1; i++)
vect[i]=i*((window.getSize().y-50)/float(elementNumber));
//we make 100 random swap
int i,j;
srand(time(NULL));
for(int p=0; p<elementNumber*2; p++)
{
i=rand()%elementNumber;
j=rand()%elementNumber;
std::swap(vect[i],vect[j]);
}
}
void displayArray(std::vector<int> &vect,int index)
{
float ratio=(float(window.getSize().x/float(elementNumber)));
window.clear(sf::Color::Black);
for(int i=0; i<vect.size(); i++)
{
sf::RectangleShape bar(sf::Vector2f(ratio,vect[i]));
if(i==index+1)
bar.setFillColor(sf::Color::Red);
else
bar.setFillColor(sf::Color::White);
bar.setPosition(ratio*i,window.getSize().y-vect[i]);
window.draw(bar);
}
sf::sleep(sf::milliseconds(delay));
window.display();
}
void displayArrayEnd(std::vector<int> &vect,int index)
{
float ratio=(float(window.getSize().x/float(elementNumber)));
window.clear(sf::Color::Black);
for(int i=0; i<vect.size(); i++)
{
sf::RectangleShape bar(sf::Vector2f(ratio,vect[i]));
if(i==index+1)
bar.setFillColor(sf::Color::Red);
else if (i <index+1)
bar.setFillColor(sf::Color::Green);
else
bar.setFillColor(sf::Color::White);
bar.setPosition(ratio*i,window.getSize().y-vect[i]);
window.draw(bar);
}
sf::sleep(sf::milliseconds(10));
window.display();
}