[英]Finding the spatial length of a list of coordinates
I have a list of spatial x and y coordinates that make up a line in space (see picture). 我有一个组成空间中线的空间x和y坐标的列表(请参见图片)。 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. 坐标是有序的,这意味着第一个x和y坐标是线的一端的坐标,最后一个x和y坐标是线的另一端的坐标。 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()
using numpy in this case seem the best option: 在这种情况下使用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 allows you to manipulate the arrays of data with more ease, and has the added bonus of being extremely fast for big datasets. numpy使您可以更轻松地操作数据数组,并具有对大型数据集极其快速的额外好处。
zip
to get (x, y)
pairs and add them all up consecutively 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
The distance between two point, let's say A(x0,y0)
and B(x1,y1)
, is sqrt((x0-x1)**2 + (y0-y1)**2)
. 假设A(x0,y0)
和B(x1,y1)
之间的两点之间的距离是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
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. 对以下所有点重复(x3-x2)和(y3-y2)等,然后将所有距离相加。
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: 我不会编写整个函数的代码,因为这不是SO的目标,但是我将推荐两件事:
This should set you in the right direction. 这应该为您设定正确的方向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.