Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当通过同一条线上的多个点时,如何计算距离?_C#_Visual Studio_Time_Distance - Fatal编程技术网

C# 当通过同一条线上的多个点时,如何计算距离?

C# 当通过同一条线上的多个点时,如何计算距离?,c#,visual-studio,time,distance,C#,Visual Studio,Time,Distance,行程时间不是计算最短路径,而是计算同一条铁路线上通过多个车站的行程 public double TravelTimeOnSameLine(IStation from, IStation to, int speed) { //check values if (from == to || from == null || to == null || speed <= 0) throw new ArgumentException(); var common

行程时间不是计算最短路径,而是计算同一条铁路线上通过多个车站的行程

public double TravelTimeOnSameLine(IStation from, IStation to, int speed)
{
    //check values
    if (from == to || from == null || to == null || speed <= 0)
        throw new ArgumentException();

    var commonLines = from.Lines.Intersect(to.Lines);
    if (!commonLines.Any())
    {
        throw new ArgumentException();
    }

    var line = commonLines.First();
    var distance = 0.0;

    //how to fimd the path?
    var path = new List<IStation>();
    var current = from;
    path.Add(from);


    while (current != to)
    {
        current = line.Next(current);
        path.Add(current);

    }

    for (int i = 0; i < path.Count; i++)
    {
        IStation firstStation = null;
        IStation secondStation = null;

        firstStation = from;
        secondStation = to;

        //calcul de distance entre deux points;
        var distanceBetween1and2 = GetDistancebtween(firstStation.X, firstStation.Y, secondStation.X, secondStation.Y);
        distance = distance + distanceBetween1and2;

    }

    var travelTime = distance / speed;
    return travelTime;
}
public void T2_travel_time_on_simple_stations()
{
    ICity c = CityFactory.CreateCity("Paris");

    IStation s = c.AddStation("Opera", 0, 0);
    IStation s1 = c.AddStation("Chatelet", 10, 0);

    ILine l = c.AddLine("RER A");
    l.AddBefore(s);
    l.AddBefore(s1);

    c.TravelTimeOnSameLine( s, s1, 1).Should().Be(10.0);
    c.TravelTimeOnSameLine( s, s1, 10).Should().Be(1.0);
    c.TravelTimeOnSameLine( s, s1, 100).Should().Be(0.1);
    c.TravelTimeOnSameLine( s, s1, 20).Should().Be(0.5);
    c.TravelTimeOnSameLine( s, s1, 20).Should().Be(c.TravelTimeOnSameLine( s1, s, 20));
}
[Test]
public void T3_travel_time_on_more_stations()
{
    ICity city = CityFactory.CreateCity("Paris");

    IStation a = city.AddStation("A", 0, 0);
    IStation b = city.AddStation("B", 0, 10);
    IStation c = city.AddStation("C", 10, 10);
    IStation d = city.AddStation("D", 10, 0);

    ILine l1 = city.AddLine("ligne 1");

    l1.AddBefore(a);
    l1.AddBefore(b);
    l1.AddBefore(c);
    l1.AddBefore(d);

    city.TravelTimeOnSameLine( a, d, 1).Should().Be(30.0);
}

错误消息预期值为10.0,但发现值为20.0。

所以您预期值为30.0。。。我假设你的失败值为10.0?假设知道它需要通过
b
c
计算从
a
d
的距离,那么
TravelTimeOnSameLine
是如何实现的呢?我就是这样失败的。距离校准中的第一个错误(A&B、B&C、C&D)。我正在使用“查找要访问的站点”旁边的功能<代码>公共IStation Next(IStation s){if(!_stations.Contains(s)){throw new ArgumentException()}var index=_stations.IndexOf(s);var isLast=index=_stations.Count-1;if(isLast){return null;}return u stations[index+1];}然后我尝试计算两个站点之间的距离,然后计算所有距离(对于我们经过两个以上站点的情况)和持续时间的总和。请发布
GetDistancebtween
。private double GetDistancebtween(intx1,inty1,intx2,inty2){返回Math.Sqrt(Math.Pow((x1-x2),2)+Math.Pow((y1-y2),2));}我有点犹豫在没有看到其余代码的情况下发布这篇文章……这可能不会有什么帮助……但我看到的是……您正在使用Intersect方法来获取
a
d
的共有行,但是它们没有共有行,也许这没关系……另外,在
foreach
中,您正在返回一个值在第一次迭代中使用e,因此它不会实际循环通过多个
IStation
。您可能需要类似
distance+=GetDistancebtween(x1:from.X,y1:from.Y,x2:finalStation.X,y2:finalStation.Y)的内容;
并修复其余代码。