<p>假设您发布的代码是正确的,我认为以下函数可以作为一种迭代方法来替换当前的递归函数:</p>
<pre><code>def iterative_move_along_path(self, points, distance, index=0):
while index < len(points) - 1:
# There is still at least one point further from this point
# Turn points into tuples for geopy format
# point1_tuple = (points[index]['latitude'], points[index]['longitude'])
# point2_tuple = (points[index + 1]['latitude'], points[index + 1]['longitude'])
point1_tuple = (points[index]['lat'], points[index]['lng'])
point2_tuple = (points[index + 1]['lat'], points[index + 1]['lng'])
# Use geodesic method to get distance between points in meters
distance_to_next_point = geopy.distance.geodesic(point1_tuple, point2_tuple).m
if distance <= distance_to_next_point:
# Distance_to_next_point is within this point and the next
# Return the destination point with moveTowards()
return self.move_towards(point1_tuple, point2_tuple, distance)
else:
# The destination is further from the next point
# Subtract distance_to_next_point from distance and continue recursively
distance -= distance_to_next_point
index += 1
# There are no further points, the distance exceeds the length of the full path.
# Return None
return None
</code></pre>
<p>当从递归返回时,由于递归的一个步骤似乎与先前计算的值没有依赖关系,因此应该简单而正确地插入<code>while</code>循环并适当地更新变量。你知道吗</p>