Table of Contents

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

float

PiOverFour

A value specifying the approximation of π/4 which is 45 degrees.

public const float PiOverFour = 0.7853982

Field Value

float

PiOverTwo

A value specifying the approximation of π/2 which is 90 degrees.

public const float PiOverTwo = 1.5707964

Field Value

float

TwoPi

A value specifying the approximation of 2π which is 360 degrees.

public const float TwoPi = 6.2831855

Field Value

float

ZeroTolerance

The value for which all absolute numbers smaller than are considered equal to zero.

public const float ZeroTolerance = 1E-06

Field Value

float

ZeroToleranceDouble

The value for which all absolute numbers smaller than are considered equal to zero.

public const double ZeroToleranceDouble = 4E-323

Field Value

double

Methods

AlignDown(int, int)

Alignes value down to match desire alignment.

public static int AlignDown(int value, int alignment)

Parameters

value int

The value.

alignment int

The alignment.

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

value int

The value.

alignment int

The alignment.

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 T

The value to fill the array with.

length int

The 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

value double

The value.

min double

The min.

max double

The max.

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

value int

The value.

min int

The min.

max int

The max.

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

value float

The value.

min float

The min.

max float

The max.

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 float

The value to convert.

Returns

float

The converted value.

DegreesToRevolutions(float)

Converts degrees to revolutions.

public static float DegreesToRevolutions(float degree)

Parameters

degree float

The 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 double

Initial value

b double

Plateau value

lambda double

Damping

dt double

Discrete 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 float

Initial value

b float

Plateau value

lambda float

Damping

dt float

Discrete 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 float

The value to convert.

Returns

float

The converted value.

GradiansToRadians(float)

Converts gradians to radians.

public static float GradiansToRadians(float gradian)

Parameters

gradian float

The value to convert.

Returns

float

The converted value.

GradiansToRevolutions(float)

Converts gradians to revolutions.

public static float GradiansToRevolutions(float gradian)

Parameters

gradian float

The 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 double

Minimum value that takes place in inverse-interpolation.

max double

Maximum value that takes place in inverse-interpolation.

value double

Value 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 float

Minimum value that takes place in inverse-interpolation.

max float

Maximum value that takes place in inverse-interpolation.

value float

Value 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

value int

The value.

alignment int

The alignment.

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 int

The value.

min int

The minimum value of the range.

max int

The 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 float

The value.

min float

The minimum value of the range.

max float

The 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 float

The 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 int

The 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 double

The 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 float

The 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 byte

Value to interpolate from.

to byte

Value to interpolate to.

amount float

Interpolation amount.

Returns

byte

The result of linear interpolation of values based on the amount.

Remarks

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 double

Value to interpolate from.

to double

Value to interpolate to.

amount double

Interpolation amount.

Returns

double

The result of linear interpolation of values based on the amount.

Remarks

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 float

Value to interpolate from.

to float

Value to interpolate to.

amount float

Interpolation amount.

Returns

float

The result of linear interpolation of values based on the amount.

Remarks

LinearToSRgb(float)

Converts a float value from linear to sRGB.

public static float LinearToSRgb(float linearValue)

Parameters

linearValue float

The 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 int

The 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 float

The 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

value float

The value.

divisor float

The divisor.

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

a float

The left value to compare.

b float

The right value to compare.

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 int

The 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 float

The 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 int

The value

Returns

int

PreviousPowerOfTwo(float)

Get the previous power of two of the provided float.

public static float PreviousPowerOfTwo(float size)

Parameters

size float

The value

Returns

float

RadiansToDegrees(float)

Converts radians to degrees.

public static float RadiansToDegrees(float radian)

Parameters

radian float

The value to convert.

Returns

float

The converted value.

RadiansToGradians(float)

Converts radians to gradians.

public static float RadiansToGradians(float radian)

Parameters

radian float

The value to convert.

Returns

float

The converted value.

RadiansToRevolutions(float)

Converts radians to revolutions.

public static float RadiansToRevolutions(float radian)

Parameters

radian float

The value to convert.

Returns

float

The converted value.

RevolutionsToDegrees(float)

Converts revolutions to degrees.

public static float RevolutionsToDegrees(float revolution)

Parameters

revolution float

The value to convert.

Returns

float

The converted value.

RevolutionsToGradians(float)

Converts revolutions to gradians.

public static float RevolutionsToGradians(float revolution)

Parameters

revolution float

The value to convert.

Returns

float

The converted value.

RevolutionsToRadians(float)

Converts revolutions to radians.

public static float RevolutionsToRadians(float revolution)

Parameters

revolution float

The 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 float

The 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 float

Value between 0 and 1 indicating interpolation amount.

Returns

float

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 float

Value between 0 and 1 indicating interpolation amount.

Returns

float

Remarks

Snap(Vector2, float)

Snaps all vector components to the nearest interval.

public static Vector2 Snap(Vector2 value, float gap)

Parameters

value Vector2

The vector to snap.

gap float

The interval gap.

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

value Vector3

The vector to snap.

gap float

The interval gap.

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

value Vector4

The vector to snap.

gap float

The interval gap.

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

value double

The value to snap.

gap double

The interval gap.

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

value float

The value to snap.

gap float

The interval gap.

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

a float

The left value to compare.

b float

The right value to compare.

epsilon float

Epsilon value

Returns

bool

true if a almost equal to b within a float epsilon, false otherwise