stephenjbell

Total distance traveled in coordinate list

Oct 2nd, 2014
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. import math
  2.  
  3. def total_dist_x_y(coord_list):
  4.     """ Total distance traveled: X + Y """
  5.     total = 0
  6.     stop_at = len(coord_list) - 1
  7.     for i in range(0,stop_at):
  8.         x1 = coord_list[i][0]
  9.         y1 = coord_list[i][1]
  10.         x2 = coord_list[i+1][0]
  11.         y2 = coord_list[i+1][1]
  12.         x_dist = abs(x1-x2)
  13.         y_dist = abs(y1-y2)
  14.         dist_traveled = x_dist + y_dist
  15.         total += dist_traveled
  16.     return total
  17.  
  18. def total_dist_diagonal(coord_list):
  19.     """ Total distance traveled between points """
  20.     total = 0
  21.     stop_at = len(coord_list) - 1
  22.     for i in range(0,stop_at):
  23.         x1 = coord_list[i][0]
  24.         y1 = coord_list[i][1]
  25.         x2 = coord_list[i+1][0]
  26.         y2 = coord_list[i+1][1]
  27.         x_dist = abs(x1-x2)
  28.         y_dist = abs(y1-y2)
  29.         dist_traveled = math.sqrt(x_dist**2 + y_dist**2)
  30.         total += dist_traveled
  31.     return total
Advertisement
Add Comment
Please, Sign In to add comment