java.lang.Object
tk.airshipcraft.commonlib.utils.math.Maths

public class Maths extends Object
A utility class offering a collection of mathematical functions commonly used in game development. It includes methods for clamping values, normalizing and linearly interpolating between ranges, and specialized rounding. These utilities are helpful for various gameplay mechanics, such as controlling player movement, managing game object properties, and handling game state transitions.
Since:
2023-04-03
Version:
1.0.0
Author:
notzune
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    clamp(double value, double min, double max)
    Limits a value between and including two values.
    static float
    clamp(float value, float min, float max)
    Limits a value between and including two values.
    static int
    clamp(int value, int min, int max)
    Limits a value between and including two values.
    static long
    clamp(long value, long min, long max)
    Limits a value between and including two values.
    static double
    lerp(double norm, double min, double max)
    Applies a normalized value (previously obtained through normalization) to a new range.
    static float
    lerp(float norm, float min, float max)
    Applies a normalized value (previously obtained through normalization) to a new range.
    static int
    lerp(int norm, int min, int max)
    Applies a normalized value (previously obtained through normalization) to a new range.
    static long
    lerp(long norm, long min, long max)
    Applies a normalized value (previously obtained through normalization) to a new range.
    static double
    norm(double value, double min, double max)
    Normalizes a value within a minimum and maximum range to a 0-1 range.
    static float
    norm(float value, float min, float max)
    Normalizes a value within a minimum and maximum range to a 0-1 range.
    static int
    norm(int value, int min, int max)
    Normalizes a value within a minimum and maximum range to a 0-1 range.
    static long
    norm(long value, long min, long max)
    Normalizes a value within a minimum and maximum range to a 0-1 range.
    static double
    roundIn(double value)
    Rounds a floating-point number towards zero to the nearest integer.
    static double
    roundOut(double value)
    Rounds a floating-point number away from zero to the nearest integer.

    Methods inherited from class java.lang.Object

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

    • Maths

      public Maths()
  • Method Details

    • clamp

      public static int clamp(int value, int min, int max)
      Limits a value between and including two values.
      Parameters:
      value - The value to clamp.
      min - The minimum value.
      max - The maximum value.
      Returns:
      The clamped value.
    • clamp

      public static long clamp(long value, long min, long max)
      Limits a value between and including two values.
      Parameters:
      value - The value to clamp.
      min - The minimum value.
      max - The maximum value.
      Returns:
      The clamped value.
    • clamp

      public static float clamp(float value, float min, float max)
      Limits a value between and including two values.
      Parameters:
      value - The value to clamp.
      min - The minimum value.
      max - The maximum value.
      Returns:
      The clamped value.
    • clamp

      public static double clamp(double value, double min, double max)
      Limits a value between and including two values.
      Parameters:
      value - The value to clamp.
      min - The minimum value.
      max - The maximum value.
      Returns:
      The clamped value.
    • norm

      public static int norm(int value, int min, int max)
      Normalizes a value within a minimum and maximum range to a 0-1 range. The normalization process translates the range of possible input values into a standard 0 to 1 scale, where 0 represents the minimum and 1 represents the maximum.
      Parameters:
      value - The value to normalize.
      min - The lower limit of the original range.
      max - The upper limit of the original range.
      Returns:
      The normalized value, proportionally adjusted between 0 and 1.
    • norm

      public static long norm(long value, long min, long max)
      Normalizes a value within a minimum and maximum range to a 0-1 range. The normalization process translates the range of possible input values into a standard 0 to 1 scale, where 0 represents the minimum and 1 represents the maximum.
      Parameters:
      value - The value to normalize.
      min - The lower limit of the original range.
      max - The upper limit of the original range.
      Returns:
      The normalized value, proportionally adjusted between 0 and 1.
    • norm

      public static float norm(float value, float min, float max)
      Normalizes a value within a minimum and maximum range to a 0-1 range. The normalization process translates the range of possible input values into a standard 0 to 1 scale, where 0 represents the minimum and 1 represents the maximum.
      Parameters:
      value - The value to normalize.
      min - The lower limit of the original range.
      max - The upper limit of the original range.
      Returns:
      The normalized value, proportionally adjusted between 0 and 1.
    • norm

      public static double norm(double value, double min, double max)
      Normalizes a value within a minimum and maximum range to a 0-1 range. The normalization process translates the range of possible input values into a standard 0 to 1 scale, where 0 represents the minimum and 1 represents the maximum.
      Parameters:
      value - The value to normalize.
      min - The lower limit of the original range.
      max - The upper limit of the original range.
      Returns:
      The normalized value, proportionally adjusted between 0 and 1.
    • lerp

      public static int lerp(int norm, int min, int max)
      Applies a normalized value (previously obtained through normalization) to a new range. This process is known as linear interpolation or 'lerping'. It's useful for translating a normalized value back into a range with different limits, often for animations, transitions, or other gradual changes over time in game properties.
      Parameters:
      norm - The normalized value, typically between 0 and 1.
      min - The lower limit of the new range.
      max - The upper limit of the new range.
      Returns:
      The linearly interpolated value within the new range.
    • lerp

      public static long lerp(long norm, long min, long max)
      Applies a normalized value (previously obtained through normalization) to a new range. This process is known as linear interpolation or 'lerping'. It's useful for translating a normalized value back into a range with different limits, often for animations, transitions, or other gradual changes over time in game properties.
      Parameters:
      norm - The normalized value, typically between 0 and 1.
      min - The lower limit of the new range.
      max - The upper limit of the new range.
      Returns:
      The linearly interpolated value within the new range.
    • lerp

      public static float lerp(float norm, float min, float max)
      Applies a normalized value (previously obtained through normalization) to a new range. This process is known as linear interpolation or 'lerping'. It's useful for translating a normalized value back into a range with different limits, often for animations, transitions, or other gradual changes over time in game properties.
      Parameters:
      norm - The normalized value, typically between 0 and 1.
      min - The lower limit of the new range.
      max - The upper limit of the new range.
      Returns:
      The linearly interpolated value within the new range.
    • lerp

      public static double lerp(double norm, double min, double max)
      Applies a normalized value (previously obtained through normalization) to a new range. This process is known as linear interpolation or 'lerping'. It's useful for translating a normalized value back into a range with different limits, often for animations, transitions, or other gradual changes over time in game properties.
      Parameters:
      norm - The normalized value, typically between 0 and 1.
      min - The lower limit of the new range.
      max - The upper limit of the new range.
      Returns:
      The linearly interpolated value within the new range.
    • roundOut

      public static double roundOut(double value)
      Rounds a floating-point number away from zero to the nearest integer. This method of rounding is commonly used when a consistent behavior is needed for both positive and negative numbers, ensuring that they are always rounded to a larger absolute value.
      • 1.5 → 2
      • -1.5 → -2
      Parameters:
      value - The floating-point value to round.
      Returns:
      The rounded value, always away from zero.
    • roundIn

      public static double roundIn(double value)
      Rounds a floating-point number towards zero to the nearest integer. This approach to rounding is useful when needing to truncate a decimal number to its integer component, disregarding the fractional part, which can be necessary for certain types of game logic that rely on discrete values.
      • 1.5 → 1
      • -1.5 → -1
      Parameters:
      value - The floating-point value to round.
      Returns:
      The rounded value, always towards zero.