fork(1) download
  1. #include<iostream>
  2. #include<iomanip>
  3. #include<cmath>
  4.  
  5.  
  6. // Source: // http://w...content-available-to-author-only...o.uk/scripts/latlong.html
  7.  
  8. static const double PI = 3.14159265358979323846, earthDiameterMeters = 6371.0 * 2 * 1000;
  9.  
  10. double degreeToRadian (const double degree) { return (degree * PI / 180); };
  11. double radianToDegree (const double radian) { return (radian * 180 / PI); };
  12.  
  13. double CoordinatesToAngle (double latitude1,
  14. const double longitude1,
  15. double latitude2,
  16. const double longitude2)
  17. {
  18. const auto longitudeDifference = degreeToRadian(longitude2 - longitude1);
  19. latitude1 = degreeToRadian(latitude1);
  20. latitude2 = degreeToRadian(latitude2);
  21.  
  22. using namespace std;
  23. const auto x = (cos(latitude1) * sin(latitude2)) -
  24. (sin(latitude1) * cos(latitude2) * cos(longitudeDifference));
  25. const auto y = sin(longitudeDifference) * cos(latitude2);
  26.  
  27. const auto degree = radianToDegree(atan2(y, x));
  28. return (degree >= 0)? degree : (degree + 360);
  29. }
  30.  
  31. double CoordinatesToMeters (double latitude1,
  32. double longitude1,
  33. double latitude2,
  34. double longitude2)
  35. {
  36. latitude1 = degreeToRadian(latitude1);
  37. longitude1 = degreeToRadian(longitude1);
  38. latitude2 = degreeToRadian(latitude2);
  39. longitude2 = degreeToRadian(longitude2);
  40.  
  41. using namespace std;
  42. auto x = sin((latitude2 - latitude1) / 2), y = sin((longitude2 - longitude1) / 2);
  43. #if 1
  44. return earthDiameterMeters * asin(sqrt((x * x) + (cos(latitude1) * cos(latitude2) * y * y)));
  45. #else
  46. auto value = (x * x) + (cos(latitude1) * cos(latitude2) * y * y);
  47. return earthDiameterMeters * atan2(sqrt(value), sqrt(1 - value));
  48. #endif
  49. }
  50.  
  51. std::pair<double,double> CoordinateToCoordinate (double latitude,
  52. double longitude,
  53. double angle,
  54. double meters)
  55. {
  56. latitude = degreeToRadian(latitude);
  57. longitude = degreeToRadian(longitude);
  58. angle = degreeToRadian(angle);
  59. meters *= 2 / earthDiameterMeters;
  60.  
  61. using namespace std;
  62. pair<double,double> coordinate;
  63.  
  64. coordinate.first = radianToDegree(asin((sin(latitude) * cos(meters))
  65. + (cos(latitude) * sin(meters) * cos(angle))));
  66. coordinate.second = radianToDegree(longitude
  67. + atan2((sin(angle) * sin(meters) * cos(latitude)),
  68. cos(meters) - (sin(latitude) * sin(coordinate.first))));
  69.  
  70. return coordinate;
  71. }
  72.  
  73. int main ()
  74. {
  75. using namespace std;
  76. const auto latitude1 = 12.968460, longitude1 = 77.641308,
  77. latitude2 = 12.967862, longitude2 = 77.653130;
  78.  
  79. cout << std::setprecision(10);
  80. cout << "(" << latitude1 << "," << longitude1 << ") --- "
  81. "(" << latitude2 << "," << longitude2 << ")\n";
  82.  
  83. auto angle = CoordinatesToAngle(latitude1, longitude1, latitude2, longitude2);
  84. cout << "Angle = " << angle << endl;
  85.  
  86. auto meters = CoordinatesToMeters(latitude1, longitude1, latitude2, longitude2);
  87. cout << "Meters = " << meters << endl;
  88.  
  89. auto coordinate = CoordinateToCoordinate(latitude1, longitude1, angle, meters);
  90. cout << "Destination = (" << coordinate.first << "," << coordinate.second << ")\n";
  91. }
  92.  
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
(12.96846,77.641308) --- (12.967862,77.65313)
Angle =  92.97009105
Meters = 1282.743434
Destination = (12.967862,77.65361386)