C++ Console - New lat/lon coordinate using azimuth and range : <p>I would like assistance understanding this code I found during a search. I have been stuck for 2 weeks trying to get it and it's holding my project back. I am honestly trying to learn as I go and not just be a script kiddie about it, but this is much more complicated than the rest of my project even though I am trying. (I just learned about <strong>auto</strong> today while trying to understand this code, for example.)</p>

<p>I am working on a weather app and I know the lat/lon of the radar site. I need the lat/lon of a feature the radar has detected based off of the azimuth/range that the radar tells me (example, 271 degrees and 7 nautical miles). I need to understand how I can use this code below to convert the azimuth/range to a new lat lon coordinate. I don't require the other functions, just to be able to put my variables (starting coords, azimuth and range) and get a result. The code below looks to do much more than that, and it is confusing me.</p>

<p>I see the following code near the end :</p>

<pre><code>auto coordinate = CoordinateToCoordinate(latitude1, longitude1, angle, meters);
</code></pre>

<p>... Which looks to be the part I would need out of this. I see how it's calculating it but once I dig deeper I just get myself confused. I have tried hacking at the code so much that I gave up and don't even have any examples.</p>

<p>I would like to be able to set my variables manually (example cin>>) and have the lat and lon output into a variable that I can save to a text file. I am able to do everything myself (ingesting the starting variables and writing the result to a text file) except the actual conversion itself.</p>

<p>How could I get started with this using the code below? </p>

<p>My example variables are :</p>

<pre><code>Original Latitude = 29.4214

Original Longitude = -98.0142

Azimuth from Origin = 271 degrees 

Range from Origin =   6 nautical miles (I can convert to meters if needed, 
in this case it's 11112 meters)
</code></pre>

<p>The actual unedited code is below and <a href="https://ideone.com/9yuONO" rel="nofollow noreferrer"> a copy at this link.</a> If I get help with this I won't just copy/paste and I will come back with the completed code after I make it. I really am wanting to understand as I go, so I can be better with these advanced topics and not be constrained in the future. Code below :</p>

<pre><code>#include&lt;iostream&gt;
#include&lt;iomanip&gt;
#include&lt;cmath&gt;


// Source: // http://w...content-available-to-author-only...o.uk/scripts/latlong.html

static const double PI = 3.14159265358979323846, earthDiameterMeters = 6371.0 * 2 * 1000;

double degreeToRadian (const double degree) { return (degree * PI / 180); };
double radianToDegree (const double radian) { return (radian * 180 / PI); };

double CoordinatesToAngle (double latitude1,
                           const double longitude1,
                           double latitude2,
                           const double longitude2)
{
  const auto longitudeDifference = degreeToRadian(longitude2 - longitude1);
  latitude1 = degreeToRadian(latitude1);
  latitude2 = degreeToRadian(latitude2);

  using namespace std;
  const auto x = (cos(latitude1) * sin(latitude2)) -
                 (sin(latitude1) * cos(latitude2) * cos(longitudeDifference));
  const auto y = sin(longitudeDifference) * cos(latitude2);

  const auto degree = radianToDegree(atan2(y, x));
  return (degree &gt;= 0)? degree : (degree + 360);
}

double CoordinatesToMeters (double latitude1,
                            double longitude1,
                            double latitude2,
                            double longitude2)
{
  latitude1 = degreeToRadian(latitude1);
  longitude1 = degreeToRadian(longitude1);
  latitude2 = degreeToRadian(latitude2);
  longitude2 = degreeToRadian(longitude2);

  using namespace std;
  auto x = sin((latitude2 - latitude1) / 2), y = sin((longitude2 - longitude1) / 2);
#if 1
  return earthDiameterMeters * asin(sqrt((x * x) + (cos(latitude1) * cos(latitude2) * y * y)));
#else
  auto value = (x * x) + (cos(latitude1) * cos(latitude2) * y * y);
  return earthDiameterMeters * atan2(sqrt(value), sqrt(1 - value));
#endif
}

std::pair&lt;double,double&gt; CoordinateToCoordinate (double latitude,
                                                 double longitude,
                                                 double angle,
                                                 double meters)
{
  latitude = degreeToRadian(latitude);
  longitude = degreeToRadian(longitude);
  angle = degreeToRadian(angle);
  meters *= 2 / earthDiameterMeters;

  using namespace std;
  pair&lt;double,double&gt; coordinate;

  coordinate.first = asin((sin(latitude) * cos(meters))
                        + (cos(latitude) * sin(meters) * cos(angle)));
  coordinate.second = longitude + atan2((sin(angle) * sin(meters) * cos(latitude)),
                                        cos(meters) - (sin(latitude) * sin(coordinate.first)));

  coordinate.first = radianToDegree(coordinate.first);
  coordinate.second = radianToDegree(coordinate.second);

  return coordinate;
}

int main ()
{
  using namespace std;
  const auto latitude1 = 12.968460, longitude1 = 77.641308,
             latitude2 = 12.967862, longitude2 = 77.653130;

  cout &lt;&lt; std::setprecision(10);
  cout &lt;&lt; "(" &lt;&lt; latitude1 &lt;&lt; "," &lt;&lt; longitude1 &lt;&lt; ") --- "
          "(" &lt;&lt; latitude2 &lt;&lt; "," &lt;&lt; longitude2 &lt;&lt; ")\n";

  auto angle = CoordinatesToAngle(latitude1, longitude1, latitude2, longitude2);
  cout &lt;&lt; "Angle =  " &lt;&lt; angle &lt;&lt; endl;

  auto meters = CoordinatesToMeters(latitude1, longitude1, latitude2, longitude2);
  cout &lt;&lt; "Meters = " &lt;&lt; meters &lt;&lt; endl;

  auto coordinate = CoordinateToCoordinate(latitude1, longitude1, angle, meters);
  cout &lt;&lt; "Destination = (" &lt;&lt; coordinate.first &lt;&lt; "," &lt;&lt; coordinate.second &lt;&lt; ")\n";
}
</code></pre>
