12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- package holeg.utility.math;
- public class Maths {
- public final static double EPSILON = 1E-6;
- /**
- * Linear Interpolation from first to second via alpha amount.
- *
- * @param first
- * @param second
- * @param alpha
- * @return
- */
- public static double lerp(double first, double second, double alpha) {
- return first * (1.0 - alpha) + second * alpha;
- }
- /**
- * Inverse Linear Interpolation from min to max to get the corresponding alpha from value.
- *
- * @param min
- * @param max
- * @param value
- * @return
- */
- public static double invLerp(double min, double max, double value) {
- if (Math.abs(max - min) < EPSILON) {
- return max;
- } else {
- return (value - min) / (max - min);
- }
- }
- public static int Clamp(int value, int min, int max) {
- return Math.max(min, Math.min(max, value));
- }
- public static float Clamp(float value, float min, float max) {
- return Math.max(min, Math.min(max, value));
- }
- public static double Clamp(double value, double min, double max) {
- return Math.max(min, Math.min(max, value));
- }
- }
|