Transform Matrix into component pieces
Question
Given an affine 2D transform matrix such as:
[a b tx]
[c d ty]
[0 0 1 ]
For a clockwise rotation about the origin,
a
is transformed bycos (θ)
andb
is transformed bysin (θ)
For a scaleX of scaleFactor sx,
a
is transformed bysx
For a shear parallel to the x axis,
x' = x + ky
b
is transformed byk
In my example, a
was transformed twice, by the rotation and the scale-x, b
was transformed twice, once by the rotation, once by the shear.
Rotation is no longer just arcsin(b)
ScaleX is no longer just 1 / a
ShearX is no longer just x - ky
How can I get the values of rotation
, shearX
, and scaleX
back from that matrix?
Solution
So rotation matrix (full) will be ( I leave out the boring part)
R=
a=cos(θ) c=sin(θ)
b=-sin(θ) d=cos(θ)
while scale and shear matrix will be (again, leaving out the boring part)
S=
a=s b=k
c=0 d=1
Now applying FIRST rotation (R), THEN scale and shear (S) will just be multiplying the matrices, which gives resulting matrix
S times R
a=s cos(θ) - k sin(θ) b=s sin(θ)+k cos(θ)
c=-sin(theta) d=cos(theta)
If you would want to get back θ, s and k from that, you can determine θ =arcsin(-c). You know sin(θ) and cos(θ), so you can solve two linear equations (a=s cos(θ) - k sin(θ) b=s sin(θ)+k cos(θ)) with two unknowns to find s and k.