查找坐标列表的空间长度

时间:2016-03-16 18:35:57

标签: python matplotlib

我有一个空间x和y坐标列表,它们构成了空间中的一条线(见图)。坐标是有序的,这意味着第一个x和y坐标是线的一端的坐标,最后的x和y坐标是线的另一端的坐标。我想找到该行的总长度。

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()

picture of the line

5 个答案:

答案 0 :(得分:2)

在这种情况下使用numpy似乎是最好的选择:

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允许您更轻松地操作数据数组,并且对于大数据集具有极快的额外优势。

答案 1 :(得分:1)

zip获取(x, y)对,并将它们全部连续添加

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

答案 2 :(得分:1)

两点之间的距离,即A(x0,y0)B(x1,y1)sqrt((x0-x1)**2 + (y0-y1)**2)。试试这个:

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

答案 3 :(得分:0)

线的总长度是连续点之间所有距离的总和。从第一个点到第二个点的距离为:

sqrt((x2-x1)**2 + (y2-y1)**2)

重复以下所有点,(x3-x2)和(y3-y2)等,并将所有距离相加。

答案 4 :(得分:0)

这听起来更像是数学问题,但基本上你需要找到图表中每对点之间的距离并总结它们。

我不会对整个功能进行编码,因为这不是SO的目标,但我会推荐两件事:

  • 使用Python的zip函数压缩这两个坐标列表,之后你将得到一个(x,y)坐标元组列表
  • 使用Python的math.hypot来计算2点之间的距离

这应该让你朝着正确的方向前进。