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.

Re: direction component of vector without case analysis

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

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

Re: direction component of vector without case analysis

Quote:

Originally Posted by

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

works as a single formula for $\displaystyle x \ne 0$, except it's in the range $\displaystyle \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.

Re: direction component of vector without case analysis

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

You can use $\displaystyle f(t)$ and $\displaystyle 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 $\displaystyle t$ is negative and 0 when $\displaystyle t$ is positive.

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

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

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

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

$\displaystyle = \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\}$

$\displaystyle = \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\}$

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

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

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

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

$\displaystyle 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.