I created a fast rounded corner rectangle algorithm

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
Post Reply
IlcIliaDev
Posts: 1
Joined: Fri Apr 15, 2022 1:53 pm

I created a fast rounded corner rectangle algorithm

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