Drawing Rounded Rects
DanielH

I looking for a way to draw a rounded rect given the radius and the thickness of the lines. Here is my code. I could have just done a circlefill instead of putpixels, but I want the rectangle to be contained in x1y1-x2y2. So the thicker I increase the thickness, the rectangle stays the same size. Just the thickness of the lines changes.

Here is a screenshot with radius=16, thickness=12,color=black
http://www.dharmon.cc/misc/sshot.png

I wanted to give more than just the color to do_line and do_arc. I wanted to add a thickness. So I created a struct and sent the struct instead.

Does anyone have a better way to do this?

Your inner radius is same as outer radius. The arches have different middle points. So the thickness is not constant. It doesn't look bad, but I'd draw them with same center point.

Guess my approach would to draw four rectangles forming the straight edges. Then I'd use quadratic bitmaps for the corners. I'd start with magic pink for the quadrates, then I'd draw a black larger circle and a magenta smaller circle, both with centre in the corner of the quadrate. Then I'd transparent blit the quadrate to the right place.

DanielH

I thought about using one bitmap for the corners, but wouldn't creating it, drawing to it, and blitting it be more time consuming?

Quote:

Does anyone have a better way to do this?

```ol::Rect( x, y, w, h, thickness, roundness ).Draw( color );
```

Seriously though...

Quote:

I thought about using one bitmap for the corners, but wouldn't creating it, drawing to it, and blitting it be more time consuming?

You mean, more time-consuming than to call rectfill per each pixel through a function pointer? The best way would be to rip Allegro's circle code and modify that to draw your thick edges - all 4 at the same time as they're mirror images of each other.