0

我有一个程序,旨在计算 Python Turtle 的一组坐标的总距离。目前,我有一个名为 def calculate_distance 的函数,它只计算两点之间的距离。我需要:

  • 设置一个变量来保存总路径距离
  • 使用循环分别计算点的距离
  • 尝试使用 calculate_distance 函数
  • 计算所有位置返回原点的路径

我该怎么做呢?我是 Python 新手,但我知道大部分基础知识。这是我的程序最后显示的内容:

点之间的距离:72.59476565152615 - 但这是两点之间的距离,而不是总和

import math

selected_map = [(12, 34), (45, -55), (-89, 33), (60, 12)]

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

def calculate_path(selected_map):

- The code I am asking for help on needs to go here in this function



print (distance)
4

3 回答 3

2
selected_map = [(12, 34), (45, -55), (-89, 33), (60, 12)]

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
    print('Segment Dist: ', distance)
    return distance

def calculate_path(selected_map, dist_travel=0):
    for i in range(len(selected_map)-1):
        dist_travel += calculate_distance(selected_map[i-len(selected_map)+1][0], selected_map[i-len(selected_map)+1][1], selected_map[i][0], selected_map[i][1])
    return dist_travel

print('Total Distance: ', calculate_path(selected_map))

输出:

Segment Dist:  94.92101980067429
Segment Dist:  160.31219541881399
Segment Dist:  150.4725888658795
Total Distance:  405.70580408536773
于 2020-04-09T16:02:19.210 回答
1

要计算路径的长度(例如,您提到的总距离,而不是净位移),只需遍历每个点并计算其与遍历的最后一个点的距离:

import math

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

def calculate_path(selected_map):
    total_distance = 0
    current_point = selected_map[0]
    for next_point in selected_map[1:]:
        current_distance = calculate_distance(
            current_point[0], current_point[1],
            next_point[0], next_point[1]
        )
        print(current_point, 'to', next_point, '=', current_distance)
        total_distance += current_distance
        current_point = next_point
    return total_distance

selected_map = [(12, 34), (45, -55), (-89, 33), (60, 12)]
distance = calculate_path(selected_map)

print ('Total Distance =', distance)

输出:

(12, 34) to (45, -55) = 94.92101980067429
(45, -55) to (-89, 33) = 160.31219541881399
(-89, 33) to (60, 12) = 150.4725888658795
Total Distance = 405.70580408536773
于 2020-04-09T15:17:39.163 回答
1

首先,您需要定义起点。例如,定义当前位置的两个变量。

x_current = 0
y_current = 0

和一个保存当前长度的变量。

current_length = 0

然后开始循环 selected_map。计算当前 x,y 与 x,y 对之间的距离,并将其添加到当前长度中。

for pair in selected_map:
  current_length += calculate_distance(x_current, y_current, pair[0], pair[1])
  x_current = pair[0]
  x_current = pair[1]

return current_length
于 2020-04-09T15:24:02.937 回答