MathHelper

class MathHelper : System.Object

Contains commonly used precalculated values and mathematical operations.

float E

Represents the mathematical constant e(2.71828175).

float Log10E

Represents the log base ten of e(0.4342945).

float Log2E

Represents the log base two of e(1.442695).

float Pi

Represents the value of pi(3.14159274).

float PiOver2

Represents the value of pi divided by two(1.57079637).

float PiOver4

Represents the value of pi divided by four(0.7853982).

float TwoPi

Represents the value of pi times two(6.28318548).

public float Barycentric(float value1, float value2, float value3, float amount1, float amount2)

Returns the Cartesian coordinate for one axis of a point that is defined by a given triangle and two normalized barycentric (areal) coordinates.

Parameters:
  • value1 (float) – The coordinate on one axis of vertex 1 of the defining triangle.
  • value2 (float) – The coordinate on the same axis of vertex 2 of the defining triangle.
  • value3 (float) – The coordinate on the same axis of vertex 3 of the defining triangle.
  • amount1 (float) – The normalized barycentric (areal) coordinate b2, equal to the weighting factor for vertex 2, the coordinate of which is specified in value2.
  • amount2 (float) – The normalized barycentric (areal) coordinate b3, equal to the weighting factor for vertex 3, the coordinate of which is specified in value3.
Returns:

Cartesian coordinate of the specified point with respect to the axis being used.

public float CatmullRom(float value1, float value2, float value3, float value4, float amount)

Performs a Catmull-Rom interpolation using the specified positions.

Parameters:
  • value1 (float) – The first position in the interpolation.
  • value2 (float) – The second position in the interpolation.
  • value3 (float) – The third position in the interpolation.
  • value4 (float) – The fourth position in the interpolation.
  • amount (float) – Weighting factor.
Returns:

A position that is the result of the Catmull-Rom interpolation.

public float Clamp(float value, float min, float max)

Restricts a value to be within a specified range.

Parameters:
  • value (float) – The value to clamp.
  • min (float) – The minimum value. If value is less than min, min will be returned.
  • max (float) – The maximum value. If value is greater than max, max will be returned.
Returns:

The clamped value.

public int Clamp(int value, int min, int max)

Restricts a value to be within a specified range.

Parameters:
  • value (int) – The value to clamp.
  • min (int) – The minimum value. If value is less than min, min will be returned.
  • max (int) – The maximum value. If value is greater than max, max will be returned.
Returns:

The clamped value.

public float Distance(float value1, float value2)

Calculates the absolute value of the difference of two values.

Parameters:
  • value1 (float) – Source value.
  • value2 (float) – Source value.
Returns:

Distance between the two values.

public float Hermite(float value1, float tangent1, float value2, float tangent2, float amount)

Performs a Hermite spline interpolation.

Parameters:
  • value1 (float) – Source position.
  • tangent1 (float) – Source tangent.
  • value2 (float) – Source position.
  • tangent2 (float) – Source tangent.
  • amount (float) – Weighting factor.
Returns:

The result of the Hermite spline interpolation.

public float Lerp(float value1, float value2, float amount)

Linearly interpolates between two values.

Parameters:
  • value1 (float) – Source value.
  • value2 (float) – Destination value.
  • amount (float) – Value between 0 and 1 indicating the weight of value2.
Returns:

Interpolated value.

public float LerpPrecise(float value1, float value2, float amount)

Linearly interpolates between two values. This method is a less efficient, more precise version of M:Microsoft.Xna.Framework.MathHelper.Lerp(System.Single,System.Single,System.Single). See remarks for more info.

Parameters:
  • value1 (float) – Source value.
  • value2 (float) – Destination value.
  • amount (float) – Value between 0 and 1 indicating the weight of value2.
Returns:

Interpolated value.

public float Max(float value1, float value2)

Returns the greater of two values.

Parameters:
  • value1 (float) – Source value.
  • value2 (float) – Source value.
Returns:

The greater value.

public int Max(int value1, int value2)

Returns the greater of two values.

Parameters:
  • value1 (int) – Source value.
  • value2 (int) – Source value.
Returns:

The greater value.

public float Min(float value1, float value2)

Returns the lesser of two values.

Parameters:
  • value1 (float) – Source value.
  • value2 (float) – Source value.
Returns:

The lesser value.

public int Min(int value1, int value2)

Returns the lesser of two values.

Parameters:
  • value1 (int) – Source value.
  • value2 (int) – Source value.
Returns:

The lesser value.

public float SmoothStep(float value1, float value2, float amount)

Interpolates between two values using a cubic equation.

Parameters:
  • value1 (float) – Source value.
  • value2 (float) – Source value.
  • amount (float) – Weighting value.
Returns:

Interpolated value.

public float ToDegrees(float radians)

Converts radians to degrees.

Parameters:
  • radians (float) – The angle in radians.
Returns:

The angle in degrees.

public float ToRadians(float degrees)

Converts degrees to radians.

Parameters:
  • degrees (float) – The angle in degrees.
Returns:

The angle in radians.

public float WrapAngle(float angle)

Reduces a given angle to a value between π and -π.

Parameters:
  • angle (float) – The angle to reduce, in radians.
Returns:

The new angle, in radians.

public bool IsPowerOfTwo(int value)

Determines if value is powered by two.

Parameters:
  • value (int) – A value.
Returns:

true if value is powered by two; otherwise false.