java.lang.Object
org.spongepowered.math.GenericMath
Class containing generic mathematical functions.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final double
A "close to zero" double epsilon value for usestatic final float
A "close to zero" float epsilon value for use -
Method Summary
Modifier and TypeMethodDescriptionstatic 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 interpolationstatic Color
Blends two colors into one.static Boolean
Casts a value to a boolean.static Byte
Casts a value to a byte.static Double
castDouble
(Object o) Casts a value to a double.static Float
Casts a value to a float.static Integer
Casts a value to an integer.static Long
Casts a value to a long.static Short
Casts a value to a short.static double
clamp
(double value, double low, double high) Clamps the value between the low and high boundariesstatic int
clamp
(int value, int low, int high) Clamps the value between the low and high boundariesstatic String
decToHex
(int dec, int minDigits) Converts an integer to hexadecimal form with at least the minimum of digits specified (by adding leading zeros).static float
degreeDifference
(float angle1, float angle2) Gets the difference between two angles This value is always positive (0 - 180)static int
floor
(double a) Rounds 'a' down to the closest integerstatic int
floor
(float a) Rounds 'a' down to the closest integerstatic long
floorl
(double a) Rounds 'a' down to the closest longstatic long
floorl
(float a) Rounds 'a' down to the closest longstatic double
inverseSqrt
(double a) Returns a fast estimate of the inverse square root of the valuestatic boolean
isPowerOfTwo
(int num) Determines if the given number is a power of two.static double
lerp
(double a, double b, double percent) Calculates the linear interpolation between a and b with the given percentstatic double
lerp
(double x, double x1, double x2, double q0, double q1) Calculates the value at x using linear interpolationstatic float
lerp
(float a, float b, float percent) Calculates the linear interpolation between a and b with the given percentstatic int
lerp
(int a, int b, int percent) Calculates the linear interpolation between a and b with the given percentstatic Color
Calculates the linear interpolation between a and b with the given percentstatic Quaterniond
lerp
(Quaterniond a, Quaterniond b, double percent) Interpolates a quaternion between two others using linear interpolation.static Quaternionf
lerp
(Quaternionf a, Quaternionf b, float percent) Interpolates a quaternion between two others using linear interpolation.static Vector2d
Calculates the linear interpolation between a and b with the given percentstatic Vector2f
Calculates the linear interpolation between a and b with the given percentstatic Vector3d
Calculates the linear interpolation between a and b with the given percentstatic Vector3f
Calculates the linear interpolation between a and b with the given percentstatic byte
max
(byte value1, byte value2) Gets the maximum byte value from two valuesstatic double
mean
(double... values) Calculates the mean of a set of values.static int
mean
(int... values) Calculates the mean of a set of valuesstatic double
mod
(double a, double div) Returns the modulo of 'a' by 'div' with corrections for negative numbers.static float
mod
(float a, float div) Returns the modulo of 'a' by 'div' with corrections for negative numbers.static int
mod
(int a, int div) Returns the modulo of 'a' by 'div' with corrections for negative numbers.static int
multiplyToShift
(int a) Converts a multiplication into a shift.static Vector2d
Attempts to normalize a vector.static Vector2f
Attempts to normalize a vector.static Vector3d
Attempts to normalize a vector.static Vector3f
Attempts to normalize a vector.static Vector4d
Attempts to normalize a vector.static Vector4f
Attempts to normalize a vector.static VectorNd
Attempts to normalize a vector.static VectorNf
Attempts to normalize a vector.static double
radianDifference
(double radian1, double radian2) Gets the difference between two radians This value is always positive (0 - PI)static double
round
(double input, int decimals) Rounds a number to the amount of decimals specifiedstatic int
roundUpPow2
(int a) Rounds an integer up to the next power of 2.static long
roundUpPow2
(long a) Rounds an integer up to the next power of 2.static Quaterniond
slerp
(Quaterniond a, Quaterniond b, double percent) Interpolates a quaternion between two others using spherical linear interpolation.static Quaternionf
slerp
(Quaternionf a, Quaternionf b, float percent) Interpolates a quaternion between two others using spherical linear interpolation.static double
sqrt
(double a) Returns a fast estimate of the square root of the valuestatic 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 interpolationstatic float
wrapAngleDeg
(float angle) Wraps the angle between -180 and 180 degreesstatic float
wrapAnglePitchDeg
(float angle) Wraps the pitch angle between -90 and 90 degreesstatic double
wrapAngleRad
(double angle) Wraps the radian between -PI and PIstatic byte
wrapByte
(int value) Wraps a byte between 0 and 256
-
Field Details
-
DBL_EPSILON
public static final double DBL_EPSILONA "close to zero" double epsilon value for use -
FLT_EPSILON
public static final float FLT_EPSILONA "close to zero" float epsilon value for use
-
-
Method Details
-
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
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
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
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
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
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
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
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
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
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
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
Casts a value to a float. May return null.- Parameters:
o
- The object to attempt to cast- Returns:
- The object as a float
-
castByte
Casts a value to a byte. May return null.- Parameters:
o
- The object to attempt to cast- Returns:
- The object as a byte
-
castShort
Casts a value to a short. May return null.- Parameters:
o
- The object to attempt to cast- Returns:
- The object as a short
-
castInt
Casts a value to an integer. May return null.- Parameters:
o
- The object to attempt to cast- Returns:
- The object as an int
-
castDouble
Casts a value to a double. May return null.- Parameters:
o
- The object to attempt to cast- Returns:
- The object as a double
-
castLong
Casts a value to a long. May return null.- Parameters:
o
- The object to attempt to cast- Returns:
- The object as a long
-
castBoolean
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
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
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
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
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
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
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
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
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
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.
-