fork(10) 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 = asin((sin(latitude) * cos(meters))
  65. + (cos(latitude) * sin(meters) * cos(angle)));
  66. coordinate.second = longitude + atan2((sin(angle) * sin(meters) * cos(latitude)),
  67. cos(meters) - (sin(latitude) * sin(coordinate.first)));
  68.  
  69. coordinate.first = radianToDegree(coordinate.first);
  70. coordinate.second = radianToDegree(coordinate.second);
  71.  
  72. return coordinate;
  73. }
  74.  
  75. int main ()
  76. {
  77. using namespace std;
  78. const auto latitude1 = 12.968460, longitude1 = 77.641308,
  79. latitude2 = 12.967862, longitude2 = 77.653130;
  80.  
  81. cout << std::setprecision(10);
  82. cout << "(" << latitude1 << "," << longitude1 << ") --- "
  83. "(" << latitude2 << "," << longitude2 << ")\n";
  84.  
  85. auto angle = CoordinatesToAngle(latitude1, longitude1, latitude2, longitude2);
  86. cout << "Angle = " << angle << endl;
  87.  
  88. auto meters = CoordinatesToMeters(latitude1, longitude1, latitude2, longitude2);
  89. cout << "Meters = " << meters << endl;
  90.  
  91. auto coordinate = CoordinateToCoordinate(latitude1, longitude1, angle, meters);
  92. cout << "Destination = (" << coordinate.first << "," << coordinate.second << ")\n";
  93. }
  94.  
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
(12.96846,77.641308) --- (12.967862,77.65313)
Angle =  92.97009105
Meters = 1282.743434
Destination = (12.967862,77.65313)