# Getting translation and rotation from resultant matrix

I have a matrix which performs a 2D rotation around any given center. Using homogenous coordinates, I have the matrices:

$$T = \begin{pmatrix} 1 & 0 & C_x \\ 0 & 1 & C_y \\ 0 & 0 & 1 \end{pmatrix}$$

where $C_x$ and $C_y$ are the coordinates of the rotation center, and

$$R = \begin{pmatrix} \cos(\theta) & -\sin(\theta) & 0 \\ \sin(\theta) & \cos(\theta) & 0 \\ 0 & 0 & 1 \end{pmatrix}$$

Now I can obtain the matrix $X = TRT^{-1}$, which translates everything to the origin, rotates and then translates back to the given center.

My question is, given $X$, is it possible to find $C_x$, $C_y$ and $\theta$ ?

At first blush, I'd think so, since (where $c$ and $s$ are the cossine and sine, respectively) $$X = \begin{pmatrix} c & -s & C_x(1-c)+C_ys \\ s & c & -C_xs+C_y(1-c) \\ 0 & 0 & 1 \end{pmatrix} = \begin{pmatrix} c & -s & \beta \\ s & c & \gamma \\ 0 & 0 & 1 \end{pmatrix}$$

It then becomes a simple matter of solving for $C_x$ and $C_y$. But the result I'm finding is

$C_y = \dfrac{\beta+C_x(c-1)}{s}$

$C_x = \dfrac{C_y(c-1)-\gamma}{s}$

However, working on from here, the results I'm getting give the center as a function of the angle of rotation, which would mean that, as the rotation occurs, the center is moving, which is obviously incorrect.

So, how can this be done?

I have seen the these questions (1, 2 and 3) but they all deal with X = TRS (S = scale). My question actually seems simpler than these, but I just can't figure it out.

• A translation is not linear. Your matrix $T$ is not what you think it is – krirkrirk Mar 13 '15 at 15:22
• $T.0_3=0_3$ ($T$ is linear, a translation is not, as indicated by krirkrirk) – anderstood Mar 13 '15 at 15:53
• $T$ works fine on vectors with homogenous coordinates. Translations are affine, that is the point of using homogeneous coordinates. At what part of the above would it matter that the OP assumed linearity? – mvw Mar 13 '15 at 15:57

Starting with the upper right block from the comparison $X = (..)$: \begin{align} X_{13} &= C_x (1-c) + C_y s= \beta \\ X_{23} &= -C_x s + C_y(1-c) = \gamma \end{align} and rewriting it as matrix equation with unknowns $C_x$, $C_y$: $$\left( \begin{matrix} 1-c & s \\ -s & 1-c \end{matrix} \right) \left( \begin{matrix} C_x \\ C_y \end{matrix} \right) = \left( \begin{matrix} \beta \\ \gamma \end{matrix} \right)$$ Using matrix inversion I get $$\left( \begin{matrix} C_x \\ C_y \end{matrix} \right) = \left( \begin{matrix} \frac{1}{2} & -\frac{s}{2(1-c)} \\ \frac{s}{2(1-c)} & \frac{1}{2} \end{matrix} \right) \left( \begin{matrix} \beta \\ \gamma \end{matrix} \right) \quad (*)$$ and $$\theta = \arccos c = \arcsin s \quad (**)$$

So a given $X$ gives $c = X_{11}, s = X_{21}, \beta = X_{13}, \gamma = X_{23}$ and can be used with the above equations to yield $C_x, C_y, \theta$, if $c \ne 1$.

The case $c = 1$ is for $X = I$, $\theta = 0$ where we do not need this transformation.

Example:

Given this matrix $X$: $$X = \left( \begin{matrix} 0.70712315999226 & -0.70709040200144 & 2.707024886019803 \\ 0.70709040200144 & 0.70712315999226 & -0.53555028397966 \\ 0 & 0 & 1 \end{matrix} \right)$$ we read \begin{align} c &= 0.70712315999226 \\ s &= 0.70709040200144 \\ \beta &= 2.707024886019803 \\ \gamma &= -0.53555028397966 \end{align} Using equation $(**)$ gives $\theta = 0.785375$ and using equation $(*)$ gives $C_x = 1.999999999999993$ and $C_y =$2.999999999999991.

And indeed I used a translation matrix $T$ with $C_x =2$ and $C_y = 3$ and a rotation matrix with $\theta = \pi / 4$ to generate $X = T\, R \, T^{-1}$.

• I appologize, but I don't understand what you did here. What is the initial 2x2 matrix? Where's the translation part? Yes, in a simple rotation matrix $X_{11} = X_{22}$ area always equal to the cosine and the other diagonal are equal to $\pm\sin$, but this question deals with more than this. Or am I missing something? – Wasabi Mar 13 '15 at 16:24
• I added an example. I hope it gets clear. – mvw Mar 13 '15 at 16:39
• Yes, thank you. I realize now my error was mostly conceptual. I noticed that the values of $C_x$ and $C_y$ were going to be functions of $\cos$, $\sin$, $\beta$ and $\gamma$ and went "wait, if the center is a function of $\cos$ and $\sin$, won't its value change for different angles?" But of course not, since $\beta$ and $\gamma$ are aliases to functions of $\cos$ and $\sin$, so internally the $\cos$ and $\sin$'s cancel out. Many thanks for the help. – Wasabi Mar 13 '15 at 17:05