OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 2:40 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: I created a fast rounded corner rectangle algorithm
PostPosted: Sat Sep 17, 2022 5:09 am 
Offline

Joined: Fri Apr 15, 2022 1:53 pm
Posts: 1
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
   }
};


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 24 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group