我正在尝试使颜色饱和并确保它们不会溢出 这样他们就能画出漂亮的 Mandlebrot 集,而不会被像素化。 我正在使用 Altera DE2 板尝试通过 VGA 连接到计算机屏幕来打印此 Mandlebrot 集,但颜色略有偏差(像素化)。
如何更正下面代码中的 if 语句,使其不总是错误?
if (iteration == 500)
{
alt_up_pixel_buffer_dma_draw(my_pixel_buffer,0,0,0);
}
else
{
//double z = sqrt(xtemp * xtemp + y * y);
//int brightness = 256. * log2(1.75 + i - log2(log2(z))) / log2(500);
//color(brightness, brightness, 255);
//color is some function of iteration
alt_u8 Red = (iteration*8);///zoom);
if(Red > 255) // this if statement is always false
Red = 255;
alt_u8 Green = (iteration*4);///zoom);
if(Green > 255) // this if statement is always false
Green = 255;
alt_u8 Blue = (iteration*2);///zoom);
if(Blue > 255) // this if statement is always false
Blue = 255;
//draw the pixels
alt_up_pixel_buffer_dma_draw(my_pixel_buffer, (Blue) + (Green<<8) + (Red<<16),j,i);
}
最佳答案
您需要使用更大的整数来获得乘法结果,以便您可以测试是否超出限制。
alt_u8 Red;
uint16_t tempRed = iteration * 8;
if (tempRed > 255) {
Red = 255;
} else {
Red = tempRed;
}
关于由于试图使颜色饱和的数据类型范围有限,比较总是错误的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36658413/