这是我到目前为止的代码。
void alternating_rectangles(fstream& image_out, int width, int height, int rectangle_width, color first_color, color second_color) {
int center_x = width / 2;
int center_y = height / 2;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int x_dist = abs(center_x - x);
int y_dist = abs(center_y - y);
bool is_first_color = (x_dist / rectangle_width) == (y_dist / rectangle_width) && (x_dist / rectangle_width) % 2 == 0;
color pixel_color = is_start_color ? first_color : second_color;
write_pixel(image_out, pixel_color);
}
}
}
I具有将像素的颜色写入.ppm文件的'Write_pixel'函数。 “宽度”和“高度”参数是图像的维度。 “ Rectangle_width”参数是每个矩形的厚度。 (如果愿意,每个单独的条纹的宽度)。
注:
我必须在一个通道中创建此模式,因此我不能仅仅在上一个较小的矩形上绘制每个较小的矩形。我不能覆盖像素。
该代码生成此(不完整)模式。
您可以直接从x和y和环数量计算颜色。
微微地,您想要:
:x
y
(width - 1) - x
告诉您您在哪一圈(从外部计数)。然后您可以拿起戒指mod 2来获得颜色。
(height - 1) - y