KS.Reactor.ksMath Class Reference

Collection of frequently used math algorithms and coversions of Math methods which operate on and return float values. More...

Public Types

enum class  Interpolation : int {
  LINEAR = 0 , EASE_IN_QUAD = 1 , EASE_IN_SIN = 2 , EASE_IN_EXP = 3 ,
  EASE_IN_CIRC = 4 , EASE_IN_CUBE = 5 , EASE_OUT_QUAD = 6 , EASE_OUT_SIN = 7 ,
  EASE_OUT_EXP = 8 , EASE_OUT_CIRC = 9 , EASE_OUT_CUBE = 10 , CONSTANT = 11
}
 

Static Public Member Functions

static int CountSetBits (int value)
 Count the number of bits set in an int More...
 
static int MaxSetBit (int value)
 Get the highest bit set from 1 to 32. More...
 
static int ClosestPowerOfTwo (int value)
 Returns the closest power of two value. More...
 
static bool IsPowerOfTwo (int value)
 Returns true if the value is power of two. More...
 
static int NextPowerOfTwo (int value)
 Returns the next power of two that is equal to, or greater than, the argument. More...
 
static float PerlinNoise (float x, float y=0.0f, float z=0.0f, float range=1.0f)
 Generate Perlin noise. More...
 
static float Sin (float f)
 Returns the sine of angle f. More...
 
static float Cos (float f)
 Returns the cosine of angle f. More...
 
static float Tan (float f)
 Returns the tangent of angle f in radians. More...
 
static float Asin (float f)
 Returns the absolute value of f. More...
 
static float Acos (float f)
 Returns the arc-cosine of f - the angle in radians whose cosine is f. More...
 
static float Atan (float f)
 Returns the arc-tangent of f - the angle in radians whose tangent is f. More...
 
static float Atan2 (float y, float x)
 Returns the angle in radians whose Tan is y/x. More...
 
static float Sqrt (float f)
 Returns square root of f. More...
 
static float Abs (float f)
 Returns the absolute value of f. More...
 
static int Abs (int value)
 Returns the absolute value of f. More...
 
static float Min (float a, float b)
 Returns the smallest of two or more values. More...
 
static float Min (params float[] values)
 Returns the smallest of two or more values. More...
 
static int Min (int a, int b)
 Returns the smallest of two or more values. More...
 
static int Min (params int[] values)
 Returns the smallest of two or more values. More...
 
static float Max (float a, float b)
 Returns largest of two or more values. More...
 
static float Max (params float[] values)
 Returns largest of two or more values. More...
 
static int Max (int a, int b)
 Returns largest of two or more values. More...
 
static int Max (params int[] values)
 Returns largest of two or more values. More...
 
static float Pow (float f, float p)
 Returns f raised to power p. More...
 
static float Exp (float power)
 Returns e raised to the specified power. More...
 
static float Log (float f, float p)
 Returns the logarithm of a specified number in a specified base. More...
 
static float Log (float f)
 Returns the logarithm of a specified number in a specified base. More...
 
static float Log10 (float f)
 Returns the base 10 logarithm of a specified number. More...
 
static float Ceil (float f)
 Returns the smallest integer greater to or equal to f. More...
 
static float Floor (float f)
 Returns the largest integer smaller than or equal to f. More...
 
static float Round (float f)
 Returns f rounded to the nearest integer. More...
 
static int CeilToInt (float f)
 Returns the smallest integer greater to or equal to f. More...
 
static int FloorToInt (float f)
 Returns the largest integer smaller to or equal to f. More...
 
static int RoundToInt (float f)
 Returns f rounded to the nearest integer. More...
 
static float Sign (float f)
 Returns the sign of f. More...
 
static float Clamp (float value, float min, float max)
 Clamps the given value between the given minimum float and maximum float values. More...
 
static int Clamp (int value, int min, int max)
 Clamps the given value between the given minimum int and maximum int values. More...
 
static float Clamp01 (float value)
 Clamps value between 0 and 1 and returns value. More...
 
static float Lerp (float a, float b, float t)
 Linearly interpolates between a and b by t. More...
 
static float LerpUnclamped (float a, float b, float t)
 Linearly interpolates between a and b by t with no limit to t. More...
 
static float LerpAngle (float a, float b, float t)
 Same as Lerp but makes sure the values interpolate correctly when they wrap around 360 degrees. More...
 
static float MoveTowards (float current, float target, float maxDelta)
 Moves a value current towards target. More...
 
static float MoveTowardsAngle (float current, float target, float maxDelta)
 Same as MoveTowards but makes sure the values interpolate correctly when they wrap around 360 degrees. More...
 
static float SmoothStep (float from, float to, float t)
 Interpolates between min and max with smoothing at the limits. More...
 
static float Gamma (float value, float absmax, float power)
 Normalize a value between negative absmax and positive absmax to -1...1 then raise the result to a power before remapping it into negative absmax to absmax . More...
 
static bool Approximately (float a, float b, float tolerance=1E-06f)
 Compares two floating point values and returns true if they are similar. More...
 
static float SmoothDamp (float current, float target, ref float currentVelocity, float smoothTime, float deltaTime, float maxSpeed=float.PositiveInfinity)
 Gradually changes a value towards a desired goal over time. More...
 
static float SmoothDampAngle (float current, float target, ref float currentVelocity, float smoothTime, float deltaTime, float maxSpeed=float.PositiveInfinity)
 Gradually changes an angle given in degrees towards a desired goal angle over time. More...
 
static float Repeat (float t, float length)
 Loops the value t, so that it is never larger than length and never smaller than 0. More...
 
static float PingPong (float t, float length)
 PingPong returns a value that will increment and decrement between the value 0 and length. More...
 
static float InverseLerp (float a, float b, float value)
 Calculates the linear parameter t that produces the interpolant value within the range [a, b]. More...
 
static float DeltaAngle (float current, float target)
 Calculates the shortest difference between two given angles given in degrees. More...
 
static bool LineIntersection (ksVector2 p1, ksVector2 p2, ksVector2 p3, ksVector2 p4, ref ksVector2 result)
 Check if the line (p1,p2) intersects with line (p3, p4) More...
 
static bool LineSegmentIntersection (ksVector2 p1, ksVector2 p2, ksVector2 p3, ksVector2 p4, ref ksVector2 result)
 Check if the line segment (p1,p2) intersects with the line segment (p3, p4) More...
 
static float Interpolate (float a, float b, float t, Interpolation equation)
 Interpolate between the value a and b according to an interpolation method. More...
 

Static Public Attributes

static readonly float Epsilon = (double)float.Epsilon == 0.0 ? 1.175494E-38f : float.Epsilon
 Smallest float value that does not equate to 0. More...
 
const double DEGREES_TO_RADIANS = Math.PI / 180.0
 
const double RADIANS_TO_DEGREES = 180.0 / Math.PI
 
const double TWO_PI = Math.PI * 2.0
 
const float FDEGREES_TO_RADIANS = (float)DEGREES_TO_RADIANS
 
const float FRADIANS_TO_DEGREES = (float)RADIANS_TO_DEGREES
 
const float FTWO_PI = (float)TWO_PI
 
const float PI = (float)Math.PI
 

Detailed Description

Collection of frequently used math algorithms and coversions of Math methods which operate on and return float values.

Member Function Documentation

◆ Abs() [1/2]

static float KS.Reactor.ksMath.Abs ( float  f)
static

Returns the absolute value of f.

Parameters
f
Returns

◆ Abs() [2/2]

static int KS.Reactor.ksMath.Abs ( int  value)
static

Returns the absolute value of f.

Parameters
value
Returns

◆ Acos()

static float KS.Reactor.ksMath.Acos ( float  f)
static

Returns the arc-cosine of f - the angle in radians whose cosine is f.

Parameters
f
Returns

◆ Approximately()

static bool KS.Reactor.ksMath.Approximately ( float  a,
float  b,
float  tolerance = 1E-06f 
)
static

Compares two floating point values and returns true if they are similar.

Parameters
a
b
toleranceThis value is mulitplied by the largest of a or b to determine the value used for the comparison.
Returns

◆ Asin()

static float KS.Reactor.ksMath.Asin ( float  f)
static

Returns the absolute value of f.

Parameters
f
Returns

◆ Atan()

static float KS.Reactor.ksMath.Atan ( float  f)
static

Returns the arc-tangent of f - the angle in radians whose tangent is f.

Parameters
f
Returns

◆ Atan2()

static float KS.Reactor.ksMath.Atan2 ( float  y,
float  x 
)
static

Returns the angle in radians whose Tan is y/x.

Parameters
y
x
Returns

◆ Ceil()

static float KS.Reactor.ksMath.Ceil ( float  f)
static

Returns the smallest integer greater to or equal to f.

Parameters
f
Returns

◆ CeilToInt()

static int KS.Reactor.ksMath.CeilToInt ( float  f)
static

Returns the smallest integer greater to or equal to f.

Parameters
f
Returns

◆ Clamp() [1/2]

static float KS.Reactor.ksMath.Clamp ( float  value,
float  min,
float  max 
)
static

Clamps the given value between the given minimum float and maximum float values.

Returns the given value if it is within the min and max range.

Parameters
value
min
max
Returns

◆ Clamp() [2/2]

static int KS.Reactor.ksMath.Clamp ( int  value,
int  min,
int  max 
)
static

Clamps the given value between the given minimum int and maximum int values.

Returns the given value if it is within the min and max range.

Parameters
value
min
max
Returns

◆ Clamp01()

static float KS.Reactor.ksMath.Clamp01 ( float  value)
static

Clamps value between 0 and 1 and returns value.

Parameters
value
Returns

◆ ClosestPowerOfTwo()

static int KS.Reactor.ksMath.ClosestPowerOfTwo ( int  value)
static

Returns the closest power of two value.

Parameters
value
Returns

◆ Cos()

static float KS.Reactor.ksMath.Cos ( float  f)
static

Returns the cosine of angle f.

Parameters
f
Returns

◆ CountSetBits()

static int KS.Reactor.ksMath.CountSetBits ( int  value)
static

Count the number of bits set in an int

Parameters
value
Returns

◆ DeltaAngle()

static float KS.Reactor.ksMath.DeltaAngle ( float  current,
float  target 
)
static

Calculates the shortest difference between two given angles given in degrees.

Parameters
current
target
Returns

◆ Exp()

static float KS.Reactor.ksMath.Exp ( float  power)
static

Returns e raised to the specified power.

Parameters
power
Returns

◆ Floor()

static float KS.Reactor.ksMath.Floor ( float  f)
static

Returns the largest integer smaller than or equal to f.

Parameters
f
Returns

◆ FloorToInt()

static int KS.Reactor.ksMath.FloorToInt ( float  f)
static

Returns the largest integer smaller to or equal to f.

Parameters
f
Returns

◆ Gamma()

static float KS.Reactor.ksMath.Gamma ( float  value,
float  absmax,
float  power 
)
static

Normalize a value between negative absmax and positive absmax to -1...1 then raise the result to a power before remapping it into negative absmax to absmax .

Parameters
value
absmax
power
Returns

◆ Interpolate()

static float KS.Reactor.ksMath.Interpolate ( float  a,
float  b,
float  t,
Interpolation  equation 
)
static

Interpolate between the value a and b according to an interpolation method.

Parameters
a
b
t
equation
Returns

◆ InverseLerp()

static float KS.Reactor.ksMath.InverseLerp ( float  a,
float  b,
float  value 
)
static

Calculates the linear parameter t that produces the interpolant value within the range [a, b].

Parameters
a
b
value
Returns

◆ IsPowerOfTwo()

static bool KS.Reactor.ksMath.IsPowerOfTwo ( int  value)
static

Returns true if the value is power of two.

Parameters
value
Returns

◆ Lerp()

static float KS.Reactor.ksMath.Lerp ( float  a,
float  b,
float  t 
)
static

Linearly interpolates between a and b by t.

Parameters
a
b
t
Returns

◆ LerpAngle()

static float KS.Reactor.ksMath.LerpAngle ( float  a,
float  b,
float  t 
)
static

Same as Lerp but makes sure the values interpolate correctly when they wrap around 360 degrees.

Parameters
a
b
t
Returns

◆ LerpUnclamped()

static float KS.Reactor.ksMath.LerpUnclamped ( float  a,
float  b,
float  t 
)
static

Linearly interpolates between a and b by t with no limit to t.

Parameters
a
b
t
Returns

◆ LineIntersection()

static bool KS.Reactor.ksMath.LineIntersection ( ksVector2  p1,
ksVector2  p2,
ksVector2  p3,
ksVector2  p4,
ref ksVector2  result 
)
static

Check if the line (p1,p2) intersects with line (p3, p4)

Parameters
p1
p2
p3
p4
result
Returns

◆ LineSegmentIntersection()

static bool KS.Reactor.ksMath.LineSegmentIntersection ( ksVector2  p1,
ksVector2  p2,
ksVector2  p3,
ksVector2  p4,
ref ksVector2  result 
)
static

Check if the line segment (p1,p2) intersects with the line segment (p3, p4)

Parameters
p1
p2
p3
p4
result
Returns

◆ Log() [1/2]

static float KS.Reactor.ksMath.Log ( float  f)
static

Returns the logarithm of a specified number in a specified base.

Parameters
f
Returns

◆ Log() [2/2]

static float KS.Reactor.ksMath.Log ( float  f,
float  p 
)
static

Returns the logarithm of a specified number in a specified base.

Parameters
f
p
Returns

◆ Log10()

static float KS.Reactor.ksMath.Log10 ( float  f)
static

Returns the base 10 logarithm of a specified number.

Parameters
f
Returns

◆ Max() [1/4]

static float KS.Reactor.ksMath.Max ( float  a,
float  b 
)
static

Returns largest of two or more values.

Parameters
a
b
Returns

◆ Max() [2/4]

static int KS.Reactor.ksMath.Max ( int  a,
int  b 
)
static

Returns largest of two or more values.

Parameters
a
b
Returns

◆ Max() [3/4]

static float KS.Reactor.ksMath.Max ( params float[]  values)
static

Returns largest of two or more values.

Parameters
values
Returns

◆ Max() [4/4]

static int KS.Reactor.ksMath.Max ( params int[]  values)
static

Returns largest of two or more values.

Parameters
values
Returns

◆ MaxSetBit()

static int KS.Reactor.ksMath.MaxSetBit ( int  value)
static

Get the highest bit set from 1 to 32.

If no bits are set, then this method returns 0.

Parameters
value
Returns

◆ Min() [1/4]

static float KS.Reactor.ksMath.Min ( float  a,
float  b 
)
static

Returns the smallest of two or more values.

Parameters
a
b
Returns

◆ Min() [2/4]

static int KS.Reactor.ksMath.Min ( int  a,
int  b 
)
static

Returns the smallest of two or more values.

Parameters
a
b
Returns

◆ Min() [3/4]

static float KS.Reactor.ksMath.Min ( params float[]  values)
static

Returns the smallest of two or more values.

Parameters
values
Returns

◆ Min() [4/4]

static int KS.Reactor.ksMath.Min ( params int[]  values)
static

Returns the smallest of two or more values.

Parameters
values
Returns

◆ MoveTowards()

static float KS.Reactor.ksMath.MoveTowards ( float  current,
float  target,
float  maxDelta 
)
static

Moves a value current towards target.

A negative value moves the value away from the target.

Parameters
current
target
maxDelta
Returns

◆ MoveTowardsAngle()

static float KS.Reactor.ksMath.MoveTowardsAngle ( float  current,
float  target,
float  maxDelta 
)
static

Same as MoveTowards but makes sure the values interpolate correctly when they wrap around 360 degrees.

Parameters
current
target
maxDelta
Returns

◆ NextPowerOfTwo()

static int KS.Reactor.ksMath.NextPowerOfTwo ( int  value)
static

Returns the next power of two that is equal to, or greater than, the argument.

Parameters
value
Returns

◆ PerlinNoise()

static float KS.Reactor.ksMath.PerlinNoise ( float  x,
float  y = 0.0f,
float  z = 0.0f,
float  range = 1.0f 
)
static

Generate Perlin noise.

Parameters
xX value
yY value
zZ value
rangeValues will be remapped onto [0...range) using valuerange.
Returns

◆ PingPong()

static float KS.Reactor.ksMath.PingPong ( float  t,
float  length 
)
static

PingPong returns a value that will increment and decrement between the value 0 and length.

Parameters
t
length
Returns

◆ Pow()

static float KS.Reactor.ksMath.Pow ( float  f,
float  p 
)
static

Returns f raised to power p.

Parameters
f
p
Returns

◆ Repeat()

static float KS.Reactor.ksMath.Repeat ( float  t,
float  length 
)
static

Loops the value t, so that it is never larger than length and never smaller than 0.

Parameters
t
length
Returns

◆ Round()

static float KS.Reactor.ksMath.Round ( float  f)
static

Returns f rounded to the nearest integer.

Parameters
f
Returns

◆ RoundToInt()

static int KS.Reactor.ksMath.RoundToInt ( float  f)
static

Returns f rounded to the nearest integer.

Parameters
f
Returns

◆ Sign()

static float KS.Reactor.ksMath.Sign ( float  f)
static

Returns the sign of f.

Parameters
f
Returns

◆ Sin()

static float KS.Reactor.ksMath.Sin ( float  f)
static

Returns the sine of angle f.

Parameters
f
Returns

◆ SmoothDamp()

static float KS.Reactor.ksMath.SmoothDamp ( float  current,
float  target,
ref float  currentVelocity,
float  smoothTime,
float  deltaTime,
float  maxSpeed = float.PositiveInfinity 
)
static

Gradually changes a value towards a desired goal over time.

Parameters
current
target
currentVelocity
smoothTime
deltaTime
maxSpeed
Returns

◆ SmoothDampAngle()

static float KS.Reactor.ksMath.SmoothDampAngle ( float  current,
float  target,
ref float  currentVelocity,
float  smoothTime,
float  deltaTime,
float  maxSpeed = float.PositiveInfinity 
)
static

Gradually changes an angle given in degrees towards a desired goal angle over time.

Parameters
current
target
currentVelocity
smoothTime
deltaTime
maxSpeed
Returns

◆ SmoothStep()

static float KS.Reactor.ksMath.SmoothStep ( float  from,
float  to,
float  t 
)
static

Interpolates between min and max with smoothing at the limits.

Parameters
from
to
t
Returns

◆ Sqrt()

static float KS.Reactor.ksMath.Sqrt ( float  f)
static

Returns square root of f.

Parameters
f
Returns

◆ Tan()

static float KS.Reactor.ksMath.Tan ( float  f)
static

Returns the tangent of angle f in radians.

Parameters
f
Returns

Member Data Documentation

◆ Epsilon

readonly float KS.Reactor.ksMath.Epsilon = (double)float.Epsilon == 0.0 ? 1.175494E-38f : float.Epsilon
static

Smallest float value that does not equate to 0.

On ARM systems, the value of the Epsilon constant is too small to be detected, so it equates to zero. In this case we use the value 1.175494E-38f.