Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Evenly distributing n points along a line

Tags:

math

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

like image 298
Arnau Mas Avatar asked Nov 14 '25 18:11

Arnau Mas


1 Answers

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.

like image 85
Spektre Avatar answered Nov 17 '25 08:11

Spektre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!