[Math] Universal formula for direction angle of a vector for any quadrant

anglevectors

I was studying how to find the angle between the (positive) $X$-axis and a position vector for a program I'm making. And I came to the conclusion that there is no universal formula for that. If I use the following formula: $$\theta = \tan^{-1}\left(\frac yx\right)$$ I have to add a particular offset depending on the quadrant where the vector is.

So I was wondering if there was another formula, another way to think about this problem, where the formula wouldn't have to change depending on the quadrant. It would be the universal formula for finding the angle. Google search didn't do any good.

Also is there a universal formula (in the sense mentioned above) for finding the angle between any two position vectors without having to offset the angle in any way?

I'm sorry for my bad English and my poor knowledge in Mathematics.

Best Answer

You are clearly seeking for the two-argument inverse tangent function $\left(x,y\right)\mapsto\arctan\left(x,y\right)$.

Its main difference from the plain old $\tan^{-1}$ is precisely that it takes the quadrant of the point $\left(x,y\right)$ in $\mathbb{R}^2$ into account, which is lost when the usual one-argument arctangent function version is used like you first intended due to the identities $$\frac xy=\frac{-x}{-y}$$ and $$\frac{-x}y=\frac x{-y}$$ which arise when the usual one-argument inverse tangent function is used like follows: $$\arctan\left(x/y\right).$$

To mitigate the problem, instead use the two-argument version $\arctan\left(x,y\right)$, which in most languages is known as arctan2, atan2 or something similar.