gpt4 book ai didi

c++ - 抗锯齿 PPM 文件

转载 作者:行者123 更新时间:2023-11-30 03:48:01 25 4
gpt4 key购买 nike

我从下面的代码中得到了这个输出 circle.ppm它有很多锯齿状的边缘。我相信我需要某种类型的抗锯齿方法,但我很难开始。

摆脱它们的最简单方法是什么?

//******************** INCLUDE: ********************

#include <fstream> // ofstream
#include <iostream> // cin, cout
#include <cmath> // sqrt, pow

using namespace std; // USING STANDARD NAMESPACE

//******************** DECLARATION OF FUNCTIONS: ********************

bool isInsideCircle(int x,int y); // CHECKS IF (X,Y) IS INSIDE CIRCLE
int Brightness(int x,int y); // DISTANCE FROM CENTER


//******************** VARIABLES: ********************

const int WIDTH = 255, HEIGHT = 255; // WIDTH AND HEIGHT RESOLUTION
const int RADIUS = 100; // RADIUS OF CIRCLE

const int CENTER_X = 0.5 * WIDTH; // CENTER X-COORDINATES
const int CENTER_Y = 0.5 * HEIGHT; // CENTER Y-COORDINATES

int r, g, b; // RED, GREEN AND BLUE

//******************** MAIN FUNCTION: ********************

int main() {

system("open circle.ppm"); // OPENS THE IMAGE
system("killall ToyViewer"); // CLOSES THE IMAGE

//******************** PPM SETUP: ********************

ofstream img ("circle.ppm"); // MAKE THE FILE
img << "P3" << endl; // MAGIC NUMBER
img << WIDTH << endl; // WIDTH RESOLUTION
img << HEIGHT << endl; // HEIGHT RESOLUTION
img << "255" << endl; // 0-255 MAX RGB VALUE

//******************** FOR EACH PIXEL: ********************

for (int y = 0; y < HEIGHT; y++) {

for (int x = 0; x < WIDTH; x++) {

if (isInsideCircle(x, y)) {

r = 50 + Brightness(x, y) % 255;
g = 50 + Brightness(x, y) % 255;
b = 50 + Brightness(x, y) % 255;
}

else {

r = 0;
g = 0;
b = 0;

}

img << r << " " << g << " " << b << endl;
}
}

//******************** PROGRAM END: ********************

system("open circle.ppm");
return 0;

}

//******************** FUNCTIONS: ********************

bool isInsideCircle(int x, int y) {

if (pow((x - CENTER_X),2) + pow((y - CENTER_Y),2) < pow(RADIUS,2)) {

return true;

} else {

return false;
}
}

int Brightness(int x, int y) {

return abs( sqrt( pow((x - CENTER_X),2) + pow((y - CENTER_Y),2)) - RADIUS);

}

最佳答案

正如 Daniel 指出的那样,您的收入急剧下降。实际上不是从 1 到 0,而是从 {50,50,50} 到 {0,0,0}。那是因为 50 + Brightness 行。

简单的解决方案是使用稍微不那么急剧的下降。定义一个从 50 渐近下降到 0 的 Brightness2(x,y)

int Brightness2(int x, int y) {
int dx = x - CENTER_X;
int dy = x - CENTER_Y;
return 50/(1+(dx*dx+dy*dy-RADIUS*RADIUS));
}

关于c++ - 抗锯齿 PPM 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33413496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com