Archived
Emergency backup
This commit is contained in:
@@ -170,6 +170,196 @@ namespace XericLibrary.Runtime.MacroLibrary
|
||||
public static int MinPositive(int a, int b)
|
||||
=> Math.Min(Math.Max(0, a), Math.Max(0, b));
|
||||
|
||||
/// <summary>
|
||||
/// Interpolates t between a and b to a value between 0 and 1 using a Hermite polynomial.
|
||||
/// </summary>
|
||||
/// <param name="a">The first value</param>
|
||||
/// <param name="b">The second value</param>
|
||||
/// <param name="t">The position value</param>
|
||||
/// <returns>A smoothed value between 0 and 1</returns>
|
||||
public static float SmoothStep(float a, float b, float t)
|
||||
{
|
||||
t = Mathf.Clamp01((t - a) / (b - a));
|
||||
return t * t * (3f - 2f * t);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Interpolates t between a and b to a value between 0 and 1.
|
||||
/// </summary>
|
||||
/// <param name="a">The first value</param>
|
||||
/// <param name="b">The second value</param>
|
||||
/// <param name="t">The position value</param>
|
||||
/// <returns>Linear value between 0 and 1</returns>
|
||||
public static float LinearStep(float a, float b, float t)
|
||||
{
|
||||
return Mathf.Clamp01((t - a) / (b - a));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps a value between min and max.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap</param>
|
||||
/// <param name="min">The minimum value</param>
|
||||
/// <param name="max">The maximum value</param>
|
||||
/// <returns></returns>
|
||||
public static double Wrap(double value, double min, double max)
|
||||
{
|
||||
double num = max - min;
|
||||
num = ((num < 0.0) ? (0.0 - num) : num);
|
||||
if(value < min)
|
||||
{
|
||||
return value + num * Math.Ceiling(Math.Abs(value / num));
|
||||
}
|
||||
|
||||
if(value >= max)
|
||||
{
|
||||
return value - num * Math.Floor(Math.Abs(value / num));
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps a value between min and max.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap</param>
|
||||
/// <param name="min">The minimum value</param>
|
||||
/// <param name="max">The maximum value</param>
|
||||
/// <returns></returns>
|
||||
public static float Wrap(float value, float min, float max)
|
||||
{
|
||||
float num = max - min;
|
||||
num = (((double)num < 0.0) ? (0f - num) : num);
|
||||
if(value < min)
|
||||
{
|
||||
return value + num * (float)Math.Ceiling(Math.Abs(value / num));
|
||||
}
|
||||
|
||||
if(value >= max)
|
||||
{
|
||||
return value - num * (float)Math.Floor(Math.Abs(value / num));
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps a value between min and max.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to wrap</param>
|
||||
/// <param name="min">The minimum value</param>
|
||||
/// <param name="max">The maximum value</param>
|
||||
/// <returns></returns>
|
||||
public static int Wrap(int value, int min, int max)
|
||||
{
|
||||
int num = max - min;
|
||||
num = ((num < 0) ? (-num) : num);
|
||||
if(value < min)
|
||||
{
|
||||
return value + num * (Math.Abs(value / num) + 1);
|
||||
}
|
||||
|
||||
if(value >= max)
|
||||
{
|
||||
return value - num * Math.Abs(value / num);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rounds a number based on a mininum difference.
|
||||
/// </summary>
|
||||
/// <param name="valueToRound">The value to round</param>
|
||||
/// <param name="minDifference">The min difference</param>
|
||||
/// <returns>The rounded value.</returns>
|
||||
public static double RoundBasedOnMinimumDifference(double valueToRound, double minDifference)
|
||||
{
|
||||
if(minDifference == 0.0)
|
||||
{
|
||||
return DiscardLeastSignificantDecimal(valueToRound);
|
||||
}
|
||||
|
||||
return (float)Math.Round(valueToRound, MacroMath.GetNumberOfDecimalsForMinimumDifference(minDifference), MidpointRounding.AwayFromZero);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Discards the least significant demicals.
|
||||
/// </summary>
|
||||
/// <param name="v">The value of insignificant decimals</param>
|
||||
/// <returns>Value with significant decimals</returns>
|
||||
public static double DiscardLeastSignificantDecimal(double v)
|
||||
{
|
||||
int digits = Math.Max(0, (int)(5.0 - Math.Log10(Math.Abs(v))));
|
||||
try
|
||||
{
|
||||
return Math.Round(v, digits);
|
||||
}
|
||||
catch(ArgumentOutOfRangeException)
|
||||
{
|
||||
return 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clamps and wraps an angle between two values.
|
||||
/// </summary>
|
||||
/// <param name="angle"></param>
|
||||
/// <param name="min"></param>
|
||||
/// <param name="max"></param>
|
||||
/// <returns></returns>
|
||||
public static float ClampWrapAngle(float angle, float min, float max)
|
||||
{
|
||||
float num = 360f;
|
||||
float num2 = min;
|
||||
float num3 = max;
|
||||
float num4 = angle;
|
||||
if(num2 < 0f)
|
||||
{
|
||||
num2 = num2 % num + num;
|
||||
}
|
||||
|
||||
if(num3 < 0f)
|
||||
{
|
||||
num3 = num3 % num + num;
|
||||
}
|
||||
|
||||
if(num4 < 0f)
|
||||
{
|
||||
num4 = num4 % num + num;
|
||||
}
|
||||
|
||||
float num5 = (float)(int)(Math.Abs(min - max) / num) * num;
|
||||
num3 += num5;
|
||||
num4 += num5;
|
||||
if(min > max)
|
||||
{
|
||||
num3 += num;
|
||||
}
|
||||
|
||||
if(num4 < num2)
|
||||
{
|
||||
num4 = num2;
|
||||
}
|
||||
|
||||
if(num4 > num3)
|
||||
{
|
||||
num4 = num3;
|
||||
}
|
||||
|
||||
return num4;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取最小差值的小数个数
|
||||
/// </summary>
|
||||
/// <param name="minDifference"></param>
|
||||
/// <returns></returns>
|
||||
public static int GetNumberOfDecimalsForMinimumDifference(double minDifference)
|
||||
{
|
||||
return Mathf.Clamp(-Mathf.FloorToInt(Mathf.Log10(Mathf.Abs((float)minDifference))), 0, 15);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 字节控制
|
||||
|
||||
Reference in New Issue
Block a user