1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-23 00:58:48 +00:00

Add MathUtility.ScaleRange

This commit is contained in:
Oliver Booth 2023-02-05 14:28:42 +00:00
parent 384ec3f61c
commit eadb66f470
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
2 changed files with 31 additions and 0 deletions

View File

@ -7,6 +7,7 @@
- Added .NET 7 target
- X10D: Added `MathUtility.InverseLerp(float, float, float)` and `MathUtility.InverseLerp(double, double, double)`
- X10D: Added `MathUtility.ScaleRange(float, float, float, float, float)` and `MathUtility.ScaleRange(double, double, double, double, double)`
- X10D: Added `Circle`, `CircleF`, `Cuboid`, `Ellipse`, `EllipseF`, `Line3D`, `Line`, `LineF`, `Polygon`, `PolygonF`, `Polyhedron`, and `Sphere`, to complement System.Drawing structs such as `Point` and `Rectangle`
- X10D: Added `Color.Deconstruct()` - with optional alpha parameter
- X10D: Added `Color.GetClosestConsoleColor()`

View File

@ -99,4 +99,34 @@ public static class MathUtility
// "precise" method: (1 - t) * a + t * b
return ((1.0 - alpha) * value) + (alpha * target);
}
/// <summary>
/// Converts a value from being a percentage of one range, to being the same percentage in a new range.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <param name="oldMin">The old minimum value.</param>
/// <param name="oldMax">The old maximum value.</param>
/// <param name="newMin">The new minimum value.</param>
/// <param name="newMax">The new maximum value.</param>
/// <returns>The scaled value.</returns>
public static float ScaleRange(float value, float oldMin, float oldMax, float newMin, float newMax)
{
float alpha = InverseLerp(value, oldMin, oldMax);
return Lerp(newMin, newMax, alpha);
}
/// <summary>
/// Converts a value from being a percentage of one range, to being the same percentage in a new range.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <param name="oldMin">The old minimum value.</param>
/// <param name="oldMax">The old maximum value.</param>
/// <param name="newMin">The new minimum value.</param>
/// <param name="newMax">The new maximum value.</param>
/// <returns>The scaled value.</returns>
public static double ScaleRange(double value, double oldMin, double oldMax, double newMin, double newMax)
{
double alpha = InverseLerp(value, oldMin, oldMax);
return Lerp(newMin, newMax, alpha);
}
}