Javier Ramírez
Javier Ramírez

Reputation: 1011

How can I get the angle from a matrix

I got a rotation matrix like this:

1.0       0.0        0.0        2.07814
0.0      -0.809017   0.587785   0.0
0.0      -0.587785  -0.809017   0.0
0.0       0.0        0.0        1.0

How can i get the angle from this? If I apply the inverse, i get this

cos exp -1 (-0.809017) = 144.0
sin exp -1 (-0.587785) = -36.0
sin exp -1 ( 0.587785) =  36.0
cos exp -1 (-0.809017) = 144.0

But my problem is I know that the angle was 216.0 degrees, How do I get back that angle?

Upvotes: 1

Views: 1316

Answers (2)

user1157391
user1157391

Reputation:

To obtain the angle of rotation (theta) from a 3x3 rotation matrix (R) you can use the following formula:

tr(R) = 1 + 2 * cos(theta),

where tr is the trace.

In your example the rotation is given by:

1.00000   0.00000   0.00000
0.00000  -0.80902   0.58779
0.00000  -0.58779  -0.80902

Hence the trace is

1 - 0.80902 - 0.80902 = -0.61804`

and the angle is acos((-0.61804 - 1) / 2) * (180/pi) = 144°

Thus the matrix represents a counterclockwise rotation by 144°. Alternatively, as 144 = -216 mod 360, it represents a clockwise rotation by 216°.

Upvotes: 7

luser droog
luser droog

Reputation: 19494

I think you're computing inverse functions incorrectly in a manner that struck me as bizarre. You should use asin for the inverse sin and acos for the inverse cos. You're computing the multiplicative inverse, not the functional inverse. Although here, it appears to be the same thing.

Use the atan2() function. It will yield the angle from the y and x values. It implicitly computes the inverse sin and inverse cos, and it checks the signs of both y and x to discover the correct quadrant.

 ATAN2(3)

NAME
       1.8 `atan2', `atan2f'--arc tangent of y/x

SYNOPSIS
            #include 
            double atan2(double Y,double X);
            float atan2f(float Y,float X);

DESCRIPTION
       `atan2'  computes  the  inverse tangent (arc tangent) of Y/X.  `atan2' 
       produces the correct result even for angles near pi/2 or -pi/2 (that
       is, when X is near 0).

          `atan2f' is identical to `atan2', save that it takes and returns `float'.

RETURNS
       `atan2' and `atan2f' return a value in radians, in the range of -pi to pi.

I assume we're ignoring that 2.07814 that's clearly not part of the rotation. atan2(0.587785, -0.809017) gives me 144.0 degrees. atan(-0.587785, -0.809017) gives 216.0.


Aha! Your matrix is "sideways" from what I'm used to. The 2.07814 part is just a translation, I think. Using this 3d rotation matrix as a guide,

1  0   0
0  cos sin
0 -sin cos

... leaves me just as confused as before. I keep getting 144.0.


A confession.

I glossed over the radians/degrees issue above, because I used Postscript instead of C.

$ gsnd -q
GS>0.587785 -0.809017 atan =
144.0
GS>-0.587785 -0.809017 atan =
216.0
GS>

Upvotes: 5

Related Questions