Mega Code Archive
Value equals in a range, and ensure value inside a range
//package edu.cmu.ri.createlab.util;
/**
* @author Chris Bartley (bartley@cmu.edu)
*/
final class MathUtils {
public static final double EPSILON = 0.0000001;
private static final double ZERO = 0.0d;
/**
* Ensures that the given value is within the range
* [-|maxValue|, |maxValue|]. Returns the given
* value if it's within the range; otherwise, returns
* -|maxValue| or |maxValue| as appropriate.
*/
public static double ensureRange(final double value, final double maxValue) {
final double absMaxValue = Math.abs(maxValue);
if (value > absMaxValue) {
return absMaxValue;
}
if (value < -absMaxValue) {
return -absMaxValue;
}
return value;
}
/**
* Ensures that the given value is within the range
* [minValue, maxValue]. Returns the given value
* if it's within the range; otherwise, returns minValue or
* maxValue as appropriate. Performs idiot checking to make
* sure that minValue isn't greater than maxValue
* (swaps the values if it is).
*/
public static double ensureRange(final double value, final double minValue,
final double maxValue) {
final double min;
final double max;
// idiot check
if (minValue > maxValue) {
min = maxValue;
max = minValue;
} else {
min = minValue;
max = maxValue;
}
if (value > max) {
return max;
}
if (value < min) {
return min;
}
return value;
}
/**
* Ensures that the given value is within the range
* [minValue, maxValue]. Returns the given value
* if it's within the range; otherwise, returns minValue or
* maxValue as appropriate. Performs idiot checking to make
* sure that minValue isn't greater than maxValue
* (swaps the values if it is).
*/
public static int ensureRange(final int value, final int minValue,
final int maxValue) {
final int min;
final int max;
// idiot check
if (minValue > maxValue) {
min = maxValue;
max = minValue;
} else {
min = minValue;
max = maxValue;
}
if (value > max) {
return max;
}
if (value < min) {
return min;
}
return value;
}
/**
* Returns true if the given numbers are equal within
* {@link #EPSILON}; false otherwise.
*/
public static boolean equals(final double d1, final double d2) {
return equalToWithin(d1, d2, EPSILON);
}
/**
* Returns true if the given numbers are equal within
* epsilon; false otherwise.
*/
public static boolean equalToWithin(final double d1, final double d2,
final double epsilon) {
return Math.abs(d1 - d2) < epsilon;
}
public static boolean isNonZero(final double d) {
return !equals(d, ZERO);
}
/**
* Normalizes the given angle (which must be in radians) and
* returns an angle in the range [-pi,pi).
*/
public static double normalizeAngle(final double angle) {
if ((angle < Math.PI) && (angle >= -Math.PI)) {
return angle;
} else if (equals(angle, Math.PI)) {
return -Math.PI;
} else if (angle > Math.PI) {
return normalizeAngle(angle - 2 * Math.PI);
} else {
return normalizeAngle(angle + 2 * Math.PI);
}
}
private MathUtils() {
// private to prevent instantiation
}
}