算法:找出路径的每一段在恒定速度下需要多长时间

Jiale Xue - MSFT 36,471 信誉分 Microsoft 供应商
2024-03-25T07:46:05.5466667+00:00

嘿,伙计们! 所以我有一个小问题,我正在努力解决。 我的概念有一个图片框,您可以在其中向列表 List<pointf> 添加点,并在图片框上绘制线条作为路径。 但是现在我需要计算出一个假想的物体以设定的速度在路径上导航,以及完成路径的设定时间,每个路径段需要多长时间才能以该速度完成。

例如: Travelduration = 1000; 速度 = 0.5F;

点数<>

{X = 66 y = 46}

{x = 57 y = 115}

{x = 62 y = 144}

{x = 74 y = 158}

{x = 167 y = 64}

{x = 177 y = 92}

{x = 169 y = 101}

{x = 171 y = 112}

{x = 193 y = 111}

{x = 229 y = 24}

图像上显示的路径

44955-points1.png

从那里,我想要一个返回的数字列表,告诉我每个路径段将需要多少帧。

Note:此问题总结整理于:algorithm find how long each segment of a path will take at a constant speed

Windows 窗体
Windows 窗体
一组用于开发图形用户界面的 .NET Framework 托管库。
99 个问题
C#
C#
一种面向对象的类型安全的编程语言,它起源于 C 语言系列,包括对面向组件的编程的支持。
138 个问题
0 个注释 无注释
{count} 票

接受的答案
  1. Hui Liu-MSFT 41,256 信誉分 Microsoft 供应商
    2024-03-25T09:31:47.6733333+00:00

    您可以使用 Math.Sqrt 函数根据坐标计算每个路段的距离,然后用已知的速度计算每个路段的时间。 下面是一个简单的代码示例:

    List<float> time = new List<float>();  
    List<Point> pointsOfList = new List<Point>();  
    private void Form1_Load(object sender, EventArgs e)  
    {  
        pointsOfList.Add(new Point(1, 2));  
        pointsOfList.Add(new Point(3, 4));  
        pointsOfList.Add(new Point(5, 6));  
        pointsOfList.Add(new Point(7, 8));  
        pointsOfList.Add(new Point(9, 10));  
        for (int i=0;i<pointsOfList.Count-1;i++)   
        {  
            var t = GetTime(i, i+1);  
            time.Add(t);    
        }  
    }  
    public float GetTime(int startIndex, int endIndex)  
    {  
        float speed = 0.5f;  
        float S1 = GetDistance(pointsOfList[startIndex], pointsOfList[endIndex]);  
        float t1 = S1 / speed;  
        return t1;  
    }  
    public static float GetDistance(Point startPoint, Point endPoint)  
    {  
        int x = System.Math.Abs(endPoint.X - startPoint.X);  
        int y = System.Math.Abs(endPoint.Y - startPoint.Y);  
        return (float)Math.Sqrt(x * x + y * y);  
    }  
    
    
    

    如果回复有帮助,请点击“接受答案”并点赞。 注意:如果您想接收此线程的相关电子邮件通知,请按照我们文档中的步骤启用电子邮件通知。

    1 个人认为此答案很有帮助。
    0 个注释 无注释

0 个其他答案

排序依据: 非常有帮助