-2

I would like to find compass head from two geo location. I am writing C application. I write a 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 this not works on the same location. Here I am passing lat1, long1, lat2, long2 in degree. Can anyone help me out here?

Updated Code from http://www.movable-type.co.uk/scripts/latlong.html which is 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;
}
  • 1
    "Does not work" is not a proper error description. Which result do you expect and what do you get? Does your formula work on paper and you have a C problem or is your formula already wrong? – Gerhardh Jul 26 '17 at 06:26
  • Besides missing mandatory information in your question, a first suggestion would be that you should look at the parameters of `atan2` a bit closer. What is expected and what do you pass? – Gerhardh Jul 26 '17 at 06:28
  • You say "compass heading" but make no correction from "true north" to "magnetic north". – Weather Vane Jul 26 '17 at 06:40
  • When I pass same angle, it gives me random number. Also I think I should pass atan2(Latitude, Longitude) Right? – Darshan Shah Jul 26 '17 at 06:43
  • The earth being not flat the algorithm is certainly more complicated than calculating atan2. – Jabberwocky Jul 26 '17 at 06:45
  • "Does not work" because your approach is naive. [See this](http://www.igismap.com/formula-to-find-bearing-or-heading-angle-between-two-points-latitude-longitude/), found in a few seconds. – Weather Vane Jul 26 '17 at 06:46
  • 1
    A quick Google search: http://www.movable-type.co.uk/scripts/latlong.html – Jabberwocky Jul 26 '17 at 06:46
  • 1
    And please when asking such a question you should include the input and the expected and the actual output. And also read this: [mcve] – Jabberwocky Jul 26 '17 at 06:47
  • Did you forget to convert the inputs to radians? "I am passing lat1, long1, lat2, long2 in degree". – Weather Vane Jul 26 '17 at 06:49
  • I write code with reference of https://stackoverflow.com/questions/4403515/heading-from-point-a-to-point-b-in-2d-space. – Darshan Shah Jul 26 '17 at 06:53
  • @DarshanShah show us a [mcve] – Jabberwocky Jul 26 '17 at 06:54
  • @DarshanShah "Also I think I should pass atan2(Latitude, Longitude) Right?" Wrong. `longitude` and `latitude` are differences of angles. My manpage for `atan2` does not ask for angles or difference of angles. – Gerhardh Jul 26 '17 at 07:52
  • From my man page "double atan2(double y, double x);" And here I am passing double value – Darshan Shah Jul 26 '17 at 08:11
  • @Darshan Shah atan2 approach is not applicable to geo coordinates. Look at `latlong` link given by Michael Walz – MBo Jul 26 '17 at 08:16
  • I think in this link also they are using atan2. See code below of link and LatLon.prototype.bearingTo = function(point) of http://www.movable-type.co.uk/scripts/latlong.html – Darshan Shah Jul 26 '17 at 08:29
  • But that code accounts for spherical coordinates. Note that y and x there are not just lat and long differences – MBo Jul 26 '17 at 09:14
  • @DarshanShah if you look a bit closer on atan2 (the meaning, not only the signature) you might find out that the parameters are distances in x and y direction. Not angles and not differences of angles. The type alone is not sufficient to make a valid parameter. – Gerhardh Jul 26 '17 at 10:20
  • Ok thanks. So for this I have to find distance between two points. So angle = atan2((lon2 - lon1), (lat2 - lat1)); should work. – Darshan Shah Jul 26 '17 at 10:32
  • In your updated code - atan2 returns value in radians, not degrees – MBo Jul 26 '17 at 12:34

1 Answers1

0
angle = atan2(y,x);
printf("Angle: %lf\n", radiansToDegrees(fmod(angle + 360.0, 360.0)));

Here you have got radian value of angle, so adding 360 degrees is nonsense. Possible corrections:

radiansToDegrees(fmod(angle + 2*Pi, 2*Pi))
or 
fmod(radiansToDegrees(angle) + 360.0, 360.0)
MBo
  • 77,366
  • 5
  • 53
  • 86