
April 15th, 2012, 06:43 PM
#1
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

April 16th, 2012, 01:43 AM
#2
Re: heading in a cartesian coordinate system question
Originally Posted by aseminov
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 yaxis 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 07:03 AM.

April 17th, 2012, 10:36 AM
#3
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 yaxis 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 thoughtout design."
If you have found this post to be useful, please Rate it.

April 17th, 2012, 11:18 AM
#4
Re: heading in a cartesian coordinate system question
Originally Posted by mlgoff
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 02:45 AM.

April 18th, 2012, 05:56 PM
#5
Re: heading in a cartesian coordinate system question
Understand that you are using a compassstyle 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( byay, bxax );
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 05:59 PM.

April 19th, 2012, 12:27 AM
#6
Re: heading in a cartesian coordinate system question
Originally Posted by MikeAThon
Understand that you are using a compassstyle 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

April 19th, 2012, 06:23 PM
#7
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

Forum Rules

Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!
