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.