Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extract rotation, scale values from 2d transformation matrix

Tags:

how can i extract rotation, scale and translation values from 2d transformation matrix? i mean a have a 2d transformation

matrix = [1, 0, 0, 1, 0, 0]  matrix.rotate(45 / 180 * PI) matrix.scale(3, 4) matrix.translate(50, 100) matrix.rotate(30 / 180 * PI) matrix.scale(-2, 4) 

now my matrix have values [a, b, c, d, tx, ty]

lets forget about the processes above and imagine that we have only the values a, b, c, d, tx, ty

how can i find total rotation and scale values via a, b, c, d, tx, ty

sorry for my english

Thanks your advance

EDIT

I think it should be an answer somewhere...

i just tried in Flash Builder (AS3) like this

   var m:Matrix = new Matrix;    m.rotate(.25 * Math.PI);    m.scale(4, 5);    m.translate(100, 50);    m.rotate(.33 * Math.PI);    m.scale(-3, 2.5);     var shape:Shape = new Shape;    shape.transform.matrix = m;     trace(shape.x, shape.y, shape.scaleX, shape.scaleY, shape.rotation); 

and the output is:

x = -23.6  y = 278.8  scaleX = 11.627334873920528  scaleY = -13.54222263865791  rotation = 65.56274134518259 (in degrees) 
like image 444
Tolgahan Albayrak Avatar asked Dec 05 '10 21:12

Tolgahan Albayrak


People also ask

How do you derive the rotation of a 2D matrix?

To find the rotation of a vector we simply multiply the required rotation matrix with the coordinates of the given vector. In 2D space, this is given by ⎡⎢⎣x′y′⎤⎥⎦ [ x ′ y ′ ] = ⎡⎢⎣cosθ−sinθsinθcosθ⎤⎥⎦ [ c o s θ − s i n θ s i n θ c o s θ ] ⎡⎢⎣xy⎤⎥⎦ [ x y ] .

Does scaling and rotation commute?

Translations and scales do not commute. Scales and rotations commute only in the special case when scaling by the same amount in all directions.

How do you combine rotation and translation matrix?

A rotation matrix and a translation matrix can be combined into a single matrix as follows, where the r's in the upper-left 3-by-3 matrix form a rotation and p, q and r form a translation vector. This matrix represents rotations followed by a translation.


2 Answers

Not all values of a,b,c,d,tx,ty will yield a valid rotation sequence. I assume the above values are part of a 3x3 homogeneous rotation matrix in 2D

    | a  b  tx | A = | c  d  ty |     | 0  0  1  | 

which transforms the coordinates [x, y, 1] into:

[x', y', 1] = A * |x|                   |y|                   |z| 
  • Thus set the traslation into [dx, dy]=[tx, ty]
  • The scale is sx = sqrt(a² + c²) and sy = sqrt(b² + d²)
  • The rotation angle is t = atan(c/d) or t = atan(-b/a) as also they should be the same.

Otherwise you don't have a valid rotation matrix.


The above transformation is expanded to:

x' = tx + sx (x Cos θ - y Sin θ) y' = ty + sy (x Sin θ + y Cos θ) 

when the order is rotation, followed by scale and then translation.

like image 75
John Alexiou Avatar answered Sep 19 '22 09:09

John Alexiou


I ran into this problem today and found the easiest solution to transform a point using the matrix. This way, you can extract the translation first, then rotation and scaling.

This only works if x and y are always scaled the same (uniform scaling).

Given your matrix m which has undergone a series of transforms,

var translate:Point; var rotate:Number; var scale:Number;  // extract translation var p:Point = new Point(); translate = m.transformPoint(p); m.translate( -translate.x, -translate.y);  // extract (uniform) scale p.x = 1.0; p.y = 0.0; p = m.transformPoint(p); scale = p.length;  // and rotation rotate = Math.atan2(p.y, p.x); 

There you go!

like image 21
bugshake Avatar answered Sep 21 '22 09:09

bugshake