Skip to main content
Code Review

Return to Answer

Bounty Awarded with 50 reputation awarded by glampert
fix typos in code
Source Link
Quuxplusone
  • 19.7k
  • 2
  • 44
  • 91
void fillBufferWithCheckerPattern(
 Color *buffer, int width, int height, int squares, const Color colors[2])
{
 assert(buffer != NULL);
 assert(squares >= 2);
 const int checkerSize = width / squares;
 assert(width > 0 && width % squares == 0);
 assert(height > 0 && height % squares == 0);
 for (int y = 0; y < height; ++y) {
 for (int x = 0; x < width; ++x) {
 const int colorIndex = ((y / checkerSize) + (x / checkerSize)) % 2;
 buffer[y*width+x] = colors[colorIndex];
 }
 }
}
static inline int min2(int a, int b) { return a < b ? a : b; }
static inline int min4(int a, int b, int c, int d)
{
 return min2(a, min2(b, min2(c, d)));
}
void fillBufferWithBoxPattern(
 Color *buffer, int width, int height,
 int lineThickness, const Color colors[2])
{
 assert(buffer != NULL);
 assert(width > 0);
 assert(height > 0);
 for (int y = 0; y < height; ++y) {
 for (int x = 0; x < width; ++x) {
 // Is it closer to the top, bottom, left, or right?
 const int mindist = min4(
 x, // distance from left side
 y, // distance from top
 width-x-1, // distance from right side
 height-y-1 // distance from bottom
 );
 const int colorIndex = (mindist / lineThickness) % 2;
 buffer[y*width+x] = colors[colorIndex];
 }
 }
}
void fillBufferWithCheckerPattern(
 Color *buffer, int width, int height, int squares, const Color colors[2])
{
 assert(buffer != NULL);
 assert(squares >= 2);
 const int checkerSize = width / squares;
 assert(width > 0 && width % squares == 0);
 assert(height > 0 && height % squares == 0);
 for (int y = 0; y < height; ++y) {
 for (int x = 0; x < width; ++x) {
 const int colorIndex = (y / checkerSize) + (x / checkerSize) % 2;
 buffer[y*width+x] = colors[colorIndex];
 }
 }
}
static inline min2(int a, int b) { return a < b ? a : b; }
static inline min4(int a, int b, int c, int d)
{
 return min2(a, min2(b, min2(c, d)));
}
void fillBufferWithBoxPattern(
 Color *buffer, int width, int height,
 int lineThickness, const Color colors[2])
{
 assert(buffer != NULL);
 assert(width > 0);
 assert(height > 0);
 for (int y = 0; y < height; ++y) {
 for (int x = 0; x < width; ++x) {
 // Is it closer to the top, bottom, left, or right?
 const int mindist = min4(
 x, // distance from left side
 y, // distance from top
 width-x-1, // distance from right side
 height-y-1 // distance from bottom
 );
 const int colorIndex = (mindist / lineThickness) % 2;
 buffer[y*width+x] = colors[colorIndex];
 }
 }
}
void fillBufferWithCheckerPattern(
 Color *buffer, int width, int height, int squares, const Color colors[2])
{
 assert(buffer != NULL);
 assert(squares >= 2);
 const int checkerSize = width / squares;
 assert(width > 0 && width % squares == 0);
 assert(height > 0 && height % squares == 0);
 for (int y = 0; y < height; ++y) {
 for (int x = 0; x < width; ++x) {
 const int colorIndex = ((y / checkerSize) + (x / checkerSize)) % 2;
 buffer[y*width+x] = colors[colorIndex];
 }
 }
}
static inline int min2(int a, int b) { return a < b ? a : b; }
static inline int min4(int a, int b, int c, int d)
{
 return min2(a, min2(b, min2(c, d)));
}
void fillBufferWithBoxPattern(
 Color *buffer, int width, int height,
 int lineThickness, const Color colors[2])
{
 assert(buffer != NULL);
 assert(width > 0);
 assert(height > 0);
 for (int y = 0; y < height; ++y) {
 for (int x = 0; x < width; ++x) {
 // Is it closer to the top, bottom, left, or right?
 const int mindist = min4(
 x, // distance from left side
 y, // distance from top
 width-x-1, // distance from right side
 height-y-1 // distance from bottom
 );
 const int colorIndex = (mindist / lineThickness) % 2;
 buffer[y*width+x] = colors[colorIndex];
 }
 }
}
Source Link
Quuxplusone
  • 19.7k
  • 2
  • 44
  • 91

Always look for a way to encode things explicitly as data, or patterns in data, instead of encoding them implicitly in the control flow of your program. For example, your function to color the squares in a checkerboard pattern could have been written like this, using "(x + y) % 2" to encode the idea of "alternating colors", instead of your mess of nested loops and counter variables.

void fillBufferWithCheckerPattern(
 Color *buffer, int width, int height, int squares, const Color colors[2])
{
 assert(buffer != NULL);
 assert(squares >= 2);
 const int checkerSize = width / squares;
 assert(width > 0 && width % squares == 0);
 assert(height > 0 && height % squares == 0);
 for (int y = 0; y < height; ++y) {
 for (int x = 0; x < width; ++x) {
 const int colorIndex = (y / checkerSize) + (x / checkerSize) % 2;
 buffer[y*width+x] = colors[colorIndex];
 }
 }
}

Notice also that I've dropped the u from your uint, which means I can also get rid of your typedef — several lines of code saved, with no loss of performance or expressiveness. (And on top of all that: if somebody does accidentally pass width = -1 to this function, they'll get a nice assertion failure instead of a segfault.) The built-in types are nice; use them, wherever possible.

Similarly, for the "concentric boxes" pattern, what you're doing is coloring each pixel according to its distance from the edge of the grid. Pixels at distance 1 get red; pixels at distance 2 get yellow; pixels at distance 3 get red again; and so on, all the way to the middle.

static inline min2(int a, int b) { return a < b ? a : b; }
static inline min4(int a, int b, int c, int d)
{
 return min2(a, min2(b, min2(c, d)));
}
void fillBufferWithBoxPattern(
 Color *buffer, int width, int height,
 int lineThickness, const Color colors[2])
{
 assert(buffer != NULL);
 assert(width > 0);
 assert(height > 0);
 for (int y = 0; y < height; ++y) {
 for (int x = 0; x < width; ++x) {
 // Is it closer to the top, bottom, left, or right?
 const int mindist = min4(
 x, // distance from left side
 y, // distance from top
 width-x-1, // distance from right side
 height-y-1 // distance from bottom
 );
 const int colorIndex = (mindist / lineThickness) % 2;
 buffer[y*width+x] = colors[colorIndex];
 }
 }
}
lang-c

AltStyle によって変換されたページ (->オリジナル) /