mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-09 22:55:42 +00:00
feat: add MathUtility.SmoothStep (#60)
This commit is contained in:
parent
105ff81713
commit
d27f4caef7
@ -13,11 +13,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
- Added new library X10D.Hosting.
|
||||
- Added .NET 7 target.
|
||||
- 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.ScaleRange(float, float, float, float, float)`
|
||||
and `MathUtility.ScaleRange(double, double, double, double, double)`
|
||||
- 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`,
|
||||
and `Sphere`, to complement System.Drawing structs such as `Point` and `Rectangle`
|
||||
- X10D: Added `Color.Deconstruct()` - with optional alpha parameter.
|
||||
|
@ -150,4 +150,25 @@ public class MathUtilityTests
|
||||
float result = MathUtility.ScaleRange(0.5f, 0.0f, 1.0f, 5.0f, 10.0f);
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
@ -308,4 +308,32 @@ public static class MathUtility
|
||||
double alpha = (value - oldMin) / oldRange;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user