OSDev.org
https://forum.osdev.org/

I created a fast rounded corner rectangle algorithm
https://forum.osdev.org/viewtopic.php?f=15&t=56488
Page 1 of 1

Author:  IlcIliaDev [ Sat Sep 17, 2022 5:09 am ]
Post subject:  I created a fast rounded corner rectangle algorithm

I have written a function to draw a filled (and only border) rectangle with rounded corners. It is very fast and small. In order to use it you just need some function to draw a horizontal line (or just need to replace the function call to a for loop that writes pixels on the x axis). The only slight problem is when the radius gets smaller then 13-14 then it starts becomming more like a diagonal line.
Feel free to use this code however you want!

Code:
void FillRoundedRect(int xPos, int yPos, int width, int height, int radius) {
   double xstart = (double)radius / 2.5 + 1.0; // Calculate the offset on the X axis
   
   for(int y = 0; y < height; y++) {
      if(y < radius) { // Top calculation
         xstart /= 1.4; // Push the line back
      }
      else if(y > height - radius) { // Bottom calculation
         xstart *= 1.4; // Push the line forward
      }
      DrawLine(xPos + xstart, y + yPos, xPos + width - xstart - 1, y + yPos); // Draw the horizontal line
   }
};

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/