Results 1 to 3 of 3

Thread: drawArc in the rectangle issues

  1. #1
    Join Date
    Feb 2017
    Posts
    1

    Question drawArc in the rectangle issues

    drawArc.jpgwanted.JPGwanted.JPG

    my program will get the startpoints(x,y) and calculate width and height when you draw on the scene, the attached photo shows it.
    the problem is i want to draw a perfect arc in the middle of rectangle as the sketch one attached.
    my code as below:
    Qt Code:
    1. void ResizableRectMarker::paintArc1(int x, int y, int width, int height, QPainter* painter)
    2. {
    3. double radius = sqrt(pow(0.5*width,2)+pow(0.5*height,2));
    4. double step = sqrt(pow(radius,2)-pow(0.5*width,2));
    5.  
    6. QPointF topLeft(x,y);
    7. QPointF bottomRight(x+width,y+height);
    8. QRectF rect(topLeft, bottomRight);
    9.  
    10. int startAngle = atan2(0.5*width,step)* 180 / M_PI * 16;
    11. int spanAngle = 2*startAngle;
    12. qDebug() << "start angle:" << startAngle << "|" << "spanAngle:" << spanAngle;
    13. painter->drawArc(rect, startAngle, spanAngle);
    14. }
    To copy to clipboard, switch view to plain text mode 
    the problem is the the arc drawn is not in the center, and also if the rectangle is not a square, the arc is wired.
    any solution for it.

    newbie here..
    Last edited by anda_skoa; 15th February 2017 at 09:04. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,418
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: drawArc in the rectangle issues

    It is not entirely clear what your objective is.

    Your drawing suggests that you want a circle arc that intersects the rectangle on its left and right side in the middle, the screenshot and code indicates an ellipse arc that fits into the rectangle.

    Cheers,
    _

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: drawArc in the rectangle issues

    I think this is what you want
    Qt Code:
    1. class ArcWidget : public QWidget
    2. {
    3. protected:
    4. void paintEvent(QPaintEvent * event) override
    5. {
    6. QPainter painter(this);
    7.  
    8. const double radius = qSqrt(qPow(width()/2.0, 2) + qPow(height()/2.0, 2));
    9. const int startAngle = qAtan((width()/2.0)/radius) * 180 / M_PI * 16;
    10. const int spanAngle = (90 - startAngle) * 2 * 16;
    11. const QRect rect = event->rect().adjusted(-(radius-width()/2.0), height()-radius, (radius-width()/2.0), radius);
    12.  
    13. painter.drawArc(rect, startAngle, spanAngle);
    14. }
    15. };
    16.  
    17. int main(int argc, char *argv[])
    18. {
    19. QApplication app(argc, argv);
    20.  
    21. ArcWidget widget;
    22. widget.show();
    23.  
    24. return app.exec();
    25. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. QwtPlotMarker rectangle
    By Khaine in forum Qwt
    Replies: 1
    Last Post: 10th April 2016, 18:08
  2. QPainter drawArc question
    By kloopa in forum Newbie
    Replies: 6
    Last Post: 12th January 2016, 07:29
  3. Replies: 2
    Last Post: 25th December 2014, 15:15
  4. Center point and drawArc
    By seniorc in forum Newbie
    Replies: 1
    Last Post: 24th March 2014, 21:19
  5. Converting MFC CDC::Arc(..) to QPainter->drawArc
    By maverick_pol in forum Qt Programming
    Replies: 8
    Last Post: 26th September 2007, 08: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.