0

So I created the function reverse and it works, it flips the image. To get to this point I had to try and repeat several times. While my logic says that:

  • First I should use a temp variable to store the pixel I will swap, so I don't overwrite it when I need to copy it the the other end of the image. But it wasn't necessary and I want to understand why.

  • Second according to my logic I would also need for n pixels n/2 times to flip the complete image, but if instead of using int times = width * 2; i use int times = width / 2; only a quarter of the image is flipped (that's the reason why I multiplied times 2, because once i saw the resulting image it took me to think I needed to repeat the process times 2)

So here goes my code:

void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    for (int i = 0; i < height; i++)
    {
        int k = 0;
        int times = width * 2;
        for (int j = times - 1; j >= 0; j--)
        {
            image[i][k] = image[i][j];
            k++;
        }

    }

    return;
}

It works. But I cannot understand why it does. Please could someone explain? Thanks

You must log in to answer this question.

Browse other questions tagged .