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$
1
  • $\begingroup$ Have you drawn a picture? $\endgroup$ May 15, 2014 at 19:18

1 Answer 1

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$
4
  • $\begingroup$ This is exactly what I was looking for. Thanks! $\endgroup$ May 15, 2014 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$ Feb 22, 2015 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, 2015 at 18:52
  • $\begingroup$ Yeah, that's it. I just wanted a velocity vector. Thanks for the help! $\endgroup$ Feb 22, 2015 at 20:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .