[英]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.