Re: Midpoint and skew
Posted: Mon May 01, 2023 6:29 pm
The wonderful Danny L from CA has kindly put me out of my misery. I was so close but not quite there on the skewed from center math.
Most developers will never need to know this level of detail. But for those who really do need it here's fully tested example code that translates position in the range [0,1] to outputValue...
LINEAR (or use midpoint == 0.5 or skewFactor == 1):
outputValue = min + range * position;
MIDPOINT:
outputValue = min + range * Math.pow( position, Math.log( ( midpoint - min ) / range ) / Math.log( 0.5 ) );
REGULAR SKEW:
outputValue = min + range * Math.pow( position, 1 / skewFactor );
SKEW FROM CENTER:
distFromMiddle = 2 * ( position - 0.5 );
outputValue = min + range * ( 1 + Math.pow( Math.abs( distFromMiddle ), 1 / skewFactor ) * ( distFromMiddle < 0 ? -1 : 1 ) ) / 2;
Most developers will never need to know this level of detail. But for those who really do need it here's fully tested example code that translates position in the range [0,1] to outputValue...
LINEAR (or use midpoint == 0.5 or skewFactor == 1):
outputValue = min + range * position;
MIDPOINT:
outputValue = min + range * Math.pow( position, Math.log( ( midpoint - min ) / range ) / Math.log( 0.5 ) );
REGULAR SKEW:
outputValue = min + range * Math.pow( position, 1 / skewFactor );
SKEW FROM CENTER:
distFromMiddle = 2 * ( position - 0.5 );
outputValue = min + range * ( 1 + Math.pow( Math.abs( distFromMiddle ), 1 / skewFactor ) * ( distFromMiddle < 0 ? -1 : 1 ) ) / 2;