Files
Voile/DaggerFramework/Source/Utils/MathUtils.cs

56 lines
1.5 KiB
C#

using System.Numerics;
namespace DaggerFramework
{
public static class MathUtils
{
public static float Lerp(float a, float b, double t) => a + (b - a) * (float)t;
public static Color LerpColor(Color colorA, Color colorB, double t)
{
var r = (byte)Lerp(colorA.R, colorB.R, t);
var g = (byte)Lerp(colorA.G, colorB.G, t);
var b = (byte)Lerp(colorA.B, colorB.B, t);
var a = (byte)Lerp(colorA.A, colorB.A, t);
return new Color(r, g, b, a);
}
public static Vector2 LerpVector2(Vector2 v1, Vector2 v2, double t)
{
var x = Lerp(v1.X, v2.X, t);
var y = Lerp(v1.Y, v2.Y, t);
return new Vector2(x, y);
}
public static Vector2 RandomVector2(Vector2 min, Vector2 max)
{
var x = _random.NextDouble(min.X, max.X);
var y = _random.NextDouble(min.Y, max.Y);
return new Vector2((float)x, (float)y);
}
public static float EaseOutBack(float x)
{
var c1 = 1.70158f;
var c3 = c1 + 1f;
return 1f + c3 * (float)Math.Pow(x - 1f, 3f) + c1 * (float)Math.Pow(x - 1, 2);
}
public static float EaseOutElastic(float x)
{
var c4 = 2f * (float)Math.PI / 3f;
return x == 0
? 0
: x == 1
? 1
: (float)Math.Pow(2, -10 * x) * (float)Math.Sin((x * 10 - 0.75f) * c4) + 1;
}
private static LehmerRandom _random = new();
}
}