I recently adapted this function from a Python solution I found on a different platform. Despite the points being evenly spaced, they are not aligned along the center of the line.
The initial point is located at position 0
, while the final point is at 83
(with the end of the line segment set at 100
).
How can I adjust the alignment of these points to be centered?
To clarify, I want the distance between the first point and 0
to be equivalent to that between the last point and 100
.
'use strict';
function generatePoints(count, length) {
const points = []
for (let i = 0; i < count; i++) {
const a = i / count;
const x = a * length;
points.push(Math.round(x));
}
return points;
}
const points = generatePoints(6, 100);
console.log(points);
// [ 0, 17, 33, 50, 67, 83 ]
I attempted the following adjustment without success:
for (let i = 0; i < points.length; i++) {
points[i] += (points[1] / 2);
points[i] = Math.round(points[i]);
}
console.log(points);
// [ 9, 26, 46, 63, 80, 96 ]
After applying this method, the initial point is now 9
units away from 0
, but the last point is only 4
units away from 100
.