Search code examples
c#mathdirect2d

Decompose 2D Transformation Matrix


So, I have a Direct2D Matrix3x2F that I use to store transformations on geometries. I want these transformations to be user-editable, and I don't want the user to have to edit a matrix directly. Is it possible to decompose a 3x2 matrix into scaling, rotation, skewing, and translation?


Solution

  • This is the solution I found for a Direct2D transformation matrix:

    • scale x = sqrt(M11 * M11 + M12 * M12)

    • scale y = sqrt(M21 * M21 + M22 * M22) * cos(shear)

    • rotation = atan2(M12, M11)

    • shear (y) = atan2(M22, M21) - PI/2 - rotation

    • translation x = M31

    • translation y = M32

    If you multiply these values back together in the order scale(x, y) * skew(0, shear) * rotate(angle) * translate(x, y) you will get a matrix that performs an equivalent transformation.