How to find angle of rotation given the source point and x-coordinate of destination point?

109 Views Asked by At

Just for the sake of simplicity, assume that the centre of rotation is $(0, 0)$. I know that I can find the destination point $(x', y')$ given angle of rotation $a$ and source point $(x, y)$ using $$\begin{cases} x' = x \cos a - y \sin a \\ y' = x \sin a + y \cos a \\ \end{cases}$$

However, I want to ask how can I find the angle of rotation $a$ given the source point $(x, y)$ and only the x-coordination of the destination point $x'$? The y-coordinate of destination point $y'$ would be according to the angle of rotation $a$ that we get. I am having trouble trying to solve the above equations for the desired value because we don't have exact value of $y'$. It would be great if someone could help. Thanks.

2

There are 2 best solutions below

2
On BEST ANSWER

Changing notation a bit, all of the possible rotations of $\mathbf p_0=(x_0,y_0)$ lie on a circle with radius $r = \|\mathbf p_0\| = \sqrt{x_0^2+y_0^2}$. An equation of this circle is $x^2+y^2=x_0^2+y_0^2$. Your problem comes down to finding the points on this circle with $x$-coordinate equal to $x_1$. Plugging $x_1$ into the above equation gives you a quadratic equation with solutions $y_1 = \pm\sqrt{r^2-x_1^2}$. (You can also get this directly from the Pythagorean theorem.) For each of the solutions $\mathbf p_1=(x_1,y_1)$ you can recover the cosine of the angle $\alpha$ via the dot product identity: $$\mathbf p_0\cdot\mathbf p_1 = \|\mathbf p_0\|\,\|\mathbf p_1\|\cos\alpha = r^2\cos\alpha$$ therefore $$\cos\alpha = {x_0x_1+y_0y_1 \over x_0^2+y_0^2}.\tag1$$ Because $\cos\alpha = \cos(-\alpha)$, this equation has a sign ambiguity, but you can resolve it by examining $$\det\begin{bmatrix}\mathbf p_0^T&\mathbf p_1^T\end{bmatrix} = x_0y_1-x_1y_0 = \|\mathbf p_0\|\,\|\mathbf p_1\|\sin\alpha.\tag2$$ You can, if you like, combine these two equations to obtain $$\tan\alpha = {x_0y_1-x_1y_0 \over x_0x_1+y_0y_1}.$$ This also has an ambiguity, this time of the quadrant of the angle, but if you’re coding this, there’s likely a two-argument form of the arctangent function available (often called something like ATAN2) that allows you to pass the numerator and denominator in separately in order to resolve this ambiguity.

2
On

First we replace $\sin a$ with $\sqrt{1-\cos^2a}$: $$x' = x \cos a - y \sqrt{1-\cos^2 a}$$

Then we eliminate the square root by isolating it and squaring: $$y \sqrt{1-\cos^2 a} = x \cos a - x'$$ $$y^2 (1-\cos^2 a) = (x \cos a - x')^2 = x^2 \cos^2 a - 2 x x' \cos a + (x')^2$$

This can now be rewritten as a quadric in $\cos a$: $$0 = (x^2+y^2) \cos^2 a - 2xx' \cos a + ((x')^2-y^2)$$ $$\cos^2 a - 2\frac{xx'}{x^2+y^2} \cos a + \frac{(x')^2-y^2}{x^2+y^2} = 0$$

It can be solve using the "$p$-$q$-formula": $$ \cos a = \frac{xx'}{x^2+y^2} \pm \sqrt{\left(\frac{xx'}{x^2+y^2}\right)^2 - \frac{(x')^2-y^2}{x^2+y^2}} \\ = \frac{xx' \pm \sqrt{(x^2+y^2)y^2-(x')^2y^2}}{x^2+y^2} $$

A couple of notes:

  1. The identity $\sin a = \sqrt{1-\cos^2a}$ is not always valid. For some $a$ we instead have $\sin a = -\sqrt{1-\cos^2a}.$ You could do this case yourself.
  2. When we square the equation we might add fake solutions. Therefore the solutions should be tested before accepted.