Package com.thegongoliers.math
Class GMath
- java.lang.Object
-
- com.thegongoliers.math.GMath
-
public class GMath extends java.lang.Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
GMath.UnequalLengthException
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
approximately(double a, double b)
Determines if a value is approximately equal to another value.static boolean
approximately(double value, double compare, double precision)
Determines if a value is approximately equal to another value.static double
clamp(double value, double min, double max)
Constrain a value to the range, where if the value is out of the range it is converted to the nearest range bound.static int
clamp(int value, int min, int max)
static double
clamp01(double value)
Constrain a value to be within 0 and 1static double
correlation(double[] x, double[] y)
Computes the correlation between two variables.static double
deadband(double value, double threshold)
Deadbands an input (makes 0 if below threshold)static double
deltaAngle(double current, double target)
Calculates the shortest difference between two angles in degrees.static boolean
divisibleBy(int value, int divisor)
Determines if a number is divisible by another number.static double
inverseLerp(double a, double b, double value)
Calculates the inverse linear interpolation of a value within the range [a, b]static double
lerp(double a, double b, double t)
Calculates the linear interpolation of t into the range [a, b]static double
lerpAngle(double a, double b, double t)
Calculates the linear interpolation of t into the range [a, b], wraps correctly around 360 degreesstatic double
lerpUnclamped(double a, double b, double t)
Calculates the linear interpolation of t into the range [a, b]static double
magnitude(double... values)
Calculate the magnitude of the given values.static double
max(double... values)
Find the maximum valuestatic int
max(int... values)
Find the maximum valuestatic double
mean(double[] x)
Computes the mean of a variable.static double
min(double... values)
Find the minimum valuestatic int
min(int... values)
Find the minimum valuestatic double
moveTowards(double current, double target, double maxDelta)
Moves a value toward a target without exceeding maxDeltastatic double
pingPong(double t, double length)
PingPongs the value t, so that it is never larger than length and never smaller than 0.static double
rateLimit(double max, double value, double lastValue)
Limit the max rate of increase.static double
repeat(double t, double length)
Loops the value t, so that it is never larger than length and never smaller than 0.static double
roundPlaces(double value, int numPlaces)
Round a value to the given number of decimal places.static int
roundToInt(double value)
Round a value to the nearest int.static int
sign(double value)
Determines the sign of a valuestatic double
signPreservingPower(double value, double power)
Calculates a value to some power, but preserves the sign if it is negative.static double
slopeFromAngleDegrees(double angle)
Get the slope from the angle in degrees.static double
slopeFromAngleRadians(double angle)
Get the slope from the angle in radians.static double
smoothStep(double from, double to, double t)
Interpolates between from and to with smoothing at the limits.static double
snap(double value, double nearest)
Snap a value to the nearest multiple of the nearest parameter.static double
standardDeviation(double[] x)
Computes the standard deviation of a variable.static double
sum(double... values)
Calculate the sum of an array.
-
-
-
Method Detail
-
magnitude
public static double magnitude(double... values)
Calculate the magnitude of the given values.- Parameters:
values
- The value to calculate the magnitude of.- Returns:
- The magnitude of the values.
-
sign
public static int sign(double value)
Determines the sign of a value- Parameters:
value
- The value which to get the sign from- Returns:
- 1 if the value is positive or 0, -1 if it is negative 0
-
signPreservingPower
public static double signPreservingPower(double value, double power)
Calculates a value to some power, but preserves the sign if it is negative.- Parameters:
value
- the value to raise to a powerpower
- the power to raise the value by- Returns:
- the value to the power, with its sign preserved
-
inverseLerp
public static double inverseLerp(double a, double b, double value)
Calculates the inverse linear interpolation of a value within the range [a, b]- Parameters:
a
- The minimum of the rangeb
- The maximum of the rangevalue
- The value between a and b- Returns:
- the percentage of value between a and b
-
lerp
public static double lerp(double a, double b, double t)
Calculates the linear interpolation of t into the range [a, b]- Parameters:
a
- The minimum of the rangeb
- The maximum of the ranget
- the percentage of value between a and b (between 0 and 1)- Returns:
- the interpolated value between a and b
-
lerpUnclamped
public static double lerpUnclamped(double a, double b, double t)
Calculates the linear interpolation of t into the range [a, b]- Parameters:
a
- The minimum of the rangeb
- The maximum of the ranget
- the percentage of value between a and b- Returns:
- the interpolated value between a and b
-
lerpAngle
public static double lerpAngle(double a, double b, double t)
Calculates the linear interpolation of t into the range [a, b], wraps correctly around 360 degrees- Parameters:
a
- The minimum of the range (degrees)b
- The maximum of the range (degrees)t
- the percentage of value between a and b (between 0 and 1)- Returns:
- the interpolated value between a and b
-
smoothStep
public static double smoothStep(double from, double to, double t)
Interpolates between from and to with smoothing at the limits.- Parameters:
from
- the starting valueto
- the ending valuet
- the percentage of value between from and to (between 0 and 1)- Returns:
- the interpolated value between from and to
-
moveTowards
public static double moveTowards(double current, double target, double maxDelta)
Moves a value toward a target without exceeding maxDelta- Parameters:
current
- the current valuetarget
- the target valuemaxDelta
- the maximum delta- Returns:
- the updated value
-
pingPong
public static double pingPong(double t, double length)
PingPongs the value t, so that it is never larger than length and never smaller than 0.- Parameters:
t
- the value to ping ponglength
- the length of the ping pong- Returns:
- the value which will move back and forth between 0 and length
-
repeat
public static double repeat(double t, double length)
Loops the value t, so that it is never larger than length and never smaller than 0. Does not work on negative numbers.- Parameters:
t
- the value to looplength
- the length of the loop- Returns:
- the 'modulus' of t and length
-
deadband
public static double deadband(double value, double threshold)
Deadbands an input (makes 0 if below threshold)- Parameters:
value
- the input valuethreshold
- the deadband threshold- Returns:
- the thresholded input
-
sum
public static double sum(double... values)
Calculate the sum of an array.- Parameters:
values
- The array to sum.- Returns:
- The sum of the array.
-
min
public static double min(double... values)
Find the minimum value- Parameters:
values
- The value- Returns:
- The min value
-
max
public static double max(double... values)
Find the maximum value- Parameters:
values
- The values- Returns:
- The max value
-
min
public static int min(int... values)
Find the minimum value- Parameters:
values
- The value- Returns:
- The min value
-
max
public static int max(int... values)
Find the maximum value- Parameters:
values
- The values- Returns:
- The max value
-
clamp
public static double clamp(double value, double min, double max)
Constrain a value to the range, where if the value is out of the range it is converted to the nearest range bound.- Parameters:
value
- The value to constrain.min
- The min of the range.max
- The max of the range.- Returns:
- The value which is constrained to the range.
-
clamp
public static int clamp(int value, int min, int max)
-
clamp01
public static double clamp01(double value)
Constrain a value to be within 0 and 1- Parameters:
value
- The value to constrain.- Returns:
- The value which is between 0 and 1
-
roundToInt
public static int roundToInt(double value)
Round a value to the nearest int.- Parameters:
value
- The value to round.- Returns:
- The rounded value as an int.
-
roundPlaces
public static double roundPlaces(double value, int numPlaces)
Round a value to the given number of decimal places.- Parameters:
value
- The value to round.numPlaces
- The number of decimal places to round to.- Returns:
- The rounded value.
-
snap
public static double snap(double value, double nearest)
Snap a value to the nearest multiple of the nearest parameter.- Parameters:
value
- The value to snap.nearest
- The value to snap to.- Returns:
- The value snapped to the nearest multiple of nearest.
-
divisibleBy
public static boolean divisibleBy(int value, int divisor)
Determines if a number is divisible by another number.- Parameters:
value
- The numerator.divisor
- The denominator.- Returns:
- True if the value is divisible by the divider.
-
approximately
public static boolean approximately(double value, double compare, double precision)
Determines if a value is approximately equal to another value.- Parameters:
value
- The value to compare.compare
- The value to compare.precision
- The precision of the equality.- Returns:
- True if the value is equal to the compare value with the given precision.
-
approximately
public static boolean approximately(double a, double b)
Determines if a value is approximately equal to another value.- Parameters:
a
- The value to compare.b
- The value to compare.- Returns:
- True if a and b are equal to within epsilon.
-
deltaAngle
public static double deltaAngle(double current, double target)
Calculates the shortest difference between two angles in degrees.- Parameters:
current
- The current angle in degreestarget
- The target angle in degrees- Returns:
- the shortest difference between the angles in degrees
-
slopeFromAngleDegrees
public static double slopeFromAngleDegrees(double angle)
Get the slope from the angle in degrees.- Parameters:
angle
- The angle in degrees.- Returns:
- The slope of the line which the angle creates.
-
slopeFromAngleRadians
public static double slopeFromAngleRadians(double angle)
Get the slope from the angle in radians.- Parameters:
angle
- The angle in radians.- Returns:
- The slope of the line which the angle creates.
-
rateLimit
public static double rateLimit(double max, double value, double lastValue)
Limit the max rate of increase.- Parameters:
max
- The maximum rate allowed.value
- The current input value.lastValue
- The last value returned by this function.- Returns:
- The updated input value.
-
standardDeviation
public static double standardDeviation(double[] x)
Computes the standard deviation of a variable.- Parameters:
x
- The set of data.- Returns:
- The standard deviation of x.
-
correlation
public static double correlation(double[] x, double[] y)
Computes the correlation between two variables.- Parameters:
x
- The first set of data.y
- The second set of data.- Returns:
- The Pearson correlation from -1 to 1
-
mean
public static double mean(double[] x)
Computes the mean of a variable.- Parameters:
x
- The set of data- Returns:
- The mean of x.
-
-