Class MathHelper
Contains commonly used precalculated values and mathematical operations.
public static class MathHelper
- Inheritance
-
MathHelper
- Inherited Members
Fields
E
Represents the mathematical constant e(2.71828175).
public const float E = 2.7182817
Field Value
Log10E
Represents the log base ten of e(0.4342945).
public const float Log10E = 0.4342945
Field Value
Log2E
Represents the log base two of e(1.442695).
public const float Log2E = 1.442695
Field Value
Pi
Represents the value of pi(3.14159274).
public const float Pi = 3.1415927
Field Value
PiOver2
Represents the value of pi divided by two(1.57079637).
public const float PiOver2 = 1.5707964
Field Value
PiOver4
Represents the value of pi divided by four(0.7853982).
public const float PiOver4 = 0.7853982
Field Value
Tau
Represents the value of pi times two(6.28318548). This is an alias of TwoPi.
public const float Tau = 6.2831855
Field Value
TwoPi
Represents the value of pi times two(6.28318548).
public const float TwoPi = 6.2831855
Field Value
Methods
Barycentric(float, float, float, float, float)
Returns the Cartesian coordinate for one axis of a point that is defined by a given triangle and two normalized barycentric (areal) coordinates.
public static float Barycentric(float value1, float value2, float value3, float amount1, float amount2)
Parameters
value1
floatThe coordinate on one axis of vertex 1 of the defining triangle.
value2
floatThe coordinate on the same axis of vertex 2 of the defining triangle.
value3
floatThe coordinate on the same axis of vertex 3 of the defining triangle.
amount1
floatThe normalized barycentric (areal) coordinate b2, equal to the weighting factor for vertex 2, the coordinate of which is specified in value2.
amount2
floatThe normalized barycentric (areal) coordinate b3, equal to the weighting factor for vertex 3, the coordinate of which is specified in value3.
Returns
- float
Cartesian coordinate of the specified point with respect to the axis being used.
CatmullRom(float, float, float, float, float)
Performs a Catmull-Rom interpolation using the specified positions.
public static float CatmullRom(float value1, float value2, float value3, float value4, float amount)
Parameters
value1
floatThe first position in the interpolation.
value2
floatThe second position in the interpolation.
value3
floatThe third position in the interpolation.
value4
floatThe fourth position in the interpolation.
amount
floatWeighting factor.
Returns
- float
A position that is the result of the Catmull-Rom interpolation.
Clamp(int, int, int)
Restricts a value to be within a specified range.
public static int Clamp(int value, int min, int max)
Parameters
value
intThe value to clamp.
min
intThe minimum value. If
value
is less thanmin
,min
will be returned.max
intThe maximum value. If
value
is greater thanmax
,max
will be returned.
Returns
- int
The clamped value.
Clamp(float, float, float)
Restricts a value to be within a specified range.
public static float Clamp(float value, float min, float max)
Parameters
value
floatThe value to clamp.
min
floatThe minimum value. If
value
is less thanmin
,min
will be returned.max
floatThe maximum value. If
value
is greater thanmax
,max
will be returned.
Returns
- float
The clamped value.
Distance(float, float)
Calculates the absolute value of the difference of two values.
public static float Distance(float value1, float value2)
Parameters
Returns
- float
Distance between the two values.
Hermite(float, float, float, float, float)
Performs a Hermite spline interpolation.
public static float Hermite(float value1, float tangent1, float value2, float tangent2, float amount)
Parameters
value1
floatSource position.
tangent1
floatSource tangent.
value2
floatSource position.
tangent2
floatSource tangent.
amount
floatWeighting factor.
Returns
- float
The result of the Hermite spline interpolation.
IsPowerOfTwo(int)
Determines if value is powered by two.
public static bool IsPowerOfTwo(int value)
Parameters
value
intA value.
Returns
- bool
true
ifvalue
is powered by two; otherwisefalse
.
Lerp(float, float, float)
Linearly interpolates between two values.
public static float Lerp(float value1, float value2, float amount)
Parameters
value1
floatSource value.
value2
floatDestination value.
amount
floatValue between 0 and 1 indicating the weight of value2.
Returns
- float
Interpolated value.
Remarks
This method performs the linear interpolation based on the following formula:
value1 + (value2 - value1) * amount
.
Passing amount a value of 0 will cause value1 to be returned, a value of 1 will cause value2 to be returned.
See LerpPrecise(float, float, float) for a less efficient version with more precision around edge cases.
LerpPrecise(float, float, float)
Linearly interpolates between two values. This method is a less efficient, more precise version of Lerp(float, float, float). See remarks for more info.
public static float LerpPrecise(float value1, float value2, float amount)
Parameters
value1
floatSource value.
value2
floatDestination value.
amount
floatValue between 0 and 1 indicating the weight of value2.
Returns
- float
Interpolated value.
Remarks
This method performs the linear interpolation based on the following formula:
((1 - amount) * value1) + (value2 * amount)
.
Passing amount a value of 0 will cause value1 to be returned, a value of 1 will cause value2 to be returned.
This method does not have the floating point precision issue that Lerp(float, float, float) has.
i.e. If there is a big gap between value1 and value2 in magnitude (e.g. value1=10000000000000000, value2=1),
right at the edge of the interpolation range (amount=1), Lerp(float, float, float) will return 0 (whereas it should return 1).
This also holds for value1=10^17, value2=10; value1=10^18,value2=10^2... so on.
For an in depth explanation of the issue, see below references:
Relevant Wikipedia Article: https://en.wikipedia.org/wiki/Linear_interpolation#Programming_language_support
Relevant StackOverflow Answer: http://stackoverflow.com/questions/4353525/floating-point-linear-interpolation#answer-23716956
Max(int, int)
Returns the greater of two values.
public static int Max(int value1, int value2)
Parameters
Returns
- int
The greater value.
Max(float, float)
Returns the greater of two values.
public static float Max(float value1, float value2)
Parameters
Returns
- float
The greater value.
Min(int, int)
Returns the lesser of two values.
public static int Min(int value1, int value2)
Parameters
Returns
- int
The lesser value.
Min(float, float)
Returns the lesser of two values.
public static float Min(float value1, float value2)
Parameters
Returns
- float
The lesser value.
SmoothStep(float, float, float)
Interpolates between two values using a cubic equation.
public static float SmoothStep(float value1, float value2, float amount)
Parameters
Returns
- float
Interpolated value.
ToDegrees(float)
Converts radians to degrees.
public static float ToDegrees(float radians)
Parameters
radians
floatThe angle in radians.
Returns
- float
The angle in degrees.
Remarks
This method uses double precision internally, though it returns single float Factor = 180 / pi
ToRadians(float)
Converts degrees to radians.
public static float ToRadians(float degrees)
Parameters
degrees
floatThe angle in degrees.
Returns
- float
The angle in radians.
Remarks
This method uses double precision internally, though it returns single float Factor = pi / 180
WrapAngle(float)
Reduces a given angle to a value between π and -π.
public static float WrapAngle(float angle)
Parameters
angle
floatThe angle to reduce, in radians.
Returns
- float
The new angle, in radians.