[英]How to calculate euclidean distance between two stations
I need to find the 2D distance between two stations. 我需要找到两个站之间的2D距离。 I need to create a station class that can be used to store a list of stations as objects, such as this: 我需要创建一个可用于存储工作站列表作为对象的工作站类,例如:
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));
}
I need to create a distance method that will calculate the distance between these two stations by running it like this: 我需要创建一个距离方法,通过运行它来计算这两个站之间的距离:
Console.WriteLine(Distance.euDistance(station[0], station[1]));
I have tried to create a method to calculate the euclidean distance but can't get it to successfully calculate the distance between the two stations. 我试图创建一种计算欧氏距离的方法,但无法成功计算两个站之间的距离。 This is what I have created for my Distance method: 这是我为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);
}
}
I want to have it print a result such as this: (that is just an example) 我想让它打印出这样的结果:(这只是一个例子)
Console.WriteLine("{0} -> {1} {2} meters, Name[0], Name[1], distance);
po -> wsx 56.6505106 meters po - > wsx 56.6505106米
Maybe you can write a extension method for Station class. 也许你可以为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.