- java.lang.Object
-
- org.spongepowered.math.GenericMath
-
public final class GenericMath extends Object
Class containing generic mathematical functions.
-
-
Field Summary
Fields Modifier and Type Field Description static doubleDBL_EPSILONA "close to zero" double epsilon value for usestatic floatFLT_EPSILONA "close to zero" float epsilon value for use
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static doublebiLerp(double x, double y, double q00, double q01, double q10, double q11, double x1, double x2, double y1, double y2)Calculates the value at x,y using bilinear interpolationstatic java.awt.Colorblend(java.awt.Color a, java.awt.Color b)Blends two colors into one.static BooleancastBoolean(Object o)Casts a value to a boolean.static BytecastByte(Object o)Casts a value to a byte.static DoublecastDouble(Object o)Casts a value to a double.static FloatcastFloat(Object o)Casts a value to a float.static IntegercastInt(Object o)Casts a value to an integer.static LongcastLong(Object o)Casts a value to a long.static ShortcastShort(Object o)Casts a value to a short.static doubleclamp(double value, double low, double high)Clamps the value between the low and high boundariesstatic intclamp(int value, int low, int high)Clamps the value between the low and high boundariesstatic StringdecToHex(int dec, int minDigits)Converts an integer to hexadecimal form with at least the minimum of digits specified (by adding leading zeros).static floatdegreeDifference(float angle1, float angle2)Gets the difference between two angles This value is always positive (0 - 180)static intfloor(double a)Rounds 'a' down to the closest integerstatic intfloor(float a)Rounds 'a' down to the closest integerstatic longfloorl(double a)Rounds 'a' down to the closest longstatic longfloorl(float a)Rounds 'a' down to the closest longstatic doubleinverseSqrt(double a)Returns a fast estimate of the inverse square root of the valuestatic booleanisPowerOfTwo(int num)Determines if the given number is a power of two.static doublelerp(double a, double b, double percent)Calculates the linear interpolation between a and b with the given percentstatic doublelerp(double x, double x1, double x2, double q0, double q1)Calculates the value at x using linear interpolationstatic floatlerp(float a, float b, float percent)Calculates the linear interpolation between a and b with the given percentstatic intlerp(int a, int b, int percent)Calculates the linear interpolation between a and b with the given percentstatic java.awt.Colorlerp(java.awt.Color a, java.awt.Color b, float percent)Calculates the linear interpolation between a and b with the given percentstatic Quaterniondlerp(Quaterniond a, Quaterniond b, double percent)Interpolates a quaternion between two others using linear interpolation.static Quaternionflerp(Quaternionf a, Quaternionf b, float percent)Interpolates a quaternion between two others using linear interpolation.static Vector2dlerp(Vector2d a, Vector2d b, double percent)Calculates the linear interpolation between a and b with the given percentstatic Vector2flerp(Vector2f a, Vector2f b, float percent)Calculates the linear interpolation between a and b with the given percentstatic Vector3dlerp(Vector3d a, Vector3d b, double percent)Calculates the linear interpolation between a and b with the given percentstatic Vector3flerp(Vector3f a, Vector3f b, float percent)Calculates the linear interpolation between a and b with the given percentstatic bytemax(byte value1, byte value2)Gets the maximum byte value from two valuesstatic doublemean(double... values)Calculates the mean of a set of values.static intmean(int... values)Calculates the mean of a set of valuesstatic doublemod(double a, double div)Returns the modulo of 'a' by 'div' with corrections for negative numbers.static floatmod(float a, float div)Returns the modulo of 'a' by 'div' with corrections for negative numbers.static intmod(int a, int div)Returns the modulo of 'a' by 'div' with corrections for negative numbers.static intmultiplyToShift(int a)Converts a multiplication into a shift.static Vector2dnormalizeSafe(Vector2d v)Attempts to normalize a vector.static Vector2fnormalizeSafe(Vector2f v)Attempts to normalize a vector.static Vector3dnormalizeSafe(Vector3d v)Attempts to normalize a vector.static Vector3fnormalizeSafe(Vector3f v)Attempts to normalize a vector.static Vector4dnormalizeSafe(Vector4d v)Attempts to normalize a vector.static Vector4fnormalizeSafe(Vector4f v)Attempts to normalize a vector.static VectorNdnormalizeSafe(VectorNd v)Attempts to normalize a vector.static VectorNfnormalizeSafe(VectorNf v)Attempts to normalize a vector.static doubleradianDifference(double radian1, double radian2)Gets the difference between two radians This value is always positive (0 - PI)static doubleround(double input, int decimals)Rounds a number to the amount of decimals specifiedstatic introundUpPow2(int a)Rounds an integer up to the next power of 2.static longroundUpPow2(long a)Rounds an integer up to the next power of 2.static Quaterniondslerp(Quaterniond a, Quaterniond b, double percent)Interpolates a quaternion between two others using spherical linear interpolation.static Quaternionfslerp(Quaternionf a, Quaternionf b, float percent)Interpolates a quaternion between two others using spherical linear interpolation.static doublesqrt(double a)Returns a fast estimate of the square root of the valuestatic doubletriLerp(double x, double y, double z, double q000, double q001, double q010, double q011, double q100, double q101, double q110, double q111, double x1, double x2, double y1, double y2, double z1, double z2)Calculates the value at x,y,z using trilinear interpolationstatic floatwrapAngleDeg(float angle)Wraps the angle between -180 and 180 degreesstatic floatwrapAnglePitchDeg(float angle)Wraps the pitch angle between -90 and 90 degreesstatic doublewrapAngleRad(double angle)Wraps the radian between -PI and PIstatic bytewrapByte(int value)Wraps a byte between 0 and 256
-
-
-
Method Detail
-
degreeDifference
public static float degreeDifference(float angle1, float angle2)Gets the difference between two angles This value is always positive (0 - 180)- Parameters:
angle1- The first angleangle2- The second angle- Returns:
- the positive angle difference
-
radianDifference
public static double radianDifference(double radian1, double radian2)Gets the difference between two radians This value is always positive (0 - PI)- Parameters:
radian1- The first angleradian2- The second angle- Returns:
- the positive radian difference
-
wrapAngleDeg
public static float wrapAngleDeg(float angle)
Wraps the angle between -180 and 180 degrees- Parameters:
angle- to wrap- Returns:
- -180 < angle <= 180
-
wrapAngleRad
public static double wrapAngleRad(double angle)
Wraps the radian between -PI and PI- Parameters:
angle- to wrap- Returns:
- -PI < radian <= PI
-
wrapAnglePitchDeg
public static float wrapAnglePitchDeg(float angle)
Wraps the pitch angle between -90 and 90 degrees- Parameters:
angle- to wrap- Returns:
- -90 < angle < 90
-
wrapByte
public static byte wrapByte(int value)
Wraps a byte between 0 and 256- Parameters:
value- to wrap- Returns:
- 0 < byte < 256
-
round
public static double round(double input, int decimals)Rounds a number to the amount of decimals specified- Parameters:
input- to rounddecimals- to round to- Returns:
- the rounded number
-
lerp
public static double lerp(double a, double b, double percent)Calculates the linear interpolation between a and b with the given percent- Parameters:
a- The first know valueb- The second know valuepercent- The percent- Returns:
- the interpolated value
-
lerp
public static float lerp(float a, float b, float percent)Calculates the linear interpolation between a and b with the given percent- Parameters:
a- The first know valueb- The second know valuepercent- The percent- Returns:
- the interpolated value
-
lerp
public static int lerp(int a, int b, int percent)Calculates the linear interpolation between a and b with the given percent- Parameters:
a- The first know valueb- The second know valuepercent- The percent- Returns:
- the interpolated value
-
lerp
public static Vector3f lerp(Vector3f a, Vector3f b, float percent)
Calculates the linear interpolation between a and b with the given percent- Parameters:
a- The first know valueb- The second know valuepercent- The percent- Returns:
- the interpolated vector
-
lerp
public static Vector3d lerp(Vector3d a, Vector3d b, double percent)
Calculates the linear interpolation between a and b with the given percent- Parameters:
a- The first know valueb- The second know valuepercent- The percent- Returns:
- the interpolated vector
-
lerp
public static Vector2f lerp(Vector2f a, Vector2f b, float percent)
Calculates the linear interpolation between a and b with the given percent- Parameters:
a- The first know valueb- The second know valuepercent- The percent- Returns:
- the interpolated vector
-
lerp
public static Vector2d lerp(Vector2d a, Vector2d b, double percent)
Calculates the linear interpolation between a and b with the given percent- Parameters:
a- The first know valueb- The second know valuepercent- The percent- Returns:
- the interpolated vector
-
lerp
public static double lerp(double x, double x1, double x2, double q0, double q1)Calculates the value at x using linear interpolation- Parameters:
x- the X coord of the value to interpolatex1- the X coord of q0x2- the X coord of q1q0- the first known value (x1)q1- the second known value (x2)- Returns:
- the interpolated value
-
lerp
public static java.awt.Color lerp(java.awt.Color a, java.awt.Color b, float percent)Calculates the linear interpolation between a and b with the given percent- Parameters:
a- The first know valueb- The second know valuepercent- The percent- Returns:
- Color
-
slerp
public static Quaternionf slerp(Quaternionf a, Quaternionf b, float percent)
Interpolates a quaternion between two others using spherical linear interpolation.- Parameters:
a- The first quaternionb- The second quaternionpercent- The percent for the interpolation, between 0 and 1 inclusively- Returns:
- The interpolated quaternion
-
slerp
public static Quaterniond slerp(Quaterniond a, Quaterniond b, double percent)
Interpolates a quaternion between two others using spherical linear interpolation.- Parameters:
a- The first quaternionb- The second quaternionpercent- The percent for the interpolation, between 0 and 1 inclusively- Returns:
- The interpolated quaternion
-
lerp
public static Quaternionf lerp(Quaternionf a, Quaternionf b, float percent)
Interpolates a quaternion between two others using linear interpolation.- Parameters:
a- The first quaternionb- The second quaternionpercent- The percent for the interpolation, between 0 and 1 inclusively- Returns:
- The interpolated quaternion
-
lerp
public static Quaterniond lerp(Quaterniond a, Quaterniond b, double percent)
Interpolates a quaternion between two others using linear interpolation.- Parameters:
a- The first quaternionb- The second quaternionpercent- The percent for the interpolation, between 0 and 1 inclusively- Returns:
- The interpolated quaternion
-
biLerp
public static double biLerp(double x, double y, double q00, double q01, double q10, double q11, double x1, double x2, double y1, double y2)Calculates the value at x,y using bilinear interpolation- Parameters:
x- the X coord of the value to interpolatey- the Y coord of the value to interpolateq00- the first known value (x1, y1)q01- the second known value (x1, y2)q10- the third known value (x2, y1)q11- the fourth known value (x2, y2)x1- the X coord of q00 and q01x2- the X coord of q10 and q11y1- the Y coord of q00 and q10y2- the Y coord of q01 and q11- Returns:
- the interpolated value
-
triLerp
public static double triLerp(double x, double y, double z, double q000, double q001, double q010, double q011, double q100, double q101, double q110, double q111, double x1, double x2, double y1, double y2, double z1, double z2)Calculates the value at x,y,z using trilinear interpolation- Parameters:
x- the X coord of the value to interpolatey- the Y coord of the value to interpolatez- the Z coord of the value to interpolateq000- the first known value (x1, y1, z1)q001- the second known value (x1, y2, z1)q010- the third known value (x1, y1, z2)q011- the fourth known value (x1, y2, z2)q100- the fifth known value (x2, y1, z1)q101- the sixth known value (x2, y2, z1)q110- the seventh known value (x2, y1, z2)q111- the eighth known value (x2, y2, z2)x1- the X coord of q000, q001, q010 and q011x2- the X coord of q100, q101, q110 and q111y1- the Y coord of q000, q010, q100 and q110y2- the Y coord of q001, q011, q101 and q111z1- the Z coord of q000, q001, q100 and q101z2- the Z coord of q010, q011, q110 and q111- Returns:
- the interpolated value
-
blend
public static java.awt.Color blend(java.awt.Color a, java.awt.Color b)Blends two colors into one.- Parameters:
a- The first colorb- The second color- Returns:
- The blended color
-
clamp
public static double clamp(double value, double low, double high)Clamps the value between the low and high boundaries- Parameters:
value- The value to clamplow- The low bound of the clamphigh- The high bound of the clamp- Returns:
- the clamped value
-
clamp
public static int clamp(int value, int low, int high)Clamps the value between the low and high boundaries- Parameters:
value- The value to clamplow- The low bound of the clamphigh- The high bound of the clamp- Returns:
- the clamped value
-
inverseSqrt
public static double inverseSqrt(double a)
Returns a fast estimate of the inverse square root of the value- Parameters:
a- The value- Returns:
- The estimate of the inverse square root
-
sqrt
public static double sqrt(double a)
Returns a fast estimate of the square root of the value- Parameters:
a- The value- Returns:
- The estimate of the square root
-
floor
public static int floor(double a)
Rounds 'a' down to the closest integer- Parameters:
a- The value to floor- Returns:
- The closest integer
-
floor
public static int floor(float a)
Rounds 'a' down to the closest integer- Parameters:
a- The value to floor- Returns:
- The closest integer
-
floorl
public static long floorl(double a)
Rounds 'a' down to the closest long- Parameters:
a- The value to floor- Returns:
- The closest long
-
floorl
public static long floorl(float a)
Rounds 'a' down to the closest long- Parameters:
a- The value to floor- Returns:
- The closest long
-
max
public static byte max(byte value1, byte value2)Gets the maximum byte value from two values- Parameters:
value1- The first valuevalue2- The second value- Returns:
- the maximum of value1 and value2
-
roundUpPow2
public static int roundUpPow2(int a)
Rounds an integer up to the next power of 2.- Parameters:
a- The integer to round- Returns:
- the lowest power of 2 greater or equal to 'a'
-
roundUpPow2
public static long roundUpPow2(long a)
Rounds an integer up to the next power of 2.- Parameters:
a- The long to round- Returns:
- the lowest power of 2 greater or equal to 'a'
-
castFloat
public static Float castFloat(Object o)
Casts a value to a float. May return null.- Parameters:
o- The object to attempt to cast- Returns:
- The object as a float
-
castByte
public static Byte castByte(Object o)
Casts a value to a byte. May return null.- Parameters:
o- The object to attempt to cast- Returns:
- The object as a byte
-
castShort
public static Short castShort(Object o)
Casts a value to a short. May return null.- Parameters:
o- The object to attempt to cast- Returns:
- The object as a short
-
castInt
public static Integer castInt(Object o)
Casts a value to an integer. May return null.- Parameters:
o- The object to attempt to cast- Returns:
- The object as an int
-
castDouble
public static Double castDouble(Object o)
Casts a value to a double. May return null.- Parameters:
o- The object to attempt to cast- Returns:
- The object as a double
-
castLong
public static Long castLong(Object o)
Casts a value to a long. May return null.- Parameters:
o- The object to attempt to cast- Returns:
- The object as a long
-
castBoolean
public static Boolean castBoolean(Object o)
Casts a value to a boolean. May return null.- Parameters:
o- The object to attempt to cast- Returns:
- The object as a boolean
-
mean
public static int mean(int... values)
Calculates the mean of a set of values- Parameters:
values- to calculate the mean of- Returns:
- the mean of the values
-
mean
public static double mean(double... values)
Calculates the mean of a set of values.- Parameters:
values- to calculate the mean of- Returns:
- the mean of the values
-
decToHex
public static String decToHex(int dec, int minDigits)
Converts an integer to hexadecimal form with at least the minimum of digits specified (by adding leading zeros).- Parameters:
dec- The integer to convertminDigits- The minimum of digits in the hexadecimal form- Returns:
- The integer in hexadecimal form
-
mod
public static int mod(int a, int div)Returns the modulo of 'a' by 'div' with corrections for negative numbers.- Parameters:
a- The number as an intdiv- The div as an int- Returns:
- The corrected modulo
-
mod
public static float mod(float a, float div)Returns the modulo of 'a' by 'div' with corrections for negative numbers.- Parameters:
a- The dividenddiv- The divider- Returns:
- The corrected modulo
-
mod
public static double mod(double a, double div)Returns the modulo of 'a' by 'div' with corrections for negative numbers.- Parameters:
a- The dividenddiv- The divider- Returns:
- The corrected modulo
-
isPowerOfTwo
public static boolean isPowerOfTwo(int num)
Determines if the given number is a power of two. A number is a power of 2 if it is 1 or greater, and it contains no similar bits of the given number - 1.- Parameters:
num- the number to check- Returns:
- true if num is a power of two
-
multiplyToShift
public static int multiplyToShift(int a)
Converts a multiplication into a shift.- Parameters:
a- the multiplicand- Returns:
- the left shift required to multiply by the multiplicand
-
normalizeSafe
public static Vector2f normalizeSafe(Vector2f v)
Attempts to normalize a vector. If this fails, the method catches the exception and return a zero vector of the same dimension instead.- Parameters:
v- The vector to attempt to normalize- Returns:
- The normalized vector, or the zero vector if it couldn't be normalized.
-
normalizeSafe
public static Vector2d normalizeSafe(Vector2d v)
Attempts to normalize a vector. If this fails, the method catches the exception and return a zero vector of the same dimension instead.- Parameters:
v- The vector to attempt to normalize- Returns:
- The normalized vector, or the zero vector if it couldn't be normalized.
-
normalizeSafe
public static Vector3f normalizeSafe(Vector3f v)
Attempts to normalize a vector. If this fails, the method catches the exception and return a zero vector of the same dimension instead.- Parameters:
v- The vector to attempt to normalize- Returns:
- The normalized vector, or the zero vector if it couldn't be normalized.
-
normalizeSafe
public static Vector3d normalizeSafe(Vector3d v)
Attempts to normalize a vector. If this fails, the method catches the exception and return a zero vector of the same dimension instead.- Parameters:
v- The vector to attempt to normalize- Returns:
- The normalized vector, or the zero vector if it couldn't be normalized.
-
normalizeSafe
public static Vector4f normalizeSafe(Vector4f v)
Attempts to normalize a vector. If this fails, the method catches the exception and return a zero vector of the same dimension instead.- Parameters:
v- The vector to attempt to normalize- Returns:
- The normalized vector, or the zero vector if it couldn't be normalized.
-
normalizeSafe
public static Vector4d normalizeSafe(Vector4d v)
Attempts to normalize a vector. If this fails, the method catches the exception and return a zero vector of the same dimension instead.- Parameters:
v- The vector to attempt to normalize- Returns:
- The normalized vector, or the zero vector if it couldn't be normalized.
-
normalizeSafe
public static VectorNf normalizeSafe(VectorNf v)
Attempts to normalize a vector. If this fails, the method catches the exception and return a zero vector of the same dimension instead.- Parameters:
v- The vector to attempt to normalize- Returns:
- The normalized vector, or the zero vector if it couldn't be normalized.
-
normalizeSafe
public static VectorNd normalizeSafe(VectorNd v)
Attempts to normalize a vector. If this fails, the method catches the exception and return a zero vector of the same dimension instead.- Parameters:
v- The vector to attempt to normalize- Returns:
- The normalized vector, or the zero vector if it couldn't be normalized.
-
-