繁体   English   中英

如何计算两个站之间的欧氏距离

[英]How to calculate euclidean distance between two stations

我需要找到两个站之间的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));
    }

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

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

我试图创建一种计算欧氏距离的方法,但无法成功计算两个站之间的距离。 这是我为Distance方法创建的:

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米

也许你可以为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));
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM