2024-01-24 12:22:04 +03:00
|
|
|
using System;
|
2024-01-30 12:03:53 +03:00
|
|
|
using System.Numerics;
|
2024-01-24 12:22:04 +03:00
|
|
|
|
|
|
|
namespace Syntriax.Engine.Core;
|
|
|
|
|
|
|
|
public static class Math
|
2024-10-20 14:40:58 +03:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// The value of Pi (π), a mathematical constant approximately equal to 3.14159.
|
|
|
|
/// </summary>
|
2024-01-24 12:22:04 +03:00
|
|
|
public const float PI = 3.1415926535897932f;
|
2024-02-01 12:56:42 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The value of Tau (τ), a mathematical constant equal to 2π, approximately equal to 6.28319.
|
|
|
|
/// </summary>
|
2024-01-24 12:28:01 +03:00
|
|
|
public const float Tau = 2f * PI;
|
2024-01-24 12:22:04 +03:00
|
|
|
|
2024-02-01 12:56:42 +03:00
|
|
|
/// <summary>
|
|
|
|
/// The base of the natural logarithm, approximately equal to 2.71828.
|
|
|
|
/// </summary>
|
|
|
|
public const float E = 2.718281828459045f;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The conversion factor from radians to degrees.
|
|
|
|
/// </summary>
|
|
|
|
public const float RadianToDegree = 180f / PI;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The conversion factor from degrees to radians.
|
|
|
|
/// </summary>
|
|
|
|
public const float DegreeToRadian = PI / 180f;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns the absolute value of a number.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T">The type of the number.</typeparam>
|
|
|
|
/// <param name="x">The number.</param>
|
|
|
|
/// <returns>The absolute value of <paramref name="x"/>.</returns>
|
2024-01-30 12:03:53 +03:00
|
|
|
public static T Abs<T>(T x) where T : INumber<T> => x > T.Zero ? x : -x;
|
2024-02-01 12:56:42 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns the arccosine of a number.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The number.</param>
|
|
|
|
/// <returns>The arccosine of <paramref name="x"/>.</returns>
|
2024-01-24 12:22:04 +03:00
|
|
|
public static float Acos(float x) => MathF.Acos(x);
|
2024-02-01 12:56:42 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns the arcsine of a number.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The number.</param>
|
|
|
|
/// <returns>The arcsine of <paramref name="x"/>.</returns>
|
2024-01-24 12:22:04 +03:00
|
|
|
public static float Asin(float x) => MathF.Asin(x);
|
2024-02-01 12:56:42 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns the angle whose tangent is the quotient of two specified numbers.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="y">The y-coordinate of a point.</param>
|
|
|
|
/// <param name="x">The x-coordinate of a point.</param>
|
|
|
|
/// <returns>The angle, measured in radians.</returns>
|
2024-01-24 12:22:04 +03:00
|
|
|
public static float Atan2(float y, float x) => MathF.Atan2(y, x);
|
2024-02-01 12:56:42 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns the hyperbolic arctangent of a number.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The number.</param>
|
|
|
|
/// <returns>The hyperbolic arctangent of <paramref name="x"/>.</returns>
|
2024-01-24 12:22:04 +03:00
|
|
|
public static float Atanh(float x) => MathF.Atanh(x);
|
2024-02-01 12:56:42 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Clamps a number between a minimum and maximum value.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T">The type of the number.</typeparam>
|
|
|
|
/// <param name="x">The number to clamp.</param>
|
|
|
|
/// <param name="min">The minimum value.</param>
|
|
|
|
/// <param name="max">The maximum value.</param>
|
|
|
|
/// <returns>The clamped value.</returns>
|
2024-01-30 12:03:53 +03:00
|
|
|
public static T Clamp<T>(this T x, T min, T max) where T : INumber<T> => (x < min) ? min : (x > max) ? max : x;
|
2024-02-01 12:56:42 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns the smallest integral value that is greater than or equal to the specified number.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The number.</param>
|
|
|
|
/// <returns>The smallest integral value that is greater than or equal to <paramref name="x"/>.</returns>
|
2024-01-24 12:22:04 +03:00
|
|
|
public static float Ceiling(float x) => MathF.Ceiling(x);
|
2024-02-01 12:56:42 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns a value with the magnitude of <paramref name="x"/> and the sign of <paramref name="y"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The magnitude value.</param>
|
|
|
|
/// <param name="y">The sign value.</param>
|
|
|
|
/// <returns>A value with the magnitude of <paramref name="x"/> and the sign of <paramref name="y"/>.</returns>
|
2024-01-24 12:22:04 +03:00
|
|
|
public static float CopySign(float x, float y) => MathF.CopySign(x, y);
|
2024-02-01 12:56:42 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns the largest integral value that is less than or equal to the specified number.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The number.</param>
|
|
|
|
/// <returns>The largest integral value that is less than or equal to <paramref name="x"/>.</returns>
|
2024-01-24 12:22:04 +03:00
|
|
|
public static float Floor(float x) => MathF.Floor(x);
|
2024-02-01 12:56:42 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns the remainder of the division of two specified numbers.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The dividend.</param>
|
|
|
|
/// <param name="y">The divisor.</param>
|
|
|
|
/// <returns>The remainder of the division of <paramref name="x"/> by <paramref name="y"/>.</returns>
|
2024-01-24 12:22:04 +03:00
|
|
|
public static float IEEERemainder(float x, float y) => MathF.IEEERemainder(x, y);
|
2024-02-01 12:56:42 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns the natural (base e) logarithm of a specified number.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The number.</param>
|
|
|
|
/// <param name="y">The base.</param>
|
|
|
|
/// <returns>The natural logarithm of <paramref name="x"/> with base <paramref name="y"/>.</returns>
|
2024-01-24 12:22:04 +03:00
|
|
|
public static float Log(float x, float y) => MathF.Log(x, y);
|
2024-02-01 12:56:42 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns the larger of two numbers.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T">The type of the numbers.</typeparam>
|
|
|
|
/// <param name="x">The first number.</param>
|
|
|
|
/// <param name="y">The second number.</param>
|
|
|
|
/// <returns>The larger of <paramref name="x"/> and <paramref name="y"/>.</returns>
|
2024-01-30 12:03:53 +03:00
|
|
|
public static T Max<T>(T x, T y) where T : INumber<T> => (x > y) ? x : y;
|
2024-02-01 12:56:42 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns the number whose absolute value is larger.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The first number.</param>
|
|
|
|
/// <param name="y">The second number.</param>
|
|
|
|
/// <returns>The number whose absolute value is larger.</returns>
|
2024-02-01 13:01:35 +03:00
|
|
|
public static T AbsMax<T>(T x, T y) where T : INumber<T> => (Abs(x) > Abs(y)) ? x : y;
|
2024-02-01 12:56:42 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns the smaller of two numbers.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T">The type of the numbers.</typeparam>
|
|
|
|
/// <param name="x">The first number.</param>
|
|
|
|
/// <param name="y">The second number.</param>
|
|
|
|
/// <returns>The smaller of <paramref name="x"/> and <paramref name="y"/>.</returns>
|
2024-01-30 12:03:53 +03:00
|
|
|
public static T Min<T>(T x, T y) where T : INumber<T> => (x < y) ? x : y;
|
2024-02-01 12:56:42 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns the number whose absolute value is smaller.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The first number.</param>
|
|
|
|
/// <param name="y">The second number.</param>
|
|
|
|
/// <returns>The number whose absolute value is smaller.</returns>
|
2024-02-01 13:01:35 +03:00
|
|
|
public static T AbsMin<T>(T x, T y) where T : INumber<T> => (Abs(x) < Abs(y)) ? x : y;
|
2024-02-01 12:56:42 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns a specified number raised to the specified power.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The number to raise to a power.</param>
|
|
|
|
/// <param name="y">The power to raise <paramref name="x"/> to.</param>
|
|
|
|
/// <returns>The number <paramref name="x"/> raised to the power <paramref name="y"/>.</returns>
|
2024-01-24 12:22:04 +03:00
|
|
|
public static float Pow(float x, float y) => MathF.Pow(x, y);
|
2024-02-01 12:56:42 +03:00
|
|
|
|
2024-10-20 14:47:42 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Performs linear interpolation between two specified values.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T">The type of the values, which must implement <see cref="IFloatingPoint{T}"/>.</typeparam>
|
|
|
|
/// <param name="x">The starting value of the interpolation.</param>
|
|
|
|
/// <param name="y">The ending value of the interpolation.</param>
|
|
|
|
/// <param name="t">The interpolation factor, typically in the range [0, 1].</param>
|
|
|
|
/// <returns>A value that represents the linear interpolation between <paramref name="x"/> and <paramref name="y"/>.</returns>
|
|
|
|
public static T Lerp<T>(T x, T y, T t) where T : IFloatingPoint<T> => x + (y - x) * t;
|
|
|
|
|
2024-02-01 12:56:42 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Rounds a number to a specified number of fractional digits.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The number to round.</param>
|
|
|
|
/// <param name="digits">The number of fractional digits in the return value.</param>
|
|
|
|
/// <param name="mode">Specification for how to round <paramref name="x"/> if it is midway between two other numbers.</param>
|
|
|
|
/// <returns>The number <paramref name="x"/> rounded to <paramref name="digits"/> fractional digits.</returns>
|
2024-01-24 12:22:04 +03:00
|
|
|
public static float Round(float x, int digits, MidpointRounding mode) => MathF.Round(x, digits, mode);
|
2024-02-01 12:56:42 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns the square of a number.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T">The type of the number.</typeparam>
|
|
|
|
/// <param name="x">The number to square.</param>
|
|
|
|
/// <returns>The square of <paramref name="x"/>.</returns>
|
2024-01-30 12:03:53 +03:00
|
|
|
public static T Sqr<T>(T x) where T : INumber<T> => x * x;
|
2024-02-01 12:56:42 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns the square root of a specified number.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The number.</param>
|
|
|
|
/// <returns>The square root of <paramref name="x"/>.</returns>
|
2024-01-25 12:49:21 +03:00
|
|
|
public static float Sqrt(float x) => MathF.Sqrt(x);
|
2024-02-01 12:56:42 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Calculates the integral part of a number.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The number.</param>
|
|
|
|
/// <returns>The integral part of <paramref name="x"/>.</returns>
|
2024-01-24 12:22:04 +03:00
|
|
|
public static float Truncate(float x) => MathF.Truncate(x);
|
2024-02-01 12:56:42 +03:00
|
|
|
|
2024-01-24 12:22:04 +03:00
|
|
|
}
|