22

I have the cartesian coordinates for three points $A$, $B$, $C$. I need to find the angle formed by $A\rightarrow B\rightarrow C$ using the 'right-hand rule' from B.

I'm having difficulty here as sometimes the angle will be exterior, and sometimes not.

Is there a single formula I can use for this?

Many thanks!

Julien
  • 44,791
James
  • 221
  • 1
  • 2
  • 3

4 Answers4

19

There are, of course, may ways to do this. One way would be to use vector. Note that

\begin{array}{ccc} \vec{AB} & = & B-A \\ \vec{BC} & = & C - B \end{array}

The scalar product (a.k.a. the dot product) has the property that

$$\vec{AB} \cdot \vec{BC} = \|\vec{AB}\| \, \|\vec{BC}\| \, \cos\theta $$

where $\| * \|$ measures the length and $\theta$ is the angle between the two vectors.

If you have $A$, $B$ and $C$ then you can work out $\vec{AB}$ and $\vec{BC}$. With that, find the dot product $\vec{AB}\cdot \vec{BC}$ and the lengths $\|\vec{AB}\|$ and $\|\vec{BC}\|$. Then substitute to find $\theta$, where

$$\theta = \arccos \left( \frac{\vec{AB}\cdot \vec{BC}}{ \|\vec{AB}\| \, \|\vec{BC}\|}\right).$$

All I did in the last step was to rearrange the formula to solve for $\theta$.

Fly by Night
  • 32,272
  • Thanks! I'm not sure I understand, the last line seems to be defined in terms of itself? As AB dot BC includes theta? – James Apr 14 '13 at 17:24
  • 3
    The dot product has its own definition and is a given number. I happens to relate to $\theta$ in the way stated.

    Let me give an example. $A=(1,2,3)$, $B=(3,2,1)$ and $C = (1,1,1)$. Then $\vec{AB} = (2,0,-2)$ and $\vec{BC} = (-2,-1,0)$. Then the dot product:

    $$\vec{AB} \cdot \vec{BC} = (-2)(-2)+(0)(-1)+(2)(0)=4$$

    The lengths are found using Pythagoras:

    $$|\vec{AB}| = \sqrt{2^2+0^2+(-2)^2} = 2\sqrt{2}$$ $$|\vec{BC}| = \sqrt{(-2)^2+(-1)^2+0^2} = \sqrt{5}$$

    Putting all of this together:

    $$\theta = \arccos\left(\frac{4}{2\sqrt{2}\sqrt{5}}\right)$$ $$\theta \approx 50.8^{\circ}$$

    – Fly by Night Apr 14 '13 at 17:44
  • 3
    Your example calculation of dot product is wrong, should have been $\vec{AB} \cdot \vec{BC} = (2)(-2) + (0)(-1) + (-2)(0) = -4$ – Michael Pankov Sep 12 '13 at 20:01
  • 1
    @FlybyNight, can you include your comment in the answer? – Mirzhan Irkegulov Jul 05 '14 at 15:46
  • 1
    What if I just need to know if an angle is acute or not? – Jack Pan Jun 30 '16 at 21:38
  • 2
    If an angle $0 \le \theta\le 180^{\circ}$ is acute then $\cos \theta$ will be positive. If it is obtuse then $\cos \theta$ will be negative. If $\theta = 90^{\circ}$ then $\cos \theta = 0$. Given two non-zero vectors ${\bf u}$ and ${\bf v}$ separated by an angle $\theta$, we have

    $$\cos \theta = \frac{{\bf u} \cdot {\bf v}}{|{\bf u}| |{\bf v}|}$$

    Since $|{\bf u}|$ and $|{\bf v}|$ are both positive, the sign of $\cos \theta$ is the sign of ${\bf u} \cdot {\bf v}$. So the angle is acute if ${\bf u} \cdot {\bf v} > 0$ and obtuse if ${\bf u} \cdot {\bf v} < 0$.

    – Fly by Night Jul 01 '16 at 15:38
  • Isn't this answer wrong? Consider A=(1,1,0) B=(0,0,0) C=(0,1,0)? This obviously is an equilateral right triangle, with the correct answer for A to B to C being 45 degrees. Yet $$\vec{AB} \cdot \vec{BC}$$ appears to be -1 ... and the denominator can easily be seen to be $$\sqrt2$$ and I thus I get 135 degrees. I think the formulas above want to use $$\vec{BA} \cdot \vec{BC}$$? – Gus Nov 08 '19 at 18:33
6

There are several answers here with a sign error, so this corrects the top voted answer and adds an example that shows why the correction gets the correct answer. I added a comment but SE is hiding it so I feel an answer is important to ensure folks are not led astray (I spent hours debugging code written based on this answer)


Corrected answer:

The question asks for the angle $A\rightarrow B \rightarrow C$ , which I take to mean the angle where B is the vertex. Thus, reformulating the question we want the angle between the vector from B to A aka $\vec{BA}$ and the vector from B to C aka $\vec{BC}$).,

\begin{array}{ccc} \vec{BA} & = & A-B \\ \vec{BC} & = & C - B \end{array}

The scalar product (a.k.a. the dot product) has the property that

$$\vec{BA} \cdot \vec{BC} = \|\vec{BA}\| \, \|\vec{BC}\| \, \cos\theta $$

where $\| * \|$ measures the length and $\theta$ is the angle between the two vectors.

If you have $A$, $B$ and $C$ then you can work out $\vec{BA}$ and $\vec{BC}$. With that, find the dot product $\vec{BA}\cdot \vec{BC}$ and the lengths $\|\vec{BA}\|$ and $\|\vec{BC}\|$. Then substitute to find $\theta$, where

$$\theta = \arccos \left( \frac{\vec{BA}\cdot \vec{BC}}{ \|\vec{BA}\| \, \|\vec{BC}\|}\right).$$

All I did in the last step was to rearrange the formula to solve for $\theta$.


Worked Example:

  • A= (1,1,0)
  • B= (0,0,0)
  • C= (0,1,0)

This obviously forms an equilateral right triangle with legs of 1 and a hypotenuse of $\sqrt2$ and most people will be instantly aware that the correct answer is 45 degrees ($\pi/4$ radians ~ 0.785) \begin{array}{ccc} \vec{BA} & = & A-B & = & (1,1,0) \\ \vec{BC} & = & C - B & = & (0,1,0) \end{array}

$$\|\vec{BA}\| = \sqrt2$$ $$\|\vec{BC}\| = 1$$ $$\vec{BA} \cdot \vec{BC} = (0\times1) +(1\times1)+(0\times0) = 0 + 1 + 0 = 1 $$ $$\theta = \arccos{(1/(1\times\sqrt2)) = \arccos(1/\sqrt2) = 45 }$$ if we choose to write the answer in degrees of course.


Wrong answers above use:

\begin{array}{ccc} \vec{AB} & = & B - A & = & (-1,-1,0) \\ \vec{BC} & = & C - B & = & (0,1,0) \end{array}

Which changes the sign of the dot product: $$\vec{AB} \cdot \vec{BC} = (0\times-1) +(1\times-1)+(0\times0) = (0) + (-1) + (0) = -1 $$ And leads to $$\theta = \arccos{(-1/(1\times\sqrt2)) = \arccos(-1/\sqrt2) = 135 }$$

There is no angle among any of the 3 points that can be described as 135 degrees.

As a side note the other answers do seem to give a correct value for the "heading change" or needed to successfully travel to C after traveling to A from B, which could be useful if you wanted to drive a robot along a polygon, but I can't see how one would get that notion from the question.

Gus
  • 161
3

First convert $AB$ and $BC$ into vectors $\vec{x}, \vec{y}$ by subtracting coordinates. Then use the dot product:

$\vec{x} \cdot \vec{y} = |\vec{x}| |\vec{y}| \cos \theta$

where $\theta$ is the angle between the vectors.

In this way you can get the angle between the vectors.

badatmath
  • 4,065
2

Using properties of triangle you can solve this problem easily,

Let in ABC triangle,

a = distance(C, B)

b = distance(C, A)

c = distance(A, B)

Now the triangle property say,

$ \quad\ 1. \quad\ a^{2} = b^{2} + c^{2} - 2.b.c.CosA $

$ \quad\ \quad\ \quad\ or, cosA = (b^{2} + c^{2} - a^{2}) / 2.b.c $

$ \quad\ \quad\ \quad\ or, A = arccos(b^{2} + c^{2} - a^{2}) / 2.b.c $

$ \quad\ 2. \quad\ b^{2} = a^{2} + c^{2} - 2.c.a.cosB $

$ \quad\ \quad\ \quad\ or, cosB = (a^{2} + c^{2} - b^{2}) / 2.c.a $

$ \quad\ \quad\ \quad\ or, B = arccos(a^{2} + c^{2} - b^{2}) / 2.c.a $

$ \quad\ 3. \quad\ c^{2} = a^{2} + b^{2} - 2.a.b.CosC $

$ \quad\ \quad\ \quad\ or, cosC = (a^{2} + b^{2} - c^{2}) / 2.a.b $

$ \quad\ \quad\ \quad\ or, C = arccos (a^{2} + b^{2} - c^{2}) / 2.a.b $