Package snap.util

Class MathUtils

java.lang.Object
snap.util.MathUtils

public class MathUtils extends Object
Utility methods for common math operations.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static Number
    add(Number n1, Number n2)
    Returns the sum of the two given Numbers.
    static boolean
    between(double a, double x, double y)
    Returns whether a real number is between two other numbers.
    static double
    ceil(double x, double y)
    Truncate x up to the nearest y.
    static double
    clamp(double f)
    Returns the given float clamped to 1/1000th precision.
    static double
    clamp(double f, double min, double max)
    Returns the given double clamped between the two values.
    static int
    clamp(int i, int min, int max)
    Returns the given in clamped between the two values.
    static double
    clamp_doubleback(double a, double x, double y)
    Returns the given double clamped between the two values.
    static double
    clamp_wrap(double a, double x, double y)
    Returns the given double clamped between the two values (wraps around if out of range).
    static int
    clamp_wrap(int a, int x, int y)
    Returns the given double clamped between the two values (wraps around if out of range).
    static double
    cos(double anAngle)
    Returns the cos of the given angle in degrees.
    static Number
    divide(Number n1, Number n2)
    Returns the result of dividing n1 by n2.
    static boolean
    equals(double a, double b)
    Returns whether two real numbers are equal within a small tolerance.
    static boolean
    equals(double a, double b, double aTolerance)
    Returns whether two real numbers are equal within given tolerance (e.g., 1e-12)
    static boolean
    equalsZero(double a)
    Returns whether a real number is practically zero.
    static boolean
    equalsZero(double a, double aTolerance)
    Returns whether a real number is practically zero.
    static double
    floor(double x, double y)
    Truncate x down to the nearest y.
    static boolean
    gt(double a, double b)
    Returns whether a real number is practically greater than another.
    static boolean
    gte(double a, double b)
    Returns whether a real number is practically greater than or equal another.
    static boolean
    lt(double a, double b)
    Returns whether a real number is practically less than another.
    static boolean
    lte(double a, double b)
    Returns whether a real number is practically less than or equal another.
    static double
    mapFractionalToRangeValue(double aValue, double aMin, double aMax)
    Returns the given fractional value expressed in range 0 to 1 mapped to given range (min/max).
    static double
    mapRangeValueToFractional(double aValue, double aMin, double aMax)
    Returns the given value expressed in given range (min/max) mapped to unit range (0 - 1).
    static double
    mapValueForRanges(double aValue, double min1, double max1, double min2, double max2)
    Returns the given double expressed in the first given range (min/max) as its relative value in second range.
    static double
    mod(double x, double y)
    Returns the given number modulo the second number (mod for floats).
    static Number
    Returns the product of the two given Numbers.
    static Number
    negate(Number aNumber)
    Returns the negative of the given Number.
    static float
    randomFloat(float aVal)
    Returns a random float up to given value.
    static int
    Returns a random int.
    static double
    round(double x, double y)
    Returns the given number rounded to the second number (rounding to arbitrary double values).
    static float
    round(float x, float y)
    Returns the given number rounded to the second number (rounding to arbitrary floating values).
    static int
    sign(double f)
    Returns the sign of a given number (as -1 or 1).
    static double
    sin(double anAngle)
    Returns the sign of the given angle in degrees.
    static Number
    Returns the difference of the two given Numbers.
    static double
    trunc(double x, double y)
    Truncate x down to the nearest y.

    Methods inherited from class java.lang.Object

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

    • MathUtils

      public MathUtils()
  • Method Details

    • equals

      public static boolean equals(double a, double b)
      Returns whether two real numbers are equal within a small tolerance.
    • equals

      public static boolean equals(double a, double b, double aTolerance)
      Returns whether two real numbers are equal within given tolerance (e.g., 1e-12)
    • equalsZero

      public static boolean equalsZero(double a)
      Returns whether a real number is practically zero.
    • equalsZero

      public static boolean equalsZero(double a, double aTolerance)
      Returns whether a real number is practically zero.
    • gt

      public static boolean gt(double a, double b)
      Returns whether a real number is practically greater than another.
    • gte

      public static boolean gte(double a, double b)
      Returns whether a real number is practically greater than or equal another.
    • lt

      public static boolean lt(double a, double b)
      Returns whether a real number is practically less than another.
    • lte

      public static boolean lte(double a, double b)
      Returns whether a real number is practically less than or equal another.
    • between

      public static boolean between(double a, double x, double y)
      Returns whether a real number is between two other numbers.
    • sign

      public static int sign(double f)
      Returns the sign of a given number (as -1 or 1).
    • mod

      public static double mod(double x, double y)
      Returns the given number modulo the second number (mod for floats).
    • round

      public static float round(float x, float y)
      Returns the given number rounded to the second number (rounding to arbitrary floating values).
    • round

      public static double round(double x, double y)
      Returns the given number rounded to the second number (rounding to arbitrary double values).
    • trunc

      public static double trunc(double x, double y)
      Truncate x down to the nearest y.
    • floor

      public static double floor(double x, double y)
      Truncate x down to the nearest y.
    • ceil

      public static double ceil(double x, double y)
      Truncate x up to the nearest y.
    • clamp

      public static double clamp(double f)
      Returns the given float clamped to 1/1000th precision.
    • clamp

      public static int clamp(int i, int min, int max)
      Returns the given in clamped between the two values.
    • clamp

      public static double clamp(double f, double min, double max)
      Returns the given double clamped between the two values.
    • clamp_wrap

      public static int clamp_wrap(int a, int x, int y)
      Returns the given double clamped between the two values (wraps around if out of range).
    • clamp_wrap

      public static double clamp_wrap(double a, double x, double y)
      Returns the given double clamped between the two values (wraps around if out of range).
    • clamp_doubleback

      public static double clamp_doubleback(double a, double x, double y)
      Returns the given double clamped between the two values.
    • mapFractionalToRangeValue

      public static double mapFractionalToRangeValue(double aValue, double aMin, double aMax)
      Returns the given fractional value expressed in range 0 to 1 mapped to given range (min/max).
    • mapRangeValueToFractional

      public static double mapRangeValueToFractional(double aValue, double aMin, double aMax)
      Returns the given value expressed in given range (min/max) mapped to unit range (0 - 1).
    • mapValueForRanges

      public static double mapValueForRanges(double aValue, double min1, double max1, double min2, double max2)
      Returns the given double expressed in the first given range (min/max) as its relative value in second range.
    • negate

      public static Number negate(Number aNumber)
      Returns the negative of the given Number.
    • add

      public static Number add(Number n1, Number n2)
      Returns the sum of the two given Numbers.
    • subtract

      public static Number subtract(Number n1, Number n2)
      Returns the difference of the two given Numbers.
    • multiply

      public static Number multiply(Number n1, Number n2)
      Returns the product of the two given Numbers.
    • divide

      public static Number divide(Number n1, Number n2)
      Returns the result of dividing n1 by n2.
    • sin

      public static double sin(double anAngle)
      Returns the sign of the given angle in degrees.
    • cos

      public static double cos(double anAngle)
      Returns the cos of the given angle in degrees.
    • randomInt

      public static int randomInt()
      Returns a random int.
    • randomFloat

      public static float randomFloat(float aVal)
      Returns a random float up to given value.