Brannon
Brannon

Reputation: 5414

given a MatrixTransform how do I determine its current angle?

I'm using WPF's MatrixTransform for managing my translation, scaling, and rotation on a Canvas (all 2D). It works well. However, I would like to be able to calculate the current angle of rotation. How is this done? Do I need to invert the matrix and then use that to rotate something? I can provide a "rotation-at" point if necessary.

Upvotes: 1

Views: 2148

Answers (1)

Dan Bryant
Dan Bryant

Reputation: 27495

Assumptions:

  1. The matrix is definitely a translation/rotation/scale matrix (no skew or other non-orthogonality).
  2. The matrix is specified such that scale is applied first, followed by rotation, followed by translation.

Steps:

  1. You can ignore the final translation component, giving you the upper left 3x3 matrix that contains scale and rotation.

  2. Scaling is just multiplication by a diagonal matrix of scaling factors (scaling each column in the matrix), so you can normalize the column vectors to give you an orthonormal matrix that purely expresses rotation.

  3. To get an angle of rotation, you either need to choose an axis of rotation or a vector you want to rotate (from which you can compute a rotation axis and angle.)

The latter case is easier:

  1. Take a sample vector v with magnitude 1 (simplifies the math.)
  2. Transform by the rotation matrix to get v'.
  3. For the rotation axis (v x v'), the angle between them is defined as acos(v * v'). 'x' is the cross product and '*' is the dot product.

Upvotes: 1

Related Questions