PDA

View Full Version : Set the rotation of a QTransform



quimnuss
7th July 2015, 10:15
I have a QGraphicsView that is transformed (scaled, translated, rotated) by a few functions.

At a given point, I'd like to set its rotation to a certain value. Even better, I'd like to have its scale, translation and rotation. However, the goal is to change its rotation but keep the translation and scale.

I don't need to have the original transformation order.

I've only seen .rotate() but there's no .setRotation() and I am not sure how to retrieve the rotation angle correctly. At the moment there's no shear, so I've read that "The simplest general way is to transform (0,0) and (1,0), then use trigonometric functions (arctan) to get the angle".

But I'd would like to know how this is usually done. At the moment I keep track of the scale and rebuild a new transform, but it's far from ideal and it would break if other transformations were made.

anda_skoa
7th July 2015, 11:52
If you always rotate around the same origin, then you could simply accumulate the angle values in another variable.

Cheers,
_

quimnuss
7th July 2015, 12:10
That's what I ended up doing, but it's not very satisfactory as method...

anda_skoa
7th July 2015, 13:23
Why not? Keeping information that you have is usually way better and trying to recover information after throwing it away.

Cheers,
_

quimnuss
7th July 2015, 13:34
It seems to me a QTransform should be able to know its rotation, as well as being able to set it to a fixed value. Then the object itself knows its status rather than relaying on an object upper in the hierarchy, like QMainWindow or having to create a wrapping widget to store that value. It feels to me that storing the value separately is error prone, specially because you can't set the rotation, but you have to rotate relative to that one value you stored. But, sure, if you're careful and deal with the visibility of it, it's ok as a

anda_skoa
7th July 2015, 14:52
A QTransform is a transformation value matrix.
It does not have anything like a rotation value.

Any transformation step that is applied is a matrix multiplication of the existing matrix values and a matrix with values depending on the type of transformation.

The QTransform object doesn't need the sequence of transformation steps, applying each step on each point would be very wasteful.
Instead it contains the final values that need to be applied to each point.

Cheers,
_

quimnuss
7th July 2015, 15:08
And that's exactly what I was asking for, the final rotation value.

anda_skoa
7th July 2015, 19:36
And that's exactly what I was asking for, the final rotation value.
Yes, you did.
And I pointed out that you can accumulate that value by tracking which rotation values you apply.

Cheers,
_

Kryzon
8th July 2015, 02:44
Keeping the transformation values in your own variables and rebuilding the QTransform when you need it is the simplest way (I would go with that as well).

But if you really need to modify the rotation from the QTransform, the first two values of the first and second rows ( [m11,m12] and [m21,m22]) describe two 2D vectors that are rotated around an origin. One vector represents the horizontal axis and the other the vertical axis of this transformed space.
They're usually perpendicular. When there's shearing involved they're not perpendicular, this is what causes shearing after all.

The length of these vectors is the scale.
Translation is not involved in this, it's the third row.

So if you want to change the rotation while preserving the scale, first obtain the length of each vector (Pythagorean theorem) , rotate the vectors by the desired amount and scale them by the original length.

vecHx = Cos( angle ) * originalScaleH
vecHy = Sin( angle ) * originalScaleH

vecVx = Cos( angle - 90 ) * originalScaleV
vecVy = Sin( angle - 90 ) * originalScaleV

[vecHx,vecHy] = [m11,m12]
[vecVx,vecVy] = [m21,m22]

If I'm not mistaken it's as simple as that. The "-90" might be wrong depending on the orientation expected by Qt (in which case "+90" would be the right choice, or something else).

quimnuss
8th July 2015, 09:14
Thanks Kryzon,

Since apparently the object's QTransform doesn't provide it, I'll go for the simple way and store the rotation and scale on my own. I'd play with the transformation affine matrix components if it became cumbersome to track the rotation.