MathUtils.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using UnityEngine;
  2. public class MathUtils {
  3. /*
  4. public static double map(double __value, double __oldMin, double __oldMax, double __newMin = 0, double __newMax = 0, bool __clamp = false) {
  5. if (__oldMin == __oldMax) return __newMin;
  6. double p = ((__value - __oldMin) / (__oldMax - __oldMin) * (__newMax - __newMin)) + __newMin;
  7. if (__clamp) p = __newMin < __newMax ? clamp(p, __newMin, __newMax) : clamp(p, __newMax, __newMin);
  8. return p;
  9. }
  10. */
  11. public static float map(float value, float oldMin, float oldMax, float newMin = 0, float newMax = 1, bool clamp = false) {
  12. if (oldMin == oldMax) return newMin;
  13. float p = ((value - oldMin) / (oldMax - oldMin) * (newMax - newMin)) + newMin;
  14. if (clamp) p = newMin < newMax ? MathUtils.clamp(p, newMin, newMax) : MathUtils.clamp(p, newMax, newMin);
  15. return p;
  16. }
  17. /*
  18. public static double map(TimeSpan __value, TimeSpan __oldMin, TimeSpan __oldMax, double __newMin = 0, double __newMax = 0, bool __clamp = false) {
  19. return MathUtils.map(__value.TotalMilliseconds, __oldMin.TotalMilliseconds, __oldMax.TotalMilliseconds, __newMin, __newMax, __clamp);
  20. }
  21. */
  22. public static float clamp(float value, float min = 0.0f, float max = 1.0f) {
  23. return value < min ? min : value > max ? max : value;
  24. }
  25. /*
  26. public static double clamp(double __value, double __min = 0.0, double __max = 1.0) {
  27. return __value < __min ? __min : __value > __max ? __max : __value;
  28. }
  29. */
  30. /**
  31. * Clamps a value to a range, by restricting it to a minimum and maximum values but folding the value to the range instead of simply resetting to the minimum and maximum. It works like a more powerful Modulo function.
  32. * @param value The value to be clamped.
  33. * @param min Minimum value allowed.
  34. * @param max Maximum value allowed.
  35. * @return The newly clamped value.
  36. * @example Some examples:
  37. *
  38. * MathUtils.rangeMod(14, 0, 10); // 4
  39. * MathUtils.rangeMod(360, 0, 360); // 0
  40. * MathUtils.rangeMod(360, -180, 180); // 0
  41. * MathUtils.rangeMod(21, 0, 10); // 1
  42. * MathUtils.rangeMod(-98, 0, 100); // 2
  43. */
  44. public static float rangeMod(float value, float min, float pseudoMax) {
  45. float range = pseudoMax - min;
  46. value = (value - min) % range;
  47. if (value < 0) value = range - (-value % range);
  48. value += min;
  49. return value;
  50. }
  51. /**
  52. * Find the "distance" two item indexes are in a list. The list cycles through.
  53. * If negative, the item index is behind the base index.
  54. */
  55. // TODO: better name?
  56. public static float listCycleDistance(float newPosition, float basePosition, int baseLength) {
  57. float posBefore, posAfter;
  58. if (newPosition > basePosition) {
  59. posBefore = - baseLength + newPosition - basePosition;
  60. posAfter = newPosition - basePosition;
  61. } else {
  62. posBefore = newPosition - basePosition;
  63. posAfter = baseLength - basePosition + newPosition;
  64. }
  65. //posBefore = posBefore % baseLength;
  66. //posAfter = posAfter % baseLength;
  67. return Mathf.Abs(posBefore) < posAfter ? posBefore : posAfter;
  68. }
  69. }