如何创建交替的矩形,这些矩形在接近图像中心时变小 我想用C ++复制此模式。 这是我到目前为止的代码。 void交流式rectangles(fstream&image_out,int width,int height,int rectangle_width,color first_color,c ...

问题描述 投票:0回答:1

这是我到目前为止的代码。

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); } } } enter image description hereI具有将像素的颜色写入.ppm文件的'Write_pixel'函数。 “宽度”和“高度”参数是图像的维度。 “ Rectangle_width”参数是每个矩形的厚度。 (如果愿意,每个单独的条纹的宽度)。 注:

我正在处理调用此文件的函数内部文件的关闭和打开。

我必须在一个通道中创建此模式,因此我不能仅仅在上一个较小的矩形上绘制每个较小的矩形。我不能覆盖像素。

该代码生成此(不完整)模式。

您可以直接从x和y和环数量计算颜色。

微微地,您想要:

enter image description heremin

c++ graphics ppm
1个回答
0
投票

x

y

  • (width - 1) - x
  • 告诉您您在哪一圈(从外部计数)。
    
    然后您可以拿起戒指mod 2来获得颜色。
  • (height - 1) - y
  • 	
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.