1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-23 01:18:46 +00:00

feat: add MathUtility.SmoothStep (#60)

This commit is contained in:
Oliver Booth 2023-04-03 15:47:18 +01:00
parent 105ff81713
commit d27f4caef7
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
3 changed files with 51 additions and 1 deletions

View File

@ -13,11 +13,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added new library X10D.Hosting. - Added new library X10D.Hosting.
- Added .NET 7 target. - Added .NET 7 target.
- X10D: Added `IntrinsicExtensions` and `IntrinsicUtility` which offer methods for vectors in `System.Runtime.Instrinsics`. (#70) - X10D: Added `IntrinsicExtensions` and `IntrinsicUtility` which offer methods for vectors in `System.Runtime.Instrinsics`. (#70)
- X10D: Added `MathUtility.Bias(float, float)`. - X10D: Added `MathUtility.Bias(float, float)` and `MathUtility.Bias(double, double)`.
- X10D: Added `MathUtility.InverseLerp(float, float, float)` and `MathUtility.InverseLerp(double, double, double)` - X10D: Added `MathUtility.InverseLerp(float, float, float)` and `MathUtility.InverseLerp(double, double, double)`
- X10D: Added `MathUtility.ScaleRange(float, float, float, float, float)` - X10D: Added `MathUtility.ScaleRange(float, float, float, float, float)`
and `MathUtility.ScaleRange(double, double, double, double, double)` and `MathUtility.ScaleRange(double, double, double, double, double)`
- X10D: - X10D:
- X10D: Added `MathUtility.SmoothStep(float, float, float)` and `MathUtility.SmoothStep(double, double, double)`.
Added `Circle`, `CircleF`, `Cuboid`, `Ellipse`, `EllipseF`, `Line3D`, `Line`, `LineF`, `Polygon`, `PolygonF`, `Polyhedron`, Added `Circle`, `CircleF`, `Cuboid`, `Ellipse`, `EllipseF`, `Line3D`, `Line`, `LineF`, `Polygon`, `PolygonF`, `Polyhedron`,
and `Sphere`, to complement System.Drawing structs such as `Point` and `Rectangle` and `Sphere`, to complement System.Drawing structs such as `Point` and `Rectangle`
- X10D: Added `Color.Deconstruct()` - with optional alpha parameter. - X10D: Added `Color.Deconstruct()` - with optional alpha parameter.

View File

@ -150,4 +150,25 @@ public class MathUtilityTests
float result = MathUtility.ScaleRange(0.5f, 0.0f, 1.0f, 5.0f, 10.0f); float result = MathUtility.ScaleRange(0.5f, 0.0f, 1.0f, 5.0f, 10.0f);
Assert.AreEqual(7.5f, result); Assert.AreEqual(7.5f, result);
} }
[TestMethod]
public void SmoothStep_ShouldReturnHigher_GivenAlpha1()
{
Assert.AreEqual(20.0f, MathUtility.SmoothStep(10.0f, 20.0f, 1.0f));
Assert.AreEqual(20.0, MathUtility.SmoothStep(10.0, 20.0, 1.0));
}
[TestMethod]
public void SmoothStep_ShouldReturnLower_GivenAlpha0()
{
Assert.AreEqual(10.0f, MathUtility.SmoothStep(10.0f, 20.0f, 0.0f));
Assert.AreEqual(10.0, MathUtility.SmoothStep(10.0, 20.0, 0.0));
}
[TestMethod]
public void SmoothStep_ShouldReturnMidPoint_GivenAlphaPoint5()
{
Assert.AreEqual(15.0f, MathUtility.SmoothStep(10.0f, 20.0f, 0.5f));
Assert.AreEqual(15.0, MathUtility.SmoothStep(10.0, 20.0, 0.5));
}
} }

View File

@ -308,4 +308,32 @@ public static class MathUtility
double alpha = (value - oldMin) / oldRange; double alpha = (value - oldMin) / oldRange;
return (alpha * newRange) + newMin; return (alpha * newRange) + newMin;
} }
/// <summary>
/// Performs smooth Hermite interpolation from one value to a target using a specified alpha.
/// </summary>
/// <param name="value">The interpolation source.</param>
/// <param name="target">The interpolation target.</param>
/// <param name="alpha">The interpolation alpha.</param>
/// <returns>The interpolation result.</returns>
public static float SmoothStep(float value, float target, float alpha)
{
alpha = System.Math.Clamp(alpha, 0.0f, 1.0f);
alpha = -2.0f * alpha * alpha * alpha + 3.0f * alpha * alpha;
return target * alpha + value * (1.0f - alpha);
}
/// <summary>
/// Performs smooth Hermite interpolation from one value to a target using a specified alpha.
/// </summary>
/// <param name="value">The interpolation source.</param>
/// <param name="target">The interpolation target.</param>
/// <param name="alpha">The interpolation alpha.</param>
/// <returns>The interpolation result.</returns>
public static double SmoothStep(double value, double target, double alpha)
{
alpha = System.Math.Clamp(alpha, 0.0, 1.0);
alpha = -2.0 * alpha * alpha * alpha + 3.0 * alpha * alpha;
return target * alpha + value * (1.0 - alpha);
}
} }