Class MathUtilsDouble
java.lang.Object
gaiasky.util.math.MathUtilsDouble
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final doublestatic final doublemultiply by this to convert from degrees to radiansstatic final doublestatic final doublestatic final doublestatic final doublestatic final doublemultiply by this to convert from radians to degreesstatic Random -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic doubleatan2(double y, double x) Returns atan2 in radians, faster but less accurate than FastMath.atan2.static doubleclamp(double value, double min, double max) static floatclamp(float value, float min, float max) static intclamp(int value, int min, int max) static longclamp(long value, long min, long max) static shortclamp(short value, short min, short max) static doublecos(double radians) Returns the cosine in radians from a lookup table.static doublecosDeg(double degrees) Returns the cosine in degrees from a lookup table.static doubledistancePointSegment(double x1, double y1, double z1, double x2, double y2, double z2, double x0, double y0, double z0) Gets the distance from the point p0 to the segment denoted by p1-p2.
Check this link.static doubleflint(double x, double x0, double x1, double y0, double y1) Fast linear interpolation.static booleanfuzzyEquals(Double a, Double b, double epsilon) Compares two doubles, with an epsilon.static Vector3DgetClosestPoint2(double x1, double y1, double z1, double x2, double y2, double z2, double x0, double y0, double z0, Vector3D result) static booleanisEqual(double a, double b) Returns true if a is nearly equal to b.static booleanisEqual(double a, double b, double tolerance) Returns true if a is nearly equal to b.static booleanisZero(double value) Returns true if the value is zero (using the default tolerance as upper bound)static booleanisZero(double value, double tolerance) Returns true if the value is zero.static doublelerp(double a, double b, double t) Linearly interpolates between two values.static doublelint(double x, double x0, double x1, double y0, double y1) Linear interpolation.static floatlint(float x, float x0, float x1, float y0, float y1) Linear interpolation.static floatlint(long x, long x0, long x1, float y0, float y1) Linear interpolation.static doublelogisticSigmoid(double x, double span) Implements the logistic sigmoid, defined asexpit(x) = 1/(1+exp(-x)).static doublelogit(double x) Implements the logit function, defined aslogit(x) = log(x/(1-x)).static doublelowPass(double newValue, double smoothedValue, double smoothing) Implements a low-pass filter to smooth the input values.static floatlowPass(float newValue, float smoothedValue, float smoothing) Implements a low-pass filter to smooth the input values.static doublemax(double... values) static floatmax(float... values) static intmax(int... values) static doublemin(double... values) static floatmin(float... values) static intmin(int... values) static doublemix(double x, double y, double a) GLSL mix function: performs a linear interpolation betweenxandyusingato weight between them.static doublenormalizeAngle(double a, double center) Normalize an angle in a 2π wide interval around a center value.static doublerandom()Returns random number between 0.0 (inclusive) and 1.0 (exclusive).static doublerandom(double range) Returns a random number between 0 (inclusive) and the specified value (exclusive).static doublerandom(double start, double end) Returns a random number between start (inclusive) and end (exclusive).static intrandom(int range) Returns a random number between 0 (inclusive) and the specified value (inclusive).static intrandom(int start, int end) Returns a random number between start (inclusive) and end (inclusive).static longrandom(long range) Returns a random number between 0 (inclusive) and the specified value (inclusive).static longrandom(long start, long end) Returns a random number between start (inclusive) and end (inclusive).static doubleroundAvoid(double value, int places) Rounds the double value to a number of decimal placesstatic doublesaturate(double value) static floatsaturate(float value) static doublesin(double radians) Returns the sine in radians from a lookup table.static doublesinDeg(double degrees) Returns the sine in degrees from a lookup table.static doublesmoothstep(double edge0, double edge1, double x) Double precision versionstatic doublesoftMin(double a, double b, double weight) Returns a soft minimum between two values, biased toward the lower one.static doublesqrt(double value) Fast sqrt method.
-
Field Details
-
FLOAT_ROUNDING_ERROR
public static final double FLOAT_ROUNDING_ERROR- See Also:
-
PI
public static final double PI- See Also:
-
PI2
public static final double PI2- See Also:
-
radiansToDegrees
public static final double radiansToDegreesmultiply by this to convert from radians to degrees- See Also:
-
radDeg
public static final double radDeg- See Also:
-
degreesToRadians
public static final double degreesToRadiansmultiply by this to convert from degrees to radians- See Also:
-
degRad
public static final double degRad- See Also:
-
random
-
-
Constructor Details
-
MathUtilsDouble
public MathUtilsDouble()
-
-
Method Details
-
sin
public static double sin(double radians) Returns the sine in radians from a lookup table. -
cos
public static double cos(double radians) Returns the cosine in radians from a lookup table. -
sinDeg
public static double sinDeg(double degrees) Returns the sine in degrees from a lookup table. For optimal precision, use degrees between -360 and 360 (both inclusive). -
cosDeg
public static double cosDeg(double degrees) Returns the cosine in degrees from a lookup table. For optimal precision, use degrees between -360 and 360 (both inclusive). -
atan2
public static double atan2(double y, double x) Returns atan2 in radians, faster but less accurate than FastMath.atan2. Average error of 0.00231 radians (0.1323 degrees), largest error of 0.00488 radians (0.2796 degrees). -
random
public static int random(int range) Returns a random number between 0 (inclusive) and the specified value (inclusive). -
random
public static int random(int start, int end) Returns a random number between start (inclusive) and end (inclusive). -
random
public static long random(long range) Returns a random number between 0 (inclusive) and the specified value (inclusive). -
random
public static long random(long start, long end) Returns a random number between start (inclusive) and end (inclusive). -
random
public static double random()Returns random number between 0.0 (inclusive) and 1.0 (exclusive). -
random
public static double random(double range) Returns a random number between 0 (inclusive) and the specified value (exclusive). -
random
public static double random(double start, double end) Returns a random number between start (inclusive) and end (exclusive). -
clamp
public static int clamp(int value, int min, int max) -
clamp
public static short clamp(short value, short min, short max) -
clamp
public static long clamp(long value, long min, long max) -
clamp
public static float clamp(float value, float min, float max) -
clamp
public static double clamp(double value, double min, double max) -
saturate
public static float saturate(float value) -
saturate
public static double saturate(double value) -
max
public static double max(double... values) -
max
public static float max(float... values) -
max
public static int max(int... values) -
min
public static double min(double... values) -
min
public static float min(float... values) -
min
public static int min(int... values) -
isZero
public static boolean isZero(double value) Returns true if the value is zero (using the default tolerance as upper bound) -
isZero
public static boolean isZero(double value, double tolerance) Returns true if the value is zero.- Parameters:
tolerance- represent an upper bound below which the value is considered zero.
-
isEqual
public static boolean isEqual(double a, double b) Returns true if a is nearly equal to b. The function uses the default doubleing error tolerance.- Parameters:
a- the first value.b- the second value.
-
isEqual
public static boolean isEqual(double a, double b, double tolerance) Returns true if a is nearly equal to b.- Parameters:
a- the first value.b- the second value.tolerance- represent an upper bound below which the two values are considered equal.
-
sqrt
public static double sqrt(double value) Fast sqrt method. Default passes it through one round of Newton's method- Parameters:
value- The value- Returns:
- The square root value
-
mix
public static double mix(double x, double y, double a) GLSL mix function: performs a linear interpolation betweenxandyusingato weight between them.- Parameters:
x- start of the range in which to interpolate.y- end of the range in which to interpolate.a- value to use to interpolate between x and y.- Returns:
- Interpolation between
xandyusingaas weight.
-
softMin
public static double softMin(double a, double b, double weight) Returns a soft minimum between two values, biased toward the lower one. Theweightcontrols how strongly the result is pulled toward the minimum.- Parameters:
a- First value.b- Second value.weight- The interpolation factor. 0 returnsa, 1 returnsmin(a, b).- Returns:
- A softly biased minimum of
aandb.
-
lerp
public static double lerp(double a, double b, double t) Linearly interpolates between two values.- Parameters:
a- The start value.b- The end value.t- The interpolation factor, typically in the range [0, 1]. A value of 0 returnsa, a value of 1 returnsb, and values in between interpolate linearly.- Returns:
- The interpolated value between
aandb.
-
flint
public static double flint(double x, double x0, double x1, double y0, double y1) Fast linear interpolation. If you can guarantee that x0 ≤ x1, then use this.- Parameters:
x- The value to interpolate.x0- Inferior limit to the independent value.x1- Superior limit to the independent value.y0- Inferior limit to the dependent value.y1- Superior limit to the dependent value.- Returns:
- The interpolated value.
-
lint
public static double lint(double x, double x0, double x1, double y0, double y1) Linear interpolation.- Parameters:
x- The value to interpolate.x0- Inferior limit to the independent value.x1- Superior limit to the independent value.y0- Inferior limit to the dependent value.y1- Superior limit to the dependent value.- Returns:
- The interpolated value.
-
lint
public static float lint(float x, float x0, float x1, float y0, float y1) Linear interpolation.- Parameters:
x- The value to interpolate.x0- Inferior limit to the independent value.x1- Superior limit to the independent value.y0- Inferior limit to the dependent value.y1- Superior limit to the dependent value.- Returns:
- The interpolated value
-
lint
public static float lint(long x, long x0, long x1, float y0, float y1) Linear interpolation.- Parameters:
x- The value to interpolate.x0- Inferior limit to the independent value.x1- Superior limit to the independent value.y0- Inferior limit to the dependent value.y1- Superior limit to the dependent value.- Returns:
- The interpolated value
-
distancePointSegment
public static double distancePointSegment(double x1, double y1, double z1, double x2, double y2, double z2, double x0, double y0, double z0) Gets the distance from the point p0 to the segment denoted by p1-p2.
Check this link.- Parameters:
x1- The first segment delimiter.x2- The second segment delimiter.x0- The point.- Returns:
- The Euclidean distance between the segment (x1, x2)
-
getClosestPoint2
-
roundAvoid
public static double roundAvoid(double value, int places) Rounds the double value to a number of decimal places- Parameters:
value- The value to roundplaces- The number of decimal places- Returns:
- The rounded value
-
normalizeAngle
public static double normalizeAngle(double a, double center) Normalize an angle in a 2π wide interval around a center value.This method has three main uses:
- normalize an angle between 0 and 2π:
a = MathUtils.normalizeAngle(a, FastMath.PI); - normalize an angle between -π and +π
a = MathUtils.normalizeAngle(a, 0.0); - compute the angle between two defining angular positions:
angle = MathUtils.normalizeAngle(end, start) - start;
Note that due to numerical accuracy and since π cannot be represented exactly, the result interval is closed, it cannot be half-closed as would be more satisfactory in a purely mathematical view.
- Parameters:
a- angle to normalizecenter- center of the desired 2π interval for the result- Returns:
- a-2kπ with integer k and center-π <= a-2kπ <= center+π
- Since:
- 1.2
- normalize an angle between 0 and 2π:
-
fuzzyEquals
-
lowPass
public static double lowPass(double newValue, double smoothedValue, double smoothing) Implements a low-pass filter to smooth the input values.- Parameters:
newValue- The new value.smoothedValue- The previous smoothed value.smoothing- The smoothing factor.- Returns:
- The new value with a low-pass filter applied.
-
lowPass
public static float lowPass(float newValue, float smoothedValue, float smoothing) Implements a low-pass filter to smooth the input values.- Parameters:
newValue- The new value.smoothedValue- The previous smoothed value.smoothing- The smoothing factor.- Returns:
- The new value with a low-pass filter applied.
-
logit
public static double logit(double x) Implements the logit function, defined aslogit(x) = log(x/(1-x)). Note that logit(0) = -Infinity, and logit(1) = Infinity.- Parameters:
x- The value to sample.
-
logisticSigmoid
public static double logisticSigmoid(double x, double span) Implements the logistic sigmoid, defined asexpit(x) = 1/(1+exp(-x)). It is the inverse of logit.- Parameters:
x- The value to sample, in [0, 1].span- The span of the function. The value gets re-mapped using this span.- Returns:
- The logistic sigmoid function.
-
smoothstep
public static double smoothstep(double edge0, double edge1, double x) Double precision version
-