Last active
March 30, 2022 09:53
-
-
Save EasyG0ing1/33938226008dca483960f2b74008b7a4 to your computer and use it in GitHub Desktop.
A utility class that will take a number and the range that the number belongs in, and re-map it to whatever the equivalent number is in your destination range.It uses the algrebraic method and is flawless. You can literally map any range to any range regardless of where on the number line the range starts and ends.Overloaded methods allow you to…
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
public class RangeMap { | |
public static double reMap(double sourceNumber, double fromRangeStart, double fromRangeEnd, double toRangeStart, double toRangeEnd, int decimalPrecision ) { | |
double deltaA = fromRangeEnd - fromRangeStart; | |
double deltaB = toRangeEnd - toRangeStart; | |
double scale = deltaB / deltaA; | |
double negA = -1 * fromRangeStart; | |
double offset = (negA * scale) + toRangeStart; | |
double finalNumber = (sourceNumber * scale) + offset; | |
int calcScale = (int) Math.pow(10, decimalPrecision); | |
return (double) Math.round(finalNumber * calcScale) / calcScale; | |
} | |
public static int reMap(double sourceNumber, double fromRangeStart, double fromRangeEnd, double toRangeStart, double toRangeEnd) { | |
return (int) reMap(sourceNumber,fromRangeStart,fromRangeEnd,toRangeStart,toRangeEnd,0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment