SoFunction
Updated on 2025-03-06

Summary of C# method to calculate the direct distance between two coordinate points

When calculating the distance between two coordinate points in C#, the choice of the method depends on the type of the coordinate system and the specific situation you need to deal with. The following are some calculation methods in common scenarios:

1. The distance between two points in a plane rectangular coordinate system

In a two-dimensional plane rectangular coordinate system, given two points A(x1, y1) and B(x2, y2), the Euclidean distance between them can be calculated by Pythagorean theorem:

public static double CalculateDistance(Point p1, Point p2)
{
    double dx =  - ;
    double dy =  - ;
    return (dx * dx + dy * dy);
}

or

public static double DistanceBetweenPoints(double x1, double y1, double x2, double y2)
{
    double dx = x2 - x1;
    double dy = y2 - y1;
    return (dx * dx + dy * dy);
}

2. The distance between two points on the earth's surface (latitude and longitude coordinates)

For geographical locations on the earth, that is, latitude and longitude coordinates, spherical geometry or an approximate ellipsoid model is usually used to calculate the distance between two points. The most commonly used algorithm is the Haversine formula, which can accurately calculate the shortest distance (large circle distance) between any two points on the earth. Here is a C# implementation that uses the Haversine formula to calculate distance:

public static double CalculateDistanceInKilometers(double lat1, double lon1, double lat2, double lon2)
{
    const double earthRadiusKm = 6371.0;
 
    // Turn angle into radians    double dLat = ToRadians(lat2 - lat1);
    double dLon = ToRadians(lon2 - lon1);
 
    lat1 = ToRadians(lat1);
    lat2 = ToRadians(lat2);
 
    double a = (dLat / 2) * (dLat / 2) +
              (dLon / 2) * (dLon / 2) *
              (lat1) * (lat2);
    double c = 2 * Math.Atan2((a), (1 - a));
 
    return earthRadiusKm * c;
}
 
private static double ToRadians(double degrees)
{
    return degrees *  / 180;
}

3. The distance between two points in three-dimensional space

In three-dimensional space, the distance between point A (x1, y1, z1) and point B (x2, y2, z2) is calculated similar to a two-dimensional situation. Just square the difference values ​​of the three-dimensional coordinate components and then square:

public static double DistanceIn3DSpace(double x1, double y1, double z1, double x2, double y2, double z2)
{
    double dx = x2 - x1;
    double dy = y2 - y1;
    double dz = z2 - z1;
    return (dx * dx + dy * dy + dz * dz);
}

According to actual needs, select the corresponding method to calculate the distance between coordinate points. If you need to deal with the latitude and longitude coordinates of the earth's surface, use the second method (Haversine formula). If it is a plane rectangular coordinate or a three-dimensional spatial coordinate, the first or third method is used respectively.

4. Knowledge supplement

C# calculates the distance between two latitudes and longitudes

//The radius of the earth, unit meter       private const double EARTH_RADIUS = 6378137;
       /// <summary>
       /// Calculate the distance between the two points and return the distance between the two points, unit meter       /// This formula is provided for GOOGLE, with an error of less than 0.2 meters       /// </summary>
       /// <param name="lat1">Latitude of the first point</param>       /// <param name="lng1">First point longitude</param>       /// <param name="lat2">Second point latitude</param>       /// <param name="lng2">Second point longitude</param>       /// &lt;returns&gt;&lt;/returns&gt;
       public static double GetDistance(double lat1, double lng1, double lat2, double lng2)
       {
           double radLat1 = Rad(lat1);
           double radLng1 = Rad(lng1);
           double radLat2 = Rad(lat2);
           double radLng2 = Rad(lng2);
           double a = radLat1 - radLat2;
           double b = radLng1 - radLng2;
           double result = 2 * ((((a / 2), 2) + (radLat1) * (radLat2) * ((b / 2), 2))) * EARTH_RADIUS;
           return result;
       }
 
       /// &lt;summary&gt;
       /// Convert latitude and longitude into arc       /// &lt;/summary&gt;
       /// &lt;param name="d"&gt;&lt;/param&gt;
       /// &lt;returns&gt;&lt;/returns&gt;
       private static double Rad(double d)
       {
           return (double)d *  / 180d;
       }

c# Calculate the coordinate distance of two points

namespace Experiment 3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            double x1 = ( );
            double y1 = ();
            double x2 = ();
            double y2 = ();
            Class1 s1 = new Class1(x1, y1, x2, y2);
             = ("{0}", ());
        }
       ......
     }
}
class Class1
{
    private double x1, y1, x2, y2;
    public double X1
    {get{return this.x1;}}
    public double Y1
    {get{return this.y1;}}
    public double X2
    {get{return this.x2;}}
    public double Y2
    {get{return this.y2;}}
    public Class1(double x1,double y1,double x2,double y2)
    {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
    }
     public double point()
    {
        return ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
    }
}

This is the article about C#’s method of calculating the direct distance between two coordinate points. For more related content on C#’s calculation of two coordinate distances, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!