MathUtility¶
Namespace:
SideXP.Core
Miscellaneous functions for mathematics.
Methods¶
Approximately(float, float)¶
Checks the two float values are close enough to be considered equal. This is meant to prevent float imprecisions.
Parameters
a: The first value to compare.b: The second value to compare.
Returns
Returns true if b is equal (or very close) to a.
Remarks
This overload relies on Approximately, which uses a relative tolerance that scales with the magnitude of the compared values. For an explicit, absolute tolerance, use Approximately.
Approximately(float, float, float)¶
Checks the two float values are within a given absolute tolerance of each other.
Parameters
a: The first value to compare.b: The second value to compare.epsilon: The absolute tolerance. Its absolute value is used, so its sign doesn't matter.
Returns
Returns true if a is within epsilon of b (bounds included).
Remarks
Unlike Approximately, this overload uses an absolute tolerance: it returns true if a is within epsilon of b, bounds included. The sign of epsilon is ignored, and an epsilon of 0 still considers strictly equal values as approximately equal.
Remap(int, int, int, int, int)¶
Remaps the given value from one range to another.
Parameters
value: The value to remap.fromMin: The lower bound of the value's current range.fromMax: The upper bound of the value's current range.toMin: The lower bound of the value's target range.toMax: The upper bound of the value's target range.
Returns
Returns the remapped value, or 0 if the input range is empty or inverted.
Example
MathUtility.Remap(5, 0, 10, 100, 200); // 150
MathUtility.Remap(0, -10, 10, -100, 100); // 0
MathUtility.Remap(-10, -10, 10, -100, 100); // -100
MathUtility.Remap(10, -10, 10, -100, 100); // 100
MathUtility.Remap(15, 0, 10, 100, 200); // 250 (out of range in, out of range out)
Remarks
If the input value is out of the fromMin..fromMax range, the remapped value will also be out of the target range (the value is not clamped). The output range may be inverted (toMin greater than toMax) to produce a descending mapping. However, if the input range is empty or inverted (fromMax is not greater than fromMin), this function returns 0. This integer overload uses integer division, so the result is truncated toward zero.
Remap(float, float, float, float, float)¶
Remaps the given value from one range to another, preserving fractional precision.
Parameters
value: The value to remap.fromMin: The lower bound of the value's current range.fromMax: The upper bound of the value's current range.toMin: The lower bound of the value's target range.toMax: The upper bound of the value's target range.
Returns
Returns the remapped value, or 0 if the input range is empty or inverted.
Example
MathUtility.Remap(5, 0, 10, 100, 200); // 150
MathUtility.Remap(0, -10, 10, -100, 100); // 0
MathUtility.Remap(-10, -10, 10, -100, 100); // -100
MathUtility.Remap(10, -10, 10, -100, 100); // 100
MathUtility.Remap(15, 0, 10, 100, 200); // 250 (out of range in, out of range out)
Remarks
Behaves like Remap, but keeps the fractional part of the result. As with the integer overload, an empty or inverted input range returns 0.
Ratio(float, float, float)¶
Remaps the given value from its range to a range between 0 and 1. If the input value is out of the given range, it's clamped between 0 and 1.
Parameters
value: The value to remap.min: The lower bound of the value's current range.max: The upper bound of the value's current range.
Returns
Returns the remapped value, clamped between 0 and 1.
Percents(float, float, float)¶
Remaps the given value from its range to a range between 0 and 100. Note that, unlike Ratio, the result is not clamped: if the input value is out of range, the remapped value will also be out of range.
Parameters
value: The value to remap.min: The lower bound of the value's current range.max: The upper bound of the value's current range.
Returns
Returns the remapped value, between 0 and 100 if the input value is within range.