-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
77 lines (65 loc) · 2.32 KB
/
Copy pathmain.cpp
File metadata and controls
77 lines (65 loc) · 2.32 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
#include <SFML/Graphics.hpp>
#include <vector>
#include <algorithm>
#include <random>
#include <chrono>
#include <iostream>
#include <thread>
#include "headers/sorting.hpp"
int main()
{
float rectangleSizeX = 10.f;
int arrSize = 35;
int maxValue = arrSize;
std::vector<int> arr(arrSize);
for (int i = 0; i < arrSize; i++){
arr[i] = i + 1;
}
//shuffle array, first get seed, then shuffle
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::shuffle(arr.begin(), arr.end(), std::default_random_engine(seed));
//create window, get size (size should probably be taken in main every frame)
sf::RenderWindow window(sf::VideoMode({800, 600}), "Algorithm visualiser");
float windowSizeX = window.getSize().x;
std::vector<sf::RectangleShape> rectangleList(arrSize);
//set up initial rectangles
for (int i = 0; i < arrSize; i++){
float height = -10.f * arr[i];
sf::RectangleShape rectangle({rectangleSizeX, height});
float currentXPosition = (windowSizeX/arrSize) * i + 5.f;
rectangle.setPosition({currentXPosition, window.getSize().y});
window.draw(rectangle);
rectangleList[i] = rectangle;
}
std::vector<std::vector<int>> steps;
std::vector<int> currentStep;
//insertionSort(arr, arrSize, steps);
mergeSort(arr, 0, arr.size() - 1, steps);
long long unsigned step = 0;
currentStep = steps.at(step);
//loop to keep open
while(window.isOpen()){
while (const std::optional event = window.pollEvent()){
if(event->is<sf::Event::Closed>()){
window.close();
}
}
//fill display with black
window.clear(sf::Color(0,0,0));
for (int i = 0; i < arrSize; i++){
float windowSizeY = window.getSize().y;
float height = -(windowSizeY/maxValue) * currentStep.at(i);
sf::RectangleShape rectangle = rectangleList[i];
rectangle.setSize({rectangleSizeX, height});
window.draw(rectangle);
}
//display what is set up
window.display();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
step++;
if(step > steps.size() - 1){
step = steps.size() - 1;
}
currentStep = steps.at(step);
}
}