I'm trying to use QPainter::drawEllipse
to draw circles. I want to be able to:
- set the width of the stroke of the circle (
QPen::width
) - choose the shape of the pixels that are at the center of the circle (
1x1
,1x2
,2x1
or2x2
) - optionally make the circle filled instead of stroked
- ensure that the circle has the correct radius (even when the stroke width is greater than 1)
These goals are surprisingly difficult to achieve. This is an example of what I want to render (drawn by hand):
The image is 32x32 (scaled up to 512x512). The red center point is at (15, 15). The center is 1x2
so there's an extra red pixel below the center pixel. The stroke has a width of 2 pixels. If the stroke was made wider, pixels would be added to the inside of the circle. The bounding box of the circle is the same regardless of stroke width. The radius is 8 pixels. Each of the blue lines is 8 pixels long. Just to be clear, the red and blue pixels are just there for describing the circle. They are not part of my desired output.
What my problem really boils down to is rendering an ellipse that fits perfectly inside a rectangle. I can calculate the rectangle using the center point, the radius, and the center shape. That part is easy. Simply calling drawEllipse
with this rectangle doesn't work. I think I have to adjust this rectangle somehow before calling drawEllipse
but I'm not too sure how to adjust it. I've tried fiddling around with it and I found some solutions that work for some pen widths but not others.
Does the pen cap matter? I've been using RoundCap
. Should I be using a different cap?
I'm almost at the point where I'm considering doing the pixel manipulation myself. I'm rendering onto a QImage
and using the Source
composite operation so my code might be slightly faster than drawEllipse
. memset
is about 10x faster than QImage::fill
so writing faster code probably won't be too hard! I'd rather not have to do that though.