如何计算两个测站之间的欧几里得距离

时间:2019-05-22 10:54:45

标签: c#

我需要找到两个测站之间的2D距离。我需要创建一个站类,该站类可用于将站列表存储为对象,例如:

class Station
{
    public Station(string name, double X, double Y)
    {
         Name = name;
         xcor = X;
         ycor = Y;
    }

    public string Name {get; set;}
    public double xcor {get; set;}
    public double ycor {get; set;}
}

class Program

    public static void Main(string[] args)
    {
        public List<Station> Stationlist = new List<Station>();
        Stationlist.Add(new Station("po",1,1));
        Stationlist.Add(new Station("wsx",200,200));
    }

我需要创建一个distance方法,通过像这样运行它来计算这两个站之间的距离:

Console.WriteLine(Distance.euDistance(station[0], station[1]));

我试图创建一种方法来计算欧几里得距离,但无法成功地计算出两个测站之间的距离。这是我为“距离”方法创建的:

class Distance
{
  public static double distanceTEST(Station current, Station next)
  {
    return Math.Sqrt((Math.Pow((current.Station.X - next.Station.X), 2) + 
                      Math.Pow((current.Station.Y - next.Station.Y), 2) * 
                                              100000.0 / 100000.0) * 1);
  }
}

我想让它打印出这样的结果:(这只是一个例子)

Console.WriteLine("{0}   ->   {1}   {2} meters, Name[0], Name[1], distance);

po-> wsx 56.6505106米

1 个答案:

答案 0 :(得分:0)

也许您可以为Station类编写扩展方法。

    class Program
{
    static void Main(string[] args)
    {
        var station1 = new Station("po", -7, 17);
        var station2 = new Station("wsx", -4, 6.5);

        Console.WriteLine(station1.CalculateEuDistance(station2));

        Console.ReadKey();
    }
}
public class Station
{
    public string Name { get; set; }
    public double X { get; set; }
    public double Y { get; set; }

    public Station()
    {

    }
    public Station(string name, double x, double y)
    {
        Name = name;
        X = x;
        Y = y;
    }
}

public static class StationExtensions
{
    public static double CalculateEuDistance(this Station currentStation, Station station)
    {
        return Math.Sqrt(Math.Pow(currentStation.Y - currentStation.X,2) + Math.Pow(station.Y - station.X,2));
    }
}