TITLE: Find a unit vector showing direction of a point to another point and the vice versa.
QUESTION [0 upvotes]: Let's say we have two points, $A(x_1, y_1)$ and $B(x_2, y_2)$, these are 2 balls linked one to the other making a spring together, I need to get 2 unit vectors to indicate the direction of elastic force of each point to the other point.
I am calculating the $\theta_1$ the angle created by point A regarding the B point, and also the $\theta_2$, the angle created by point B regarding the A point
$\theta_1 =  \arctan(\frac{y_2-y_1}{x_2-x_1})$;
$\theta_2 =  \theta_1 + \pi$
And then I create the 2 unit vectors(where r is equals to 1, using complex numbers for 2D rendering) needed for pointing a point to the other and vice versa:
$\text{Vec}_1 =  (\cos(\theta_1), \sin(\theta_1));$
$\text{Vec}_2 =  (\cos(\theta_2), \sin(\theta_2));$
However, upon testing my solution out it seems that it doesn't working completely! 
When the point A is to the right of the point B, all that I did doesn't work(cause it seems that it is calculating the same results as if point A would be change to the left symmetrically), but it works when the point A is to the left side of B. What I am doing wrong?

REPLY [1 votes]: If $A $ and $B $ are two points, the vector that points from $A $ to $B $ is $B-A $. If you need a unit vector, just scale it down with its own length:
$$\frac{B-A}{||B-A||} $$
Where $B-A = (x_2 - x_1, y_2 - y_1) $
Your method is not working because the arctan function, or atan, only returns angles $\theta \in (-\frac\pi2, \frac\pi2) $ BUT we know that both $\tan(\alpha) $ and $\tan(\alpha + \pi) $ are equal. That is to say, the atan function cannot distinguish angles in the 1st quadrant from angles in the third, and cannot distinguish angles in the second quadrant from angles in the fourth. If you stick with your method, in the end you must make a direction check by comparing the coordinates of $A $ with the coordinates of $B $.
{"set_name": "stack_exchange", "score": 0, "question_id": 2025784}