How to determine the direction of one point from another, given their coordinates?

17.5k Views Asked by At

If I have the coordinates of two points, how would I determine what direction the second point lies in, relative to the first point? Specifically, I'm writing an application that involves basically drawing an arrow starting at a certain area on the screen, pointing in the direction of the mouse.

1

There are 1 best solutions below

4
On BEST ANSWER

From $(x_1, y_1)$ to $(x_2, y_2)$ the direction is $\text{atan2}(y_2 - y_1, x_2 - x_1)$

You wrote you were writing a program, so atan2 is the easiest solution, it's usually found in your languages math package. In java it's Math.atan2(y, x)