Can anyone point out what's wrong with my code? I tried swapping the vector's element to another element of the vector itself using std::swap(). It worked, the member class of each other have swapped but the problem is that in the left image the Blue Bar did not swap with the Red Bar.
(http://i.imgur.com/qBshbKt.png)
Main.cpp
int main()
{
unsigned int width = 800;
unsigned int height = 600;
sf::RenderWindow window(sf::VideoMode(width, height), "Algorithm Visualizer");
std::vector<Bar> bars;
for (size_t i = 0; i < 10; i++)
{
Bar bar(sf::Vector2f(5, i + 10), sf::Vector2f(100 + i * 10, 300));
bars.push_back(bar);
}
std::cout << "Blue 1 height: " << bars[1].getHeight() << std::endl;
std::cout << "Blue 1 pos X: " << bars[1].getPos().x << std::endl;
std::cout << "Red 9 height: " << bars[9].getHeight() << std::endl;
std::cout << "Red 9 pos X: " << bars[9].getPos().x << std::endl;
bars[1].setColor(sf::Color::Blue);
bars[9].setColor(sf::Color::Red);
std::swap(bars[1], bars[9]);
std::cout << "After swapping new value: " << std::endl;
std::cout << "Blue 1 height: " << bars[1].getHeight() << std::endl;
std::cout << "Blue 1 pos X: " << bars[1].getPos().x << std::endl;
std::cout << "Red 9 height: " << bars[9].getHeight() << std::endl;
std::cout << "Red 9 pos X: " << bars[9].getPos().x << std::endl;
Bar.cpp
Bar::Bar(sf::Vector2f size, sf::Vector2f pos)
{
rect.setSize(size);
rect.setOrigin(rect.getOrigin().x, size.y);
rect.setPosition(pos);
}
Bar::~Bar()
{
}
void Bar::setColor(sf::Color color)
{
rect.setFillColor(color);
}
void Bar::setProperty(ShapeProperty property)
{
rect.setFillColor(property.fill_);
rect.setOutlineColor(property.outline_);
rect.setOutlineThickness(property.thickness_);
}
void Bar::setPosition(sf::Vector2f pos)
{
rect.setPosition(pos);
}
void Bar::setSize(sf::Vector2f size)
{
rect.setOrigin(rect.getOrigin().x, size.y);
rect.setSize(size);
}
void Bar::draw(sf::RenderWindow & window)
{
window.draw(rect);
}
void Bar::update()
{
}
int Bar::getWidth()
{
return rect.getGlobalBounds().width;
}
int Bar::getHeight()
{
return rect.getGlobalBounds().height;
}
sf::Vector2f Bar::getPos()
{
return rect.getPosition();
}
After swapping the red/190 bar is no longer at index 9, so what you print are the properties of the blue/110 bar. But the thing is, there's still a red/190 and a blue/110 bar, just organized differently in your vector.
Oh, I see I'm outputting the wrong way. but how should I swap each other properties?
Will this good enough?
sf::Vector2f temp = bars[1].getPos();
bars[1].setPosition(bars[9].getPos());
bars[9].setPosition(temp);
bar[
Yeah, it worked. thanks by the way 8) I'm trying to make a bubble sort algorithm. Do you think this would completely swap them both?
std::swap(bars[1], bars[9]);
sf::Vector2f temp = bars[1].getPos();
bars[1].setPosition(bars[9].getPos());
bars[9].setPosition(temp);
(http://i.imgur.com/nU5Wo7H.png)
I would probably do it differently. Since their graphical position is tied to their index in the vector, and not to the bar instance itself, I would set the position of each tile according to its index everytime it is drawn. This way you just have to swap bars, like you did initially.
// draw code...
for (std::size_t i = 0; i < bars.size(); ++i)
{
bars[i].setPosition({100 + i * 10, 300});
bars[i].draw(window);
}
Or even
// draw code...
for (std::size_t i = 0; i < bars.size(); ++i)
bars[i].draw(window, sf::Transform().translate(100 + i * 10, 300));
... since the position is not an attribute of the bar itself, it's just where you draw it according to the output of the algorithm.