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.
Asked
Active
Viewed 1.8k times
1 Answers
3
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)
Alice Ryhl
- 7,853
-
-
Hi! Sorry to be an absolute idiot, but how would I convert that into movement? Would that be the sine and cosine or the angle? I would like to shoot an arrow from one archer to another one. – user2722083 Feb 22 '15 at 17:55
-
2@user2722083 What do you mean with movement? The change in $x$ is $x_2 - x_2$ and the change in $y$ is $y_2 - y_1$. If you wanted a point with distance $1$ from $(0,0)$ in the correct direction it would be $(\cos\theta, \sin\theta)$ where $\theta$ is the angle from the answer, if you wanted the distance between the points that is $\sqrt{\left(x_2-x_1\right)^2+\left(y_2-y_1\right)^2}$ – Alice Ryhl Feb 22 '15 at 18:52
-
Yeah, that's it. I just wanted a velocity vector. Thanks for the help! – user2722083 Feb 22 '15 at 20:45