I can't figure out the solution to a simple math problem. I have an array of 0...n items. I know the mid point of this area and the height of each item. I need to set the yAxis for each item so they're evenly spaced.
let group = [..., {id: A, height: 20, yAxis: 0}, {id: B, height: 20, yAxis: 0}, {id: C, height: 20, yAxis: 0}];
let midPoint = 100;
I've tried getting the total height of the group/group.length, but how do I set the positions relative to the establised midPoint?
CodePudding user response:
I'm assuming that in your case a value of 0
for the yAxis
is the mid point.
If this assumptions are true, you could set the yAxis
for each object using this logic (in pseudocode)
totalBetweenSpace = (betweenSpace * (group.size - 1))
maxYAxis = (totalHeight totalBetweenSpace) / 2
currentYAxis = maxYAxis
for obj in group
currentYAxis = currentYAxis - obj.height
if (obj is not first object in group)
currentYAxis = currentYAxis - betweenSpace
obj.yAxis = currentYAxis
Where
totalHeight
is the sum of all the objects' heights in the groupbetweenSpace
is the amount of space you want in between each object