Is this version of the algorithm correct for line drawing? or might it have an off-by-one error?
I am assuming that a line from A to B should include point A yet for the notebook's initial conditions, with m === 320, the first array value is 1 instead of 0.(Can that be right?)
Or perhaps I'm just not understanding how you would use the results.
Wouldn't you want to be sure to get the first and last array values and then sample the others as uniformly spaced as possible?
FWIW @jonhelfman/uniform-sampling-variants has a one-liner for this, which is not optimized to eliminate division, but does return the first and last array values:
return d3.range(n).map(i => Math.round(i * 1 / (n - 1) * (arrayLength-1)))
Thanks!
Including the first and last value wasn’t a requirement for me, but sure, you could make it one. It’s nice that you found a solution that requires only a Math.round; when I tried it before I think I was using Math.floor and I couldn’t avoid occasional duplicate indexes when m ≅ n.
Now I’m confused why I didn’t just use Array.from({length: m}, (_, i) => Math.floor(i / m * n))… There must have been some other bug in the original code that inspired this notebook.