找不到CS50 Pset4反映出什么问题?

时间:2020-09-10 15:10:07

标签: c cs50

我已经为反射编写了这段代码,当我使用check50时,得到了以下内容:

:( reflect correctly filters 1x2 image
    expected "0 0 255\n255 0...", not "0 0 255\n0 0 2..."

:( reflect correctly filters 1x3 image
    expected "0 0 255\n0 255...", not "0 0 255\n0 255..."

:) reflect correctly filters image that is its own mirror image

:( reflect correctly filters 3x3 image
    expected "70 80 90\n40 5...", not "70 80 90\n40 5..."

:( reflect correctly filters 4x4 image
    expected "100 110 120\n7...", not "100 110 120\n7..."

但是这些数字似乎是相同的...

这是我的反射功能:

/ Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    //Cycle through rows
    for (int i = 0; i < height; i++)
    {   //Cycle through columns
        for (int j = 0; j < width; j++)
        {   //Swap the current cell with oppsite cell.
            image[i][j].rgbtRed = image[i][width - j - 1].rgbtRed;
            image[i][j].rgbtBlue = image[i][width - j - 1].rgbtBlue;
            image[i][j].rgbtGreen = image[i][width - j - 1].rgbtGreen;
        }    
    }
    
    return;
} 

1 个答案:

答案 0 :(得分:0)

首先,我想指出一下check50仅显示了很小的像素子集;仅仅因为您正确反射了前2-3个像素,并不意味着所有像素都是正确的。

也就是说,这是改善代码的三个建议:

  1. 您的目标是将图像的一半移到另一半。因此,您将不需要遍历所有列,因为正如0x5453所说的那样,这将导致您只反射两次,从而使图像保持原样,但只有一半。您可以通过在第二个for循环中设置j < round(width / 2.0)来实现此目的。
  2. 请记住David Malan的演讲,您需要一个临时杯子来切换两个杯子的内容。因此,您应该将temp变量设置为原始像素,然后将原始像素设置为另一侧的像素,然后将另一侧的原始像素设置为temp变量。这将切换原始像素,使另一像素在另一侧。即反思。
  3. 由于您只是在切换像素,因此无需理会各个颜色;您可以只处理image[i][j]而不是image[i][j].rgbtRed和其他所有颜色。在涉及颜色失真的其他部分,您需要处理颜色。