I need to find the 2D distance between two stations. 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:
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
Maybe you can write a extension method for Station class.
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));
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.