feat: Added MathUtils methods for converting to and from percentages

This commit is contained in:
Chris Cameron
2019-12-04 11:53:44 -05:00
parent 3deec98f44
commit b68c5f1c97
2 changed files with 29 additions and 2 deletions

View File

@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased]
### Added
- Added MathUtils methods for converting to and from percentages
## [10.2.0] - 2019-12-04
### Added
- Added shim methods for finding closest DateTimes from a sequence

View File

@@ -72,7 +72,7 @@ namespace ICD.Common.Utils
public static double MapRange(double inputStart, double inputEnd, double outputStart, double outputEnd, double value)
{
if (inputStart.Equals(inputEnd))
throw new DivideByZeroException();
return outputStart;
double slope = (outputEnd - outputStart) / (inputEnd - inputStart);
return outputStart + slope * (value - inputStart);
@@ -90,7 +90,7 @@ namespace ICD.Common.Utils
public static decimal MapRange(decimal inputStart, decimal inputEnd, decimal outputStart, decimal outputEnd, decimal value)
{
if (inputStart.Equals(inputEnd))
throw new DivideByZeroException();
return outputStart;
decimal slope = (outputEnd - outputStart) / (inputEnd - inputStart);
return outputStart + slope * (value - inputStart);
@@ -235,5 +235,29 @@ namespace ICD.Common.Utils
long remainder = number % mod;
return remainder < 0 ? remainder + mod : remainder;
}
/// <summary>
/// Converts the value to a percentage.
/// </summary>
/// <param name="min"></param>
/// <param name="max"></param>
/// <param name="value"></param>
/// <returns></returns>
public static float ToPercent(float min, float max, float value)
{
return MapRange(min, max, 0.0f, 1.0f, value);
}
/// <summary>
/// Converts the value from a percentage.
/// </summary>
/// <param name="min"></param>
/// <param name="max"></param>
/// <param name="percent"></param>
/// <returns></returns>
public static float FromPercent(float min, float max, float percent)
{
return MapRange(0.0f, 1.0f, min, max, percent);
}
}
}