Getting the True Bearing between 2 points. [Still Not Working]
How can I find the true bearing between 2 points? Ie;
A . _ _ _ _ _ . B
The true bearing of A from B would be 270o, but how can I work that out in code, given the coordinates of both?
I've searched all over the forums and found nothing that helps me.
[Edited by - Zmurf on November 4, 2005 11:59:13 PM]
Heres the code i'm using, but it isn't working ><
But i'm getting weird results, it's facing the opposite direction and and only seems to turn in a 90 degree area.
double CLimb::PointDir(double x1, double y1, double x2, double y2){ double rise, run, dir; rise = y2 - y1; run = x2 - x1; if (x1 > x2) dir = 90 - atan(rise/run); else dir = 90 + atan(rise/run); dir *= RADTODEG; if (dir >= 360) dir = dir - 360; if (dir < 0) dir = 360 - abs(dir); return dir;}
But i'm getting weird results, it's facing the opposite direction and and only seems to turn in a 90 degree area.
If you are using C/C++, use the function atan2(). It is much superior to atan() in this case.
Quote:A . _ _ _ _ _ . BGiven the usual convention of 0 degrees being along the +x axis, the solution would be:
The true bearing of A from B would be 270o, but how can I work that out in code, given the coordinates of both?
float x = A.x-B.x;float y = A.y-B.y;float angle = atan2(y,x);
From your example it looks like you want a different orientation for 0 degrees, so you'll have to add some (integer) multiple of 90 to your result, or swap and/or negate the arguments to atan2().For this particular problem you should prefer atan2() over atan() as it has better behavior over the range of inputs. Also, remember that c++ math functions work with radians, so you'll need to convert back and forth if you're using degrees.
Well, for one in C/C++ the trig functions are in radians so I'm surprised you get turned within 90 degrees since atan is going to return values between -pi/2 and pi/2, i.e. ~+/-1.57. You have code for radians to degrees, buy you have that "90 - atan" in there before you get to it. Second you can use atan2 to get the full 0 to 2pi range so you don't need the if statements.
hmm, my new code isn't working just right.
Since atan2 gets the angle from the origin, i adjust the position its finding as if (x1, y1) were at the origin and looking to (x2, y2) so then it should return the correct direciton from xy1 to xy2. but for some reson it doesn't, instead its doing this:
A ....... 0....
...............
...............
....B..........X
A should be direction to X, but instead its facing to B.
It's kinda confusing but it's hard to explain.
double PointDir(double x1, double y1, double x2, double y2){ double dir, x, y; x = x2 - x1; y = y2 - y1; dir = atan2(y, x) * RADTODEG; if (dir> 360) dir = dir - 360; if (dir < 0) dir = 360 - abs(dir); return dir;}
Since atan2 gets the angle from the origin, i adjust the position its finding as if (x1, y1) were at the origin and looking to (x2, y2) so then it should return the correct direciton from xy1 to xy2. but for some reson it doesn't, instead its doing this:
A ....... 0....
...............
...............
....B..........X
A should be direction to X, but instead its facing to B.
It's kinda confusing but it's hard to explain.
I'm not sure what your problem is here, you've got the direction vector from A to B being the vector X, your diagram appears to be correct. The direction vector in this case is the length and direction of how to get to point B from point A.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement