0

I tried this:

// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE temp[height][width];
    RGBTRIPLE reflection[height][width];
    struct reflection *p = malloc((height * width) + 1);
    for (int i = 0; i < height; i ++)
    {
        for (int j = 0; j < width; j++)
        {
            //need swap each pixel from left to right...
          temp[i][j] = image[i][j];
        }
    }

    for (int i = 0; i < height; i ++)
    {
        for (int j = 0; j < width; j++)
        {
            //need swap each pixel from left to right...
          image[i][j] = reflection[i][width - j];
        }
    }

    for (int i = 0; i < height; i ++)
    {
        for (int j = 0; j < width; j++)
        {
            //need swap each pixel from left to right...
          reflection[i][width - j] = temp[i][j];
        }
    }
    return;
}

It did not work.

Now I tried this. And it works.

// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    void swap(RGBTRIPLE *a, RGBTRIPLE *b);
    RGBTRIPLE temp[height][width];
    RGBTRIPLE reflection[height][width];
    struct reflection *p = malloc((height * width) + 1);
    for (int i = 0; i < height; i ++)
    {
        for (int j = 0; j < (width/2); j++)
        {
            //need swap each pixel from left to right...
          swap(&image[i][j], &image[i][width-j]);
        }
    }
   return;
}
    void swap(RGBTRIPLE *a, RGBTRIPLE *b)
    {
        RGBTRIPLE temp;
        RGBTRIPLE reflection;
        temp = *a;
        reflection = *b;
        *b = temp;
    }

Why wasn't my first method a good one? Why did I need to specifically use the swap function... I was basically doing the swap without using the function.. so what went wrong?

Also, my thing is only reflecting half of the image so my image gets stuck in the middle. To prevent this, I split the width in half on the for loop, but it doesn't seem to help.

Please advise. Thanks!

You must log in to answer this question.

Browse other questions tagged .