Latitude and longitude to calculate the distance

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

HarvenSin namespace
{
class Program
{
/// <Summary>
/// latitude and longitude, calculates the distance between the two points.
/// </ Summary>
/// <param name = "args"> </ param>
static void the Main (String [] args)
{
//39.94607,116.32793 31.24063,121.42575
Console.WriteLine (Distance (39.94607, 116.32793, 31.24063, 121.42575));

}


public static double HaverSin(double theta)
{
var v = Math.Sin(theta / 2);
return v * v;
}


static double EARTH_RADIUS = 6371.0; // km average radius of the Earth, one thousand meters

/// <Summary>
/// given longitude 1, latitude 1; 2 longitude, latitude 2 calculates the distance between two latitude and longitude.
/// </ Summary>
/// <param name = "LAT1"> longitude. 1 </ param>
/// <param name = "lon1"> Lat. 1 </ param>
/// <param name = "LAT2 "> longitude 2 </ param>
/// <param name =" lon2 "> latitude 2 </ param>
/// <Returns> (km, kilometers) </ Returns>
public static Double distance (Double LAT1, lon1 Double, Double LAT2, Double lon2)
{
// calculate the distance between two points using spherical haversine formula.
// Coordinates converted to radians
LAT1 = ConvertDegreesToRadians (LAT1);
lon1 = ConvertDegreesToRadians (lon1);
LAT2 = ConvertDegreesToRadians (LAT2);
lon2 = ConvertDegreesToRadians (lon2);

// difference
var = vLon the Math.Abs (lon1 - lon2);
var = VLAT the Math.Abs (LAT1 - LAT2);

// h is the great circle distance in radians, great circle is a section on the sphere, its center that is a center of the sphere of the largest circumference of a circle.
var h = HaverSin (vLat) + Math.Cos (lat1) * Math.Cos (lat2) * HaverSin (vLon);

var distance = 2 * EARTH_RADIUS * Math.Asin(Math.Sqrt(h));

return distance;
}

/// <Summary>
/// terms of the angle in radians.
/// </ Summary>
/// <param name = "degrees"> angle </ param>
/// <Returns> rad </ Returns>
public static Double ConvertDegreesToRadians (Double degrees)
{
return Math.PI degrees * / 180;
}

public static double ConvertRadiansToDegrees(double radian)
{
return radian * 180.0 / Math.PI;
}

}
}

Quote

https://www.cnblogs.com/zhoug2020/p/8993750.html

Guess you like

Origin www.cnblogs.com/ilookbo/p/11265580.html