How to extract rotation angle from QTransform?

I have a QTransform object and you want to know the angle in degrees at which the object rotates, however there is no clear example of how to do this:

http://doc.trolltech.com/4.4/qtransform.html#basic-matrix-operations

Setting up is easy; returning it back is difficult.

+6
c ++ math qt transform
source share
5 answers

Assuming the conversion ONLY contains a rotation: just take the acos of element m11.

It still works if the transformation contains translation, but if it contains shift or scaling, you're out of luck. They can be restored by decomposing the matrix into a matrix of shift, scaling, and rotation, but the results you get are most likely not what you are looking for.

+8
source share

The easiest common way is to convert (0,0) and (1,0), then use trigonometric functions (arctan) to get the angle

+8
source share

A transformation matrix is ​​an implementation used for 3D graphics. This simplifies math to accelerate three-dimensional positional / rotational orientations of points / objects. Indeed, it is very difficult to deduce the orientation from the Transformation because of how it accumulates consecutive translations / rotations / scales.

Here is a suggestion. Take a vector that points in a simple direction (1,0,0), and then apply the transformation to it. Your resulting vector will be translated and rotated to give you something like this: (27.8, 19.2, 77.4). Apply Transform to (0,0,0) to get something like (26.1, 19.4, 50.8). You can use these two points to calculate the turns that were applied based on the knowledge of their starting points (1,0,0).

Does it help?

+4
source share

Typically, you need the inverse trigger function, but you need to keep an eye on the ambiguity of the quadrant, and that is what you should use atan2 (sometimes written arctan2). Therefore, either rotate the unit vector [0, 1] by [x, y], and then use atan2 (y, x), or if the matrix implements only rotation, you can use atan2 (m12, m11). (They are similar to the answers of Javier and Nils, except that they do not use atan2.)

+2
source share

I used QGraphicsItem only with setRotate and did not experience any problems until I added the functionality of the rotation group. The problem is that when calling destroyItemGroup, it uses rotation as a conversion to elements, not as a rotation. Because of this, I had to restore rotation from this QTransform object.

My fix was to add the following lines to the itemChange method (credit for tom10 answer):

QVariant MyGraphicItem::itemChange(GraphicsItemChange change, const QVariant &value) { if(change == ItemTransformChange) { auto transform = value.value<QTransform>(); setRotation(rotation() + qRadiansToDegrees(qAtan2(transform.m12(), transform.m11()))); return QVariant(); } ... } 

PS: Another solution with acos and m11 () does not work. It crashes for certain values, as explained by tom10.

+1
source share

All Articles