PSET 4 REFLECT 错误未反映最后一行
PSET 4 REFLECT error not reflecting last row
我不确定发生了什么。它反映了所有内容,除了每列的最后一个像素根本没有改变。请帮我解决这个问题,谢谢 :)
作业来自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];
}
}
}
只是创建另一个答案,我发现了实际问题!您必须创建一个临时数组来存储 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;
}
我不确定发生了什么。它反映了所有内容,除了每列的最后一个像素根本没有改变。请帮我解决这个问题,谢谢 :) 作业来自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];
}
}
}
只是创建另一个答案,我发现了实际问题!您必须创建一个临时数组来存储 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;
}