algorithm find how long each segment of a path will take at a constant speed

James 21 Reputation points
2020-12-04T02:37:58.55+00:00

Hey guys!
so I have a little issue I am trying to solve.
my concept has a picture box where you can add points to a list List<pointf>, and it draws the lines as a path on the picturebox.
however now I need to work out for an imaginary object navigating the path at a set speed, and a set time to complete the path, how long would each path segment take to complete at that speed.

so example:
Travelduration = 1000;
speed = 0.5f;
Points<>
{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}

Path shown on image
44955-points1.png

from that I would want a returned list of numbers that told me how many frames each path segment would take.

Thanks so much guys!
Regards
James

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,903 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,011 questions
0 comments No comments
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,626 Reputation points
    2020-12-04T07:56:05.833+00:00

    Hi James-1709,
    You can use the Math.Sqrt function to calculate the distance of each segment according to the coordinates, and then calculate the time of each segment with the known speed.
    Here is a simple code example:

    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);  
    }  
    

    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.