admin管理员组文章数量:1279176
I want to replicate this pattern with C++.
This is the code that I have so far.
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 have a 'write_pixel' function that writes the color of a pixel to a .ppm file. The 'width' and 'height' parameters are the dimensions of the image. The 'rectangle_width' parameter is the thickness of each rectangle. (the width of each individual stripe if you like). Note: I'm handling the closing and the opening of the file inside the function that calls this one.
I have to create this pattern in one pass, so I can't just draw each smaller rectangle on top of the previous one. I can't overwrite pixels.
My code generates this (incomplete) pattern.
How can I generate the pattern in the first picture if I only know the dimensions of the image and the 'thickness' of each stripe?
I want to replicate this pattern with C++.
This is the code that I have so far.
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 have a 'write_pixel' function that writes the color of a pixel to a .ppm file. The 'width' and 'height' parameters are the dimensions of the image. The 'rectangle_width' parameter is the thickness of each rectangle. (the width of each individual stripe if you like). Note: I'm handling the closing and the opening of the file inside the function that calls this one.
I have to create this pattern in one pass, so I can't just draw each smaller rectangle on top of the previous one. I can't overwrite pixels.
My code generates this (incomplete) pattern.
How can I generate the pattern in the first picture if I only know the dimensions of the image and the 'thickness' of each stripe?
Share Improve this question edited Feb 24 at 20:20 Tony asked Feb 24 at 18:19 TonyTony 3443 silver badges13 bronze badges 01 Answer
Reset to default 1You can compute the color directly from the x and y and the number of rings (width and height).
Basically you want the min
of:
x
y
(width - 1) - x
(height - 1) - y
That tells you which ring you're in (counting from the outside).
Then you can take the ring mod 2 to get the color.
int width = 19; // for example
int height = 15; // for example
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
int ring = std::min(std::min(x, y), std::min(width - 1 - x, height - 1 - y));
bool is_first_color = ring % 2 == 0;
// You can set pixel here.
setPixel(x, y, is_first_color);
}
}
本文标签:
版权声明:本文标题:c++ - How to create alternating rectangles that get smaller as they approach the center of the image - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741248003a2365255.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论