Class WbsMath

java.lang.Object
wbs.utils.util.WbsMath

public final class WbsMath extends Object
A variety of math-related utilities.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    chance(double percent)
    Returns true with a chance of a specified percentage
    static double
    clamp(double a, double b, double value)
     
    static int
    clamp(int a, int b, int value)
     
    static ArrayList<org.bukkit.util.Vector>
    get2Disc(int n, double radius, double rotation)
    Get a disc filled in by concentric circles in the X-Z plane.
    static ArrayList<org.bukkit.util.Vector>
    get2Ring(int n, double radius, double rotation)
    Get points in a ring in the X-Z plane
    static ArrayList<org.bukkit.util.Vector>
    get3Disc(int n, double radius, org.bukkit.util.Vector about, double rotation)
    Get a disc filled in by concentric circles in 3 space
    static ArrayList<org.bukkit.util.Vector>
    get3Ring(int n, double radius, org.bukkit.util.Vector about, double rotation)
    Get the points in a ring around the given vector.
    static org.bukkit.util.Vector
    getFacingVector(org.bukkit.entity.Entity entity)
    Gets a unit vector in the direction the specified entity is facing.
    static org.bukkit.util.Vector
    getFacingVector(org.bukkit.entity.Entity entity, double magnitude)
    Gets a vector in the direction the specified entity is facing.
    static ArrayList<org.bukkit.util.Vector>
    getFibonacciSphere(int amount, double radius)
     
    static ArrayList<org.bukkit.util.Vector>
    getLine(int pointsInLine, org.bukkit.util.Vector finish)
    Get a list of points in a line, starting at the origin, and ending at finish, evenly distributed based on the amount of points.
    static ArrayList<org.bukkit.util.Vector>
    getLine(int pointsInLine, org.bukkit.util.Vector start, org.bukkit.util.Vector finish)
    Get a list of points in a line between two vectors evenly distributed based on the amount of points.
    static ArrayList<org.bukkit.util.Vector>
    getRandom2Disc(int n, double radius)
     
    static org.bukkit.util.Vector
    getRandomPointOn2Disc(double radius)
     
    static org.bukkit.Location
    getRandomPointOn2Disc(org.bukkit.Location loc, double radius)
     
    static <T, N extends Number>
    T
    getRandomWeighted(Map<T,N> weightedMap)
    static double
    lerp(double a, double b, double t)
    Linear interpolation from a to b, given 0 ≤ t ≤ 1 representing the progress from a to b, where t = 0 returns a and t = 1 returns b
    static org.bukkit.util.Vector
    limitToSlope(org.bukkit.util.Vector vector, double slope)
    Limit the given vector to have a slope in Y between slope and -slope
    static ArrayList<org.bukkit.Location>
    offsetPoints(org.bukkit.Location origin, ArrayList<org.bukkit.util.Vector> vectors)
    Translate a list of vectors to a new origin, and return it as a list of locations about that origin.
    static double
    parseDoubleBetween(String input, double min, double max)
     
    static int
    parseIntBetween(String input, int min, int max)
     
    static org.bukkit.util.Vector
    Gets a random unit vector that may point in any direction
    static org.bukkit.util.Vector
    randomVector(double magnitude)
    Gets a random vector that may point in any direction
    static org.bukkit.util.Vector
    reflectVector(org.bukkit.util.Vector vector, org.bukkit.util.Vector normal)
     
    static List<org.bukkit.util.Vector>
    rotateFrom(List<org.bukkit.util.Vector> toRotate, org.bukkit.util.Vector from, org.bukkit.util.Vector with)
    Rotate a list of Vectors based on a vector.
    static org.bukkit.util.Vector
    rotateFrom(org.bukkit.util.Vector toRotate, org.bukkit.util.Vector from, org.bukkit.util.Vector with)
     
    static org.bukkit.util.Vector
    rotateVector(org.bukkit.util.Vector toRotate, org.bukkit.util.Vector about, double degrees)
     
    static List<org.bukkit.util.Vector>
    rotateVectors(List<org.bukkit.util.Vector> toRotate, org.bukkit.util.Vector about, double degrees)
     
    static double
    roundTo(double number, int decimalPlaces)
    Round the given double to specified amount of decimal places.
    static org.bukkit.util.Vector
    scaleVector(org.bukkit.util.Vector original, double magnitude)
    Get a copy of the given vector scaled to the desired magnitude.

    Methods inherited from class java.lang.Object

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

    • PHI

      public static final double PHI
  • Method Details

    • chance

      public static boolean chance(double percent)
      Returns true with a chance of a specified percentage
      Parameters:
      percent - The percent chance of success
      Returns:
      True with a percent% chance.
    • getRandomWeighted

      @Deprecated @NotNull public static <T, N extends Number> T getRandomWeighted(Map<T,N> weightedMap)
      Gets a weighted value from a set
      Parameters:
      weightedMap - A map of values to their weights
      Returns:
      A random value from the key set of the provided map, weighted by the value.
    • roundTo

      public static double roundTo(double number, int decimalPlaces)
      Round the given double to specified amount of decimal places. Possibly undefined behaviour at large numbers rounded to many decimal places.
      Parameters:
      number - The number to round
      decimalPlaces - The amount of decimal places to round to
      Returns:
      The rounded double
    • lerp

      public static double lerp(double a, double b, double t)
      Linear interpolation from a to b, given 0 ≤ t ≤ 1 representing the progress from a to b, where t = 0 returns a and t = 1 returns b
      Parameters:
      a - The starting value
      b - The end value
      t - The progress from a to b
      Returns:
      The value between a and b represented by t
    • clamp

      public static double clamp(double a, double b, double value)
    • clamp

      public static int clamp(int a, int b, int value)
    • limitToSlope

      public static org.bukkit.util.Vector limitToSlope(org.bukkit.util.Vector vector, double slope)
      Limit the given vector to have a slope in Y between slope and -slope
      Parameters:
      vector - The vector to limit
      slope - The maximum slope of the returned vector
      Returns:
      A clone of with slope between slope and -slope
    • rotateFrom

      public static List<org.bukkit.util.Vector> rotateFrom(List<org.bukkit.util.Vector> toRotate, org.bukkit.util.Vector from, org.bukkit.util.Vector with)
      Rotate a list of Vectors based on a vector. This method assumes the given vectors are populated around the "from" and rotates from that vector to the given "with" vector.
      Parameters:
      toRotate - The vectors to rotate
      with - The vector of rotation
      Returns:
      A copy of toRotate, rotated.
    • rotateFrom

      public static org.bukkit.util.Vector rotateFrom(org.bukkit.util.Vector toRotate, org.bukkit.util.Vector from, org.bukkit.util.Vector with)
    • rotateVectors

      public static List<org.bukkit.util.Vector> rotateVectors(List<org.bukkit.util.Vector> toRotate, org.bukkit.util.Vector about, double degrees)
    • rotateVector

      public static org.bukkit.util.Vector rotateVector(org.bukkit.util.Vector toRotate, org.bukkit.util.Vector about, double degrees)
    • getLine

      public static ArrayList<org.bukkit.util.Vector> getLine(int pointsInLine, org.bukkit.util.Vector start, org.bukkit.util.Vector finish)
      Get a list of points in a line between two vectors evenly distributed based on the amount of points.
      Parameters:
      pointsInLine - The amount of points to generate. Negative values will generate that many points per unit.
      start - The starting position
      finish - The finishing position
      Returns:
      The list of points
    • getLine

      public static ArrayList<org.bukkit.util.Vector> getLine(int pointsInLine, org.bukkit.util.Vector finish)
      Get a list of points in a line, starting at the origin, and ending at finish, evenly distributed based on the amount of points.
      Parameters:
      pointsInLine - The amount of points to generate. Negative values will generate that many points per unit.
      finish - The end of the line
      Returns:
      The points in the line, as vectors from the origin. The first point is always the origin.
    • getRandomPointOn2Disc

      public static org.bukkit.util.Vector getRandomPointOn2Disc(double radius)
    • getRandomPointOn2Disc

      public static org.bukkit.Location getRandomPointOn2Disc(org.bukkit.Location loc, double radius)
    • offsetPoints

      public static ArrayList<org.bukkit.Location> offsetPoints(org.bukkit.Location origin, ArrayList<org.bukkit.util.Vector> vectors)
      Translate a list of vectors to a new origin, and return it as a list of locations about that origin.
      Parameters:
      origin - The origin about which the vectors define locations
      vectors - The set of vectors to translate and turn into Locations.
      Returns:
      The set of locations about the new origin.
    • getFibonacciSphere

      public static ArrayList<org.bukkit.util.Vector> getFibonacciSphere(int amount, double radius)
    • reflectVector

      public static org.bukkit.util.Vector reflectVector(org.bukkit.util.Vector vector, org.bukkit.util.Vector normal)
    • get2Ring

      public static ArrayList<org.bukkit.util.Vector> get2Ring(int n, double radius, double rotation)
      Get points in a ring in the X-Z plane
      Parameters:
      n - The number of points in the ring
      radius - The radius of the ring
      rotation - The rotation in degrees
      Returns:
      An ArrayList containing all points in the ring
    • get3Ring

      public static ArrayList<org.bukkit.util.Vector> get3Ring(int n, double radius, org.bukkit.util.Vector about, double rotation)
      Get the points in a ring around the given vector.
      Parameters:
      n - The number of points in the ring
      radius - The radius of the ring
      about - The vector about which to draw the ring
      rotation - The rotation in degrees
      Returns:
      An ArrayList containing all points in the ring
    • getRandom2Disc

      public static ArrayList<org.bukkit.util.Vector> getRandom2Disc(int n, double radius)
    • get2Disc

      public static ArrayList<org.bukkit.util.Vector> get2Disc(int n, double radius, double rotation)
      Get a disc filled in by concentric circles in the X-Z plane.
      Parameters:
      n - The amount of points on the outer ring (also informs point density on the discs surface)
      radius - The radius of the disc
      rotation - The rotation of the disc in degrees around the Y axis
      Returns:
      A disc of points
    • get3Disc

      public static ArrayList<org.bukkit.util.Vector> get3Disc(int n, double radius, org.bukkit.util.Vector about, double rotation)
      Get a disc filled in by concentric circles in 3 space
      Parameters:
      n - The amount of points on the outer ring (also informs point density on the discs surface)
      radius - The radius of the disc
      about - The vector about which to draw the disc
      rotation - The rotation of the disc in degrees around the about axis
      Returns:
      A disc of points
    • getFacingVector

      public static org.bukkit.util.Vector getFacingVector(org.bukkit.entity.Entity entity)
      Gets a unit vector in the direction the specified entity is facing.
      Parameters:
      entity - The entity to get the facing vector from
      Returns:
      The facing vector
    • getFacingVector

      public static org.bukkit.util.Vector getFacingVector(org.bukkit.entity.Entity entity, double magnitude)
      Gets a vector in the direction the specified entity is facing.
      Parameters:
      entity - The entity to get the facing vector from
      magnitude - The length of the facing vector
      Returns:
      The facing vector with the specified magnitude
    • scaleVector

      public static org.bukkit.util.Vector scaleVector(org.bukkit.util.Vector original, double magnitude)
      Get a copy of the given vector scaled to the desired magnitude.
      Parameters:
      original - The vector to be cloned and then scaled
      magnitude - The magnitude of the resulting vector
      Returns:
      A copy of the vector with the desired magnitude
    • randomVector

      public static org.bukkit.util.Vector randomVector()
      Gets a random unit vector that may point in any direction
      Returns:
      The vector with random direction
    • randomVector

      public static org.bukkit.util.Vector randomVector(double magnitude)
      Gets a random vector that may point in any direction
      Parameters:
      magnitude - The magnitude of the resulting vector
      Returns:
      The vector with random direction
    • parseIntBetween

      public static int parseIntBetween(String input, int min, int max) throws IllegalArgumentException
      Throws:
      IllegalArgumentException
    • parseDoubleBetween

      public static double parseDoubleBetween(String input, double min, double max) throws IllegalArgumentException
      Throws:
      IllegalArgumentException