PSET 4 REFLECT错误未反映出最后一行

时间:2020-06-24 23:17:19

标签: cs50

我不确定发生了什么。它反映了所有内容,除了每列的最后一个像素外,其他所有内容都没有改变。请帮助我,谢谢:) 任务来自cs50x。

// Reflect image horizontally

void reflect(int height, int width, RGBTRIPLE image[height][width]){
    
    for (int h = 0; h < height; h++)
    {
        for (int w = 0; w < width; w++)
        {
            int a = h;
            int b = w;
            
            image[a][b] = image[h][width - 1 - w];
            image[h][width - 1- w] = image[h][w];
            image[h][w] = image[a][b];
        }
    }
}

1 个答案:

答案 0 :(得分:0)

仅创建另一个答案,我就发现了实际问题!您必须创建一个临时数组来存储RGB值。这是正确的代码:

// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    //Iterating over all pixels
    for (int h = 0; h < height; h++)
    {

        for (int w = 0; w < width / 2; w++)
        {

            //Creating array to store pixel values
            int temppixel[3];

            temppixel[0] = image[h][w].rgbtRed;
            temppixel[1] = image[h][w].rgbtGreen;
            temppixel[2] = image[h][w].rgbtBlue;

            //Making the last pixel be equal the first
            image[h][w].rgbtRed = image[h][width - w - 1].rgbtRed;
            image[h][w].rgbtGreen = image[h][width - w - 1].rgbtGreen;
            image[h][w].rgbtBlue = image[h][width - w - 1].rgbtBlue;

            //Now placing the first pixel in the last position; done reflecting
            image[h][width - w - 1].rgbtRed = temppixel[0];
            image[h][width - w - 1].rgbtGreen = temppixel[1];
            image[h][width - w - 1].rgbtBlue = temppixel[2];

        }

    }

    return;
}