我有一个x,y,z点列表。使用公式找出3-D中两点之间的距离
import math
import numpy as np
point0 = x0, y0, z0
point1 = x1, y1, z1
dist = math.sqrt((x0-x1)**2+(y0-y1)**2+(z0-z1)**2)
def dist3d((x0, y0, z0), (x1, y1, z1)):
return math.sqrt((x0-x1)**2+(y0-y1)**2+(z0-z1)**2)
我希望编写一个优化的循环并存储距离
points = [(472765.09,6191522.78,13.0),(472764.82,6191524.09,9.0),(472763.8,6191525.68,8.0),(472764.07,6191524.39,16.0)]
dist01 = dist3d(test[0],test[1])
dist02 = dist3d(test[0],test[2])
dist03 = dist3d(test[0],test[2])
dist04 = dist3d(test[0],test[2])
dist12 = dist3d(test[1],test[2])
dist13 = dist3d(test[1],test[3])
dist23 = dist3d(test[2],test[3])
3d_l=[(dist01),(dist02),(dist03),(dist04),(dist12),(dist13),(dist23)]
3d_max =max(3d_l)
3d_min = min(3d_l)
3d_mean = np.average(3d_l)
我写了以下函数(它没有优化)
def dist3d((x0, y0, z0), (x1, y1, z1)):
return math.sqrt((x0-x1)**2+(y0-y1)**2+(z0-z1)**2)
def dist_3d(obs):
dist_list = list()
while len(obs) != 1:
obs_g = [(obs[0], x) for x in obs[1:]]
dist_list.append([dist3d(obs_g[i][0], obs_g[i][1]) for i in xrange(len(obs_g))])
obs.pop(0)
return dist_list
points = [(472765.09, 6191522.78, 13.0), (472764.82, 6191524.09, 9.0), (472763.8, 6191525.68, 8.0), (472764.07, 6191524.39, 16.0)]
print dist_3d(points)
[[4.217700795331081, 5.922339064664832, 3.554222840244929], [2.1374049685457694, 7.046453008421205], [8.107835716151763]]
答案 0 :(得分:2)
如果你不介意使用scipy,这是相当简单的:
import numpy as np
import scipy.spatial.distance as distance
points = np.array([(472765.09, 6191522.78, 13.0), (472764.82, 6191524.09, 9.0), (472763.8, 6191525.68, 8.0), (472764.07, 6191524.39, 16.0)])
dist = distance.pdist(points)
print dist.max()
print dist.min()
print np.median(dist)
print np.average(dist)
答案 1 :(得分:1)
这是一个尽可能使用内置和模块功能的通用版本。我没有安装numpy
,但如果它中有3-D或n-D距离函数,请使用下面的dist3D()
代替numpy
。
实际上,import math
import numpy as np
points = [(472765.09, 6191522.78, 13.0), (472764.82, 6191524.09, 9.0),
(472763.8, 6191525.68, 8.0), (472764.07, 6191524.39, 16.0)]
points += [points[0]] # dup first point to include dist from last to first
dist3D = lambda a, b: math.sqrt((a[0]-b[0])**2 + (a[1]-b[1])**2 + (a[2]-b[2])**2)
dists = sorted(dist3D(points[i], points[i+1]) for i in xrange(len(points)-1))
min_dist, max_dist = dists[0], dists[-1]
#mean_dist = sum(dists) / len(dists)
mean_dist = np.average(dists)
print 'min_dist: {:.2f}, mean_dist: {:.2f}, max_dist: {:.2f}'.format(
min_dist, mean_dist, max_dist)
包含几个(其他)函数,可用于加速其中一些立即计算。如果您正在寻找更多基于它的答案,您应该通过至少修改问题的标签来表明这一点。
{{1}}
答案 2 :(得分:-1)
math.sqrt
是一项相对繁重的操作。您可以存储距离的平方,这足以找到最小值,中值和最大值,然后得到平方根。此外,尝试手动单次迭代查找max和min。