using System; namespace DTT.Utils.Extensions { /// /// Provides extension methods for math operations. /// public static class MathExtensions { /// /// Returns the inversed value. This means a positive value /// if the given value is negative and negative value if the /// given one is positive. /// /// The value to inverse. /// The inversed value. public static int Inverse(this int value) => value * -1; /// /// Returns the inversed value. This means a positive value /// if the given value is negative and negative value if the /// given one is positive. /// /// The value to inverse. /// The inversed value. public static double Inverse(this double value) => value *= -1d; /// /// Returns the inversed value so a positive value /// if this one is negative and negative if this one is positive. /// /// The value to inverse. /// The inversed value. public static float Inverse(this float value) => value * -1f; /// /// Returns the complement of the value so (1 - 'value'). /// /// The value to get the complement of. /// The complement. public static float Complement(this float value) { if (value < 0.0f || value > 1.0f) throw new ArgumentOutOfRangeException(nameof(value), "Expects value between in range 0 to 1."); return 1.0f - value; } /// /// Returns the complement of the value so (1 - 'value'). /// /// The value to get the complement of. /// The complement. public static double Complement(this double value) { if (value < 0.0d || value > 1.0d) throw new ArgumentOutOfRangeException(nameof(value), "Expects value between in range 0 to 1."); return 1.0d - value; } /// /// Returns whether the value is greater than or equal to a minimal value /// and smaller than or equal to a maximum value. /// /// The value to check. /// The minimal value. /// The maximum value. /// Whether the value is in the range. public static bool InRange(this int value, int min, int max) => value >= min && value <= max; /// /// Returns the normalized (between 0 and 1) value. /// /// The value to normalize. /// The minimum value to use. /// The maximum value to use. /// The normalized value. public static float Normalize(this float value, float min, float max) => (value - min) / (max - min); /// /// Returns the value mapped to a new scale. /// /// The value to map. /// The minimum range. /// The maximum range. /// The new minimum range. /// The new maximum range. /// The mapped value. public static float Map(this float value, float min, float max, float targetMin, float targetMax) => (value - min) * ((targetMax - targetMin) / (max - min)) + targetMin; } }