Advertisement
Zgragselus

Geopoint

Feb 1st, 2024 (edited)
990
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1. /// Geopoint class
  2. ///
  3. /// Used for working with latitude/longitude and angles/distances. One instance
  4. /// represents single geographic location.
  5. class GeoPoint
  6. {
  7. public:
  8.     double latitude;        /// Latitude on the spherical surface
  9.     double longitude;       /// Longitude on the spherical surface
  10.  
  11.     /// Empty constructor
  12.     GeoPoint()
  13.     {
  14.     }
  15.  
  16.     /// Constructor from latitude and longitude
  17.     GeoPoint(double _lat, double _long)
  18.     {
  19.         latitude = (_lat * 3.14159265f / 180.0f);
  20.         longitude = (_long * 3.14159265f / 180.0f);
  21.     }
  22.  
  23.     /// Calculates distance between two geographic location, uses spherical geometry
  24.     friend double distance(const GeoPoint& a, const GeoPoint& b)
  25.     {
  26.         double d = 2.0 * 6372795.477598 * asin(sqrt(pow(sin((b.latitude - a.latitude) / 2.0), 2.0) +
  27.             cos(a.latitude) * cos(b.latitude) * pow(sin((b.longitude - a.longitude) / 2.0), 2.0)));
  28.         return d;
  29.     }
  30.  
  31.     /// Calculates angle from a to b related to equator
  32.     friend double angle(const GeoPoint& a, const GeoPoint& b)
  33.     {
  34.         double z = log(tan(b.latitude / 2.0 + 3.14159265 / 4.0) / tan(a.latitude / 2.0 + 3.14159265 / 4.0));
  35.         double x = a.longitude - b.longitude;
  36.         if (x < 0.0)
  37.         {
  38.             x = -x;
  39.         }
  40.         return atan2(z, x);
  41.     }
  42.  
  43.     /// Calculates direction from a to b (z being north-south, x being east-west)
  44.     friend float4 direction(const GeoPoint& a, const GeoPoint& b)
  45.     {
  46.         float4 result = float4(0.0f);
  47.         result.z = (float)log(tan(b.latitude / 2.0 + (double)M_PI / 4.0) / tan(a.latitude / 2.0 + (double)M_PI / 4.0));
  48.         result.x = (float)(a.longitude - b.longitude);
  49.         return result;
  50.     }
  51.  
  52.     /// Calculates angle from a to b related to north
  53.     friend double bearing(const GeoPoint& a, const GeoPoint& b)
  54.     {
  55.         double longitudeDiffRad = b.longitude - a.longitude;
  56.  
  57.         double y = sin(longitudeDiffRad) * cos(b.latitude);
  58.         double x = cos(a.latitude)*sin(b.latitude) -
  59.             sin(a.latitude)*cos(b.latitude)*cos(longitudeDiffRad);
  60.         return atan2(y, x);
  61.     }
  62. };
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement