Package wbs.utils.util
Class WbsMath
java.lang.Object
wbs.utils.util.WbsMath
A variety of math-related utilities.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanchance(double percent) Returns true with a chance of a specified percentagestatic doubleclamp(double a, double b, double value) static intclamp(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 planestatic 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 spacestatic 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.VectorgetFacingVector(org.bukkit.entity.Entity entity) Gets a unit vector in the direction the specified entity is facing.static org.bukkit.util.VectorgetFacingVector(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.VectorgetRandomPointOn2Disc(double radius) static org.bukkit.LocationgetRandomPointOn2Disc(org.bukkit.Location loc, double radius) static <T,N extends Number>
TgetRandomWeighted(Map<T, N> weightedMap) Deprecated.static doublelerp(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 bstatic org.bukkit.util.VectorlimitToSlope(org.bukkit.util.Vector vector, double slope) Limit the given vector to have a slope in Y between slope and -slopestatic 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 doubleparseDoubleBetween(String input, double min, double max) static intparseIntBetween(String input, int min, int max) static org.bukkit.util.VectorGets a random unit vector that may point in any directionstatic org.bukkit.util.VectorrandomVector(double magnitude) Gets a random vector that may point in any directionstatic org.bukkit.util.VectorreflectVector(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.VectorrotateFrom(org.bukkit.util.Vector toRotate, org.bukkit.util.Vector from, org.bukkit.util.Vector with) static org.bukkit.util.VectorrotateVector(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 doubleroundTo(double number, int decimalPlaces) Round the given double to specified amount of decimal places.static org.bukkit.util.VectorscaleVector(org.bukkit.util.Vector original, double magnitude) Get a copy of the given vector scaled to the desired magnitude.
-
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.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 rounddecimalPlaces- 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 valueb- The end valuet- 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 limitslope- 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 rotatewith- 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
-
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 positionfinish- 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 locationsvectors- The set of vectors to translate and turn into Locations.- Returns:
- The set of locations about the new origin.
-
getFibonacciSphere
-
reflectVector
public static org.bukkit.util.Vector reflectVector(org.bukkit.util.Vector vector, org.bukkit.util.Vector normal) -
get2Ring
Get points in a ring in the X-Z plane- Parameters:
n- The number of points in the ringradius- The radius of the ringrotation- 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 ringradius- The radius of the ringabout- The vector about which to draw the ringrotation- The rotation in degrees- Returns:
- An ArrayList containing all points in the ring
-
getRandom2Disc
-
get2Disc
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 discrotation- 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 discabout- The vector about which to draw the discrotation- 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 frommagnitude- 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 scaledmagnitude- 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
- Throws:
IllegalArgumentException
-
parseDoubleBetween
public static double parseDoubleBetween(String input, double min, double max) throws IllegalArgumentException - Throws:
IllegalArgumentException
-
WbsCollectionUtil.getRandomWeighted(Map)