Reputation: 2227
I have a list of spatial x and y coordinates that make up a line in space (see picture). The coordinates are ordered which means that the first x and y coordinates are the coordinates of one end of the line and the last x and y coordinates are the coordinates of the opposite end of the line. I want to find the total length of the line.
xcoordinates = [-95.10786437988281, -94.80496215820312, -94.5020751953125, -94.19918060302734, -93.89629364013672, -93.59339904785156, -93.29051208496094, -92.98760986328125, -92.68472290039062, -92.3818359375, -92.07894134521484, -91.77605438232422, -91.47315979003906, -91.17027282714844, -90.86737823486328, -90.56449127197266, -90.2615966796875, -89.95870971679688, -89.65582275390625, -89.35292053222656, -89.05003356933594, -88.74713897705078, -88.44425201416016, -88.141357421875, -87.83847045898438, -87.53556823730469, -87.23268127441406, -86.9297866821289, -86.62689971923828, -86.32401275634766, -86.0211181640625, -85.71823120117188, -85.41533660888672, -85.1124496459961, -84.80955505371094, -84.50666809082031, -84.20376586914062, -83.90087890625, -83.59799194335938, -83.29509735107422, -82.9922103881836, -82.68931579589844, -82.38642883300781, -82.08352661132812, -81.7806396484375, -81.47774505615234, -81.17485809326172, -80.87196350097656, -80.56907653808594, -80.26618957519531, -79.96329498291016, -79.660400390625, -79.35751342773438, -79.05461883544922, -78.7517318725586, -78.44883728027344, -78.14595031738281, -77.84305572509766, -77.5401611328125, -77.23727416992188, -76.93437957763672, -76.63148498535156, -76.32859802246094, -76.02570343017578, -75.72281646728516, -75.41992950439453, -75.11703491210938, -74.81414031982422, -74.5112533569336, -74.20835876464844, -73.90547180175781, -73.60257720947266]
ycoordinates = [-22.71684455871582, -22.413955688476562, -22.413955688476562, -22.11106300354004, -22.11106300354004, -22.11106300354004, -21.808170318603516, -21.808170318603516, -21.808170318603516, -21.808170318603516, -21.808170318603516, -21.808170318603516, -21.808170318603516, -22.11106300354004, -22.11106300354004, -22.11106300354004, -22.11106300354004, -22.11106300354004, -22.413955688476562, -22.413955688476562, -22.413955688476562, -22.71684455871582, -22.71684455871582, -23.01973533630371, -23.01973533630371, -23.01973533630371, -23.322628021240234, -23.322628021240234, -23.625518798828125, -23.92841148376465, -24.231300354003906, -24.231300354003906, -24.534191131591797, -24.83708381652832, -24.83708381652832, -25.139976501464844, -25.139976501464844, -25.442867279052734, -25.442867279052734, -25.745756149291992, -25.745756149291992, -26.048648834228516, -26.048648834228516, -26.048648834228516, -26.351539611816406, -26.351539611816406, -26.351539611816406, -26.65443229675293, -26.65443229675293, -26.65443229675293, -26.65443229675293, -26.65443229675293, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082]
plt.plot(xcoordinates,ycoordinates)
plt.show()
Upvotes: 0
Views: 1684
Reputation: 6095
using numpy in this case seem the best option:
x = np.array(xcoordinates)
y = np.array(ycoordinates)
dist_array = (x[:-1]-x[1:])**2 + (y[:-1]-y[1:])**2
np.sum(np.sqrt(dist_array))
#24.0145246
numpy allows you to manipulate the arrays of data with more ease, and has the added bonus of being extremely fast for big datasets.
Upvotes: 2
Reputation: 191874
zip
to get (x, y)
pairs and add them all up consecutively
import math
def getDistance(x1, x2, y1, y2):
diffX = abs(x2 - x1)
diffY = abs(y2 - y1)
return math.hypot(diffX, diffY) # a^2 + b^2
coords = zip(xcoordinates, ycoordinates)
dist = 0
for i in range(len(coords)-1):
x1, y1 = coords[i]
x2, y2 = coords[i+1]
dist += getDistance(x1, x2, y1, y2)
print dist # 24.0145246229
Upvotes: 1
Reputation: 665
The distance between two point, let's say A(x0,y0)
and B(x1,y1)
, is sqrt((x0-x1)**2 + (y0-y1)**2)
. try this:
from math import sqrt
xcoordinates = [-95.10786437988281, -94.80496215820312, -94.5020751953125, -94.19918060302734, -93.89629364013672, -93.59339904785156, -93.29051208496094, -92.98760986328125, -92.68472290039062, -92.3818359375, -92.07894134521484, -91.77605438232422, -91.47315979003906, -91.17027282714844, -90.86737823486328, -90.56449127197266, -90.2615966796875, -89.95870971679688, -89.65582275390625, -89.35292053222656, -89.05003356933594, -88.74713897705078, -88.44425201416016, -88.141357421875, -87.83847045898438, -87.53556823730469, -87.23268127441406, -86.9297866821289, -86.62689971923828, -86.32401275634766, -86.0211181640625, -85.71823120117188, -85.41533660888672, -85.1124496459961, -84.80955505371094, -84.50666809082031, -84.20376586914062, -83.90087890625, -83.59799194335938, -83.29509735107422, -82.9922103881836, -82.68931579589844, -82.38642883300781, -82.08352661132812, -81.7806396484375, -81.47774505615234, -81.17485809326172, -80.87196350097656, -80.56907653808594, -80.26618957519531, -79.96329498291016, -79.660400390625, -79.35751342773438, -79.05461883544922, -78.7517318725586, -78.44883728027344, -78.14595031738281, -77.84305572509766, -77.5401611328125, -77.23727416992188, -76.93437957763672, -76.63148498535156, -76.32859802246094, -76.02570343017578, -75.72281646728516, -75.41992950439453, -75.11703491210938, -74.81414031982422, -74.5112533569336, -74.20835876464844, -73.90547180175781, -73.60257720947266]
ycoordinates = [-22.71684455871582, -22.413955688476562, -22.413955688476562, -22.11106300354004, -22.11106300354004, -22.11106300354004, -21.808170318603516, -21.808170318603516, -21.808170318603516, -21.808170318603516, -21.808170318603516, -21.808170318603516, -21.808170318603516, -22.11106300354004, -22.11106300354004, -22.11106300354004, -22.11106300354004, -22.11106300354004, -22.413955688476562, -22.413955688476562, -22.413955688476562, -22.71684455871582, -22.71684455871582, -23.01973533630371, -23.01973533630371, -23.01973533630371, -23.322628021240234, -23.322628021240234, -23.625518798828125, -23.92841148376465, -24.231300354003906, -24.231300354003906, -24.534191131591797, -24.83708381652832, -24.83708381652832, -25.139976501464844, -25.139976501464844, -25.442867279052734, -25.442867279052734, -25.745756149291992, -25.745756149291992, -26.048648834228516, -26.048648834228516, -26.048648834228516, -26.351539611816406, -26.351539611816406, -26.351539611816406, -26.65443229675293, -26.65443229675293, -26.65443229675293, -26.65443229675293, -26.65443229675293, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082, -26.95732307434082]
sum_dist = 0
for i in range(len(xcoordinates) - 1):
sum_dist += sqrt((xcoordinates[i+1] - xcoordinates[i])**2 + (ycoordinates[i+1] - ycoordinates[i])**2)
print sum_dist #24.0145
Upvotes: 1
Reputation: 5948
This sounds more like a Math problem but basically you need to find the distance between every pair of points in the chart and sum them up.
I won't code the entire function as this is not SO's goal, but I'll recommend 2 things:
This should set you in the right direction.
Upvotes: 0
Reputation: 13158
The total length of the line would be the sum of all the distances between consecutive points. The distance from the first point to the second point would be:
sqrt((x2-x1)**2 + (y2-y1)**2)
Repeat for all the following points, (x3-x2) and (y3-y2), etc., and add up all of the distances.
Upvotes: 0