如何在没有numpy的情况下计算距离

时间:2019-12-04 18:34:46

标签: python list 3d gps

 myList =  [[0, 0, 0], #0
       [6.888437030500963, 5.159088058806049, -1.5885683833831], #1
       [2.0667720363602307, 5.384582486178219, -3.4898856343748133], #2
       [7.742743817055683, 1.4508370077567676,-3.957946551327696],#3
       [9.410384606156306, 9.613094711663472, -3.864209434979891],#4
       [5.047141494150383, 14.72917879480795, -1.4968295014732576],#5
       [0.05726832139919402,22.924103914172754, 8.158880019279806],#6
       [6.261613041330982, 30.96742292296441,4.361831405666459], #7
       [10.858248006533554, 38.94418868232428, 8.041510043975286],#8
       [10.30110231558782, 30.958212843691598, 6.724946753050958],#9
       [12.518841784463852,39.21843390844956, 16.057074108466132]]#10

import math

def distance (myList):

    dist = math.sqrt ((xa-xb)**2 + (ya-yb)**2 + (za-zb)**2)
    return dist

print("Distance:",(distance(myList)))

如果没有NumPy,如何计算所有这些点的距离?我知道如何用2而不是超过2

2 个答案:

答案 0 :(得分:0)

protected function validator(array $data)
{
    $messages = [
        'company.regex' => 'Invalid format.'
    ];

    return Validator::make($data, [
        'name' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => ['required', 'string', 'min:8', 'confirmed'],
        'mobile_no' => ['required', 'string', 'min:10'],
        'company' => ['required', 'max:255', "regex:/\b((http|https):\/\/?)[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|\/?))/"],
        'username' => ['required', 'string', 'max:255', 'unique:users'],

    ],$messages);
}

您必须将每个结果附加到先前生成的列表中,否则将仅存储最后一个值

答案 1 :(得分:0)

我们可以用以下公式找到欧几里得距离: d = sqrt((px1-px2)^ 2 +(py1-py2)^ 2 +(pz1-pz2)^ 2)

在python中实现

import math

def dist(list):
  cummulativeDist = 0

  # iterate over sets of points
  for i in range(len(list) - 1):
    coordInit = list[i]
    coordFinal = list[i+1]

    # distance from one pt to the next
    dist = math.sqrt(((coordInit[0] - coordFinal[0]) ** 2) 
                    + ((coordInit[1] - coordFinal[2]) ** 2) 
                    + ((coordInit[2] - coordFinal[2]) ** 2)) 

    cummulativeDist += dist

  return cummulativeDist

print(f"Distance: {dist(myList)}")

这将采用3维距离,从一个点到下一个点,并返回行进的总距离。