求坐标列表的空间长度

2024-03-28 16:22:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个空间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


Tags: 列表plotshow空间plt直线有序总长度
3条回答

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

在这种情况下使用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允许您更轻松地操作数据数组,而且对于大数据集,它还具有非常快的额外好处。在

两点之间的距离,比如说A(x0,y0)和{},是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

相关问题 更多 >