【问题标题】:calculating the total distance between multiple points计算多个点之间的总距离
【发布时间】:2020-04-09 12:09:13
【问题描述】:

如何使用循环计算列表中多个点之间的距离。

def create_list(x_range,y_range, locations):
  generated_list = []
  for x in range(locations):
    x_range = random.randint(-300,300)
    y_range = random.randint(-300,300)
    generated_list.append([x_range, y_range])
  return generated_list

上面创建了一个随机列表,我需要使用以下代码计算所有点返回起点的总距离:

def calculate_distance(starting_x, starting_y, destination_x, destination_y):
    distance = math.hypot(destination_x - starting_x, destination_y - starting_y)  # calculates Euclidean distance (straight-line) distance between two points
    return distance

这里我需要使用上面的函数循环计算所有点之间的距离,我将如何使用循环计算所有点之间的距离

【问题讨论】:

  • 所有点之间是什么意思?喜欢来自[p1, p2, p3] 计算d(p1, p2), d(p1, p3),d(p2, p3)
  • 所以第一个函数将生成随机坐标,如 [[5,3],[6,9],[7,2]] 我需要使用第二个函数计算这些点之间的距离.我知道我需要遍历列表,但我将如何计算总距离?
  • 请回答我之前评论中的问题,因为您重复使用了“所有点之间”这个棘手的表达方式
  • 抱歉,由于某种原因无法看到您的完整评论。例如,我将 a(x1,y2), b(x3,y4), c(x5,y6) 作为图上的点。我想计算欧几里得距离。所以我需要迭代所以它这样做:(x1 - x3) + (y2 - y4) 这将是点 a 和点 b 之间的距离。然后将此距离分配给一个变量并存储它。然后我想计算点 b 和点 c 之间的距离,然后计算点 c 到点 a 之间的距离,并添加所有这些值,这将等于所有点之间的总距离。我希望这更清楚一点?
  • 你可以对这个问题说 :D,我现在正在回答

标签: python distance


【解决方案1】:

您可以从(x, y) 的配对列表中

  • 使用itertools.permutations 获取此列表中的排列。这是一个有 3 个点的示例,通过指定长度为 2,您得到了 6 个排列。

    values  = [(2, 1), (0, 3), (3, 2)]
    
    perm = [((2, 1), (0, 3)),   ((2, 1), (3, 2)),   ((0, 3), (2, 1)), 
            ((0, 3), (3, 2)),   ((3, 2), (2, 1)),   ((3, 2), (0, 3))]
    
  • 用积分拨打您的calculate_distance

    p1 = (1,2)
    p2 = (2,3)
    dist = calculate_distance(*p1, *p2) #or calculate_distance(p1[0], p1[1], p2[0], p2[1])
    

使用所有这些

values = create_list(300, 300, 3)
for p1, p2 in permutations(values, r=2):
    dist = calculate_distance(*p1, *p2)
    print(p1, p2, dist)

总结它们都在循环中添加一个值来做total+=dist,或者使用sum,它接受一个可迭代和缩短

values = create_list(300, 300, 3)
total = sum(calculate_distance(*p1, *p2) for p1, p2 in permutations(values, r=2))

使用循环

仅对连续点求和

# Way 1 : d(a,b) + d(b,c) + d(c,d) + d(d,e) + d(e,a)
total = calculate_distance(*values[-1], *values[0])  # between  last and first
for i in range(len(values) - 1):
    total += calculate_distance(*values[i], *values[i + 1])
print(total)

对所有点的组合求和

# Way 2: d(a,b) + d(a,c) + d(a,d) + d(a,e) + d(b,c) + d(b,d) + d(b,e) + d(c,d) + d(c,e) + d(d,e)
total = 0
for p1 in values:
    for p2 in values:
        if p1 != p2:
            total += calculate_distance(*p1, *p2)
print(total)

【讨论】:

  • 但我的 create_list 函数正在创建随机生成的列表,所以我不知道要点
  • @Marzan 看最后,排列方法通过迭代返回2个点,所以这里有点,点值是随机的,只是
  • 排列比这个问题复杂得多。目标(对我来说)是学习如何在循环中将数字相加。我认为这只是从 a 到 b + b 到 c + c 到 d + d 到 e + e 到 a 的距离(如果有 5 个点)。
  • 有没有办法通过在范围内使用 for 循环来做到这一点,这样我就可以在 calculate_distance 函数中迭代?
  • @Marichyasana 是的,这就是我想要做的,但我将如何在calculate_distance 函数的范围内使用 for 循环,以便计算从 a 到 b 的距离并将其存储在变量中,然后计算从 b 到 c 的下一个距离,并将该距离添加到我们已经保存在变量中的距离
【解决方案2】:
import math

def calculate_distance(x1, y1, x2, y2):
    distance = math.hypot(x2 - x1, y2 - y1)
    return distance


def total_dist(ml):  # ml is a list of pairs of x,y points
    n = len(ml)
    total = 0.0
    for i in range (n):
        x1 = ml[i][0]
        y1 = ml[i][1]
        if i < n-1:
            x2 = ml[i+1][0]
            y2 = ml[i+1][1]
        else:
            x2 = ml[0][0]
            y2 = ml[0][1]
        dist = calculate_distance(x1,y1,x2,y2)
        total += dist
    return total

# main program
copy_list = [[110, -260], [284, -152], [-197, -167], [-253, 295], [-89, 26], [-165, 43], [-84, 148], [-188, 38], [201, 191], [232, 251]]
print total_dist(copy_list)

【讨论】:

  • 我将如何通过此列表传​​递我的列表,因为我正在从函数 create_list 生成一个随机列表,并且它可以有多个 x、y 对。如果您查看我的列表函数,我已将其分配给 copy_list,我将如何通过您的循环在一个单独的函数中传递它,然后该函数将调用 calculate_distance 函数?
  • 我没有看到“copy_list”,只有两个函数。我提供的代码是使用这两个功能的主程序;首先调用“create_list”来获取 x,y 对的列表。其余代码使用“calculate_distance”并将它们添加在一起。如果您有其他函数来创建对列表,请使用它而不是“mylist = create_list(0,0,n)”我可以尝试制作一个完整的程序如果你愿意。
猜你喜欢
  • 1970-01-01
  • 2014-11-29
  • 2017-11-03
  • 2020-01-28
  • 2014-02-28
  • 2010-10-30
  • 1970-01-01
相关资源
最近更新 更多