199 lines
6.2 KiB
C#
199 lines
6.2 KiB
C#
using System;
|
|
using JD.Kernel.StaticHelpers;
|
|
|
|
namespace JD.Maths.Utils
|
|
{
|
|
/// <summary>
|
|
/// Math Constants.
|
|
/// </summary>
|
|
public static class MathConstants
|
|
{
|
|
#region Constants
|
|
|
|
/// <summary>
|
|
/// 2 * PI = 360°
|
|
/// </summary>
|
|
public const float TwoPI = 2f * PI;
|
|
|
|
/// <summary>
|
|
/// PI (3.141592...) = 180°
|
|
/// </summary>
|
|
public const float PI = (float)System.Math.PI;
|
|
|
|
/// <summary>
|
|
/// PI / 2 = 90°
|
|
/// </summary>
|
|
public const float PIOverTwo = PI / 2f;
|
|
|
|
/// <summary>
|
|
/// PI / 4 = 45°
|
|
/// </summary>
|
|
public const float PIOverFour = PI / 4f;
|
|
|
|
/// <summary>
|
|
/// PI / 6 = 30°
|
|
/// </summary>
|
|
public const float PIOverSix = PI / 6f;
|
|
|
|
/// <summary>
|
|
/// PI / 12 = 15°
|
|
/// </summary>
|
|
public const float PIOverTwelve = PI / 12f;
|
|
|
|
/// <summary>
|
|
/// General epsilon that fit most of commun case. Equals 10e-5.
|
|
/// </summary>
|
|
/// <remarks>float.Epsilon is way to small to made difference check.</remarks>
|
|
public const float Epsilon = 10e-5f;
|
|
|
|
/// <summary>
|
|
/// Factor on which to multiply radians to express them in degrees.
|
|
/// </summary>
|
|
public const float RadiansToDegreesFactor = 180f / PI;
|
|
|
|
/// <summary>
|
|
/// Factor on which to multiply degrees to express them in radians.
|
|
/// </summary>
|
|
public const float DegreesToRadiansFactor = PI / 180f;
|
|
|
|
#endregion
|
|
|
|
#region Methods
|
|
|
|
/// <summary>
|
|
/// Converts the given <paramref name="degrees"/> into radians.
|
|
/// </summary>
|
|
/// <param name="degrees"></param>
|
|
/// <returns></returns>
|
|
public static float DegreesToRadians(float degrees)
|
|
{
|
|
return degrees / RadiansToDegreesFactor;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts the given <paramref name="radians"/> into degrees.
|
|
/// </summary>
|
|
/// <param name="radians"></param>
|
|
/// <returns></returns>
|
|
public static float RadiansToDegrees(float radians)
|
|
{
|
|
return radians * RadiansToDegreesFactor;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check if <paramref name="lhs"/> and <paramref name="rhs"/> are equals with <paramref name="epsilon"/>.
|
|
/// </summary>
|
|
public static bool EqualsWithEpsilon(float rhs, float lhs, float epsilon)
|
|
{
|
|
Throw.InvalidArgumentIf(epsilon < 0f, "The epsilon must but equals or greater than zero.", "epsilon");
|
|
|
|
return System.Math.Abs(rhs - lhs) <= epsilon;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check if <paramref name="lhs"/> and <paramref name="rhs"/> are equals with <paramref name="epsilon"/>.
|
|
/// </summary>
|
|
public static bool EqualsWithEpsilon(double rhs, double lhs, double epsilon)
|
|
{
|
|
Throw.InvalidArgumentIf(epsilon < 0d, "The epsilon must but equals or greater than zero.", "epsilon");
|
|
|
|
return System.Math.Abs(rhs - lhs) <= epsilon;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check if <paramref name="lhs"/> and <paramref name="rhs"/> are equals with <see cref="Epsilon"/>.
|
|
/// </summary>
|
|
public static bool EqualsWithEpsilon(float rhs, float lhs)
|
|
{
|
|
return EqualsWithEpsilon(lhs, rhs, Epsilon);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check if <paramref name="greater"/> is greater that <paramref name="less"/>.
|
|
/// </summary>
|
|
/// <remarks>Equality is check with <see cref="Epsilon"/></remarks>
|
|
public static bool GreaterThanWithEpsilon(float greater, float less)
|
|
{
|
|
return greater > less && !EqualsWithEpsilon(greater, less);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check if <paramref name="greater"/> is greater or equals that <paramref name="less"/>.
|
|
/// </summary>
|
|
public static bool GreaterThanOrEqualsWithEpsilon(float greater, float less)
|
|
{
|
|
return GreaterThanOrEqualsWithEpsilon(greater, less, Epsilon);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check if <paramref name="greater"/> is greater or equals that <paramref name="less"/>.
|
|
/// </summary>
|
|
public static bool GreaterThanOrEqualsWithEpsilon(float greater, float less, float epsilon)
|
|
{
|
|
return greater + epsilon > less;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check if <paramref name="x"/> is an odd number
|
|
/// </summary>
|
|
public static bool IsOdd(int x)
|
|
{
|
|
return ((x % 2) != 0);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check if <paramref name="integer"/> is a power of two.
|
|
/// </summary>
|
|
|
|
public static bool IsPowerOfTwo(uint integer)
|
|
{
|
|
return (integer != 0) &&
|
|
((integer & (integer - 1)) == 0);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check if <paramref name="integer"/> is a power of two.
|
|
/// </summary>
|
|
|
|
public static bool IsPowerOfTwo(ushort integer)
|
|
{
|
|
return (integer != 0) &&
|
|
((integer & (integer - 1)) == 0);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check if <paramref name="integer"/> is a power of two.
|
|
/// </summary>
|
|
public static bool IsPowerOfTwo(byte integer)
|
|
{
|
|
return (integer != 0) &&
|
|
((integer & (integer - 1)) == 0);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns <paramref name="actual"/> clamped between <paramref name="min"/> and <paramref name="max"/>.
|
|
/// </summary>
|
|
public static T Clamp<T>(this T actual, T min, T max)
|
|
where T : IComparable<T>
|
|
{
|
|
Throw.InvalidArgumentIf(min.CompareTo(max) > 0, "The minimum cannot be higher than the maximum.", "min");
|
|
|
|
if (actual.CompareTo(max) > 0)
|
|
{
|
|
return max;
|
|
}
|
|
|
|
return actual.CompareTo(min) < 0 ?
|
|
min :
|
|
actual;
|
|
}
|
|
|
|
public static bool IsBetween<T>(this T actual, T min, T max)
|
|
where T : struct, IComparable<T>
|
|
{
|
|
return actual.CompareTo(min) >= 0 && actual.CompareTo(max) <= 0; ;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|