Algorithm or formula for shortest direction to travel between two points relative to a point

87 Views Asked by At

Given point $(x_0,y_0)$ representing a laser gun, I must find the direction (clockwise or anti-clockwise) to rotate the laser to get it from pointing to point $(x_1,y_1)$ to pointing to point $(x_2,y_2)$ so the spin will be as short as possible. No need to find the angle of the turn.
I believe I need to use polar coordinates somehow.
Any help would be greatly appreciated.

1

There are 1 best solutions below

0
On

Assuming my interpretation of your question (from the "comments" section) was correct:

Let $u = (x_1 - x_0, y_1 - y_0), v = (y_0 - y_1, x_1 - x_0), w = (x_2 - x_0, y_2 - y_0)$.

Compute $$ \theta = atan2(w \cdot v, w \cdot u) $$ and that's the angle you need to rotate.

Here $w \cdot v$ denotes the "dot product" of vectors, and "atan2" is a builtin function in most programming languages. The angle $\theta$ is positive in the direction that moves from the $x$-axis towards the $y$-axis, however these are drawn in your coordinate system.