Hoang Huynh
Hoang Huynh

Reputation: 1403

Given a 2D transformation matrix, how to calculate the scalings, skews, rotations and translations?

I wrote a program (like this) that generate and apply a transformation matrix to an object thanks to user's input on scalings, skews, rotations and translations.

Now I want to do it inversely: given the transformation matrix, how to calculate the these values? The matrix is 2D (a, b, c, d, tx, ty).

I know this is a pure math problem but I was not concentrated enough in high school...

Upvotes: 1

Views: 4018

Answers (2)

Sneftel
Sneftel

Reputation: 41484

Assuming a translate/rotate/scale&skew order of operations, the easiest thing to do is to snip them off one at a time.

  1. Extract the last column; it is the translation. discard it and the last row.
  2. Find the QR decomposition of the resultant 3x3 matrix. The orthogonal component is the rotation; discard it and keep the upper triangular component.
  3. The diagonal of the upper triangular component is the scaling. Reverse it by left-multiplying the upper triangular matrix by the scaling's inverse.
  4. The result is the skew matrix.

Upvotes: 2

Hoang Huynh
Hoang Huynh

Reputation: 1403

I found a lot of useful info on this tutorial which solved the problem for me. Here is the formula for those who cares:

Translation (pixel)

x := tx

y := ty

Scale (time)

scale_x := sqrt(a^2 + b^2)

scale_y := sqrt(c^2 + d^2)

Skew (degree)

skew_x := 180/pi * atan2(d, c) - 90

skew_y := 180/pi * atan2(b, a)

Rotation (degree)

This is the tricky part: rotation is not a basic transform function; in fact it is the result of the combination of skew and scale. You can skew an object by its x and y axis and then scale it to make it look like it has been rotated. The angle of x skew is usually regarded as the rotation angle:

rotation := skew_x

Sometimes:

rotation := skew_y

Upvotes: 5

Related Questions