iPhoneSDK calculate Rotation angle from CATransform3D

后端 未结 2 515
南笙
南笙 2020-12-23 12:44

How do I calculate the Rotation in Radians around Z-axis by giving a CATransform3D struct as the input?

basically what I need is the other way round of CATrans

2条回答
  •  渐次进展
    2020-12-23 13:24

    It depends on what axis you are doing the rotation on.

    Rotation about the z-axis is represented as:

    a  = angle in radians
    x' = x*cos.a - y*sin.a
    y' = x*sin.a + y*cos.a
    z' = z
    
    ( cos.a  sin.a  0  0)
    (-sin.a  cos.a  0  0)
    ( 0        0    1  0)
    ( 0        0    0  1)
    

    so angle should be a = atan2(transform.m12, transform.m11);

    Rotation about x-axis:

    a  = angle in radians
    y' = y*cos.a - z*sin.a
    z' = y*sin.a + z*cos.a
    x' = x
    
    (1    0      0    0)
    (0  cos.a  sin.a  0)
    (0 -sin.a  cos.a  0)
    (0    0     0     1)
    

    Rotation about y-axis:

    a  = angle in radians
    z' = z*cos.a - x*sin.a
    x' = z*sin.a + x*cos.a
    y' = y
    
    (cos.a  0  -sin.a   0)
    (0      1    0      0)
    (sin.a  0  cos.a    0)
    (0      0    0      1) 
    

提交回复
热议问题