简体   繁体   中英

Find the distance traveled from (x,y) coordinates

I currently have a python script that reads in a 3 column text file containing x and y coordinates for a walker and the time they have been walking.

I have read in this data and allocated it in numpy arrays as shown in the code below:

import numpy as np
import matplotlib.pyplot as plt

data = np.loadtxt("info.txt", delimiter = ',')

x = data[:,0]
y = data[:,1]
t = data[:,2]

File is following format (x,y,t):

5907364.2371    -447070.881709  2193094
5907338.306978  -447058.019176  2193116
5907317.260891  -447042.192668  2193130

I now want to find the distance traveled as a function of time by the walker. One way I can think of doing that is by summing the differences in x coordinates and all the differences in y coordinates in a loop. This seems a very long winded method however and I think it could be solved with a type of numerical integration. Does anyone have any ideas of what I could do?

To compute the distance "along the way", you must first obtain the distance of each step.

This can be obtained, component-wise, by the indexing dx = x[1:]-x[:-1] . The distance per step is then "square root of dx**2+dy**2" Note that the length of this array is less by one as there is one less interval with respect to the number of steps. This can be completed by assigning the distance "0" to the the first time data. This is the role of the "concatenate" line below.

There is no numerical integration here, but a cumulative sum. To perform numerical integration, you would need equations of motion (for instance).

Extra change: I use np.loadtxt with the unpack=True argument to save a few lines.

import numpy as np
import matplotlib.pyplot as plt

x, y, t = np.loadtxt("info.txt", unpack=True)

dx = x[1:]-x[:-1]
dy = y[1:]-y[:-1]

step_size = np.sqrt(dx**2+dy**2)

cumulative_distance = np.concatenate(([0], np.cumsum(step_size)))

plt.plot(t, cumulative_distance)

plt.show()

typically, to get distance walked, you sum up smaller distances. Your walker probably isn't walking on a grid (that is, a step in x and a step in y) but rather in diagonals (think Pythagorean theorem)

so, in python it might look like this...

distanceWalked = 0
for x_y_point in listOfPoints:
    distanceWalked = distanceWalked  + (x_y_point[0] **2 + x_y_point[1] **2)**.5

Where listOfPoints is something like [[0,0],[0,1],[0,2],[1,2],[2,2]]

Alternatively, you can use pandas.

import pandas as pd
df = pd.read_csv('info.txt',sep = '\t')
df['helpercol'] = (df['x']**2 +df['y']**2 )**.5
df['cumDist'] = df['helpercol'].cumsum()

now, you'll have cumulative distance per time in your dataframe

There are several ways to obtain the Euclidean Distance between the points:

Numpy:

import numpy as np
dist = np.linalg.norm(x-y)
dist1= np.sqrt(np.sum((x-y)**2)))

Scipy:

from scipy.spatial import distance
dist = distance.euclidean(x,y)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM