Created
July 25, 2017 10:51
-
-
Save ufuk/a13c9771e567ac2f4d95abf90867d261 to your computer and use it in GitHub Desktop.
Scales and translates a value (x) into a new range [a, b].
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Scales and translates a value (x) into a new range [a, b]. | |
* | |
* Formula: | |
* f(x) = a + (b - a)(x - min)/(max - min) | |
* | |
* See also: https://stackoverflow.com/a/5295202 | |
*/ | |
public BigDecimal scaleAndTranslateIntoRange(BigDecimal value, BigDecimal actualMin, BigDecimal actualMax, BigDecimal desiredMin, BigDecimal desiredMax) { | |
return desiredMax.subtract(desiredMin).multiply(value.subtract(actualMin)) | |
.divide(actualMax.subtract(actualMin), RoundingMode.HALF_UP) | |
.add(desiredMin) | |
.setScale(2, RoundingMode.HALF_UP); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment