Results 1 to 6 of 6

Thread: Rotating a rectangle about its origin

  1. #1
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Rotating a rectangle about its origin

    hi
    How to rotate the rectangle about its origin. i have tried first by rotating the matrix and the calculating the distance between the topLeft() corner of a rectangle and the distance to which it has rotated ,and then translating it.I am unable to rotate about origin of rectangle, But i am able to rotate around the topLeft() corner of the rectangle.

    Here is some code which rotates the about the topLeft() of a rectangle .Please help modify the code to make the rectangle rotate around its centre.

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Widget : public QWidget
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. void paintEvent(QPaintEvent * event)
    9. {
    10. QMatrix matrix;
    11. QRect rect(20,20,150,100);
    12. matrix.translate(rect.left(), rect.top());
    13. static int degree=0;
    14. matrix.rotate(++degree);
    15.  
    16. QPainter painter(this);
    17. painter.setMatrix(matrix);
    18. rect.setX(0);rect.setY(0);
    19. painter.drawRect(rect);
    20. painter.drawPoint(rect.center());
    21.  
    22. QTimer::singleShot(100,this, SLOT(update()));
    23. }
    24. };
    25.  
    26. int main(int argc, char *argv[])
    27. {
    28. QApplication a(argc, argv);
    29. a.setStyleSheet("QWidget{background: white}");
    30. Widget w;
    31. w.show();
    32. return a.exec();
    33. }
    34.  
    35. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    Last edited by babu198649; 21st August 2008 at 12:25.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Rotating a rectangle about its origin

    This should work:

    Qt Code:
    1. QPainter painter(this);
    2. painter.fillRect(rect(), Qt::white);
    3. QRect rct(...);
    4. QPoint center = rct.center();
    5. painter.save();
    6. painter.translate(center.x(), center.y());
    7. painter.rotate(degrees);
    8. rct = QRect(-center.x(), -center.y(), rect.width(), rect.height());
    9. painter.drawRect(rct);
    10. painter.restore();
    To copy to clipboard, switch view to plain text mode 

    BTW. Changing the state of the object in paintEvent like you do with the "degree" variable is not a smart thing to do (not speaking about running a timer from it - I could kill your system within seconds with such an app). Your homework is to think about why is so.

  3. #3
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: Rotating a rectangle about its origin

    Thanks for u r reply.
    I tried u r code but it did not worked. i want to rotate a rectangle about its own origin.

    After rotation the rectangle and the moving it to half of the width of the window ,and half the height of the window does not bring the rectangle to its centre ,because the on every rotation the width and height the rectangle moved is different.

    i have attached a image which shows that the width and height to which the rectangle should be moved varies on every rotation.


    /home/bala/Desktop/wyot.png

    BTW. Changing the state of the object in paintEvent like you do with the "degree" variable is not a smart thing to do (not speaking about running a timer from it - I could kill your system within seconds with such an app).
    I have read from the QPainter docs that QPainter has an internal stack which maintains the state of a painter i suppose that the internal state is changed by QMatrix which may lead to crash,but i am not sure ,Please explain how will u crash the system externally and please tell me how to avoid it.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Rotating a rectangle about its origin

    Quote Originally Posted by babu198649 View Post
    i want to rotate a rectangle about its own origin.
    Take a piece of paper, draw a cartesian plane with the y axis inverted and the origin in top left corner, draw an arbitrary rectangle on the plane and think what needs to be done to rotate the rectangle around its origin assuming that you can only rotate around the plane origin ([0,0] point).

    I have read from the QPainter docs that QPainter has an internal stack which maintains the state of a painter i suppose that the internal state is changed by QMatrix which may lead to crash,but i am not sure ,Please explain how will u crash the system externally and please tell me how to avoid it.
    No, this has nothing to do with the painter object. It's strictly about doing anything concerning application logic in a paint event. Think again

  5. #5
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: Rotating a rectangle about its origin

    I figured that after rotation, the rectangle must be translated to a distance such that the centre of the rectangle must be at the origin from where it is rotated.

    I have attached a image which shows the distance(ch and cw) to which the rectangle should be moved and below is the code which moves the rectangle.But the code is not working as expected.

    w2.jpg
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Widget : public QWidget
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. void paintEvent(QPaintEvent * event)
    9. {
    10. QMatrix matrix;
    11. QRect rect(20,20,150,100);
    12. matrix.translate(rect.left(), rect.top());
    13. static int degree=0;
    14. matrix.rotate(++degree);
    15.  
    16. int OA=sqrt(pow(rect.width(),2) + pow(rect.height(),2))/2;//OA is half the size of, diagonal of the rectangle
    17. int D1=acos(OA*2 / rect.width());
    18. int D=D1+degree;
    19. int ch = OA * cos(D);
    20. int cw = OA * sin(D);
    21. matrix.translate(cw,ch);
    22.  
    23. QPainter painter(this);
    24. painter.setMatrix(matrix);
    25. rect.setX(0);rect.setY(0);
    26. painter.drawRect(rect);
    27. painter.drawPoint(rect.center());
    28.  
    29. QTimer::singleShot(100,this, SLOT(update()));
    30. }
    31. };
    32.  
    33. int main(int argc, char *argv[])
    34. {
    35. QApplication a(argc, argv);
    36. a.setStyleSheet("QWidget{background: white}");
    37. Widget w;
    38. w.show();
    39. return a.exec();
    40. }
    41.  
    42. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    It's strictly about doing anything concerning application logic in a paint event
    I am just rotating the rectangle in the paintevent which can not be done outside the paintevent ,other than this i am incrementing the variable degree.Please tell me how does incrementing the variable in the paintevent will cause a threat to the program.
    Last edited by babu198649; 4th September 2008 at 11:48.

  6. #6
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: Rotating a rectangle about its origin

    Finally i am able to rotate the rectangle about its origin.Below is the working code.
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Widget : public QWidget
    4. {
    5. Q_OBJECT
    6. public:
    7. void paintEvent(QPaintEvent * event)
    8. {
    9. QRect rect(0,0,150,100);
    10. QPainter painter(this);
    11. painter.translate(150,150);
    12. painter.drawRect(rect);
    13. static qreal degree=0;
    14. painter.rotate(++degree);
    15. painter.translate(-rect.width()/2,-rect.height()/2);
    16. painter.drawRect(rect);
    17. painter.drawPoint(rect.center());
    18.  
    19. QTimer::singleShot(0,this, SLOT(update()));
    20. }
    21. };
    22.  
    23. int main(int argc, char *argv[])
    24. {
    25. QApplication a(argc, argv);
    26. a.setStyleSheet("QWidget{background: white}");
    27. Widget w;
    28. w.show();
    29. return a.exec();
    30. }
    31.  
    32. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    It's strictly about doing anything concerning application logic in a paint event
    Please tell the mistake in my program and how to avoid it.How would u have killed it.
    Last edited by babu198649; 25th October 2008 at 12:34.

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.