Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Qt's QTransform rotate function

As a result of this question, I'd like to understand more about Qt's QTransform::rotate function. In the documentation, it says:

QTransform & QTransform::rotate ( qreal angle, Qt::Axis axis = Qt::ZAxis )

Rotates the coordinate system counterclockwise by the given angle about the specified axis and returns a reference to the matrix.

Note that if you apply a QTransform to a point defined in widget coordinates, the direction of the rotation will be clockwise because the y-axis points downwards.

The angle is specified in degrees.

From my previous question, I learned that to rotate a QPolygonF clockwise, I must actually rotate it 90 degrees counter-clockwise, according to the rotate function:

QPolygonF original = QPolygonF() << QPoint(0, 1) << QPoint(4, 1) << QPoint(4, 2) << QPoint(0, 2);
QTransform transform = QTransform().translate(2, 2).rotate(90).translate(-2, -2);
QPolygonF rotated = transform.map(original);
qDebug() << rotated;

Output:

QPolygonF(QPointF(3, 0) QPointF(3, 4) QPointF(2, 4) QPointF(2, 0) ) 

E.g. for this rectangle:

original rectangle rotation

To rotate to here:

new rectangle rotation

Why is this? Why does the documentation say that my call to QTransform::rotate is actually causing a clockwise rotation when I believe I'm not in "widget coordinates" - there are no widgets involved here.

like image 719
Mitch Avatar asked Apr 17 '12 13:04

Mitch


1 Answers

QTransform operates in mathematical Cartesian coordinate system. ie. the top-right quadrant is the positive-x and positive-y one (quadrant 1). Your chart is in the y-axis-flipped Widget coordinates with quadrant 1 at lower-right.

Rotate does rotate counterclockwise. But since Y-axis is flipped in Widget coordinates, it becomes clockwise.

As you mentioned, polygon has nothing to do with widgets. It does rotate CCW, but you are visualizing it with widget coordinates.

like image 99
Stephen Chu Avatar answered Sep 19 '22 12:09

Stephen Chu