TITLE: Finding the angle between three points?
QUESTION [22 upvotes]: 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!

REPLY [5 votes]: 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.