Class Mathf
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final doubleThe golden angle in radians: 2*PI / PHI^2 ~> 2.39996, or about 137.5077 degrees.static final doubleπ/2 radians, a right angle.static final doubleThe golden ratio: (1 + sqrt(5)) / 2 ~> 1.618.static final doubleπ, the ratio of a circle's circumference to its diameter.static final doubleπ/4 radians, a 45-degree angle.static final double2π radians, one complete revolution. -
Method Summary
Modifier and TypeMethodDescriptionstatic doubleclamp01(double value) Clamps a value to the range[0.0, 1.0].static doubledamp(double current, double target, double rate, double dt) Smoothly approaches a target using exponential damping.static doubledegrees(double radians) Converts radians to degrees.static doubleinverseLerp(double a, double b, double v) The reverse oflerp(double, double, double): given a valuevbetweenaandb, returns how far along it is (0.0 to 1.0).static doublelerp(double a, double b, double t) Blends betweenaandb.static doublemap(double v, double inMin, double inMax, double outMin, double outMax) Converts a value from one range to another.static doublemapClamped(double v, double inMin, double inMax, double outMin, double outMax) Likemap(double, double, double, double, double), but clamps the result to the output range.static doubleradians(double degrees) Converts degrees to radians.static doublesmoothingFactor(double rate, double dt) Computes the interpolation factor for exponential damping over one time step.static doublewrap(double x, double period) Returns x wrapped into [0, period). period must be > 0.static doublewrapDegrees(double degrees) Returns degrees wrapped into [0, 360).
-
Field Details
-
PI
public static final double PIπ, the ratio of a circle's circumference to its diameter.- See Also:
-
TWO_PI
public static final double TWO_PI2π radians, one complete revolution.- See Also:
-
HALF_PI
public static final double HALF_PIπ/2 radians, a right angle.- See Also:
-
QUARTER_PI
public static final double QUARTER_PIπ/4 radians, a 45-degree angle.- See Also:
-
PHI
public static final double PHIThe golden ratio: (1 + sqrt(5)) / 2 ~> 1.618. Best known for showing up in nautilus shells, sunflower spirals, pinecones, and the ratio of a credit card's sides(!). Less mystical than internet folklore suggests, but genuinely fundamental in combinatorics and as the "most irrational number" - the constant hardest to approximate by rational fractions (look up the continued fraction form...). -
GOLDEN_ANGLE
public static final double GOLDEN_ANGLEThe golden angle in radians: 2*PI / PHI^2 ~> 2.39996, or about 137.5077 degrees. The "most irrational" rotation - no matter how many times you rotate by this angle, the resulting points never repeat or line up. That's why plants use it for seed and leaf placement: it maximizes packing density without favoring any direction.
-
-
Method Details
-
radians
public static double radians(double degrees) Converts degrees to radians.- Parameters:
degrees- angle in degrees- Returns:
- angle in radians
-
degrees
public static double degrees(double radians) Converts radians to degrees.- Parameters:
radians- angle in radians- Returns:
- angle in degrees
-
lerp
public static double lerp(double a, double b, double t) Blends betweenaandb.When
tis 0 returnsa, when 1 returnsb, when 0.5 returns the midpoint, etc. Values outside [0, 1] extrapolate.- Parameters:
a- starting valueb- ending valuet- interpolation factor- Returns:
- interpolated value
-
inverseLerp
public static double inverseLerp(double a, double b, double v) The reverse oflerp(double, double, double): given a valuevbetweenaandb, returns how far along it is (0.0 to 1.0). Values outside that range may return values outside [0, 1].Returns 0.0 if
a == b.- Parameters:
a- starting valueb- ending valuev- value whose position in the range is sought- Returns:
- interpolation factor
-
clamp01
public static double clamp01(double value) Clamps a value to the range[0.0, 1.0].- Parameters:
value- value to clamp- Returns:
- clamped value
-
map
public static double map(double v, double inMin, double inMax, double outMin, double outMax) Converts a value from one range to another.For example,
map(0.5, 0, 1, 0, 100)returns 50. Does not clamp - values outside the input range will go outside the output range.- Parameters:
v- value to mapinMin- start of the input rangeinMax- end of the input rangeoutMin- start of the output rangeoutMax- end of the output range- Returns:
- mapped value
- See Also:
-
mapClamped
public static double mapClamped(double v, double inMin, double inMax, double outMin, double outMax) Likemap(double, double, double, double, double), but clamps the result to the output range.- Parameters:
v- value to mapinMin- start of the input rangeinMax- end of the input rangeoutMin- start of the output rangeoutMax- end of the output range- Returns:
- mapped value within the output range
-
wrap
public static double wrap(double x, double period) Returns x wrapped into [0, period). period must be > 0.Unlike the remainder operator
%by itself, this handles negative inputs too:wrap(-1, 360)is 359, not -1.- Parameters:
x- value to wrapperiod- length of the repeating interval- Returns:
- wrapped value
- Throws:
IllegalArgumentException- if the period is not positive and finite
-
wrapDegrees
public static double wrapDegrees(double degrees) Returns degrees wrapped into [0, 360).- Parameters:
degrees- angle to wrap- Returns:
- wrapped angle in degrees
-
smoothingFactor
public static double smoothingFactor(double rate, double dt) Computes the interpolation factor for exponential damping over one time step. The factor is 0 for zero elapsed time or zero rate and approaches 1 asrate * dtincreases.- Parameters:
rate- convergence rate in inverse seconds (nonnegative)dt- elapsed time in seconds (nonnegative)- Returns:
- interpolation factor between 0 and 1
- Throws:
IllegalArgumentException- if rate or dt is negative or non-finite
-
damp
public static double damp(double current, double target, double rate, double dt) Smoothly approaches a target using exponential damping.Unlike
lerp(double, double, double), this accounts for elapsed time, so the motion remains consistent across frame rates. The rate controls how quickly the value approaches its target: larger rates mean faster convergence, while zero means no movement.For example,
damp(x, targetX, 5, dt)closes about 99% of the gap in one second, whether your laptop manages 60 FPS or limps along at 20. A fixedlerp(..., 0.1)every frame won't!The target is approached asymptotically not at a fixed time.
- Parameters:
current- current valuetarget- value to approachrate- convergence rate in inverse seconds (nonnegative)dt- elapsed time in seconds (nonnegative)- Returns:
- updated value
- Throws:
IllegalArgumentException- if rate or dt is negative or non-finite
-