# Calculating axis angle from matrix without wrapping at PI

I need to get the axis angle from a matrix of the form:

$$\begin{matrix} \cos \theta & -\sin \theta & tx\\ \sin \theta & \cos \theta & ty\\ 0 & 0 & 1\\ \end{matrix}$$

Into an axis angle. I've tried the standard formula:

$\theta$ = $\arccos$ ( (m[0][0] + m[1][1] + m[2][2] - 1) / 2 )

However this formula inverts when the angle reaches PI. For example, given a rotational matrix for an angle of 225 degrees, we get a returned value of about 135 degrees, which in itself is perfectly correct, but I really need the full value.

Is there any way of handling these cases?

-

You may have an Atan2 function available, which sorts out the quadrants. If so, you want Atan2$(\cos\theta,\sin\theta)$ Otherwise, you can do $\arccos\theta$ but then you have to sort out the quadrants afterward using the value for $\sin\theta$

-
So the angle would be atan2(m[0][0],m[1][0])? –  Ben Feb 11 '11 at 15:57
Thanks for the help Ross, I ended up using the arccos approach and made it adjust the quadrant when sin theta is negative. Works fine now! –  Ben Feb 11 '11 at 16:38