heading in a cartesian coordinate system question
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Member
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. Elite Member
Join Date
May 2009
Posts
2,413

## 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 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. Member
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!

4. Elite Member
Join Date
May 2009
Posts
2,413

## 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 01:45 AM.

5. Elite Member Power Poster
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 );
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 */;
{
/* 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. Elite Member
Join Date
May 2009
Posts
2,413

## Re: heading in a cartesian coordinate system question

Originally Posted by MikeAThon
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,

7. Elite Member Power Poster
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
•