MathUtil Class
Namespace: Stride.Core.MathematicsAssembly: Stride.Core.Mathematics.dll
Common utility methods for math operations.
public static class MathUtil
| Name | Description | |
|---|---|---|
| Fields | ||
| Pi | A value specifying the approximation of π which is 180 degrees. |
|
| PiOverFour | A value specifying the approximation of π/4 which is 45 degrees. |
|
| PiOverTwo | A value specifying the approximation of π/2 which is 90 degrees. |
|
| TwoPi | A value specifying the approximation of 2π which is 360 degrees. |
|
| ZeroTolerance | The value for which all absolute numbers smaller than are considered equal to zero. |
|
| ZeroToleranceDouble | The value for which all absolute numbers smaller than are considered equal to zero. |
|
| Methods | ||
| AlignDown(Int32, Int32) | Alignes value down to match desire alignment. |
|
| AlignUp(Int32, Int32) | Alignes value up to match desire alignment. |
|
| Array<T>(T, Int32) | Creates a one-dimensional array of the specified |
|
| Clamp(Double, Double, Double) | Clamps the specified value. |
|
| Clamp(Int32, Int32, Int32) | Clamps the specified value. |
|
| Clamp(Single, Single, Single) | Clamps the specified value. |
|
| DegreesToRadians(Single) | Converts degrees to radians. |
|
| DegreesToRevolutions(Single) | Converts degrees to revolutions. |
|
| GradiansToDegrees(Single) | Converts gradians to degrees. |
|
| GradiansToRadians(Single) | Converts gradians to radians. |
|
| GradiansToRevolutions(Single) | Converts gradians to revolutions. |
|
| InverseLerp(Double, Double, Double) | Inverse-interpolates a value linearly. |
|
| InverseLerp(Single, Single, Single) | Inverse-interpolates a value linearly. |
|
| IsAligned(Int32, Int32) | Determines whether the specified value is aligned. |
|
| IsInRange(Int32, Int32, Int32) | Determines whether the value is inside the given range (inclusively). |
|
| IsInRange(Single, Single, Single) | Determines whether the value is inside the given range (inclusively). |
|
| IsOne(Single) | Determines whether the specified value is close to one (1.0f). |
|
| IsPow2(Int32) | Determines whether the specified x is pow2. |
|
| IsZero(Double) | Determines whether the specified value is close to zero (0.0f). |
|
| IsZero(Single) | Determines whether the specified value is close to zero (0.0f). |
|
| Lerp(Byte, Byte, Single) | Interpolates between two values using a linear function by a given amount. |
|
| Lerp(Double, Double, Double) | Interpolates between two values using a linear function by a given amount. |
|
| Lerp(Single, Single, Single) | Interpolates between two values using a linear function by a given amount. |
|
| LinearToSRgb(Single) | Converts a float value from linear to sRGB. |
|
| Log2(Int32) | Calculate the logarithm 2 of an integer. |
|
| Log2(Single) | Calculate the logarithm 2 of a floating point. |
|
| Mod(Single, Single) | Computes standard mathematical modulo (as opposed to remainder). |
|
| NearEqual(Single, Single) | Checks if a and b are almost equals, taking into account the magnitude of floating point numbers (unlike WithinEpsilon(Single, Single, Single) method). See Remarks. See remarks. |
|
| NextPowerOfTwo(Int32) | Get the next power of two of an integer. |
|
| NextPowerOfTwo(Single) | Get the next power of two for a size. |
|
| PreviousPowerOfTwo(Int32) | Get the previous power of two of the provided integer. |
|
| PreviousPowerOfTwo(Single) | Get the previous power of two of the provided float. |
|
| RadiansToDegrees(Single) | Converts radians to degrees. |
|
| RadiansToGradians(Single) | Converts radians to gradians. |
|
| RadiansToRevolutions(Single) | Converts radians to revolutions. |
|
| RevolutionsToDegrees(Single) | Converts revolutions to degrees. |
|
| RevolutionsToGradians(Single) | Converts revolutions to gradians. |
|
| RevolutionsToRadians(Single) | Converts revolutions to radians. |
|
| SmootherStep(Single) | Performs a smooth(er) interpolation between 0 and 1 with 1st and 2nd order derivatives of zero at endpoints. |
|
| SmoothStep(Single) | Performs smooth (cubic Hermite) interpolation between 0 and 1. |
|
| Snap(Vector2, Single) | Snaps all vector components to the nearest interval. |
|
| Snap(Vector3, Single) | Snaps all vector components to the nearest interval. |
|
| Snap(Vector4, Single) | Snaps all vector components to the nearest interval. |
|
| Snap(Double, Double) | Snaps a value to the nearest interval. |
|
| Snap(Single, Single) | Snaps a value to the nearest interval. |
|
| SRgbToLinear(Single) | Converts a float value from sRGB to linear. |
|
| WithinEpsilon(Single, Single, Single) | Checks if a - b are almost equals within a float epsilon. |
|
Fields
Pi
A value specifying the approximation of π which is 180 degrees.
public const float Pi = 3.14159274F
Field Value
| Type | Description |
|---|---|
| System.Single |
PiOverFour
A value specifying the approximation of π/4 which is 45 degrees.
public const float PiOverFour = 0.7853982F
Field Value
| Type | Description |
|---|---|
| System.Single |
PiOverTwo
A value specifying the approximation of π/2 which is 90 degrees.
public const float PiOverTwo = 1.57079637F
Field Value
| Type | Description |
|---|---|
| System.Single |
TwoPi
A value specifying the approximation of 2π which is 360 degrees.
public const float TwoPi = 6.28318548F
Field Value
| Type | Description |
|---|---|
| System.Single |
ZeroTolerance
The value for which all absolute numbers smaller than are considered equal to zero.
public const float ZeroTolerance = 1E-06F
Field Value
| Type | Description |
|---|---|
| System.Single |
ZeroToleranceDouble
The value for which all absolute numbers smaller than are considered equal to zero.
public const double ZeroToleranceDouble = 3.9525251667299724E-323
Field Value
| Type | Description |
|---|---|
| System.Double |
Methods
AlignDown(Int32, Int32)
Alignes value down to match desire alignment.
public static int AlignDown(int value, int alignment)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | value | The value. |
| System.Int32 | alignment | The alignment. |
Returns
| Type | Description |
|---|---|
| System.Int32 | Aligned value (multiple of alignment). |
AlignUp(Int32, Int32)
Alignes value up to match desire alignment.
public static int AlignUp(int value, int alignment)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | value | The value. |
| System.Int32 | alignment | The alignment. |
Returns
| Type | Description |
|---|---|
| System.Int32 | Aligned value (multiple of alignment). |
Array<T>(T, Int32)
Creates a one-dimensional array of the specified T and length filled with the specified value.
public static T[] Array<T>(T value, int length)
Parameters
| Type | Name | Description |
|---|---|---|
| T | value | The value to fill the array with. |
| System.Int32 | length | The size of the array to create. |
Returns
| Type | Description |
|---|---|
| T[] | A new one-dimensional array of the specified type with the specified length and filled with the specified value. |
Type Parameters
| Name | Description |
|---|---|
| T | The Type of the array to create. |
Clamp(Double, Double, Double)
Clamps the specified value.
public static double Clamp(double value, double min, double max)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Double | value | The value. |
| System.Double | min | The min. |
| System.Double | max | The max. |
Returns
| Type | Description |
|---|---|
| System.Double | The result of clamping a value between min and max |
Clamp(Int32, Int32, Int32)
Clamps the specified value.
public static int Clamp(int value, int min, int max)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | value | The value. |
| System.Int32 | min | The min. |
| System.Int32 | max | The max. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The result of clamping a value between min and max |
Clamp(Single, Single, Single)
Clamps the specified value.
public static float Clamp(float value, float min, float max)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Single | value | The value. |
| System.Single | min | The min. |
| System.Single | max | The max. |
Returns
| Type | Description |
|---|---|
| System.Single | The result of clamping a value between min and max |
DegreesToRadians(Single)
Converts degrees to radians.
public static float DegreesToRadians(float degree)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Single | degree | The value to convert. |
Returns
| Type | Description |
|---|---|
| System.Single | The converted value. |
DegreesToRevolutions(Single)
Converts degrees to revolutions.
public static float DegreesToRevolutions(float degree)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Single | degree | The value to convert. |
Returns
| Type | Description |
|---|---|
| System.Single | The converted value. |
GradiansToDegrees(Single)
Converts gradians to degrees.
public static float GradiansToDegrees(float gradian)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Single | gradian | The value to convert. |
Returns
| Type | Description |
|---|---|
| System.Single | The converted value. |
GradiansToRadians(Single)
Converts gradians to radians.
public static float GradiansToRadians(float gradian)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Single | gradian | The value to convert. |
Returns
| Type | Description |
|---|---|
| System.Single | The converted value. |
GradiansToRevolutions(Single)
Converts gradians to revolutions.
public static float GradiansToRevolutions(float gradian)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Single | gradian | The value to convert. |
Returns
| Type | Description |
|---|---|
| System.Single | The converted value. |
InverseLerp(Double, Double, Double)
Inverse-interpolates a value linearly.
public static double InverseLerp(double min, double max, double value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Double | min | Minimum value that takes place in inverse-interpolation. |
| System.Double | max | Maximum value that takes place in inverse-interpolation. |
| System.Double | value | Value to get inverse interpolation. |
Returns
| Type | Description |
|---|---|
| System.Double | Returns an inverse-linearly interpolated coeficient. |
InverseLerp(Single, Single, Single)
Inverse-interpolates a value linearly.
public static float InverseLerp(float min, float max, float value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Single | min | Minimum value that takes place in inverse-interpolation. |
| System.Single | max | Maximum value that takes place in inverse-interpolation. |
| System.Single | value | Value to get inverse interpolation. |
Returns
| Type | Description |
|---|---|
| System.Single | Returns an inverse-linearly interpolated coeficient. |
IsAligned(Int32, Int32)
Determines whether the specified value is aligned.
public static bool IsAligned(int value, int alignment)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | value | The value. |
| System.Int32 | alignment | The alignment. |
Returns
| Type | Description |
|---|---|
| System.Boolean |
|
IsInRange(Int32, Int32, Int32)
Determines whether the value is inside the given range (inclusively).
public static bool IsInRange(int value, int min, int max)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | value | The value. |
| System.Int32 | min | The minimum value of the range. |
| System.Int32 | max | The maximum value of the range. |
Returns
| Type | Description |
|---|---|
| System.Boolean |
|
IsInRange(Single, Single, Single)
Determines whether the value is inside the given range (inclusively).
public static bool IsInRange(float value, float min, float max)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Single | value | The value. |
| System.Single | min | The minimum value of the range. |
| System.Single | max | The maximum value of the range. |
Returns
| Type | Description |
|---|---|
| System.Boolean |
|
IsOne(Single)
Determines whether the specified value is close to one (1.0f).
public static bool IsOne(float a)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Single | a | The floating value. |
Returns
| Type | Description |
|---|---|
| System.Boolean |
|
IsPow2(Int32)
Determines whether the specified x is pow2.
public static bool IsPow2(int x)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | x | The x. |
Returns
| Type | Description |
|---|---|
| System.Boolean |
|
IsZero(Double)
Determines whether the specified value is close to zero (0.0f).
public static bool IsZero(double a)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Double | a | The floating value. |
Returns
| Type | Description |
|---|---|
| System.Boolean |
|
IsZero(Single)
Determines whether the specified value is close to zero (0.0f).
public static bool IsZero(float a)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Single | a | The floating value. |
Returns
| Type | Description |
|---|---|
| System.Boolean |
|
Lerp(Byte, Byte, Single)
Interpolates between two values using a linear function by a given amount.
public static byte Lerp(byte from, byte to, float amount)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Byte | from | Value to interpolate from. |
| System.Byte | to | Value to interpolate to. |
| System.Single | amount | Interpolation amount. |
Returns
| Type | Description |
|---|---|
| System.Byte | The result of linear interpolation of values based on the amount. |
Remarks
See http://www.encyclopediaofmath.org/index.php/Linear_interpolation and http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/
Lerp(Double, Double, Double)
Interpolates between two values using a linear function by a given amount.
public static double Lerp(double from, double to, double amount)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Double | from | Value to interpolate from. |
| System.Double | to | Value to interpolate to. |
| System.Double | amount | Interpolation amount. |
Returns
| Type | Description |
|---|---|
| System.Double | The result of linear interpolation of values based on the amount. |
Remarks
See http://www.encyclopediaofmath.org/index.php/Linear_interpolation and http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/
Lerp(Single, Single, Single)
Interpolates between two values using a linear function by a given amount.
public static float Lerp(float from, float to, float amount)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Single | from | Value to interpolate from. |
| System.Single | to | Value to interpolate to. |
| System.Single | amount | Interpolation amount. |
Returns
| Type | Description |
|---|---|
| System.Single | The result of linear interpolation of values based on the amount. |
Remarks
See http://www.encyclopediaofmath.org/index.php/Linear_interpolation and http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/
LinearToSRgb(Single)
Converts a float value from linear to sRGB.
public static float LinearToSRgb(float linearValue)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Single | linearValue | The linear value. |
Returns
| Type | Description |
|---|---|
| System.Single | The encoded sRGB value. |
Log2(Int32)
Calculate the logarithm 2 of an integer.
public static int Log2(int i)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | i | The input integer |
Returns
| Type | Description |
|---|---|
| System.Int32 |
Log2(Single)
Calculate the logarithm 2 of a floating point.
public static float Log2(float x)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Single | x | The input float |
Returns
| Type | Description |
|---|---|
| System.Single |
Mod(Single, Single)
Computes standard mathematical modulo (as opposed to remainder).
public static float Mod(float value, float divisor)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Single | value | The value. |
| System.Single | divisor | The divisor. |
Returns
| Type | Description |
|---|---|
| System.Single | A value between 0 and divisor. The result will have the same sign as divisor. |
NearEqual(Single, Single)
Checks if a and b are almost equals, taking into account the magnitude of floating point numbers (unlike WithinEpsilon(Single, Single, Single) method). See Remarks. See remarks.
public static bool NearEqual(float a, float b)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Single | a | The left value to compare. |
| System.Single | b | The right value to compare. |
Returns
| Type | Description |
|---|---|
| System.Boolean |
|
Remarks
The code is using the technique described by Bruce Dawson in Comparing Floating point numbers 2012 edition.
NextPowerOfTwo(Int32)
Get the next power of two of an integer.
public static int NextPowerOfTwo(int x)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | x | The size. |
Returns
| Type | Description |
|---|---|
| System.Int32 | System.Int32. |
Remarks
NextPowerOfTwo(Single)
Get the next power of two for a size.
public static float NextPowerOfTwo(float size)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Single | size | The size. |
Returns
| Type | Description |
|---|---|
| System.Single | System.Int32. |
PreviousPowerOfTwo(Int32)
Get the previous power of two of the provided integer.
public static int PreviousPowerOfTwo(int size)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | size | The value |
Returns
| Type | Description |
|---|---|
| System.Int32 |
PreviousPowerOfTwo(Single)
Get the previous power of two of the provided float.
public static float PreviousPowerOfTwo(float size)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Single | size | The value |
Returns
| Type | Description |
|---|---|
| System.Single |
RadiansToDegrees(Single)
Converts radians to degrees.
public static float RadiansToDegrees(float radian)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Single | radian | The value to convert. |
Returns
| Type | Description |
|---|---|
| System.Single | The converted value. |
RadiansToGradians(Single)
Converts radians to gradians.
public static float RadiansToGradians(float radian)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Single | radian | The value to convert. |
Returns
| Type | Description |
|---|---|
| System.Single | The converted value. |
RadiansToRevolutions(Single)
Converts radians to revolutions.
public static float RadiansToRevolutions(float radian)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Single | radian | The value to convert. |
Returns
| Type | Description |
|---|---|
| System.Single | The converted value. |
RevolutionsToDegrees(Single)
Converts revolutions to degrees.
public static float RevolutionsToDegrees(float revolution)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Single | revolution | The value to convert. |
Returns
| Type | Description |
|---|---|
| System.Single | The converted value. |
RevolutionsToGradians(Single)
Converts revolutions to gradians.
public static float RevolutionsToGradians(float revolution)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Single | revolution | The value to convert. |
Returns
| Type | Description |
|---|---|
| System.Single | The converted value. |
RevolutionsToRadians(Single)
Converts revolutions to radians.
public static float RevolutionsToRadians(float revolution)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Single | revolution | The value to convert. |
Returns
| Type | Description |
|---|---|
| System.Single | The converted value. |
SmootherStep(Single)
Performs a smooth(er) interpolation between 0 and 1 with 1st and 2nd order derivatives of zero at endpoints.
public static float SmootherStep(float amount)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Single | amount | Value between 0 and 1 indicating interpolation amount. |
Returns
| Type | Description |
|---|---|
| System.Single |
Remarks
SmoothStep(Single)
Performs smooth (cubic Hermite) interpolation between 0 and 1.
public static float SmoothStep(float amount)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Single | amount | Value between 0 and 1 indicating interpolation amount. |
Returns
| Type | Description |
|---|---|
| System.Single |
Remarks
Snap(Vector2, Single)
Snaps all vector components to the nearest interval.
public static Vector2 Snap(Vector2 value, float gap)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | value | The vector to snap. |
| System.Single | gap | The interval gap. |
Returns
| Type | Description |
|---|---|
| Vector2 | A vector which components are snapped to the nearest interval. |
Snap(Vector3, Single)
Snaps all vector components to the nearest interval.
public static Vector3 Snap(Vector3 value, float gap)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | value | The vector to snap. |
| System.Single | gap | The interval gap. |
Returns
| Type | Description |
|---|---|
| Vector3 | A vector which components are snapped to the nearest interval. |
Snap(Vector4, Single)
Snaps all vector components to the nearest interval.
public static Vector4 Snap(Vector4 value, float gap)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector4 | value | The vector to snap. |
| System.Single | gap | The interval gap. |
Returns
| Type | Description |
|---|---|
| Vector4 | A vector which components are snapped to the nearest interval. |
Snap(Double, Double)
Snaps a value to the nearest interval.
public static double Snap(double value, double gap)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Double | value | The value to snap. |
| System.Double | gap | The interval gap. |
Returns
| Type | Description |
|---|---|
| System.Double | The nearest interval to the provided value. |
Snap(Single, Single)
Snaps a value to the nearest interval.
public static float Snap(float value, float gap)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Single | value | The value to snap. |
| System.Single | gap | The interval gap. |
Returns
| Type | Description |
|---|---|
| System.Single | The nearest interval to the provided value. |
SRgbToLinear(Single)
Converts a float value from sRGB to linear.
public static float SRgbToLinear(float sRgbValue)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Single | sRgbValue | The sRGB value. |
Returns
| Type | Description |
|---|---|
| System.Single | A linear value. |
WithinEpsilon(Single, Single, Single)
Checks if a - b are almost equals within a float epsilon.
public static bool WithinEpsilon(float a, float b, float epsilon)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Single | a | The left value to compare. |
| System.Single | b | The right value to compare. |
| System.Single | epsilon | Epsilon value |
Returns
| Type | Description |
|---|---|
| System.Boolean |
|