Solved Rotate QRectF
-
Hi,
I have a QRectF and now i would like to rotate it along.
is there any way to rotate it easily and without use QPainter?I try this code:
QRectF FirstRect = QRectF(0,0,100,50); QTransform t; t.rotate(45); QRectF SecondRect = t.mapToRect(FirstRect);
I want to rotate the rectangle 45 degrees clockwise, but the SecondRect has a different origin and even the 4 corners position is not where they should be.
For me it is important that the SecondRect is exactly the FirstRect rotated by 45 degrees.
Did I get something wrong? I miss code?thank you
-
What's the centre of rotation supposed to be?
-
@Gianluca86
Well you need to adjust the offset than.//untested, out of my head ;-) I use something like this for a QPixmap rotation int iOffX = (FirstRect .width() - SecondRect .width())/2; int iOffY = (FirstRect .height()- SecondRect .height())/2; SecondRect.move(iOffX,iOffY);
-
https://forum.qt.io/topic/78281/obb-oriented-bounding-box
PS.
In short, rotate the points, not the rectangle. The rectangle will always be a normal one - aligned to the axes. -
@VRonin : the center of rotation is the top left corner, which should be the default one, right?
@kshegunov : so if I have to intersect two rectangles, one of which rotates, I need to rotate the points and create a polygon with the resulting 4 points?
-
Or create a polygon from the points and then rotate it, but yes you do.
-
Thanks for the explanation, now I try.
-
I try to do this:
QTransform t; t.rotate(45); QPolygon Second = t.mapToPolygon(QRect(0,0,100,50));
And the result is ok but with this code
QTransform t; t.rotate(45); QPolygon Second = t.mapToPolygon(QRect(10,10,100,50));
the origin point (first point of polygon() changed
Did I misunderstand your explanation?
-
I don't why, but with this is ok:
QTransform t; t.translate(10,10); t.rotate(45); t.translate(-10,-10); QPolygon Second = t.mapToPolygon(QRect(10,10,100,50));
-
That is because rotations are done around the origin of the coordinate system. Whenever a rotation should be done around another axis it is necessary to move the origin of the coordinate system to that axis, perform the rotation and then return the origin to its former position. This is basic linear algebra, about which you can read more here (and on its related pages).