[Math] Angle between 3 points

geometry

I have three points $(x_1, y_1), (x_c, y_c), (x_3, y_3)$, where I know $(x_1, y_1), (x_c, y_c)$, the angle $\theta$, and $c$ on the dash line in the following figure. How to calculate the point $(x_3, y_3)$?

I think of this form:

$$
\theta = arccos\left(\frac{a\cdot b}{||a||\cdot ||b||}\right)
$$

where

$$
a = (x_1 – x_c, y_1 – y_c)\\
b = (x_1 – x_3, y_1 – y_3)
$$

More information:

enter image description here

Best Answer

Let's approach the problem via a simplified case, where $(x_c,y_c)$ is at origin, and $(x_1, y_1)$ is on the $x$ axis at $(a, 0)$: Triangle illustration Obviously, we can calculate $a$ from the original coordinates, $$a = \sqrt{\left(x_1 - x_c\right)^2 + \left(y_1 - y_c\right)^2}$$

We have three unknowns, $x$, $y$, and $b$ (the distance from origin to $(x,y)$), and three equations: $$\begin{cases} x = b \cos \theta \\ y = b \sin \theta \\ (x - a)^2 + y^2 = c^2 \end{cases}$$ There is a pair of solutions: $$\begin{cases} x = a \left(\cos\theta\right)^2 \pm \cos\theta \sqrt{c^2 - a^2 \left(\sin\theta\right)^2} \\ y = a \sin\theta \cos\theta \pm \sin\theta \sqrt{c^2 - a^2 \left(\sin\theta\right)^2} \\ b = a \cos\theta \pm \sqrt{c^2 - a^2\left(\sin\theta\right)^2} \end{cases}$$ Pick either the upper or the lower signs for all three, but note that only the triplet for which $b \ge 0$ is actually valid. Indeed, for my illustration above, I've shown the "-" solution; the "+" solution would have $(x,y)$ somewhere near $(a,c)$, making the lower right corner angle somewhat near ninety degrees.

However, now that we do know (possibly two valid values of) $b$, we can go back to looking at the situation in the original coordinates.

Numerical solutions, using atan2():

The simplest way is to use the atan2() function available in most programming languages. ($\operatorname{atan2}(y,x) = \tan(y/x)$, except the former also takes account the quadrant, too.) With it, in original coordinates, $$\begin{cases} b = a \cos\theta \pm \sqrt{c^2 - a^2\left(\sin\theta\right)^2}, & b \ge 0 \\ \theta_0 = \operatorname{atan2}(y_1 - y_c, x_1 - x_c) \\ x_3 = x_c + b \cos \left ( \theta_0 + \theta \right ) \\ y_3 = y_c + b \sin \left ( \theta_0 + \theta \right ) \end{cases}$$ If you want positive $\theta$ to be clockwise, use $(\theta_0 - \theta)$ instead in the formulas for $x_3$ and $y_3$, above.

Symbolic solutions, via coordinate system transformation:

A two-dimensional rotation matrix is defined as $$\mathbf{R} = \left[\begin{matrix}\cos\varphi & -\sin\varphi \\ \sin\varphi & \cos\varphi\end{matrix}\right] = \left[\begin{matrix}C&-S\\S&C\end{matrix}\right]$$ and rotating a point $\vec{p} = (x, y)$ by matrix $\mathbf{R}$ is $$\mathbf{R} \vec{p} = \left[\begin{matrix}C&-S\\S&C\end{matrix}\right]\left[\begin{matrix}x\\y\end{matrix}\right]$$ i.e. $$\begin{cases} x' = C x - S y \\ y' = S x + C y \end{cases}$$

In this particular case, we need to rotate our simplified case solutions using a matrix which rotates point $(a,0)$ to $(x_1-x_c, y_1-y_c)$: $$\begin{cases} x_1 - x_c = C a \\ y_1 - y_c = S a \end{cases} \iff \begin{cases} C = \frac{x_1 - x_c}{a} \\ S = \frac{y_1 - y_c}{a} \end{cases}$$

Applying the above rotation to our simplified case results, and a translation to move $(x_c, y_c)$ back to its proper place from origin, we get: $$\begin{cases} b = a \cos\theta \pm \sqrt{c^2 - a^2\left(\sin\theta\right)^2}, & b \ge 0 \\ x = b \cos\theta \\ y = b \sin\theta \\ C = \frac{x_1 - x_c}{a} \\ S = \frac{y_1 - y_c}{a} \\ x_3 = x_c + C x - S y \\ y_3 = y_c + S x + C y \end{cases}$$ or equivalently $$\begin{cases} b = a \cos\theta \pm \sqrt{c^2 - a^2\left(\sin\theta\right)^2}, & b \ge 0 \\ x_3 = x_c + \frac{b}{a}(x_1 - x_c)\cos\theta - \frac{b}{a}(y_1 - y_c)\sin\theta \\ y_3 = y_c + \frac{b}{a}(y_1 - y_c)\cos\theta + \frac{b}{a}(x_1 - x_c)\sin\theta \end{cases}$$ or equivalently $$\begin{cases} z = \frac{b}{a} = \cos\theta \pm \sqrt{\frac{c^2}{a^2} - \left(\sin\theta\right)^2}, & z \ge 0 \\ x_3 = x_c + z(x_1 - x_c)\cos\theta - z(y_1 - y_c)\sin\theta \\ y_3 = y_c + z(y_1 - y_c)\cos\theta + z(x_1 - x_c)\sin\theta \end{cases}$$ .

Related Question