我想知道两点之间的等距点.例如.
p1 = (1,1)
p2 = (5,5)
Run Code Online (Sandbox Code Playgroud)
我期待的答案是:
def getEquidistantPoints(p1,p2,HowManyParts):
#some code
return (array with points)
Run Code Online (Sandbox Code Playgroud)
在这个例子中,使用p1和p2:
A = getEquidistantPoints(p1,p2,4)
A = [(1,1),(2,2),(3,3),(4,4),(5,5)]
Run Code Online (Sandbox Code Playgroud)
永远都是一条直线.在这种情况下,HowManyParts是划分的整个距离
像numpy.linspace这样的东西,但是在两个维度上.谢谢.
ran*_*mir 11
由于连接两个点的线的线性,您可以单独使用numpy.linspace每个维度:
import numpy
def getEquidistantPoints(p1, p2, parts):
return zip(numpy.linspace(p1[0], p2[0], parts+1), numpy.linspace(p1[1], p2[1], parts+1))
Run Code Online (Sandbox Code Playgroud)
例如:
>>> list(getEquidistantPoints((1,1), (5,5), 4))
>>> [(1.0, 1.0), (2.0, 2.0), (3.0, 3.0), (4.0, 4.0), (5.0, 5.0)]
Run Code Online (Sandbox Code Playgroud)
使用纯Python解决方案linear interpolation:
首先创建一个linear interpolation函数:
def lerp(v0, v1, i):
return v0 + i * (v1 - v0)
Run Code Online (Sandbox Code Playgroud)
然后interpolate在x和y坐标之间使用它:
def getEquidistantPoints(p1, p2, n):
return [(lerp(p1[0],p2[0],1./n*i), lerp(p1[1],p2[1],1./n*i)) for i in range(n+1)]
Run Code Online (Sandbox Code Playgroud)
并使用您的价值观进行测试:
>>> getEquidistantPoints((1,1), (5,5), 4)
[(1.0, 1.0), (2.0, 2.0), (3.0, 3.0), (4.0, 4.0), (5.0, 5.0)]
Run Code Online (Sandbox Code Playgroud)