Results 1 to 7 of 7

Thread: QPainter drawArc question

  1. #1
    Join Date
    Dec 2015
    Posts
    12
    Qt products
    PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Question QPainter drawArc question

    Hi. I'm trying to use QPainter drawArc(const QRectF & rectangle, int startAngle, int spanAngle) for arc drawing.
    And have a problem with it.
    I have a coordinates of 3 arc points A, B, C.
    A - arc start point
    B - arc end point
    C - arc center with known radius (10).

    Question. How to calculate rectangle, startAngle and spanAngle for this arc?

    question.jpg

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QPainter drawArc question

    The rectangle is the rectangle that would contain the entire circle if drawn completely, and you get the angles with basic trigonometry:
    Qt Code:
    1. QPointF const A(6.31, 5.773);
    2. QPointF const B(20.53, 19.836);
    3. QPointF const C(13.42, 12.805);
    4. double const radius(10.0);
    5.  
    6. QRectF const r(C.x() - radius, C.y() - radius, radius * 2, radius * 2);
    7. double const startAngle = 16 * atan2(A.y() - C.y(), A.x() - C.x()) * 180.0 / M_PI;
    8. double const endAngle = 16 * atan2(B.y() - C.y(), B.x() - C.x()) * 180.0 / M_PI;
    9. double const spanAngle = endAngle - startAngle;
    10.  
    11. QImage img(500, 500, QImage::Format_RGB32);
    12. img.fill(Qt::white);
    13. QPainter p(&img);
    14. p.scale(10.0, 10.0);
    15. p.setPen(Qt::red);
    16. p.drawRect(r);
    17. p.setPen(Qt::black);
    18. p.drawArc(r, startAngle, spanAngle);
    19. p.end();
    20. img.save("test.png");
    To copy to clipboard, switch view to plain text mode 
    You might need to tweak this for some cases
    Last edited by ChrisW67; 30th December 2015 at 08:43.

  3. #3
    Join Date
    Dec 2015
    Posts
    12
    Qt products
    PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter drawArc question

    Thank you for reply! It works.
    How to draw clockwise and counter-clockwise? (using drawArc and arcTo)
    How to calculate sweepLength for QPainterPath.arcTo?
    Last edited by kloopa; 2nd January 2016 at 07:24.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QPainter drawArc question

    Experiment with swapping A and B and changing the sign of the spanAngle.
    If you want to draw the shortest arc between two points and spanAngle is greater than 180 degrees (+ve or -ve) then you need to add/subtract 360 degrees to the spanAngle (changes the sign and hence drawing direction).

    The angle for arcTo is calculated the same way without the 16 multiplier used by QPainter.

  5. #5
    Join Date
    Dec 2015
    Posts
    12
    Qt products
    PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter drawArc question

    ChrisW67, Can you recommend me how to draw all known arcs using your code? Arcs may be more than (+-)180, full circle(360). I tried top play with code myself many times, but without result.

    For example, i'm reading file with coordinates:

    START: X143.178 Y82.461
    LINE-TO: X13.178 Y82.461
    ARC-TO[Clockwise]: X2.278 Y93.361 cX13.178 cY93.361
    LINE-TO: X2.278 Y151.361
    ARC-TO[Clockwise]: X13.178 Y162.261 cX13.178 cY151.361
    LINE-TO: X143.178 Y162.261
    ARC-TO[Clockwise]: X154.078 Y151.361 cX143.178 cY151.361

    Where cX, cY arc center coordinates. Arcs may drawing counter clockwise.

    I want to see like this:

    1.png

    Now i see:

    2.png

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QPainter drawArc question

    Everything you need is in my earlier post http://www.qtcentre.org/threads/6470...095#post286095
    Read the docs for QPainter::drawArc() to understand how span angle determines the drawing direction.

  7. #7
    Join Date
    Dec 2015
    Posts
    12
    Qt products
    PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter drawArc question

    For clockwise / counter-clockwise drawing question solved.
    But arcs still incorrect. I think problem when calculating angles. Because angles may be + or -.
    My program reading coordinates form TXT file (file generated by other software from DXF format, where 0 degree at 12 o'clock).

    Part of my code (Python3):
    Qt Code:
    1. def arcAngles(self, a, b, c):
    2. startAngle = 16*atan2(a.y() - c.y(), a.x() - c.x())*180 / pi
    3. endAngle = 16*atan2(b.y() - c.y(), b.x() - c.x())*180 / pi
    4.  
    5. spanAngle = abs(endAngle) - abs(startAngle)
    6.  
    7. #Clocksiwe
    8. if self.direction == -1:
    9. if spanAngle > 0:
    10. spanAngle = -spanAngle
    11.  
    12. print("START: " + str(startAngle / 16) + "\nEND: " + str(endAngle / 16) + "\nSPAN: " + str(spanAngle / 16))
    13. angles = {'start': startAngle, 'end': endAngle, 'span': spanAngle}
    14.  
    15. return angles
    To copy to clipboard, switch view to plain text mode 

    Result:

    =====1st Arc====
    START: -90.0
    END: 180.0
    SPAN: -90.0
    LastPoint: PyQt5.QtCore.QPointF(13.178, 82.461)
    EndPoint: PyQt5.QtCore.QPointF(2.278, 93.361)

    =====2nd Arc====
    START: 180.0
    END: 90.0
    SPAN: -90.0
    LastPoint: PyQt5.QtCore.QPointF(2.278, 151.361)
    EndPoint: PyQt5.QtCore.QPointF(13.178, 162.261)

    =====3rd Arc====
    START: 90.0
    END: 0.0
    SPAN: -90.0
    LastPoint: PyQt5.QtCore.QPointF(143.178, 162.261)
    EndPoint: PyQt5.QtCore.QPointF(154.078, 151.361)
    Drawing result:
    1.png

    I tried to solve this by manipulation with offset (90 degree), but still not solved.

    Substracting offset from angles:
    Qt Code:
    1. offset = 90*16
    2.  
    3. if startAngle < 0:
    4. startAngle -= -offset
    5. else:
    6. startAngle -= offset
    7.  
    8. if endAngle < 0:
    9. endAngle -= -offset
    10. else:
    11. endAngle -= offset
    To copy to clipboard, switch view to plain text mode 

    When substract offset from angles i see:

    11.png

    Add offset to angles:
    Qt Code:
    1. offset = 90*16
    2.  
    3. if startAngle < 0:
    4. startAngle += -offset
    5. else:
    6. startAngle += offset
    7.  
    8. if endAngle < 0:
    9. endAngle += -offset
    10. else:
    11. endAngle += offset
    To copy to clipboard, switch view to plain text mode 

    When adding offset to angles i see:

    22.png
    Last edited by kloopa; 12th January 2016 at 10:58.

Similar Threads

  1. Center point and drawArc
    By seniorc in forum Newbie
    Replies: 1
    Last Post: 24th March 2014, 22:19
  2. Replies: 2
    Last Post: 22nd February 2010, 22:39
  3. QPainter - drawRoundedRect Question
    By ChrisReath in forum Qt Programming
    Replies: 2
    Last Post: 14th May 2008, 15:14
  4. QPainter Question
    By atb in forum Qt Programming
    Replies: 3
    Last Post: 18th March 2008, 12:28
  5. Converting MFC CDC::Arc(..) to QPainter->drawArc
    By maverick_pol in forum Qt Programming
    Replies: 8
    Last Post: 26th September 2007, 09:57

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.