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.
Code:
#include <QtGui>
{
Q_OBJECT
public:
{
QRect rect
(20,
20,
150,
100);
matrix.translate(rect.left(), rect.top());
static int degree=0;
matrix.rotate(++degree);
painter.setMatrix(matrix);
rect.setX(0);rect.setY(0);
painter.drawRect(rect);
painter.drawPoint(rect.center());
QTimer::singleShot(100,
this,
SLOT(update
()));
}
};
int main(int argc, char *argv[])
{
a.setStyleSheet("QWidget{background: white}");
Widget w;
w.show();
return a.exec();
}
#include "main.moc"
Re: Rotating a rectangle about its origin
This should work:
Code:
painter.fillRect(rect(), Qt::white);
painter.save();
painter.translate(center.x(), center.y());
painter.rotate(degrees);
rct
= QRect(-center.
x(),
-center.
y(), rect.
width(), rect.
height());
painter.drawRect(rct);
painter.restore();
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.
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
Quote:
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.
Re: Rotating a rectangle about its origin
Quote:
Originally Posted by
babu198649
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).
Quote:
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 :)
1 Attachment(s)
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.
Attachment 2512
Code:
#include <QtGui>
{
Q_OBJECT
public:
{
QRect rect
(20,
20,
150,
100);
matrix.translate(rect.left(), rect.top());
static int degree=0;
matrix.rotate(++degree);
int OA=sqrt(pow(rect.width(),2) + pow(rect.height(),2))/2;//OA is half the size of, diagonal of the rectangle
int D1=acos(OA*2 / rect.width());
int D=D1+degree;
int ch = OA * cos(D);
int cw = OA * sin(D);
matrix.translate(cw,ch);
painter.setMatrix(matrix);
rect.setX(0);rect.setY(0);
painter.drawRect(rect);
painter.drawPoint(rect.center());
QTimer::singleShot(100,
this,
SLOT(update
()));
}
};
int main(int argc, char *argv[])
{
a.setStyleSheet("QWidget{background: white}");
Widget w;
w.show();
return a.exec();
}
#include "main.moc"
Quote:
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.
Re: Rotating a rectangle about its origin
Finally i am able to rotate the rectangle about its origin.Below is the working code.
Code:
#include <QtGui>
{
Q_OBJECT
public:
{
painter.translate(150,150);
painter.drawRect(rect);
static qreal degree=0;
painter.rotate(++degree);
painter.translate(-rect.width()/2,-rect.height()/2);
painter.drawRect(rect);
painter.drawPoint(rect.center());
QTimer::singleShot(0,
this,
SLOT(update
()));
}
};
int main(int argc, char *argv[])
{
a.setStyleSheet("QWidget{background: white}");
Widget w;
w.show();
return a.exec();
}
#include "main.moc"
Quote:
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.