Search code examples
c++qtdrawingqpainter

QPainter - draw objects along a circle


I would like to draw those black dots around the whole circle with regular spacing in between each other (rough image below). Those black dots should sit directly on the circle. Is there an easy way to do it somehow with painter.drawArc() function or something like that?

Desired ouput


Solution

  • No, there isn't. I am afraid you will have to use... math!

    Actually, you could get away without that. For sweeping around a circle in particular there is an easy way to do it by transforming the painter. You initially transform the painter to the center of the circle, and draw the first dot at the circle's 12 o'clock position. That means drawing it into negative y space. Then you simply rotate the painter by 360 / numOfObjects degree and draw the same circle again.

    They key thing here is to transform the painter to the center and draw the object offset, because otherwise it will be more complicated to calculate the position and angle yourself. The angle doesn't play a role here, since you are drawing dots, but it will make a huge difference if you draw something other than dots. This way you can easily sweep the circle by rotating the painter around its origin.

    To put it in pseudo code:

    draw big circle
    move painter to big circle center
    degree = 360 / numOfObjects
    while numOfObjects--
      draw dot at 12 o'clock
      rotate painter by degree