c - Find compass heading with geo-cordinates -
i find compass head 2 geo location. writing c application. write function like,
double calculateheading(double lat1, double long1, double lat2, double long2) { double latitude = lat1 - lat2; double longitude = long1 - long2; double angle = 0; printf("\n#################################\n"); printf("lat1: %lf lat2: %lf\n", lat1, lat2); printf("long1: %lf long2: %lf\n", long1, long2); printf("lat diff: %lf\n", latitude); printf("long diff: %lf\n", longitude); angle = atan2(longitude, latitude); angle = radianstodegrees(angle); printf("angle: %lf\n", angle); printf("#################################\n"); return angle; }
but not works on same location. here passing lat1, long1, lat2, long2 in degree. can me out here?
updated code http://www.movable-type.co.uk/scripts/latlong.html working,
#define degreestoradians(angledegrees) (angledegrees * m_pi / 180.0) #define radianstodegrees(angleradians) (angleradians * 180.0 / m_pi) double calculateheading(double lat1, double lon1, double lat2, double lon2) { double lat1_rad = degreestoradians(lat1); double lat2_rad = degreestoradians(lat2); double difflon = lon2-lon1; double dlon = degreestoradians(difflon); double y = sin(dlon) * cos(lat2_rad); double x = cos(lat1_rad) * sin(lat2_rad) - sin(lat1_rad) * cos(lat2_rad) * cos(dlon); double angle = 0; angle = atan2(y,x); printf("angle: %lf\n", radianstodegrees(fmod(angle + 360.0, 360.0))); return angle; }
angle = atan2(y,x); printf("angle: %lf\n", radianstodegrees(fmod(angle + 360.0, 360.0)));
here have got radian value of angle, adding 360 degrees nonsense. possible corrections:
radianstodegrees(fmod(angle + 2*pi, 2*pi)) or fmod(radianstodegrees(angle) + 360.0, 360.0)
Comments
Post a Comment