Class MathUtils

java.lang.Object
net.dermetfan.utils.math.MathUtils
Direct Known Subclasses:
MathUtils

public class MathUtils
extends java.lang.Object
math utility methods
  • Constructor Summary

    Constructors
    Constructor Description
    MathUtils()  
  • Method Summary

    Modifier and Type Method Description
    static float[] abs​(float[] items)  
    static float[] abs​(float[] items, int offset, int length)  
    static float[] add​(float[] items, float value)  
    static float[] add​(float[] items, int offset, int length, float value)  
    static float amplitude2​(float[] items)  
    static float amplitude2​(float[] items, int offset, int length)  
    static float approachZero​(float value, float amount)  
    static boolean between​(float value, float min, float max)  
    static boolean between​(float value, float min, float max, boolean inclusive)
    min and max will be swapped if they are given in the wrong order
    static float[] clamp​(float[] items, float min, float max)  
    static float[] clamp​(float[] items, int offset, int length, float min, float max)  
    static float clamp​(float value, float min, float max)  
    static int clamp​(int value, int min, int max)  
    static int crossSum​(int n)  
    static float det​(float x1, float y1, float x2, float y2, float x3, float y3)  
    static float[] div​(float[] items, float divisor)  
    static float[] div​(float[] items, int offset, int length, float divisor)  
    static <T> T elementAtSum​(float sum, float[] values, T[] elements)  
    static <T> T elementAtSum​(float sum, float[] values, T[] elements, int valuesOffset, int valuesLength, int elementsOffset, int elementsLength)  
    static float factorial​(float n)  
    static int factorial​(int n)
    int wrapper for factorial(float)
    static float max​(float[] items)  
    static float max​(float[] items, int offset, int length)  
    static float min​(float[] items)  
    static float min​(float[] items, int offset, int length)  
    static float mirror​(float value, float baseline)  
    static float[] mul​(float[] items, float factor)  
    static float[] mul​(float[] items, int offset, int length, float factor)  
    static float nearest​(float value, float[] items)  
    static float nearest​(float value, float[] items, int offset, int length)  
    static float nearest​(float value, float minDiff, float[] items)  
    static float nearest​(float value, float minDiff, float[] items, int offset, int length)  
    static float normalize​(float value, float min, float max, boolean minExclusive, boolean maxExclusive)
    Normalizes/repeats the given value in the given interval [min, max] as if min and max were portals the value travels through.
    static int normalize​(int value, int min, int max, boolean minExclusive, boolean maxExclusive)  
    static float replaceNaN​(float value, float replacement)  
    static float[] scale​(float[] items, float min, float max)  
    static float[] scale​(float[] items, int offset, int length, float min, float max)
    scales the given float array to have the given min and max values
    static float[] sub​(float[] items, float value)  
    static float[] sub​(float[] items, int offset, int length, float value)  
    static float sum​(float[] items)  
    static float sum​(float[] items, int offset, int length)  

    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

    • clamp

      public static float clamp​(float value, float min, float max)
      Parameters:
      value - the value to clamp
      min - the min value
      max - the max value
      Returns:
      the value clamped in between min and max
    • clamp

      public static int clamp​(int value, int min, int max)
      Parameters:
      value - the value to clamp
      min - the min value
      max - the max value
      Returns:
      the value clamped in between min and max
    • crossSum

      public static int crossSum​(int n)
      Parameters:
      n - the number which cross sum to calculate
      Returns:
      the cross sum (sum of a number's digits)
    • factorial

      public static int factorial​(int n)
      int wrapper for factorial(float)
      See Also:
      factorial(float)
    • factorial

      public static float factorial​(float n)
      Parameters:
      n - the number to find the factorial for
      Returns:
      the factorial of the given number
    • between

      public static boolean between​(float value, float min, float max)
      Returns:
      if the given value is in between min and max (exclusive)
      See Also:
      between(float, float, float, boolean)
    • between

      public static boolean between​(float value, float min, float max, boolean inclusive)
      min and max will be swapped if they are given in the wrong order
      Parameters:
      inclusive - if the given value is allowed to be equal to min or max
      Returns:
      if the given value is in between min and max
    • det

      public static float det​(float x1, float y1, float x2, float y2, float x3, float y3)
      Returns:
      the determinant of the given 3x3 matrix
    • normalize

      public static float normalize​(float value, float min, float max, boolean minExclusive, boolean maxExclusive)
      Normalizes/repeats the given value in the given interval [min, max] as if min and max were portals the value travels through. For example (min and max are both inclusive):
      value min max result
      150 0 100 50
      200 -100 100 0
      50 0 100 50
      min may be greater than max - if so, they will be swapped.
      Parameters:
      value - the value to normalize in the interval [min, max]
      min - the minimum
      max - the maximum
      minExclusive - whether the minimum is exclusive
      maxExclusive - whether the maximum is exclusive
      Returns:
      the value repeated in the interval [min, max]
      Throws:
      java.lang.IllegalArgumentException - if both minExclusive and maxExclusive are true
    • normalize

      public static int normalize​(int value, int min, int max, boolean minExclusive, boolean maxExclusive)
      See Also:
      normalize(float, float, float, boolean, boolean)
    • mirror

      public static float mirror​(float value, float baseline)
      Parameters:
      value - the value to mirror
      baseline - the baseline of symmetry
      Returns:
      the value mirrored at baseline
    • replaceNaN

      public static float replaceNaN​(float value, float replacement)
      Returns:
      replacement if value is NaN
    • elementAtSum

      public static <T> T elementAtSum​(float sum, float[] values, T[] elements, int valuesOffset, int valuesLength, int elementsOffset, int elementsLength)
      Parameters:
      sum - the sum at which to return the element
      values - the values to add together to calculate sum
      elements - the elements from which to return one when sum is reached
      Returns:
      the element from elements when sum was reached by adding the given values together
    • elementAtSum

      public static <T> T elementAtSum​(float sum, float[] values, T[] elements)
      See Also:
      elementAtSum(float, float[], Object[], int, int, int, int)
    • clamp

      public static float[] clamp​(float[] items, int offset, int length, float min, float max)
      Returns:
      the given array clamped to min and max
    • clamp

      public static float[] clamp​(float[] items, float min, float max)
      See Also:
      clamp(float[], int, int, float, float)
    • abs

      public static float[] abs​(float[] items, int offset, int length)
      Returns:
      the given values with each element being the absolute of its value
    • abs

      public static float[] abs​(float[] items)
      See Also:
      abs(float[], int, int)
    • add

      public static float[] add​(float[] items, int offset, int length, float value)
      Returns:
      the given values with the given value added to each element
    • add

      public static float[] add​(float[] items, float value)
      See Also:
      add(float[], int, int, float)
    • sub

      public static float[] sub​(float[] items, int offset, int length, float value)
      Returns:
      the given values with the given value subtracted from each element
    • sub

      public static float[] sub​(float[] items, float value)
      See Also:
      sub(float[], int, int, float)
    • mul

      public static float[] mul​(float[] items, int offset, int length, float factor)
      Returns:
      the given values with each element multiplied with the given factor
    • mul

      public static float[] mul​(float[] items, float factor)
      See Also:
      mul(float[], int, int, float)
    • div

      public static float[] div​(float[] items, int offset, int length, float divisor)
      Returns:
      the given values with each element divided by the given divisor
    • div

      public static float[] div​(float[] items, float divisor)
      See Also:
      div(float[], int, int, float)
    • approachZero

      public static float approachZero​(float value, float amount)
      Parameters:
      value - the value from which to start
      amount - the amount by which to approach zero
      Returns:
      the given value modified into the direction of zero by the given amount
    • sum

      public static float sum​(float[] items, int offset, int length)
      Returns:
      the sum of all values in the given array
    • sum

      public static float sum​(float[] items)
      See Also:
      sum(float[], int, int)
    • amplitude2

      public static float amplitude2​(float[] items, int offset, int length)
      Returns:
      the peak-to-peak amplitude of the given array
    • amplitude2

      public static float amplitude2​(float[] items)
      See Also:
      amplitude2(float[], int, int)
    • max

      public static float max​(float[] items, int offset, int length)
      Returns:
      the largest element of the given array
    • max

      public static float max​(float[] items)
      See Also:
      max(float[], int, int)
    • min

      public static float min​(float[] items, int offset, int length)
      Returns:
      the smallest element of the given array
    • min

      public static float min​(float[] items)
      See Also:
      min(float[], int, int)
    • nearest

      public static float nearest​(float value, float minDiff, float[] items, int offset, int length)
      Parameters:
      value - the desired value
      items - the values to inspect
      minDiff - the minimal difference to the given value
      Returns:
      the nearest to value in values, NaN if none is found
    • nearest

      public static float nearest​(float value, float minDiff, float[] items)
      See Also:
      nearest(float, float, float[], int, int)
    • nearest

      public static float nearest​(float value, float[] items, int offset, int length)
      Returns:
      the nearest to value in values
      See Also:
      nearest(float, float, float[], int, int)
    • nearest

      public static float nearest​(float value, float[] items)
      See Also:
      nearest(float, float[], int, int)
    • scale

      public static float[] scale​(float[] items, int offset, int length, float min, float max)
      scales the given float array to have the given min and max values
      Parameters:
      items - the values to scale
      min - the desired minimal value in the array
      max - the desired maximal value in the array
      Returns:
      the scaled array
    • scale

      public static float[] scale​(float[] items, float min, float max)
      See Also:
      scale(float[], int, int, float, float)