PDA

View Full Version : Rotating a rectangle about its origin



babu198649
21st August 2008, 13:15
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.


#include <QtGui>

class Widget : public QWidget
{
Q_OBJECT

public:
void paintEvent(QPaintEvent * event)
{
QMatrix matrix;
QRect rect(20,20,150,100);
matrix.translate(rect.left(), rect.top());
static int degree=0;
matrix.rotate(++degree);

QPainter painter(this);
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[])
{
QApplication a(argc, argv);
a.setStyleSheet("QWidget{background: white}");
Widget w;
w.show();
return a.exec();
}

#include "main.moc"

wysota
21st August 2008, 17:17
This should work:


QPainter painter(this);
painter.fillRect(rect(), Qt::white);
QRect rct(...);
QPoint center = rct.center();
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.

babu198649
2nd September 2008, 16:20
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.

wysota
3rd September 2008, 02:05
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 :)

babu198649
4th September 2008, 12:01
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.

2512

#include <QtGui>

class Widget : public QWidget
{
Q_OBJECT

public:
void paintEvent(QPaintEvent * event)
{
QMatrix matrix;
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);

QPainter painter(this);
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[])
{
QApplication a(argc, argv);
a.setStyleSheet("QWidget{background: white}");
Widget w;
w.show();
return a.exec();
}

#include "main.moc"


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.

babu198649
25th October 2008, 13:24
Finally i am able to rotate the rectangle about its origin.Below is the working code.

#include <QtGui>

class Widget : public QWidget
{
Q_OBJECT
public:
void paintEvent(QPaintEvent * event)
{
QRect rect(0,0,150,100);
QPainter painter(this);
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[])
{
QApplication a(argc, argv);
a.setStyleSheet("QWidget{background: white}");
Widget w;
w.show();
return a.exec();
}

#include "main.moc"




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.