Page 1 of 1

I created a fast rounded corner rectangle algorithm

Posted: Sat Sep 17, 2022 5:09 am
by IlcIliaDev
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: Select all

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
	}
};