I have a rather simple requirement. I wish to find the angle of resultant vector with respect to x-axis, which I assume to be zero degrees. The problem I am facing is that, I want my mathematical formula to provide me the correct angle from the two values of inverse tangent. The method I am using are as follows:
Assuming two vectors A and B, which are at α and β from x-axis.
- Resolve A and B to Ax, Ay, Bx, and By. $$Ax=A\cos \alpha, Ay=\sin \alpha$$ Similarly for Bx and By.
- Resolve to find resultant: $$Rx=Ax+Bx, Ry=Ay+By$$ and $$R=\sqrt(Rx^2+Ry^2)$$
- Resolve the angle $$\gamma = \arctan ((Ay+By)/(Ax+Bx))$$ Here in lies my problem. Assuming $$A=200,B=100,\alpha = 45, \beta=225$$ I get two values for arctan 45 & 225.
I want a mathematical formula that will automatically produce the correct number in this case 225. The above approach works correctly if angles are less than 180 but not otherwise.
The atan2(y,x) function is an enhanced version of arctangent which does exactly what you desire. You may familiarize yourself with the piecewise definition here.