Results 1 to 4 of 4
Like Tree1Thanks
  • 1 Post By johnsomeone

Math Help - direction component of vector without case analysis

  1. #1
    Member
    Joined
    Aug 2011
    Posts
    76
    Thanks
    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.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Super Member
    Joined
    Sep 2012
    From
    Washington DC USA
    Posts
    525
    Thanks
    147

    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)
    Last edited by johnsomeone; October 10th 2012 at 03:35 PM.
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Member
    Joined
    Aug 2011
    Posts
    76
    Thanks
    1

    Re: direction component of vector without case analysis

    Quote Originally Posted by johnsomeone View Post
    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.
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Super Member
    Joined
    Sep 2012
    From
    Washington DC USA
    Posts
    525
    Thanks
    147

    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.
    Last edited by johnsomeone; October 11th 2012 at 07:37 AM.
    Thanks from infraRed
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Component of vector a in direction of vector b.
    Posted in the Pre-Calculus Forum
    Replies: 3
    Last Post: January 2nd 2012, 10:36 AM
  2. Component of a vector in direction of another
    Posted in the Advanced Algebra Forum
    Replies: 2
    Last Post: July 12th 2010, 03:28 AM
  3. Component of a vector in direction of another
    Posted in the Calculus Forum
    Replies: 1
    Last Post: July 12th 2010, 01:15 AM
  4. Replies: 1
    Last Post: April 22nd 2010, 12:28 AM
  5. Replies: 4
    Last Post: November 5th 2009, 05:03 PM

Search Tags


/mathhelpforum @mathhelpforum