Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Swapping element in a Vector  (Read 3309 times)

0 Members and 1 Guest are viewing this topic.

Sparcsky

  • Newbie
  • *
  • Posts: 9
    • View Profile
Swapping element in a Vector
« on: June 13, 2017, 11:36:55 am »
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.




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();
}
 

« Last Edit: June 13, 2017, 11:41:11 am by Sparcsky »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Swapping element in a Vector
« Reply #1 on: June 13, 2017, 11:41:25 am »
You just changed their index in the vector, but their properties remain the same. Moving a bar from index 9 to index 1 won't magically change its graphical position, after the swap it is still red and at x = 190.
Laurent Gomila - SFML developer

Sparcsky

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Swapping element in a Vector
« Reply #2 on: June 13, 2017, 11:51:15 am »
You just changed their index in the vector, but their properties remain the same. Moving a bar from index 9 to index 1 won't magically change its graphical position, after the swap it is still red and at x = 190.

You said that x = 190 after the swap but I'm confused why's output  X = 110 after the swapping?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Swapping element in a Vector
« Reply #3 on: June 13, 2017, 11:55:19 am »
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.
Laurent Gomila - SFML developer

Sparcsky

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Swapping element in a Vector
« Reply #4 on: June 13, 2017, 12:14:22 pm »
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[


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Swapping element in a Vector
« Reply #5 on: June 13, 2017, 12:33:16 pm »
Looks better. Have you tried it?
Laurent Gomila - SFML developer

Sparcsky

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Swapping element in a Vector
« Reply #6 on: June 13, 2017, 12:43:55 pm »
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);



Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Swapping element in a Vector
« Reply #7 on: June 13, 2017, 02:12:46 pm »
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.
Laurent Gomila - SFML developer