Smallest turning angle

82 Views Asked by At

I know it's probably been answered, but my google-fu is failing me today...

I have two 2D points; A and B
A has a known Heading. The X and Y coordinates are always positive, if that helps at all.

What I would like to do is figure out the angle that A needs to turn in order for the Heading to be pointing toward B. Once I know the turn angle, figuring out if it's the smallest (i.e. is turning left quicker than turning right, or vice versa) is easy.

1

There are 1 best solutions below

0
On

If your points are in the plane, and given by Cartesian coordinates $A = (x_1,y_1)$, $B = (x_2,y_2)$, then the heading from $A$ to $B$ is $\tan^{-1}(\frac{y_2 - y_1}{x_2 - x_1})$. Unfortunately, this does a bad job of distinguishing directions when the differences aren't positive. If you have access to atan2, a function provided by many programming languages, it provides the correct angle in all cases via $\operatorname{atan2}(y_2 - y_1,x_2 - x_1)$.

If $A$ already has heading $\theta$, relative to the positive $x$-direction, then it needs to turn $\operatorname{atan2}(y_2 - y_1,x_2 - x_1) - \theta$.