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