From eadb66f47002724c8a149142e81c83e25f6a987f Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Sun, 5 Feb 2023 14:28:42 +0000 Subject: [PATCH] Add MathUtility.ScaleRange --- CHANGELOG.md | 1 + X10D/src/Math/MathUtility.cs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b205879..e43fa92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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()` diff --git a/X10D/src/Math/MathUtility.cs b/X10D/src/Math/MathUtility.cs index a75cc95..b6b2c41 100644 --- a/X10D/src/Math/MathUtility.cs +++ b/X10D/src/Math/MathUtility.cs @@ -99,4 +99,34 @@ public static class MathUtility // "precise" method: (1 - t) * a + t * b return ((1.0 - alpha) * value) + (alpha * target); } + + /// + /// Converts a value from being a percentage of one range, to being the same percentage in a new range. + /// + /// The value to convert. + /// The old minimum value. + /// The old maximum value. + /// The new minimum value. + /// The new maximum value. + /// The scaled value. + 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); + } + + /// + /// Converts a value from being a percentage of one range, to being the same percentage in a new range. + /// + /// The value to convert. + /// The old minimum value. + /// The old maximum value. + /// The new minimum value. + /// The new maximum value. + /// The scaled value. + 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); + } }