-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualizer.cpp
More file actions
77 lines (56 loc) · 1.52 KB
/
Copy pathvisualizer.cpp
File metadata and controls
77 lines (56 loc) · 1.52 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 "visualizer.hpp"
#include "params.hpp"
#include <cstdlib>
#include <ctime>
#include <iostream>
Visualizer::Visualizer() {
mNbBars = WIN_WIDTH/BAR_WIDTH;
mIndex = 0;
std::srand(std::time(nullptr));
for(int i = 0; i < mNbBars; i++)
{
mBars.push_back(Bar {rand()%(int)WIN_HEIGHT});
if(i > 0)
mBars.back().move(sf::Vector2f(BAR_WIDTH*i, 0));
}
}
void Visualizer::sort() {
mBars.at(mIndex).setColor(sf::Color::Green);
if(mIndex > 0)
mBars.at(mIndex - 1).setColor(sf::Color::White);
else
mBars.at(mNbBars - 1).setColor(sf::Color::White);
//Bubble Sort//
if(mIndex+1 < mNbBars)
{
Bar &bar1 = mBars.at(mIndex);
Bar &bar2 = mBars.at(mIndex + 1);
int val1 = bar1.getValue();
int val2 = bar2.getValue();
std::cout << "A: " << val1 << ", B: " << val2 << " swap ?" << (val1 > val2) << std::endl;
if(val1 > val2)
{
swap_bars(mIndex,mIndex+1);
}
}
///////////////
mIndex++;
if(mIndex >= mNbBars)
mIndex = 0;
}
void Visualizer::swap_bars(int index1, int index2) {
Bar &bar1 = mBars.at(index1);
Bar &bar2 = mBars.at(index2);
const sf::Vector2f tempPos = bar1.getPosition();
bar1.setPosition(bar2.getPosition());
bar2.setPosition(tempPos);
Bar tempBar = bar1;
bar1 = bar2;
bar2 = tempBar;
}
void Visualizer::draw(sf::RenderWindow &window) {
for(Bar bar : mBars)
{
bar.draw(window);
}
}