Skip to content

Instantly share code, notes, and snippets.

@Ferfalk
Created January 17, 2019 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ferfalk/ea353b60e351a1bf25dd9dd693525d0c to your computer and use it in GitHub Desktop.
Save Ferfalk/ea353b60e351a1bf25dd9dd693525d0c to your computer and use it in GitHub Desktop.
public class ScaleUtils {
private ScaleUtils() {
}
public static double linearMap(double source, double sourceMin, double sourceMax, double targetMin, double targetMax) {
return ((source - sourceMin) / (sourceMax - sourceMin)) * (targetMax - targetMin) + targetMin;
}
// public static double linearMap(double source, double sourceMin, double sourceMax, double targetMin, double targetMax) {
// return normalizedToScale(normalizeScale(source, sourceMin, sourceMax), targetMin, targetMax);
// }
// public static double normalizeScale(double source, double sourceMin, double sourceMax) {
// double sourceRange = sourceMax - sourceMin;
// return (source - sourceMin) / sourceRange;
// }
//
// public static double normalizedToScale(double source, double targetMin, double targetMax) {
// double targetRange = targetMax - targetMin;
// return (source * targetRange) + targetMin;
// }
}
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ScaleUtilsTest {
@Test
public void linearMap() {
double result = ScaleUtils.linearMap(25, 0, 100, 0, 0.5);
assertEquals(0.125, result, 0);
}
@Test
public void linearMapInverted() {
double result = ScaleUtils.linearMap(25, 100, 0, 0.5, 1);
assertEquals(0.875, result, 0);
}
// @Test
// public void normalizeScaleNormal() {
// double result = ScaleUtils.normalizeScale(62.5, 50, 100);
// assertEquals(0.25, result, 0);
// }
//
// @Test
// public void normalizeScaleInverted() {
// double result = ScaleUtils.normalizeScale(87.5, 100, 50);
// assertEquals(0.25, result, 0);
// }
//
// @Test
// public void normalizedToScale() {
// double result = ScaleUtils.normalizedToScale(0.25, 50, 100);
// assertEquals(62.5, result, 0);
// }
//
// @Test
// public void normalizedToScaleInverted() {
// double result = ScaleUtils.normalizedToScale(0.25, 100, 50);
// assertEquals(87.5, result, 0);
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment