qtqpainterqtguiqrect

How to draw an arc with Qt?


Consider the following diagram:

I have information about the center point of both the lines, the angle in between, and the length of both the lines.

The issue is to draw an arc starting at the end of the bottom line and touching the above slanting line (the way shown below):

     /
    /
   /
  /.
 /  .
/___.

I saw these arc drawing functions of Qt:
http://qt-project.org/doc/qt-5.1/qtgui/qpainter.html#drawArc

These functions need a rectangle as an argument where as I don't have any.

How should I use these functions to draw the arc as shown above?


Solution

  • QPointF O; // intersection of lines
    QPointF B; // end point of horizontal line
    QPointF A; // end point of other line
    
    float halfSide = B.x-O.x;
    QRectF rectangle(O.x - halfSide,
                     O.y - halfSide,
                     O.x + halfSide,
                     O.y + halfSide);
    
    int startAngle = 0;
    int spanAngle = (atan2(A.y-O.y,A.x-O.x) * 180 / M_PI) * 16;
    
    QPainter painter(this);
    painter.drawArc(rectangle, startAngle, spanAngle);
    

    You have to calculate the boundary rectangle, than the angle between the lines using atan.