Class MathUtil
- Namespace
- Stride.Core.Mathematics
- Assembly
- Stride.Core.Mathematics.dll
Common utility methods for math operations.
public static class MathUtil
- Inheritance
-
MathUtil
Fields
Pi
A value specifying the approximation of π which is 180 degrees.
public const float Pi = 3.1415927
Field Value
PiOverFour
A value specifying the approximation of π/4 which is 45 degrees.
public const float PiOverFour = 0.7853982
Field Value
PiOverTwo
A value specifying the approximation of π/2 which is 90 degrees.
public const float PiOverTwo = 1.5707964
Field Value
TwoPi
A value specifying the approximation of 2π which is 360 degrees.
public const float TwoPi = 6.2831855
Field Value
ZeroTolerance
The value for which all absolute numbers smaller than are considered equal to zero.
public const float ZeroTolerance = 1E-06
Field Value
ZeroToleranceDouble
The value for which all absolute numbers smaller than are considered equal to zero.
public const double ZeroToleranceDouble = 4E-323
Field Value
Methods
AlignDown(int, int)
Alignes value down to match desire alignment.
public static int AlignDown(int value, int alignment)
Parameters
Returns
- int
Aligned value (multiple of alignment).
AlignUp(int, int)
Alignes value up to match desire alignment.
public static int AlignUp(int value, int alignment)
Parameters
Returns
- int
Aligned value (multiple of alignment).
Array<T>(T, int)
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
value
TThe value to fill the array with.
length
intThe size of the array to create.
Returns
- T[]
A new one-dimensional array of the specified type with the specified length and filled with the specified value.
Type Parameters
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
Returns
- double
The result of clamping a value between min and max
Clamp(int, int, int)
Clamps the specified value.
public static int Clamp(int value, int min, int max)
Parameters
Returns
- int
The result of clamping a value between min and max
Clamp(float, float, float)
Clamps the specified value.
public static float Clamp(float value, float min, float max)
Parameters
Returns
- float
The result of clamping a value between min and max
DegreesToRadians(float)
Converts degrees to radians.
public static float DegreesToRadians(float degree)
Parameters
degree
floatThe value to convert.
Returns
- float
The converted value.
DegreesToRevolutions(float)
Converts degrees to revolutions.
public static float DegreesToRevolutions(float degree)
Parameters
degree
floatThe value to convert.
Returns
- float
The converted value.
ExpDecay(double, double, double, double)
Exponential damping. Alternative to
a = lerp(a, b, damping * dt)
using the exponential function flipped around the Y axis: e^(-t)
public static double ExpDecay(double a, double b, double lambda, double dt)
Parameters
a
doubleInitial value
b
doublePlateau value
lambda
doubleDamping
dt
doubleDiscrete time unit, delta time.
Returns
- double
A value interpolated from a to b depending on lambda and dt.
ExpDecay(float, float, float, float)
Exponential damping. Alternative to
a = lerp(a, b, damping * dt)
using the exponential function flipped around the Y axis: e^(-t)
public static float ExpDecay(float a, float b, float lambda, float dt)
Parameters
a
floatInitial value
b
floatPlateau value
lambda
floatDamping
dt
floatDiscrete time unit, delta time.
Returns
- float
A value interpolated from a to b depending on lambda and dt.
GradiansToDegrees(float)
Converts gradians to degrees.
public static float GradiansToDegrees(float gradian)
Parameters
gradian
floatThe value to convert.
Returns
- float
The converted value.
GradiansToRadians(float)
Converts gradians to radians.
public static float GradiansToRadians(float gradian)
Parameters
gradian
floatThe value to convert.
Returns
- float
The converted value.
GradiansToRevolutions(float)
Converts gradians to revolutions.
public static float GradiansToRevolutions(float gradian)
Parameters
gradian
floatThe value to convert.
Returns
- float
The converted value.
InverseLerp(double, double, double)
Inverse-interpolates a value linearly.
public static double InverseLerp(double min, double max, double value)
Parameters
min
doubleMinimum value that takes place in inverse-interpolation.
max
doubleMaximum value that takes place in inverse-interpolation.
value
doubleValue to get inverse interpolation.
Returns
- double
Returns an inverse-linearly interpolated coeficient.
InverseLerp(float, float, float)
Inverse-interpolates a value linearly.
public static float InverseLerp(float min, float max, float value)
Parameters
min
floatMinimum value that takes place in inverse-interpolation.
max
floatMaximum value that takes place in inverse-interpolation.
value
floatValue to get inverse interpolation.
Returns
- float
Returns an inverse-linearly interpolated coeficient.
IsAligned(int, int)
Determines whether the specified value is aligned.
public static bool IsAligned(int value, int alignment)
Parameters
Returns
- bool
true
if the specified value is aligned; otherwise,false
.
IsInRange(int, int, int)
Determines whether the value is inside the given range (inclusively).
public static bool IsInRange(int value, int min, int max)
Parameters
value
intThe value.
min
intThe minimum value of the range.
max
intThe maximum value of the range.
Returns
- bool
true
if value is inside the specified range; otherwise,false
.
IsInRange(float, float, float)
Determines whether the value is inside the given range (inclusively).
public static bool IsInRange(float value, float min, float max)
Parameters
value
floatThe value.
min
floatThe minimum value of the range.
max
floatThe maximum value of the range.
Returns
- bool
true
if value is inside the specified range; otherwise,false
.
IsOne(float)
Determines whether the specified value is close to one (1.0f).
public static bool IsOne(float a)
Parameters
a
floatThe floating value.
Returns
- bool
true
if the specified value is close to one (1.0f); otherwise,false
.
IsPow2(int)
Determines whether the specified x is pow2.
public static bool IsPow2(int x)
Parameters
x
intThe x.
Returns
- bool
true
if the specified x is pow2; otherwise,false
.
IsZero(double)
Determines whether the specified value is close to zero (0.0f).
public static bool IsZero(double a)
Parameters
a
doubleThe floating value.
Returns
- bool
true
if the specified value is close to zero (0.0f); otherwise,false
.
IsZero(float)
Determines whether the specified value is close to zero (0.0f).
public static bool IsZero(float a)
Parameters
a
floatThe floating value.
Returns
- bool
true
if the specified value is close to zero (0.0f); otherwise,false
.
Lerp(byte, byte, float)
Interpolates between two values using a linear function by a given amount.
public static byte Lerp(byte from, byte to, float amount)
Parameters
from
byteValue to interpolate from.
to
byteValue to interpolate to.
amount
floatInterpolation amount.
Returns
- 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
from
doubleValue to interpolate from.
to
doubleValue to interpolate to.
amount
doubleInterpolation amount.
Returns
- 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(float, float, float)
Interpolates between two values using a linear function by a given amount.
public static float Lerp(float from, float to, float amount)
Parameters
from
floatValue to interpolate from.
to
floatValue to interpolate to.
amount
floatInterpolation amount.
Returns
- float
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(float)
Converts a float value from linear to sRGB.
public static float LinearToSRgb(float linearValue)
Parameters
linearValue
floatThe linear value.
Returns
- float
The encoded sRGB value.
Log2(int)
Calculate the logarithm 2 of an integer.
public static int Log2(int i)
Parameters
i
intThe input integer
Returns
- int
the log2(i) rounded to lower integer
Log2(float)
Calculate the logarithm 2 of a floating point.
public static float Log2(float x)
Parameters
x
floatThe input float
Returns
- float
Log2(x)
Mod(float, float)
Computes standard mathematical modulo (as opposed to remainder).
public static float Mod(float value, float divisor)
Parameters
Returns
- float
A value between 0 and divisor. The result will have the same sign as divisor.
NearEqual(float, float)
Checks if a and b are almost equals, taking into account the magnitude of floating point numbers (unlike WithinEpsilon(float, float, float) method). See Remarks. See remarks.
public static bool NearEqual(float a, float b)
Parameters
Returns
- bool
true
if a almost equal to b,false
otherwise
Remarks
The code is using the technique described by Bruce Dawson in Comparing Floating point numbers 2012 edition.
NextPowerOfTwo(int)
Get the next power of two of an integer.
public static int NextPowerOfTwo(int x)
Parameters
x
intThe size.
Returns
- int
System.Int32.
Remarks
NextPowerOfTwo(float)
Get the next power of two for a size.
public static float NextPowerOfTwo(float size)
Parameters
size
floatThe size.
Returns
- float
System.Int32.
PreviousPowerOfTwo(int)
Get the previous power of two of the provided integer.
public static int PreviousPowerOfTwo(int size)
Parameters
size
intThe value
Returns
PreviousPowerOfTwo(float)
Get the previous power of two of the provided float.
public static float PreviousPowerOfTwo(float size)
Parameters
size
floatThe value
Returns
RadiansToDegrees(float)
Converts radians to degrees.
public static float RadiansToDegrees(float radian)
Parameters
radian
floatThe value to convert.
Returns
- float
The converted value.
RadiansToGradians(float)
Converts radians to gradians.
public static float RadiansToGradians(float radian)
Parameters
radian
floatThe value to convert.
Returns
- float
The converted value.
RadiansToRevolutions(float)
Converts radians to revolutions.
public static float RadiansToRevolutions(float radian)
Parameters
radian
floatThe value to convert.
Returns
- float
The converted value.
RevolutionsToDegrees(float)
Converts revolutions to degrees.
public static float RevolutionsToDegrees(float revolution)
Parameters
revolution
floatThe value to convert.
Returns
- float
The converted value.
RevolutionsToGradians(float)
Converts revolutions to gradians.
public static float RevolutionsToGradians(float revolution)
Parameters
revolution
floatThe value to convert.
Returns
- float
The converted value.
RevolutionsToRadians(float)
Converts revolutions to radians.
public static float RevolutionsToRadians(float revolution)
Parameters
revolution
floatThe value to convert.
Returns
- float
The converted value.
SRgbToLinear(float)
Converts a float value from sRGB to linear.
public static float SRgbToLinear(float sRgbValue)
Parameters
sRgbValue
floatThe sRGB value.
Returns
- float
A linear value.
SmoothStep(float)
Performs smooth (cubic Hermite) interpolation between 0 and 1.
public static float SmoothStep(float amount)
Parameters
amount
floatValue between 0 and 1 indicating interpolation amount.
Returns
Remarks
SmootherStep(float)
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
amount
floatValue between 0 and 1 indicating interpolation amount.
Returns
Remarks
Snap(Vector2, float)
Snaps all vector components to the nearest interval.
public static Vector2 Snap(Vector2 value, float gap)
Parameters
Returns
- Vector2
A vector which components are snapped to the nearest interval.
Snap(Vector3, float)
Snaps all vector components to the nearest interval.
public static Vector3 Snap(Vector3 value, float gap)
Parameters
Returns
- Vector3
A vector which components are snapped to the nearest interval.
Snap(Vector4, float)
Snaps all vector components to the nearest interval.
public static Vector4 Snap(Vector4 value, float gap)
Parameters
Returns
- 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
Returns
- double
The nearest interval to the provided value.
Snap(float, float)
Snaps a value to the nearest interval.
public static float Snap(float value, float gap)
Parameters
Returns
- float
The nearest interval to the provided value.
WithinEpsilon(float, float, float)
Checks if a - b are almost equals within a float epsilon.
public static bool WithinEpsilon(float a, float b, float epsilon)
Parameters
Returns
- bool
true
if a almost equal to b within a float epsilon,false
otherwise