gps_distance.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include <adc_tools/gps_distance.h>
  2. #include <math.h>
  3. #define M_PI (3.1415926535897932384626433832795)
  4. // 计算弧度
  5. double iv::decition::rad(double d)
  6. {
  7. const double PI = 3.1415926535898;
  8. return d * PI / 180.0;
  9. }
  10. // 从两个gps坐标点(经纬度)获得两点的直线距离,单位是米
  11. double iv::decition::CalcDistance(double fLati1, double fLong1, double fLati2, double fLong2)
  12. {
  13. const float EARTH_RADIUS = 6378.137;
  14. double radLat1 = rad(fLati1);
  15. double radLat2 = rad(fLati2);
  16. double a = radLat1 - radLat2;
  17. double b = rad(fLong1) - rad(fLong2);
  18. double s = 2 * asin(sqrt(pow(sin(a / 2), 2) + cos(radLat1)*cos(radLat2)*pow(sin(b / 2), 2)));
  19. s = s * EARTH_RADIUS;
  20. // s = (int)(s * 10000000) / 10000;
  21. return s;
  22. }
  23. // 从直角坐标两点的直线距离,单位是米
  24. double iv::decition::DirectDistance(double x1, double y1, double x2, double y2)
  25. {
  26. double s = sqrt(pow((x1-x2), 2) +pow((y1-y2), 2));
  27. return s;
  28. }
  29. // 从直角坐标计算两点的夹角
  30. double iv::decition::DirectAngle(double x1, double y1, double x2, double y2)
  31. {
  32. double angle = atan((y2 - y1) / (x2 - x1)) * 180 / M_PI;
  33. return angle;
  34. }