extract rotation, scale values from 2d transformation matrix

后端 未结 4 2003
野性不改
野性不改 2020-11-30 20:24

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]

matri         


        
4条回答
  •  青春惊慌失措
    2020-11-30 21:02

    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!

提交回复
热议问题