Equidistant points between two points?
A pure Python
solution using linear
interpolation
:
First create a linear interpolation
function:
def lerp(v0, v1, i):
return v0 + i * (v1 - v0)
and then just use this to interpolate
between the x
and y
coordinates:
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)]
and a test with your values:
>>> 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)]
Thanks to linearity of the line connecting two points, you can simply use numpy.linspace
for each dimension independently:
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))
For example:
>>> 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)]