How do we really get the angle of a vector from the components?

If you want an answer in form of a mathematical function definition, using only functions that were in common use in undergraduate instruction fifty years ago, I do not think you can do much better than the excellent answer by Rhys Hughes. As noted in the answer, this is how mathematicians often define a function equivalent to yours in textbooks. The only detail you might want to add is something to deal with the case $x = y = 0.$

Note that the formulas in that answer do not tell you how to find $\theta,$ but they do uniquely identify the output of the function for any possible input. There is always some value of $\theta$ that will satisfy both equations when $x^2 + y^2 \neq 0,$ and there will never be more than one value of $\theta$ that satisfies both equations.

If you want a formula to compute the angle using only functions that were in common use in undergraduate instruction fifty years ago, I think the formula you wrote is close to the best you can get, though I would handle one or two cases a bit differently.

If you want a nice way to represent your function in other formulas, you can borrow the two-parameter arc tangent function that is defined in many software packages. That is, define a function $\operatorname{atan2}(y, x)$ whose value is the angle of the vector $\begin{bmatrix} x \\ y \end{bmatrix}.$ You can define $\operatorname{atan2}(y, x)$ either in the style of the complex analysis textbooks described in the other answer, or you can define it in your style:

$$ \operatorname{atan2}(y, x) = \begin{cases} \arctan\left(\frac yx\right) & x \gt 0 \\ \arctan\left(\frac yx\right) + \pi \quad & x \lt 0, \ y \geq 0 \\ \arctan\left(\frac yx\right) - \pi \quad & x \lt 0, \ y < 0 \\ \frac\pi2 & x = 0,\ y > 0 \\ -\frac\pi2 & x = 0,\ y < 0 \\ \text{undefined} & x = 0,\ y = 0. \end{cases} $$

When this is implemented in software I think the "undefined" case usually returns $0.$

If you have to write formulas involving the direction angles of several two-dimensional vectors in terms of their components, then you might find the notation $\operatorname{atan2}(y, x)$ convenient.


This looks very similar to complex numbers, where $\text{Magnitude}(v)$ is displayed by $|v|$ and $\text{Angle}(v)$ is written $\text{arg}(v)$. We have that $\arg(v)$ is the unique angle $\in (-\pi, \pi]$ where:

$$\cos\theta =\frac{x}{|v|}=\frac{x}{\sqrt{x^2+y^2}}$$ and $$\sin\theta=\frac{y}{|v|}=\frac{y}{\sqrt{x^2+y^2}}$$