Show / Hide Table of Contents

Class MathHelpers

Static class consisting of mathematical "helper" functions and constants -- things like angle unit conversions, and other helpful functions.

Inheritance
object
MathHelpers
Inherited Members
object.GetType()
object.MemberwiseClone()
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
Namespace: GoRogue
Assembly: GoRogue.dll
Syntax
public static class MathHelpers

Methods

View Source

RoundToMultiple(int, int)

Rounds the given number up (toward highest number), to the nearest multiple of the specified value.

Declaration
public static int RoundToMultiple(int number, int toMultipleOf)
Parameters
Type Name Description
int number

Number to round.

int toMultipleOf

Number given is rounded up to nearest multiple of this number.

Returns
Type Description
int

The number parameter, rounded up to the nearest multiple of toMultipleOf.

View Source

ScaledAtan2Approx(double, double)

Approximation of the Atan2 function that scales the returned value to the range [0.0, 1.0], in order to remain agnostic of units (radius vs degrees). It will never return a negative number, so is also useful to avoid floating-point modulus. Credit to the SquidLib java RL library and this suggestion from user njuffa for this math.

Declaration
public static double ScaledAtan2Approx(double y, double x)
Parameters
Type Name Description
double y

Y-component of point to find angle towards.

double x

X-component of point to find angle towards.

Returns
Type Description
double

A value representing the angle to the given point, scaled to range [0.0, 1.0].

View Source

WrapAround(double, double)

Same effect as WrapAround(int, int), but for doubles.

Declaration
public static double WrapAround(double num, double wrapTo)
Parameters
Type Name Description
double num

The number to wrap.

double wrapTo

The number to wrap to.

Returns
Type Description
double

The wrapped result. Guaranteed to lie in range [0, wrapTo).

View Source

WrapAround(int, int)

A modified modulo operator, which practically differs from num % wrapTo in that it wraps from 0 to wrapTo - 1, as well as from wrapTo - 1 to 0.

Declaration
public static int WrapAround(int num, int wrapTo)
Parameters
Type Name Description
int num

The number to wrap.

int wrapTo

The number to wrap to -- the result of the function is as outlined in function description, and guaranteed to be between [0, wrapTo - 1], inclusive.

Returns
Type Description
int

The wrapped result, as outlined in function description. Guaranteed to lie in range [0, wrapTo - 1], inclusive.

Remarks

A modified modulo operator. Returns the result of the formula (num % wrapTo + wrapTo) % wrapTo. Practically it differs from regular modulo in that the values it returns when negative values for num are wrapped around like one would want an array index to (if wrapTo is list.length, -1 wraps to list.length - 1). For example, 0 % 3 = 0, -1 % 3 = -1, -2 % 3 = -2, -3 % 3 = 0, and so forth, but WrapTo(0, 3) = 0, WrapTo(-1, 3) = 2, WrapTo(-2, 3) = 1, WrapTo(-3, 3) = 0, and so forth. This can be useful if you're trying to "wrap" a number around at both ends, for example wrap to 3, such that 3 wraps to 0, and -1 wraps to 2. This is common if you are wrapping around an array index to the length of the array and need to ensure that positive numbers greater than or equal to the length of the array wrap to the beginning of the array (index 0), AND that negative numbers (under 0) wrap around to the end of the array (Length - 1).

  • View Source
In this article
Back to top Generated by DocFX