Skip to content
Advertisement

Equidistant points between two points?

I am trying to know equidistant points between two points. For example:

p1 = (1,1)
p2 = (5,5)

The answer that I am expecting is:

def getEquidistantPoints(p1, p2, HowManyParts):
    #some code
    return (array with points)

In this example, with p1, and p2:

A = getEquidistantPoints(p1,p2,4)
A = [(1,1),(2,2),(3,3),(4,4),(5,5)]

Always will be a straight line. HowManyParts in this case is the whole distance that is divided
something like numpy.linspace() but in two dimensions.

Advertisement

Answer

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)]
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement