3
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ Have you drawn a picture? $\endgroup$ – ncmathsadist May 15 '14 at 19:18
3
$\begingroup$

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)

$\endgroup$
  • $\begingroup$ This is exactly what I was looking for. Thanks! $\endgroup$ – Hydrothermal May 15 '14 at 19:06
  • $\begingroup$ 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. $\endgroup$ – user2722083 Feb 22 '15 at 17:55
  • 2
    $\begingroup$ @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}$ $\endgroup$ – Alice Ryhl Feb 22 '15 at 18:52
  • $\begingroup$ Yeah, that's it. I just wanted a velocity vector. Thanks for the help! $\endgroup$ – user2722083 Feb 22 '15 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.