Class Utils

Namespace
Elegy.Common.Maths
Assembly
Elegy.Common.dll
public static class Utils
Inheritance
Utils
Inherited Members

Methods

AngleDifference(double, double)

Returns the difference between the two angles, in range of -PI, PI. When from and to are opposite, returns -PI if from is smaller than to, or PI otherwise.

public static double AngleDifference(double from, double to)

Parameters

from double

The start angle.

to double

The destination angle.

Returns

double

The difference between the two angles.

AngleDifference(float, float)

Returns the difference between the two angles, in range of -PI, PI. When from and to are opposite, returns -PI if from is smaller than to, or PI otherwise.

public static float AngleDifference(float from, float to)

Parameters

from float

The start angle.

to float

The destination angle.

Returns

float

The difference between the two angles.

Bezier(double, double, double, double, double)

Returns the point at the given t on a one-dimensional Bezier curve defined by the given control1, control2, and end points.

public static double Bezier(double start, double control1, double control2, double end, double t)

Parameters

start double

The start value for the interpolation.

control1 double

Control point that defines the bezier curve.

control2 double

Control point that defines the bezier curve.

end double

The destination value for the interpolation.

t double

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

Returns

double

The resulting value of the interpolation.

Bezier(float, float, float, float, float)

Returns the point at the given t on a one-dimensional Bezier curve defined by the given control1, control2, and end points.

public static float Bezier(float start, float control1, float control2, float end, float t)

Parameters

start float

The start value for the interpolation.

control1 float

Control point that defines the bezier curve.

control2 float

Control point that defines the bezier curve.

end float

The destination value for the interpolation.

t float

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

Returns

float

The resulting value of the interpolation.

BezierDerivative(double, double, double, double, double)

Returns the derivative at the given t on a one dimensional Bezier curve defined by the given control1, control2, and end points.

public static double BezierDerivative(double start, double control1, double control2, double end, double t)

Parameters

start double

The start value for the interpolation.

control1 double

Control point that defines the bezier curve.

control2 double

Control point that defines the bezier curve.

end double

The destination value for the interpolation.

t double

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

Returns

double

The resulting value of the interpolation.

BezierDerivative(float, float, float, float, float)

Returns the derivative at the given t on a one dimensional Bezier curve defined by the given control1, control2, and end points.

public static float BezierDerivative(float start, float control1, float control2, float end, float t)

Parameters

start float

The start value for the interpolation.

control1 float

Control point that defines the bezier curve.

control2 float

Control point that defines the bezier curve.

end float

The destination value for the interpolation.

t float

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

Returns

float

The resulting value of the interpolation.

Cubic(double, double, double, double, double)

Cubic interpolates between two values by the factor defined in weight with pre and post values.

public static double Cubic(double from, double to, double pre, double post, double weight)

Parameters

from double

The start value for interpolation.

to double

The destination value for interpolation.

pre double

The value which before "from" value for interpolation.

post double

The value which after "to" value for interpolation.

weight double

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

Returns

double

The resulting value of the interpolation.

Cubic(float, float, float, float, float)

Cubic interpolates between two values by the factor defined in weight with pre and post values.

public static float Cubic(float from, float to, float pre, float post, float weight)

Parameters

from float

The start value for interpolation.

to float

The destination value for interpolation.

pre float

The value which before "from" value for interpolation.

post float

The value which after "to" value for interpolation.

weight float

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

Returns

float

The resulting value of the interpolation.

CubicAngle(double, double, double, double, double)

Cubic interpolates between two rotation values with shortest path by the factor defined in weight with pre and post values. See also LerpAngle(double, double, double).

public static double CubicAngle(double from, double to, double pre, double post, double weight)

Parameters

from double

The start value for interpolation.

to double

The destination value for interpolation.

pre double

The value which before "from" value for interpolation.

post double

The value which after "to" value for interpolation.

weight double

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

Returns

double

The resulting value of the interpolation.

CubicAngle(float, float, float, float, float)

Cubic interpolates between two rotation values with shortest path by the factor defined in weight with pre and post values. See also LerpAngle(float, float, float).

public static float CubicAngle(float from, float to, float pre, float post, float weight)

Parameters

from float

The start value for interpolation.

to float

The destination value for interpolation.

pre float

The value which before "from" value for interpolation.

post float

The value which after "to" value for interpolation.

weight float

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

Returns

float

The resulting value of the interpolation.

CubicAngleInTime(double, double, double, double, double, double, double, double)

Cubic interpolates between two rotation values with shortest path by the factor defined in weight with pre and post values. See also LerpAngle(double, double, double). It can perform smoother interpolation than CubicAngle(double, double, double, double, double) by the time values.

public static double CubicAngleInTime(double from, double to, double pre, double post, double weight, double toT, double preT, double postT)

Parameters

from double

The start value for interpolation.

to double

The destination value for interpolation.

pre double

The value which before "from" value for interpolation.

post double

The value which after "to" value for interpolation.

weight double

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

toT double
preT double
postT double

Returns

double

The resulting value of the interpolation.

CubicAngleInTime(float, float, float, float, float, float, float, float)

Cubic interpolates between two rotation values with shortest path by the factor defined in weight with pre and post values. See also LerpAngle(float, float, float). It can perform smoother interpolation than CubicAngle(float, float, float, float, float) by the time values.

public static float CubicAngleInTime(float from, float to, float pre, float post, float weight, float toT, float preT, float postT)

Parameters

from float

The start value for interpolation.

to float

The destination value for interpolation.

pre float

The value which before "from" value for interpolation.

post float

The value which after "to" value for interpolation.

weight float

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

toT float
preT float
postT float

Returns

float

The resulting value of the interpolation.

CubicInTime(double, double, double, double, double, double, double, double)

Cubic interpolates between two values by the factor defined in weight with pre and post values. It can perform smoother interpolation than Cubic(double, double, double, double, double) by the time values.

public static double CubicInTime(double from, double to, double pre, double post, double weight, double toT, double preT, double postT)

Parameters

from double

The start value for interpolation.

to double

The destination value for interpolation.

pre double

The value which before "from" value for interpolation.

post double

The value which after "to" value for interpolation.

weight double

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

toT double
preT double
postT double

Returns

double

The resulting value of the interpolation.

CubicInTime(float, float, float, float, float, float, float, float)

Cubic interpolates between two values by the factor defined in weight with pre and post values. It can perform smoother interpolation than Cubic(float, float, float, float, float) by the time values.

public static float CubicInTime(float from, float to, float pre, float post, float weight, float toT, float preT, float postT)

Parameters

from float

The start value for interpolation.

to float

The destination value for interpolation.

pre float

The value which before "from" value for interpolation.

post float

The value which after "to" value for interpolation.

weight float

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

toT float
preT float
postT float

Returns

float

The resulting value of the interpolation.

Ease(double, double)

Easing function, based on exponent. The curve values are: 0 is constant, 1 is linear, 0 to 1 is ease-in, 1 or more is ease-out. Negative values are in-out/out-in.

public static double Ease(double s, double curve)

Parameters

s double

The value to ease.

curve double

0 is constant, 1 is linear, 0 to 1 is ease-in, 1 or more is ease-out.

Returns

double

The eased value.

Ease(float, float)

Easing function, based on exponent. The curve values are: 0 is constant, 1 is linear, 0 to 1 is ease-in, 1 or more is ease-out. Negative values are in-out/out-in.

public static float Ease(float s, float curve)

Parameters

s float

The value to ease.

curve float

0 is constant, 1 is linear, 0 to 1 is ease-in, 1 or more is ease-out.

Returns

float

The eased value.

InverseLerp(double, double, double)

Returns a normalized value considering the given range. This is the opposite of Lerp(double, double, double).

public static double InverseLerp(double from, double to, double weight)

Parameters

from double

The start value for interpolation.

to double

The destination value for interpolation.

weight double

The interpolated value.

Returns

double

The resulting value of the inverse interpolation. The returned value will be between 0.0 and 1.0 if weight is between from and to (inclusive).

InverseLerp(float, float, float)

Returns a normalized value considering the given range. This is the opposite of Lerp(float, float, float).

public static float InverseLerp(float from, float to, float weight)

Parameters

from float

The start value for interpolation.

to float

The destination value for interpolation.

weight float

The interpolated value.

Returns

float

The resulting value of the inverse interpolation. The returned value will be between 0.0 and 1.0 if weight is between from and to (inclusive).

IsEqualApprox(double, double)

Returns true if a and b are approximately equal to each other. The comparison is done using a tolerance calculation with Epsilon.

public static bool IsEqualApprox(double a, double b)

Parameters

a double

One of the values.

b double

The other value.

Returns

bool

A bool for whether or not the two values are approximately equal.

IsEqualApprox(float, float)

Returns true if a and b are approximately equal to each other. The comparison is done using a tolerance calculation with Epsilon.

public static bool IsEqualApprox(float a, float b)

Parameters

a float

One of the values.

b float

The other value.

Returns

bool

A bool for whether or not the two values are approximately equal.

IsEqualApprox(float, float, float)

Returns true if a and b are approximately equal to each other. The comparison is done using a tolerance calculation with Epsilon.

public static bool IsEqualApprox(float a, float b, float radius)

Parameters

a float

One of the values.

b float

The other value.

radius float

Returns

bool

A bool for whether or not the two values are approximately equal.

IsZeroApprox(double)

Returns true if s is zero or almost zero. The comparison is done using a tolerance calculation with Epsilon.

This method is faster than using IsEqualApprox(double, double) with one value as zero.

public static bool IsZeroApprox(double s)

Parameters

s double

The value to check.

Returns

bool

A bool for whether or not the value is nearly zero.

IsZeroApprox(float)

Returns true if s is zero or almost zero. The comparison is done using a tolerance calculation with Epsilon.

This method is faster than using IsEqualApprox(float, float) with one value as zero.

public static bool IsZeroApprox(float s)

Parameters

s float

The value to check.

Returns

bool

A bool for whether or not the value is nearly zero.

Lerp(double, double, double)

Linearly interpolates between two values by a normalized value. This is the opposite InverseLerp(double, double, double).

public static double Lerp(double from, double to, double weight)

Parameters

from double

The start value for interpolation.

to double

The destination value for interpolation.

weight double

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

Returns

double

The resulting value of the interpolation.

Lerp(float, float, float)

Linearly interpolates between two values by a normalized value. This is the opposite InverseLerp(float, float, float).

public static float Lerp(float from, float to, float weight)

Parameters

from float

The start value for interpolation.

to float

The destination value for interpolation.

weight float

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

Returns

float

The resulting value of the interpolation.

LerpAngle(double, double, double)

Linearly interpolates between two angles (in radians) by a normalized value.

Similar to Lerp(double, double, double), but interpolates correctly when the angles wrap around Tau.

public static double LerpAngle(double from, double to, double weight)

Parameters

from double

The start angle for interpolation.

to double

The destination angle for interpolation.

weight double

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

Returns

double

The resulting angle of the interpolation.

LerpAngle(float, float, float)

Linearly interpolates between two angles (in radians) by a normalized value.

Similar to Lerp(float, float, float), but interpolates correctly when the angles wrap around Tau.

public static float LerpAngle(float from, float to, float weight)

Parameters

from float

The start angle for interpolation.

to float

The destination angle for interpolation.

weight float

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

Returns

float

The resulting angle of the interpolation.

MoveToward(double, double, double)

Moves from toward to by the delta value.

Use a negative delta value to move away.

public static double MoveToward(double from, double to, double delta)

Parameters

from double

The start value.

to double

The value to move towards.

delta double

The amount to move by.

Returns

double

The value after moving.

MoveToward(float, float, float)

Moves from toward to by the delta value.

Use a negative delta value to move away.

public static float MoveToward(float from, float to, float delta)

Parameters

from float

The start value.

to float

The value to move towards.

delta float

The amount to move by.

Returns

float

The value after moving.

Remap(double, double, double, double, double)

Maps a value from [inFrom, inTo] to [outFrom, outTo].

public static double Remap(double value, double inFrom, double inTo, double outFrom, double outTo)

Parameters

value double

The value to map.

inFrom double

The start value for the input interpolation.

inTo double

The destination value for the input interpolation.

outFrom double

The start value for the output interpolation.

outTo double

The destination value for the output interpolation.

Returns

double

The resulting mapped value mapped.

Remap(float, float, float, float, float)

Maps a value from [inFrom, inTo] to [outFrom, outTo].

public static float Remap(float value, float inFrom, float inTo, float outFrom, float outTo)

Parameters

value float

The value to map.

inFrom float

The start value for the input interpolation.

inTo float

The destination value for the input interpolation.

outFrom float

The start value for the output interpolation.

outTo float

The destination value for the output interpolation.

Returns

float

The resulting mapped value mapped.

RotateToward(double, double, double)

Rotates from toward to by the delta amount. Will not go past to. Similar to MoveToward(double, double, double) but interpolates correctly when the angles wrap around Tau. If delta is negative, this function will rotate away from to, toward the opposite angle, and will not go past the opposite angle.

public static double RotateToward(double from, double to, double delta)

Parameters

from double

The start angle.

to double

The angle to move towards.

delta double

The amount to move by.

Returns

double

The angle after moving.

RotateToward(float, float, float)

Rotates from toward to by the delta amount. Will not go past to. Similar to MoveToward(float, float, float) but interpolates correctly when the angles wrap around Tau. If delta is negative, this function will rotate away from to, toward the opposite angle, and will not go past the opposite angle.

public static float RotateToward(float from, float to, float delta)

Parameters

from float

The start angle.

to float

The angle to move towards.

delta float

The amount to move by.

Returns

float

The angle after moving.

Snapped(double, double)

Snaps float value s to a given step. This can also be used to round a floating point number to an arbitrary number of decimals.

public static double Snapped(double s, double step)

Parameters

s double

The value to snap.

step double

The step size to snap to.

Returns

double

The snapped value.

Snapped(float, float)

Snaps float value s to a given step. This can also be used to round a floating point number to an arbitrary number of decimals.

public static float Snapped(float s, float step)

Parameters

s float

The value to snap.

step float

The step size to snap to.

Returns

float

The snapped value.

StepDecimals(double)

Returns the position of the first non-zero digit, after the decimal point. Note that the maximum return value is 10, which is a design decision in the implementation.

public static int StepDecimals(double step)

Parameters

step double

The input value.

Returns

int

The position of the first non-zero digit.