Search code examples
c++for-loopbitmappixeldrawrectangle

Trying to draw a Rectangle (Bitmap)


Ive used two simple for loops which sets pixels to create a left side and top side to a rectangle. My code looks this:

for (int i = 0; i < Width; i++)
{
    SetViewportPixel(X+i, Y, PixelColour);
}

for (int j = 0; j < Height; j++)
{
    SetViewportPixel(X , Y+j, PixelColour);
}

I've tried many different ways to create the other half but keep stumbling. I've tried using nested for loops to no avail. Was wondering if anyone could help? Thanks.


Solution

  • for (int i = 0; i < Width; i++)
    {
        SetViewportPixel(X+i, Y+Height-1, PixelColour);
    }
    
    for (int j = 0; j < Height; j++)
    {
        SetViewportPixel(X+Witdh-1 , Y+j, PixelColour);
    }
    

    Should take care of the other half.