Home > Back-end >  Evenly distributing n points along a line
Evenly distributing n points along a line

Time:09-18

I'd like to know which formula am I supposed to use in order to distribute n points along a line so they are uniformly distribute.

Note: length/2 <= n > 0

The distance between the points have to be the same, so:

For length = 10, points = 1: ----0-----

For length = 10, points = 2: --0----0--

For length = 10, points = 3: -0--0--0-- (Note how you always "start" by the left, if you started from the right, it'd be like this: --0--0--0-)

For length = 10, points = 5: 0-0-0-0-0-0

For length = 10, points 7: It's not possible, 7 is bigger than length/2

CodePudding user response:

So you got line given its 2 endpoints p0,p1 and number of points n which are to be placed evenly on interior of the line where gap between those points and endpoit s is half of the avg distance between points ...

For floating point values linear interpolation will give you the points:

p(t) = p0   (p1-p0)*t

where t = <0.0,1.0> will give you any point along line parametrized by t. Now its just a matter to compute t so:

dt = |p1-p0| / n
t = 0.5*dt   i*dt
i = { 0,1,2,3, ... ,n-1 }

Once integer values are involved you can truncate/round the resulting position into integers or use line rasterization algorithms like DDA or Bresenham for this where you can add another axis with difference equal to 2 2*n and use only every even points with that axis coordinate change.

  •  Tags:  
  • math
  • Related