Direction Angle of 2D Vector in Terms of Arctangent

Theorem
Let $\mathbf a$ be a vector quantity embedded in a Cartesian plane $P$ expressed in component form as:
 * $\mathbf a = x \mathbf i + y \mathbf j$

Let $\theta$ denote the direction of $\mathbf a$.

Then:
 * $\theta = \begin{cases}

\map \arctan {\dfrac y x} & : x > 0 \\ \map \arctan {\dfrac y x} + \pi & : x < 0 \text{ and } y \ge 0 \\ \map \arctan {\dfrac y x} - \pi & : x < 0 \text{ and } y < 0 \\ \dfrac \pi 2 & : x = 0 \text{ and } y > 0 \\ -\dfrac \pi 2 & : x = 0 \text{ and } y < 0 \\ \text{undefined} & : x = 0 \text { and } y = 0 \end{cases}$

Where $\theta$ is conventionally measured off of the positive direction of the $x$ -axis in the interval $\hointl {-\pi} \pi$

Note that this function could be remodeled to fit the interval $\hointr 0 {2 \pi}$

Proof
Let $\mathbf a$ be such that one of the following holds:


 * $\mathbf a$ is in Quadrant $\text{I}$ or Quadrant $\text{IV}$
 * $\mathbf a$ is on the positive direction of the $x$-axis.

Then:
 * $x > 0$

and:
 * $-\frac \pi 2 < \theta < \frac \pi 2$

The components of $\mathbf a$ form the legs of a right triangle where:

Let $\mathbf a$ be such that one of the following holds:


 * $\mathbf a$ is in Quadrant $\text{II}$
 * $\mathbf a$ is on the negative direction of the $x$-axis.

Then:
 * $x < 0$
 * $y \ge 0$

and:
 * $\dfrac \pi 2 < \theta \le \pi$

In this case, trying to find the direction angle using the right triangle defined by its components using $\map \arctan {\frac y x}$ returns the diametrically opposite angle, so adding $\pi$ returns the true direction angle in the interval $\hointl {-\pi} \pi$:

Let $\mathbf a$ be in the Quadrant $\text{III}$.

Then:
 * $x < 0$
 * $y < 0$

and:
 * $-\pi < \theta < \dfrac {-\pi} 2$

Similarly, $\map \arctan {\dfrac y x}$ returns the diametrically opposite angle, so subtracting $\pi$ returns the true direction angle in the interval $\hointl {-\pi} \pi$:

Let $\mathbf a$ be on the positive direction of the $y$-axis.

Then:
 * $x = 0$
 * $y > 0$

Then the arctangent of $\theta$ is undefined.

We have:

Let $\mathbf a$ be on the negative direction of the $y$-axis.

Then:
 * $x = 0$
 * $y < 0$

Then the arctangent of $\theta$ is undefined.

We have:

Finally, let $\mathbf a$ be the zero vector.

We have:
 * $x = 0$
 * $y = 0$

and hence:

Hence, $\theta$ can be described using the following piecewise function:

Hence the result.

Also known as
This piecewise function is often presented in computer languages as $\map {\text {atan2} } {y, x}$.