views:

102

answers:

1

Hi everybody, I'm working in another iPhone App that uses AR, and I'm creating my own framework, but I'm having trouble trying to get the angle of a second coordinate relative to the current device position, anyone know a good resource that could help me with this?

Thanks in advance!

+3  A: 

If the two points are close enough together, and well away from the poles, you can use some simple trig:

float dy = lat2 - lat1;
float dx = cosf(M_PI/180*lat1)*(long2 - long1);
float angle = atan2f(dy, dx);

EDIT: I forgot to mention that latN and longN — and therefore dx and dy — can be in degrees or radians, so long as you don't mix units. angle, however, will always come back in radians. Of course, you can get it back to degrees if you multiply by 180/M_PI.

Marcelo Cantos
Something seems a little off. dy is in degrees. It looks like your trying to convert dx to radians. But why is lat1 converted to radians then multiplied by the delta longitude? But otherwise, atan2() should work for close points as you say.
Alex Blakemore
@Alex: dx and dy are both in degrees. But dx has to be scaled by the cosine of the latitude, and cosf requires radians.
Marcelo Cantos