gpsmath.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright (c) 2019 Clementine Computing LLC.
  3. *
  4. * This file is part of PopuFare.
  5. *
  6. * PopuFare is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * PopuFare is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with PopuFare. If not, see <https://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include <stdio.h>
  21. #include <math.h>
  22. #include "gpsmath.h"
  23. #define EARTH_RAD (6372795.0)
  24. #define DEG2RAD(deg) (deg * 0.0174532925)
  25. #define DECDEG(gps) (trunc(gps / 100.0) + ((gps - (trunc(gps / 100.0) * 100.0)) / 60.0))
  26. double trunc(double x);
  27. // calculated GPS distance in meters between two points
  28. //
  29. float GPS_Dist(float lat1, float lon1, float lat2, float lon2) {
  30. float dr_lat1,dr_lat2,delta_lon;
  31. float sinlat1,coslat1,sinlat2,coslat2,sindl,cosdl;
  32. float y,x;
  33. dr_lat1 = DEG2RAD(DECDEG(lat1));
  34. dr_lat2 = DEG2RAD(DECDEG(lat2));
  35. delta_lon = DEG2RAD(DECDEG(lon1)) - DEG2RAD(DECDEG(lon2));
  36. sinlat1=sin(dr_lat1);
  37. sinlat2=sin(dr_lat2);
  38. coslat1=cos(dr_lat1);
  39. coslat2=cos(dr_lat2);
  40. sindl=sin(delta_lon);
  41. cosdl=cos(delta_lon);
  42. y=sqrt( pow(coslat2 * sindl , 2) + pow((coslat1 * sinlat2) - (sinlat1 * coslat2 * cosdl), 2));
  43. x=(sinlat1 * sinlat2) + (coslat2 * coslat2 * cosdl);
  44. return EARTH_RAD * atan2(y,x);
  45. }
  46. /* int main() */
  47. /* { */
  48. /* printf("Distance in meters = %f\n",GPS_Dist(-3607.2,-8640.2, -3356.4,-11824.0)/1000); */
  49. /* return 0; */
  50. /* } */