static double polarRadius = 6356752.314245D; // Semi-minor axis b #endregion Fields #region Methods // flat earth approximation public static void GetCourseAndDistance(wptType pt1, wptType pt2, ref double course, ref double dist) { // convert latitude and longitude to radians double lat1 = DegreesToRadians((double)pt1.lat); double lon1 = DegreesToRadians((double)pt1.lon); double lat2 = DegreesToRadians((double)pt2.lat); double lon2 = DegreesToRadians((double)pt2.lon); // compute latitude and longitude differences double dlat = lat2 - lat1; double dlon = lon2 - lon1; double distanceNorth = dlat; double distanceEast = dlon * Math.Cos(lat1); // compute the distance in radians dist = Math.Sqrt(distanceNorth * distanceNorth + distanceEast * distanceEast); // and convert the radians to meters dist = RadiansToMeters(dist); // add the elevation difference to the calculation double dele = (double)pt2.ele - (double)pt1.ele; dist = Math.Sqrt(dist * dist + dele * dele); // compute the course course = Math.Atan2(distanceEast, distanceNorth) % (2 * Math.PI); course = RadiansToDegrees(course); if (course < 0) course += 360; }
/// <summary> /// Distance between two WayPoints from GPX /// </summary> /// <param name="wpt1"></param> /// <param name="wpt2"></param> /// <returns></returns> public static double Haversine(wptType wpt1, wptType wpt2) { // convert latitude and longitude to radians double lat1 = DegreesToRadians((double)wpt1.lat); double lon1 = DegreesToRadians((double)wpt1.lon); double lat2 = DegreesToRadians((double)wpt2.lat); double lon2 = DegreesToRadians((double)wpt2.lon); return Haversine( lat1, lon1, lat2, lon2, (double)wpt2.ele, (double)wpt1.ele); }