C-Math
C-Math

Reputation: 63

Create uniformly spaced points in a given direction

Suppose I have a line in 2D and in an arbitrary direction. I would like to create uniformly spaced points along this line. I thought of one way to code this but seems like it can get messy, are there any libraries in Python/Numpy that do this?

Upvotes: 0

Views: 735

Answers (1)

Valdi_Bo
Valdi_Bo

Reputation: 31011

I'm afraid that there is no any library function to generate your list of points and you have to generate it with your own code.

Assume that your line is defined by 2 following points (x and y coordinates):

p0 = np.array([1.0, 1.0])
p1 = np.array([3.0, 2.0])

Assume also the number of "additional" points to create as:

n = 10

Then, to create a list of points (a Numpy array of shape n + 2, 2), compute the difference between these points:

dlt = p1 - p0

and run:

result = np.vstack([ p0 + i * dlt for i in range(n + 2) ])

The result is:

array([[ 1.,  1.],
       [ 3.,  2.],
       [ 5.,  3.],
       [ 7.,  4.],
       [ 9.,  5.],
       [11.,  6.],
       [13.,  7.],
       [15.,  8.],
       [17.,  9.],
       [19., 10.],
       [21., 11.],
       [23., 12.]])

so that first 2 points are p0 and p1 and other points are located further on the same line, in equal steps of size dlt.

Edit

To generate a list of n points between 2 points (p0 and p1), including both these terminal points, you can run:

result = np.vstack([np.linspace(p0[0], p1[0], n), np.linspace(p0[1], p1[1], n)]).T

But if you have a polygon then, for each side you should define the number of intermediate points and then use the above formula.

In a general case, there can be problem concerning how to select the number of points.

Upvotes: 1

Related Questions