CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2012
    Posts
    49

    heading in a cartesian coordinate system question

    Hello,

    I have 2 points in a cartesian coordinate system, where the first point is always (0,0). Given the second point and a heading in degrees, how would I write an algorithm to determine if the 2nd point is traveling in the general direction of (0,0) ?

    For example:

    Point a (0,0)
    Point b (10,0)
    Point b heading = 270 degrees

    This would be true this 270 degrees points right and point b is 10 units right of point a. I would probably also want this is be fuzzy somewhat, in that anything from say + or - 30 degrees would also be true, or something like this.

    Does there exist any known algorithms, implemented in C++, to help me with this problem?

    Thanks!
    Alex

  2. #2
    Join Date
    May 2009
    Posts
    2,413

    Re: heading in a cartesian coordinate system question

    Quote Originally Posted by aseminov View Post
    For example:

    Point a (0,0)
    Point b (10,0)
    Point b heading = 270 degrees

    This would be true this 270 degrees points right and point b is 10 units right of point a.
    I don't quite get your definition of heading. With a heading of 270 degrees point b is headed straight south isn't it? It means it must be somewhere on the positive y-axis to be going towards a, like at (0,10) or something.

    The problem itself is basic trigonometry: Determine the heading of the vector starting at b ending at a. That's the heading b must have to end up at a (travelling along a straight line).
    Last edited by nuzzle; April 16th, 2012 at 06:03 AM.

  3. #3
    Join Date
    Mar 2004
    Location
    Central Florida
    Posts
    293

    Re: heading in a cartesian coordinate system question

    I don't quite get your definition of heading. With a heading of 270 degrees point b is headed straight south isn't it? It means it must be somewhere on the positive y-axis to be going towards a, like at (0,10) or something.
    If xX is horizontal and Y is vertical, then 10, 0 is 10 units to the right of 0,0 alnong the X axis. A 270 degree heading would be due West. And point straight to 0,0.

    The problem itself is basic trigonometry: Determine the heading of the vector starting at b ending at a. That's the heading b must have to end up at a (travelling along a straight line).
    Absoulely!
    "Effective teaching is the essence of leadership..."

    "There is no substitute for a carefully thought-out design."

    If you have found this post to be useful, please Rate it.

  4. #4
    Join Date
    May 2009
    Posts
    2,413

    Re: heading in a cartesian coordinate system question

    Quote Originally Posted by mlgoff View Post
    If xX is horizontal and Y is vertical, then 10, 0 is 10 units to the right of 0,0 alnong the X axis. A 270 degree heading would be due West. And point straight to 0,0.
    No, 270 degrees would not be due west, 180 would.

    Making a 180 degree turn means going in the opposite direction. If you're starting at origo travelling eastward and then make a 180 degree turn you will be going westward back toward origo. 270 degrees would mean going south (as does also -90 degrees).
    Last edited by nuzzle; April 18th, 2012 at 01:45 AM.

  5. #5
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: heading in a cartesian coordinate system question

    Understand that you are using a compass-style definition of headings, which differs from standard angular measurements used by most trig functions by direction (clockwise vs. counterclockwise) and by 90 degrees (or, pi/2 radians, since most trig functions use radians, not degrees).

    With that understanding, look at the atan2() function. To get the compass heading in degrees from point b to point a, use the following:
    Code:
    double compass_heading = 90.0 - 57.29578 * atan2( by-ay, bx-ax );
    if (compass_heading >= 360.0)
      compass_heading -= 360.;
    if (compass_heading < 0.0)
      compass_heading += 360.;
    In your case, where point a is at the origin, ay=ax=0, which simplifies the equation a bit.

    To determine if the actual heading is "close enough" to the compass heading, just test against limits:
    Code:
    double limit = 30.0;  /* in degrees */
    double actual_heading = /* whatever */;
    if ( fabs( actual_heading - compass_heading ) < limit
    {
      /* it's close enough
    }
    else
    {
      /* it's not close enough */
    }
    fabs() is the floating point absolute value function.

    Mike
    Last edited by MikeAThon; April 18th, 2012 at 04:59 PM.

  6. #6
    Join Date
    May 2009
    Posts
    2,413

    Re: heading in a cartesian coordinate system question

    Quote Originally Posted by MikeAThon View Post
    Understand that you are using a compass-style definition of headings,
    The OP has started several threads on this topic and this best describes the actual problem I think,

    http://forums.codeguru.com/showthread.php?t=523000

  7. #7
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: heading in a cartesian coordinate system question

    Thanks, nuzzle, you're right! That thread is much more complete than this one.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured