Floats.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using UnityEngine;
  3. namespace FirstGearGames.Utilities.Maths
  4. {
  5. public static class Floats
  6. {
  7. private static System.Random _random = new System.Random();
  8. /// <summary>
  9. /// Provides a random inclusive int within a given range. Preferred over Unity's Random to eliminate confusion as Unity uses inclusive for floats max, and exclusive for int max.
  10. /// </summary>
  11. /// <param name="minimum">Inclusive minimum value.</param>
  12. /// <param name="maximum">Inclusive maximum value.</param>
  13. /// <returns></returns>
  14. public static float RandomInclusiveRange(float minimum, float maximum)
  15. {
  16. double min = Convert.ToDouble(minimum);
  17. double max = Convert.ToDouble(maximum);
  18. double result = (_random.NextDouble() * (max - min)) + min;
  19. return Convert.ToSingle(result);
  20. }
  21. /// <summary>
  22. /// Returns a random float between 0f and 1f.
  23. /// </summary>
  24. /// <returns></returns>
  25. public static float Random01()
  26. {
  27. return RandomInclusiveRange(0f, 1f);
  28. }
  29. /// <summary>
  30. /// Returns if a target float is within variance of the source float.
  31. /// </summary>
  32. /// <param name="a"></param>
  33. /// <param name="b"></param>
  34. /// <param name="tolerance"></param>
  35. public static bool Near(this float a, float b, float tolerance = 0.01f)
  36. {
  37. return (Mathf.Abs(a - b) <= tolerance);
  38. }
  39. /// <summary>
  40. /// Clamps a float and returns if the float required clamping.
  41. /// </summary>
  42. /// <param name="value"></param>
  43. /// <param name="min"></param>
  44. /// <param name="max"></param>
  45. /// <param name="clamped"></param>
  46. /// <returns></returns>
  47. public static float Clamp(float value, float min, float max, ref bool clamped)
  48. {
  49. clamped = (value < min);
  50. if (clamped)
  51. return min;
  52. clamped = (value > min);
  53. if (clamped)
  54. return max;
  55. clamped = false;
  56. return value;
  57. }
  58. /// <summary>
  59. /// Returns a float after being adjusted by the specified variance.
  60. /// </summary>
  61. /// <param name="source"></param>
  62. /// <param name="variance"></param>
  63. /// <returns></returns>
  64. public static float Variance(this float source, float variance)
  65. {
  66. float pickedVariance = RandomInclusiveRange(1f - variance, 1f + variance);
  67. return (source * pickedVariance);
  68. }
  69. /// <summary>
  70. /// Sets a float value to result after being adjusted by the specified variance.
  71. /// </summary>
  72. /// <param name="source"></param>
  73. /// <param name="variance"></param>
  74. /// <returns></returns>
  75. public static void Variance(this float source, float variance, ref float result)
  76. {
  77. float pickedVariance = RandomInclusiveRange(1f - variance, 1f + variance);
  78. result = (source * pickedVariance);
  79. }
  80. /// <summary>
  81. /// Returns negative-one, zero, or postive-one of a value instead of just negative-one or positive-one.
  82. /// </summary>
  83. /// <param name="value">Value to sign.</param>
  84. /// <returns>Precise sign.</returns>
  85. public static float PreciseSign(float value)
  86. {
  87. if (value == 0f)
  88. return 0f;
  89. else
  90. return (Mathf.Sign(value));
  91. }
  92. /// <summary>
  93. /// Returns if a float is within a range.
  94. /// </summary>
  95. /// <param name="source">Value of float.</param>
  96. /// <param name="rangeMin">Minimum of range.</param>
  97. /// <param name="rangeMax">Maximum of range.</param>
  98. /// <returns></returns>
  99. public static bool InRange(this float source, float rangeMin, float rangeMax)
  100. {
  101. return (source >= rangeMin && source <= rangeMax);
  102. }
  103. /// <summary>
  104. /// Randomly flips a float value.
  105. /// </summary>
  106. /// <param name="value"></param>
  107. /// <returns></returns>
  108. public static float RandomlyFlip(float value)
  109. {
  110. if (Ints.RandomInclusiveRange(0, 1) == 0)
  111. return value;
  112. else
  113. return (value *= -1f);
  114. }
  115. }
  116. }