Bom dia pessoal, de momento ando a fazer o curso cs50 introduction to CS, e gostava da vossa opinião/experiência num exercicio que resolvi mas que não fiquei satisfeito porque sei que existe uma solução mais elegante mas simplesmente não consegui chegar lá:
Sei que é possível colocar estes if statemants todos em 2 nested for loops, totalizando 4, mas não consigo fazer dessa forma, apenas desta maneira mais "feia", alguém consegue dar-me uma dica de como o conseguir?
cumps
Código:
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE image2[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image2[i][j] = image[i][j];
}
}
for (int h = 0; h < height; h++)
{
for (int w = 0; w < width; w++)
{
int red = 0;
int green = 0;
int blue = 0;
int counter = 0;
if (h >= 0 && w >= 0)
{
red += image2[h][w].rgbtRed;
green += image2[h][w].rgbtGreen;
blue += image2[h][w].rgbtBlue;
counter++;
}
if (h >= 0 && w - 1 >= 0)
{
red += image2[h][w-1].rgbtRed;
green += image2[h][w-1].rgbtGreen;
blue += image2[h][w-1].rgbtBlue;
counter++;
}
if ((h >= 0 && w + 1 >= 0) && (h >= 0 && w + 1 < width))
{
red += image2[h][w+1].rgbtRed;
green += image2[h][w+1].rgbtGreen;
blue += image2[h][w+1].rgbtBlue;
counter++;
}
if (h - 1 >= 0 && w >= 0)
{
red += image2[h-1][w].rgbtRed;
green += image2[h-1][w].rgbtGreen;
blue += image2[h-1][w].rgbtBlue;
counter++;
}
if (h - 1 >= 0 && w - 1 >= 0)
{
red += image2[h-1][w-1].rgbtRed;
green += image2[h-1][w-1].rgbtGreen;
blue += image2[h-1][w-1].rgbtBlue;
counter++;
}
if ((h - 1 >= 0 && w + 1 >= 0) && (h - 1 >= 0 && w + 1 < width))
{
red += image2[h-1][w+1].rgbtRed;
green += image2[h-1][w+1].rgbtGreen;
blue += image2[h-1][w+1].rgbtBlue;
counter++;
}
if ((h + 1 >= 0 && w >= 0) && (h + 1 < height && w >= 0))
{
red += image2[h+1][w].rgbtRed;
green += image2[h+1][w].rgbtGreen;
blue += image2[h+1][w].rgbtBlue;
counter++;
}
if ((h + 1 >= 0 && w - 1 >= 0) && (h + 1 < height && w - 1 >= 0))
{
red += image2[h+1][w-1].rgbtRed;
green += image2[h+1][w-1].rgbtGreen;
blue += image2[h+1][w-1].rgbtBlue;
counter++;
}
if ((h + 1 >= 0 && w + 1 >= 0) && (h + 1 < height && w + 1 < width))
{
red += image2[h+1][w+1].rgbtRed;
green += image2[h+1][w+1].rgbtGreen;
blue += image2[h+1][w+1].rgbtBlue;
counter++;
}
image[h][w].rgbtRed = round((float)red / counter);
image[h][w].rgbtGreen = round((float)green / counter);
image[h][w].rgbtBlue = round((float)blue / counter);
}
}
return;
}
Sei que é possível colocar estes if statemants todos em 2 nested for loops, totalizando 4, mas não consigo fazer dessa forma, apenas desta maneira mais "feia", alguém consegue dar-me uma dica de como o conseguir?
cumps