long get_bearing(struct Location *loc1, struct Location *loc2){long off_x = loc2->lng - loc1->lng;long off_y = (loc2->lat - loc1->lat) * scaleLongUp;long bearing = 9000 + atan2(-off_y, off_x) * 5729.57795;if (bearing < 0) bearing += 36000;return bearing;}
I cant seem to find where the 9000, and 5729.57795 numbers come from, I looked up things about GPS maths and great circle maths etc,, but cant seem to get where these are coming from (am I missing something)
Further The scaleLongUp I know is used to reverse the longitude scaling, but why do we need to use this? Is it from another part of code where scaleLongDown was used?
And lastly what is the get_bearing2 used for?
Thanks
Alex
Replies
9000 is 90° expanded to an integer * 100.
I use scaling to stretch out the longitude based on the cosine of the latitude. This turns the local area into a normal grid so that atan2 can do it's job. The output is an angle in radians which is rotated 90°. I just scale to degrees (5729.57795 ), add 90 and wrap the value if it's higher than 360.
I use this method because it's really, really fast. If I were in a plane and needed to get a bearing between two cities, it would be off a bit, but for a few miles it's perfectly accurate. The truely accurate methods are about 8-10x more computationally expensive.
get_bearing2 is an older method that was giving me problems. I just haven't removed it yet.
Jason