Movement/Trajectory generation in Python

Viewed 107

i m searching for something (library,function ecc..) in Python to generate a random discrete trajectory in the 2-D space.

For example: by providing the dimensions lenght and width of the plane and a starting point (x,y) i need to generate a sequence of points that represent the movement of an object (E.G. a human walking) over a random path.

Are you aware of any such library or tool that helps accomplishing this?

I have tried searching for something like that, but without success, I was searching for a shortcut/an easy to implement method

2 Answers

I don’t know any tools that can make that, however you can easily make a function that generate randoms points on a plane (representing a path). If you don’t want points to be too far away from the previous one, you generate a random point in a specific area around the point.

Assuming the trajectory is linear, the implimentation isn't very complicated. All we need is the point slope form of a line.

The formula is y-y1=m(x-x1). Solved for y, this turns to y=m(x-x1)+y1.

In terms of python code, it will look like this:

def calculate_pos(start: tuple[int, int], velocity: float, time: float):
    return velocity * (start[0] - time) + start[1]

We can then easily randomize this by multiplying the result by some sort of factor. A small degree of randomness might look like this:


def calculate_pos(start: tuple[int, int], velocity: float, time: float):
    return (velocity * (start[0] - time) + start[1]) * (random.randint(900, 1100) / 1000)
Related