Maths.java 1019 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package holeg.utility.math;
  2. public class Maths {
  3. public final static double EPSILON = 1E-6;
  4. /**
  5. * Linear Interpolation from first to second via alpha amount.
  6. * @param first
  7. * @param second
  8. * @param alpha
  9. * @return
  10. */
  11. public static double lerp(double first, double second, double alpha) {
  12. return first * (1.0 - alpha) + second * alpha;
  13. }
  14. /**
  15. * Inverse Linear Interpolation from min to max to get the corresponding alpha from value.
  16. * @param min
  17. * @param max
  18. * @param value
  19. * @return
  20. */
  21. public static double invLerp(double min, double max, double value) {
  22. if (Math.abs(max - min) < EPSILON) return max;
  23. else return (value - min) / (max - min);
  24. }
  25. public static int Clamp(int value, int min, int max){
  26. return Math.max(min, Math.min(max, value));
  27. }
  28. public static float Clamp(float value, float min, float max){
  29. return Math.max(min, Math.min(max, value));
  30. }
  31. public static double Clamp(double value, double min, double max){
  32. return Math.max(min, Math.min(max, value));
  33. }
  34. }