# Math Help - direction component of vector without case analysis

1. ## direction component of vector without case analysis

Vector related question here - mostly out of curiousity: Is there a way, starting with a vector in component form (<x, y>) to get the direction angle in [0, 2 * pi) without using case analysis based on the quadrant the vector is in? In a programming project I wanted to write a function that pulls out the direction angle, in radians, from a vector in component form. So I wrote this:

Code:
vectorDirection :: (Double, Double) -- vector
-> Double -- angle in radians
vectorDirection (0, 0) = 0.0 / 0.0 -- NaN
vectorDirection (x, y)
| x >= 0 && y >= 0 = a
| x < 0 && y >= 0 = pi - a
| x < 0 && y < 0 = pi + a
| otherwise = 2 * pi - a
where a = atan (abs y / abs x)
In other words, I used arctan (|y| / |x|) to get an angle, but since that angle's meaning changes depending on which quadrant the vector is in, I have to use case analysis to adjust it so that it becomes a nice standard angle between 0 and 2 * pi. This is the only way I saw it done in my old trigonometry book, but I was wondering if there was a more direct method.

2. ## Re: direction component of vector without case analysis

Angle = $\arctan(y/x) + \frac{|x| - x}{2|x|} \ \pi$

works as a single formula for $x \ne 0$, except it's in the range $\left(-\frac{\pi}{2}, \frac{\pi}{2}\right) \bigcup \left(\frac{\pi}{2}, \frac{3\pi}{2}\right)$

3. ## Re: direction component of vector without case analysis

Originally Posted by johnsomeone
Angle = $\arctan(y/x) + \frac{|x| - x}{2|x|} \ \pi$

works as a single formula for $x \ne 0$, except it's in the range $\left(-\frac{\pi}{2}, \frac{\pi}{2}\right) \bigcup \left(\frac{\pi}{2}, \frac{3\pi}{2}\right)$
Erm... well thanks, but [0, 2*pi) is most definitely the range I am looking for. Much more ideal for programming applications.

4. ## Re: direction component of vector without case analysis

$f(t) = \frac{|t|-t}{2|t|}$

You can use $f(t)$ and $f(-t)$ to setup up your cases algebraically within a function. It's like building a switch statement into the formula, because it's an indicator function - it's 1 when $t$ is negative and 0 when $t$ is positive.

So far, you have what you seek, except that in the $\left(-\frac{\pi}{2}, 0 \right)$ case, you'd like to add $2 \pi$.

So we need to add $2 \pi$ only if the result is in the 4th quadrant, and so use $f$ to incorporate that. So do:

$Angle = \arctan(y/x) + \pi f(x) + 2 \pi f(-x)f(y)$

$= \arctan(y/x) + \pi ( f(x) + 2 f(-x)f(y))$

$= \arctan(y/x) + \pi \left\{ \left(\frac{|x|-x}{2|x|}\right) + 2\left(\frac{|-x|-(-x)}{2|-x|}\right)\left(\frac{|y|-y}{2|y|}\right) \right\}$

$= \arctan(y/x) + \pi \left\{ \left(\frac{|x|-x}{2|x|}\right) + 2\left(\frac{|x|+x}{2|x|}\right)\left(\frac{|y|-y}{2|y|}\right) \right\}$

$= \arctan(y/x) + \frac{\pi}{2|x||y|} \{ |y|(|x|-x) + (|x|+x)(|y|-y) \}$

$= \arctan(y/x) + \frac{\pi}{2|x||y|} \{ (|x||y|-x|y|) + (|x||y|+x|y|-|x|y - xy) \}$

$= \arctan(y/x) + \frac{\pi}{2|x||y|} \{ 2|x||y| -|x|y - xy \}$

Thus, when $x \ne 0$ and $y \ne 0$, have

$Angle = \arctan(y/x) + \pi \ \frac{2|x||y| -|x|y - xy}{2|x||y|} \in [0, 2\pi)$

Of course, you'll still need if-statements to check for x or y being 0.