Creating a for loop that draws pixels close together to make a square shape look filled in with color
Solution 1:
There'se issues with your code. First, the loop definition for (int x = 0; x > parameterX; x++)
states that x > parameterX
in order for the loop body to execute, likewise with for (int y = 0; y > parameterY; y++)
with y > parameterY
. From the code you posted it would seem that the rectangle (or square if parameterY == parameterX
) goes from 0 to parameterX
in the x-axis and from 0 to parameterY
in the y-axis. Hence you should change your code to
void drawRectangle(int parameterX, int parameterY) {
// draw rectangle
for (int x = 0; x < parameterX; x++) {
for (int y = 0; y < parameterY; y++) {
SetPixel(x, y, 255, 255, 255);
}
}
}
Note that SetPixel(int x, int y, unsigned char red, unsigned char green, unsigned char blue)
seems to take x
and y
as the current pixel's x and y coordinate, that's why it is called like SetPixel(x, y, 255, 255, 255);
. Also note that MAX / 2
is dropped because it would fix your y-coordinate to that value always during the loop, and finally note that parameters red
, green
and blue
are the color components of an RGB color space, the final color is the combination of those three channel values, and those values go from 0 to 255, that's why these parameters are defined as unsigned char
. The new code is read as, for every pixel in the rectangle going from 0 to parameterX
in the x-axis and from 0 to parameterY
in the y-axis then set the pixel's red component to 255, green component to 255 and blue component also to 255. In RGB colorspace that color is white.