问题描述
我在 Python 中有 2 个带有纬度和经度坐标的数组。它们对应于电路的坐标。它的情节是这样的: Plot of the race
事实上,我的赛道上需要更多点,理想情况下,我需要所有点彼此之间的距离相同。
我想这与插值有关,因为 Python 在绘制时链接点没有问题,但我找不到用于此的插值函数。
提前致谢!
解决方法
如果点对之间的距离太大,您可以在点对之间添加点。伪代码示例:
points_array = a new empty array of points
p1,p2 = two successive points
distance_p1p2 = sqrt( (p1.x - p2.x)² + (p1.y - p2.y)²)
# Add p1 to the new array of points
points_array.push(p1)
if distance_p1p2 > threshold: # threshold = 0.00005 for example,you can define it
# Create an intermediate point in between p1 and p2
intermediate_point.x = (p1.x + p2.x) / 2
intermediate_point.y = (p1.y + p2.y) / 2
# Add this intermediate point to the new array of points
points_array.push(intermediate_point)
# Add p2 to the new array of points
points_array.push(p2)
这些简单的步骤允许您在彼此相距太远的 2 个点之间添加一个中间点。从一个空数组开始并在所有点之间循环,您将得到一个包含更多点的新数组(如果阈值太大,则获得相同数量的点)。
然后,您可以根据需要多次迭代和执行此算法。
我想指出的是,这是基本算法,没有优化。例如,您可以分析每对点之间的距离并创建 n
个中间点,而不是一次创建 1 个。