Class Maths

java.lang.Object
lol.trq.helios.math.Maths

public final class Maths extends Object
Small numeric helpers used across rendering and animation. All static and allocation-free.
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    approach(double current, double target, double maxStep)
    Moves a value towards a target by at most a step, never past it.
    static double
    clamp(double v, double min, double max)
    Clamps a value.
    static float
    clamp(float v, float min, float max)
    Clamps a value.
    static double
    gaussian(double x, double sigma)
    The normal distribution's density.
    static void
    gaussianKernel(double sigma, float[] out)
    Fills normalized one-sided Gaussian weights for a separable blur: out[0] is the centre tap, and the centre plus twice the rest sums to 1.
    static double
    inverseLerp(double a, double b, double v)
    The position of a value between two others.
    static double
    lerp(double a, double b, double t)
    Linear interpolation, not clamped.
    static float
    lerp(float a, float b, float t)
    Linear interpolation, not clamped.
    static double
    lerpDegrees(double a, double b, double t)
    Interpolates angles the short way round.
    static double
    random(double min, double max)
    A uniformly random value, from a thread-local generator.
    static int
    random(int min, int max)
    A uniformly random int, from a thread-local generator.
    static double
    remap(double v, double fromMin, double fromMax, double toMin, double toMax)
    Maps a value from one range to another, not clamped.
    static double
    round(double v, int places)
    Rounds to decimal places, half away from zero, without BigDecimal.
    static double
    snap(double v, double step)
    Rounds to the nearest multiple of a step, such as 0.5 or a pixel size.
    static double
    wrapDegrees(double degrees)
    Wraps an angle to [-180, 180).

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • clamp

      public static double clamp(double v, double min, double max)
      Clamps a value.
      Parameters:
      v - the value
      min - the lower bound
      max - the upper bound
      Returns:
      v within [min, max]
    • clamp

      public static float clamp(float v, float min, float max)
      Clamps a value.
      Parameters:
      v - the value
      min - the lower bound
      max - the upper bound
      Returns:
      v within [min, max]
    • lerp

      public static double lerp(double a, double b, double t)
      Linear interpolation, not clamped.
      Parameters:
      a - the value at 0
      b - the value at 1
      t - the position
      Returns:
      a + (b - a) * t
    • lerp

      public static float lerp(float a, float b, float t)
      Linear interpolation, not clamped.
      Parameters:
      a - the value at 0
      b - the value at 1
      t - the position
      Returns:
      a + (b - a) * t
    • inverseLerp

      public static double inverseLerp(double a, double b, double v)
      The position of a value between two others.
      Parameters:
      a - the value at 0
      b - the value at 1
      v - the value
      Returns:
      (v - a) / (b - a), or 0 when a == b
    • remap

      public static double remap(double v, double fromMin, double fromMax, double toMin, double toMax)
      Maps a value from one range to another, not clamped.
      Parameters:
      v - the value
      fromMin - source range start
      fromMax - source range end
      toMin - target range start
      toMax - target range end
      Returns:
      the mapped value
    • approach

      public static double approach(double current, double target, double maxStep)
      Moves a value towards a target by at most a step, never past it.
      Parameters:
      current - the value
      target - the target
      maxStep - the largest move, not negative
      Returns:
      the moved value
    • wrapDegrees

      public static double wrapDegrees(double degrees)
      Wraps an angle to [-180, 180).
      Parameters:
      degrees - the angle
      Returns:
      the wrapped angle
    • lerpDegrees

      public static double lerpDegrees(double a, double b, double t)
      Interpolates angles the short way round.
      Parameters:
      a - the angle at 0, degrees
      b - the angle at 1, degrees
      t - the position
      Returns:
      the interpolated angle, not wrapped
    • round

      public static double round(double v, int places)
      Rounds to decimal places, half away from zero, without BigDecimal.
      Parameters:
      v - the value
      places - decimal places, 0 to 15
      Returns:
      the rounded value
    • snap

      public static double snap(double v, double step)
      Rounds to the nearest multiple of a step, such as 0.5 or a pixel size.
      Parameters:
      v - the value
      step - the step, positive
      Returns:
      the snapped value
    • gaussian

      public static double gaussian(double x, double sigma)
      The normal distribution's density.
      Parameters:
      x - the offset from the mean
      sigma - the standard deviation
      Returns:
      the density
    • gaussianKernel

      public static void gaussianKernel(double sigma, float[] out)
      Fills normalized one-sided Gaussian weights for a separable blur: out[0] is the centre tap, and the centre plus twice the rest sums to 1.
      Parameters:
      sigma - the standard deviation in taps
      out - receives the weights; its length is the radius plus one
    • random

      public static double random(double min, double max)
      A uniformly random value, from a thread-local generator.
      Parameters:
      min - inclusive lower bound
      max - exclusive upper bound
      Returns:
      the value
    • random

      public static int random(int min, int max)
      A uniformly random int, from a thread-local generator.
      Parameters:
      min - inclusive lower bound
      max - inclusive upper bound
      Returns:
      the value