Class Mathf

java.lang.Object
cit244.util.Mathf

public final class Mathf extends Object
Math utilities for graphics and animation.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double
    The 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 double
    The 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 double
    2π radians, one complete revolution.
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    clamp01(double value)
    Clamps a value to the range [0.0, 1.0].
    static double
    damp(double current, double target, double rate, double dt)
    Smoothly approaches a target using exponential damping.
    static double
    degrees(double radians)
    Converts radians to degrees.
    static double
    inverseLerp(double a, double b, double v)
    The reverse of lerp(double, double, double): given a value v between a and b, returns how far along it is (0.0 to 1.0).
    static double
    lerp(double a, double b, double t)
    Blends between a and b.
    static double
    map(double v, double inMin, double inMax, double outMin, double outMax)
    Converts a value from one range to another.
    static double
    mapClamped(double v, double inMin, double inMax, double outMin, double outMax)
    Like map(double, double, double, double, double), but clamps the result to the output range.
    static double
    radians(double degrees)
    Converts degrees to radians.
    static double
    smoothingFactor(double rate, double dt)
    Computes the interpolation factor for exponential damping over one time step.
    static double
    wrap(double x, double period)
    Returns x wrapped into [0, period). period must be > 0.
    static double
    wrapDegrees(double degrees)
    Returns degrees wrapped into [0, 360).

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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_PI
      2π 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 PHI
      The 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_ANGLE
      The 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 between a and b.

      When t is 0 returns a, when 1 returns b, when 0.5 returns the midpoint, etc. Values outside [0, 1] extrapolate.

      Parameters:
      a - starting value
      b - ending value
      t - interpolation factor
      Returns:
      interpolated value
    • inverseLerp

      public static double inverseLerp(double a, double b, double v)
      The reverse of lerp(double, double, double): given a value v between a and b, 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 value
      b - ending value
      v - 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 map
      inMin - start of the input range
      inMax - end of the input range
      outMin - start of the output range
      outMax - 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)
      Like map(double, double, double, double, double), but clamps the result to the output range.
      Parameters:
      v - value to map
      inMin - start of the input range
      inMax - end of the input range
      outMin - start of the output range
      outMax - 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 wrap
      period - 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 as rate * dt increases.
      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 fixed lerp(..., 0.1) every frame won't!

      The target is approached asymptotically not at a fixed time.

      Parameters:
      current - current value
      target - value to approach
      rate - 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