com.bergerkiller.bukkit.common.utils

## Class MathUtil

• java.lang.Object
• com.bergerkiller.bukkit.common.utils.MathUtil

• ```public class MathUtil
extends java.lang.Object```
Multiple Math utilities to compare and calculate using Vectors and raw values
• ### Field Summary

Fields
Modifier and Type Field and Description
`static float` `DEGTORAD`
`static double` `HALFROOTOFTWO`
`static float` `RADTODEG`
• ### Constructor Summary

Constructors
Constructor and Description
`MathUtil()`
• ### Method Summary

All Methods
Modifier and Type Method and Description
`static float` `atan(double value)`
Gets the inverse tangent of the value in degrees
`static float` ```atan2(double y, double x)```
Gets the inverse tangent angle in degrees of the rectangle vector
`static int` `ceil(double value)`
Gets the ceiling integer value from a double value
`static int` `ceil(float value)`
Gets the ceiling integer value from a float value
`static double` ```clamp(double value, double limit)```
Clamps the value between -limit and limit
`static double` ```clamp(double value, double min, double max)```
Clamps the value between the min and max values
`static float` ```clamp(float value, float limit)```
Clamps the value between -limit and limit
`static float` ```clamp(float value, float min, float max)```
Clamps the value between the min and max values
`static int` ```clamp(int value, int limit)```
Clamps the value between -limit and limit
`static int` ```clamp(int value, int min, int max)```
Clamps the value between the min and max values
`static long` ```clamp(long value, long limit)```
Clamps the value between -limit and limit
`static long` ```clamp(long value, long min, long max)```
Clamps the value between the min and max values
`static double` ```distance(double x1, double y1, double x2, double y2)```
`static double` ```distance(double x1, double y1, double z1, double x2, double y2, double z2)```
`static double` ```distanceSquared(double x1, double y1, double x2, double y2)```
`static double` ```distanceSquared(double x1, double y1, double z1, double x2, double y2, double z2)```
`static double` `fixNaN(double value)`
Returns 0 if the value is not-a-number
`static double` ```fixNaN(double value, double def)```
Returns the default if the value is not-a-number
`static float` `fixNaN(float value)`
Returns 0 if the value is not-a-number
`static float` ```fixNaN(float value, float def)```
Returns the default if the value is not-a-number
`static int` `floor(double value)`
Gets the floor integer value from a double value
`static int` `floor(float value)`
Gets the floor integer value from a float value
`static int` ```floorDiv(int x, int y)```
Returns the floor division of the int arguments
`static long` ```floorDiv(long x, long y)```
Returns the floor division of the long arguments
`static int` ```floorMod(int x, int y)```
Returns the floor modulus of the int arguments
`static long` ```floorMod(long x, long y)```
Returns the floor modulus of the long arguments
`static float` ```getAngleDifference(float angle1, float angle2)```
Gets the angle difference between two angles
`static int` ```getAngleDifference(int angle1, int angle2)```
Gets the angle difference between two angles
`static double` ```getAngleDifference(org.bukkit.util.Vector v0, org.bukkit.util.Vector v1)```
Calculates the angle difference between two vectors in degrees
`static org.bukkit.util.Vector` ```getDirection(float yaw, float pitch)```
Gets the direction of yaw and pitch angles
`static float` ```getLookAtPitch(double dY, double dXZ)```
Gets the pitch angle in degrees to look into the direction specified
`static float` ```getLookAtPitch(double dX, double dY, double dZ)```
Gets the pitch angle in degrees to look into the direction specified
`static float` ```getLookAtYaw(org.bukkit.block.Block loc, org.bukkit.block.Block lookat)```
`static float` ```getLookAtYaw(double dx, double dz)```
Gets the horizontal look-at angle in degrees to look into the 2D-direction specified
`static float` ```getLookAtYaw(org.bukkit.entity.Entity loc, org.bukkit.entity.Entity lookat)```
`static float` ```getLookAtYaw(org.bukkit.Location loc, org.bukkit.Location lookat)```
`static float` `getLookAtYaw(org.bukkit.util.Vector motion)`
`static double` ```getNormalizationFactor(double x, double y)```
Calculates the normalization factor for a 2D vector.
`static double` ```getNormalizationFactor(double x, double y, double z)```
Calculates the normalization factor for a 3D vector.
`static double` ```getNormalizationFactor(double x, double y, double z, double w)```
Calculates the normalization factor for a 4D vector.
`static double` `getNormalizationFactor(org.bukkit.util.Vector v)`
Calculates the normalization factor for a 3D vector.
`static double` `getNormalizationFactorLS(double lengthSquared)`
Calculates the normalization factor for a squared length.
`static double` ```getPercentage(int subtotal, int total)```
Gets a percentags of 2 values
`static double` ```getPercentage(int subtotal, int total, int decimals)```
Gets a percentage and round it with a cusotm amound of decimals
`static double` ```invert(double value, boolean negative)```
Turns a value negative or keeps it positive based on a boolean input
`static float` ```invert(float value, boolean negative)```
Turns a value negative or keeps it positive based on a boolean input
`static int` ```invert(int value, boolean negative)```
Turns a value negative or keeps it positive based on a boolean input
`static boolean` ```isHeadingTo(org.bukkit.block.BlockFace direction, org.bukkit.util.Vector velocity)```
`static boolean` ```isHeadingTo(org.bukkit.Location from, org.bukkit.Location to, org.bukkit.util.Vector velocity)```
`static boolean` ```isHeadingTo(org.bukkit.util.Vector offset, org.bukkit.util.Vector velocity)```
`static boolean` ```isInverted(double value1, double value2)```
Checks whether one value is negative and the other positive, or opposite
`static double` `length(double... values)`
`static double` `lengthSquared(double... values)`
`static double` ```lerp(double d1, double d2, double stage)```
`static org.bukkit.Location` ```lerp(org.bukkit.Location loc1, org.bukkit.Location loc2, double stage)```
`static org.bukkit.util.Vector` ```lerp(org.bukkit.util.Vector vec1, org.bukkit.util.Vector vec2, double stage)```
`static long` `longFloor(double value)`
Gets the floor long value from a double value
`static long` ```longHashAdd(long key_a, long key_b)```
Shorthand equivalent of:
longHashToLong(longHashMsw(a)+longHashMsw(b), longHashLsw(a)+longHashLsw(b))
`static int` `longHashLsw(long key)`
`static int` `longHashMsw(long key)`
`static long` ```longHashToLong(int msw, int lsw)```
`static org.bukkit.Location` ```move(org.bukkit.Location loc, double dx, double dy, double dz)```
Moves a Location into the yaw and pitch of the Location in the offset specified
`static org.bukkit.Location` ```move(org.bukkit.Location loc, org.bukkit.util.Vector offset)```
Moves a Location into the yaw and pitch of the Location in the offset specified
`static double` ```normalize(double x, double z, double reqx, double reqz)```
Normalizes a 2D-vector to be the length of another 2D-vector
Calculates the normalization factor to multiply the input vector with, to get the requested length
`static org.bukkit.util.Vector` ```rotate(float yaw, float pitch, double x, double y, double z)```
Rotates a 3D-vector using yaw and pitch
`static org.bukkit.util.Vector` ```rotate(float yaw, float pitch, org.bukkit.util.Vector vector)```
Rotates a 3D-vector using yaw and pitch
`static double` ```round(double value, int decimals)```
Rounds the specified value to the amount of decimals specified
`static void` ```setVectorLength(org.bukkit.util.Vector vector, double length)```
`static void` ```setVectorLengthSquared(org.bukkit.util.Vector vector, double lengthsquared)```
`static int` `toChunk(double loc)`
Converts a location value into a chunk coordinate
`static int` `toChunk(int loc)`
Converts a location value into a chunk coordinate
`static long` ```toLong(int msw, int lsw)```
Merges two ints into a long
`static double` ```useOld(double oldvalue, double newvalue, double peruseold)```
`static float` `wrapAngle(float angle)`
Wraps the angle to be between -180 and 180 degrees
`static int` `wrapAngle(int angle)`
Wraps the angle to be between -180 and 180 degrees
• ### Methods inherited from class java.lang.Object

`equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
• ### Field Detail

`public static final float DEGTORAD`
Constant Field Values

`public static final float RADTODEG`
Constant Field Values
• #### HALFROOTOFTWO

`public static final double HALFROOTOFTWO`
Constant Field Values
• ### Constructor Detail

• #### MathUtil

`public MathUtil()`
• ### Method Detail

• #### lengthSquared

`public static double lengthSquared(double... values)`
• #### length

`public static double length(double... values)`
• #### distance

```public static double distance(double x1,
double y1,
double x2,
double y2)```
• #### distanceSquared

```public static double distanceSquared(double x1,
double y1,
double x2,
double y2)```
• #### distance

```public static double distance(double x1,
double y1,
double z1,
double x2,
double y2,
double z2)```
• #### distanceSquared

```public static double distanceSquared(double x1,
double y1,
double z1,
double x2,
double y2,
double z2)```
• #### getPercentage

```public static double getPercentage(int subtotal,
int total,
int decimals)```
Gets a percentage and round it with a cusotm amound of decimals
Parameters:
`subtotal` - to get percentags for
`total` - to use as 100% value
`decimals` - to round with
Returns:
Percentage for subtotal with custom decimals
• #### getPercentage

```public static double getPercentage(int subtotal,
int total)```
Gets a percentags of 2 values
Parameters:
`subtotal` - to get percentage for
`total` - to sue as 100% value
Returns:
percentage
• #### getAngleDifference

```public static int getAngleDifference(int angle1,
int angle2)```
Gets the angle difference between two angles
Parameters:
`angle1` -
`angle2` -
Returns:
angle difference
• #### getAngleDifference

```public static float getAngleDifference(float angle1,
float angle2)```
Gets the angle difference between two angles
Parameters:
`angle1` -
`angle2` -
Returns:
angle difference
• #### wrapAngle

`public static int wrapAngle(int angle)`
Wraps the angle to be between -180 and 180 degrees
Parameters:
`angle` - to wrap
Returns:
[-180 > angle >= 180]
• #### wrapAngle

`public static float wrapAngle(float angle)`
Wraps the angle to be between -180 and 180 degrees
Parameters:
`angle` - to wrap
Returns:
[-180 > angle >= 180]
• #### normalize

```public static double normalize(double x,
double z,
double reqx,
double reqz)```
Normalizes a 2D-vector to be the length of another 2D-vector
Calculates the normalization factor to multiply the input vector with, to get the requested length
Parameters:
`x` - axis of the vector
`z` - axis of the vector
`reqx` - axis of the length vector
`reqz` - axis of the length vector
Returns:
the normalization factor
• #### getLookAtYaw

```public static float getLookAtYaw(org.bukkit.entity.Entity loc,
org.bukkit.entity.Entity lookat)```
• #### getLookAtYaw

```public static float getLookAtYaw(org.bukkit.block.Block loc,
org.bukkit.block.Block lookat)```
• #### getLookAtYaw

```public static float getLookAtYaw(org.bukkit.Location loc,
org.bukkit.Location lookat)```
• #### getLookAtYaw

`public static float getLookAtYaw(org.bukkit.util.Vector motion)`
• #### getLookAtYaw

```public static float getLookAtYaw(double dx,
double dz)```
Gets the horizontal look-at angle in degrees to look into the 2D-direction specified
Parameters:
`dx` - axis of the direction
`dz` - axis of the direction
Returns:
the angle in degrees
• #### getLookAtPitch

```public static float getLookAtPitch(double dX,
double dY,
double dZ)```
Gets the pitch angle in degrees to look into the direction specified
Parameters:
`dX` - axis of the direction
`dY` - axis of the direction
`dZ` - axis of the direction
Returns:
look-at angle in degrees
• #### getLookAtPitch

```public static float getLookAtPitch(double dY,
double dXZ)```
Gets the pitch angle in degrees to look into the direction specified
Parameters:
`dY` - axis of the direction
`dXZ` - axis of the direction (length of x and z)
Returns:
look-at angle in degrees
• #### atan

`public static float atan(double value)`
Gets the inverse tangent of the value in degrees
Parameters:
`value` -
Returns:
inverse tangent angle in degrees
• #### atan2

```public static float atan2(double y,
double x)```
Gets the inverse tangent angle in degrees of the rectangle vector
Parameters:
`y` - axis
`x` - axis
Returns:
inverse tangent 2 angle in degrees
• #### longFloor

`public static long longFloor(double value)`
Gets the floor long value from a double value
Parameters:
`value` - to get the floor of
Returns:
floor value
• #### floor

`public static int floor(double value)`
Gets the floor integer value from a double value
Parameters:
`value` - to get the floor of
Returns:
floor value
• #### floor

`public static int floor(float value)`
Gets the floor integer value from a float value
Parameters:
`value` - to get the floor of
Returns:
floor value
• #### ceil

`public static int ceil(double value)`
Gets the ceiling integer value from a double value
Parameters:
`value` - to get the ceiling of
Returns:
ceiling value
• #### ceil

`public static int ceil(float value)`
Gets the ceiling integer value from a float value
Parameters:
`value` - to get the ceiling of
Returns:
ceiling value
• #### move

```public static org.bukkit.Location move(org.bukkit.Location loc,
org.bukkit.util.Vector offset)```
Moves a Location into the yaw and pitch of the Location in the offset specified
Parameters:
`loc` - to move
`offset` - vector
Returns:
Translated Location
• #### move

```public static org.bukkit.Location move(org.bukkit.Location loc,
double dx,
double dy,
double dz)```
Moves a Location into the yaw and pitch of the Location in the offset specified
Parameters:
`loc` - to move
`dx` - offset
`dy` - offset
`dz` - offset
Returns:
Translated Location
• #### rotate

```public static org.bukkit.util.Vector rotate(float yaw,
float pitch,
org.bukkit.util.Vector vector)```
Rotates a 3D-vector using yaw and pitch
Parameters:
`yaw` - angle in degrees
`pitch` - angle in degrees
`vector` - to rotate
Returns:
Vector rotated by the angle (new instance)
• #### rotate

```public static org.bukkit.util.Vector rotate(float yaw,
float pitch,
double x,
double y,
double z)```
Rotates a 3D-vector using yaw and pitch
Parameters:
`yaw` - angle in degrees
`pitch` - angle in degrees
`x` - axis of the vector
`y` - axis of the vector
`z` - axis of the vector
Returns:
Vector rotated by the angle
• #### floorMod

```public static int floorMod(int x,
int y)```
Returns the floor modulus of the int arguments
Parameters:
`x` - the dividend
`y` - the divisor
• #### floorMod

```public static long floorMod(long x,
long y)```
Returns the floor modulus of the long arguments
Parameters:
`x` - the dividend
`y` - the divisor
• #### floorDiv

```public static int floorDiv(int x,
int y)```
Returns the floor division of the int arguments
Parameters:
`x` - the dividend
`y` - the divisor
Returns:
floor division x
• #### floorDiv

```public static long floorDiv(long x,
long y)```
Returns the floor division of the long arguments
Parameters:
`x` - the dividend
`y` - the divisor
Returns:
floor division x
• #### round

```public static double round(double value,
int decimals)```
Rounds the specified value to the amount of decimals specified
Parameters:
`value` - to round
`decimals` - count
Returns:
value round to the decimal count specified
• #### fixNaN

`public static double fixNaN(double value)`
Returns 0 if the value is not-a-number
Parameters:
`value` - to check
Returns:
The value, or 0 if it is NaN
• #### fixNaN

`public static float fixNaN(float value)`
Returns 0 if the value is not-a-number
Parameters:
`value` - to check
Returns:
The value, or 0 if it is NaN
• #### fixNaN

```public static double fixNaN(double value,
double def)```
Returns the default if the value is not-a-number
Parameters:
`value` - to check
`def` - value
Returns:
The value, or the default if it is NaN
• #### fixNaN

```public static float fixNaN(float value,
float def)```
Returns the default if the value is not-a-number
Parameters:
`value` - to check
`def` - value
Returns:
The value, or the default if it is NaN
• #### toChunk

`public static int toChunk(double loc)`
Converts a location value into a chunk coordinate
Parameters:
`loc` - to convert
Returns:
chunk coordinate
• #### toChunk

`public static int toChunk(int loc)`
Converts a location value into a chunk coordinate
Parameters:
`loc` - to convert
Returns:
chunk coordinate
• #### useOld

```public static double useOld(double oldvalue,
double newvalue,
double peruseold)```
• #### lerp

```public static double lerp(double d1,
double d2,
double stage)```
• #### lerp

```public static org.bukkit.util.Vector lerp(org.bukkit.util.Vector vec1,
org.bukkit.util.Vector vec2,
double stage)```
• #### lerp

```public static org.bukkit.Location lerp(org.bukkit.Location loc1,
org.bukkit.Location loc2,
double stage)```
• #### isInverted

```public static boolean isInverted(double value1,
double value2)```
Checks whether one value is negative and the other positive, or opposite
Parameters:
`value1` - to check
`value2` - to check
Returns:
True if value1 is inverted from value2
• #### getDirection

```public static org.bukkit.util.Vector getDirection(float yaw,
float pitch)```
Gets the direction of yaw and pitch angles
Parameters:
`yaw` - angle in degrees
`pitch` - angle in degrees
Returns:
Direction Vector
• #### clamp

```public static double clamp(double value,
double limit)```
Clamps the value between -limit and limit
Parameters:
`value` - to clamp
`limit` -
Returns:
value, -limit or limit
• #### clamp

```public static double clamp(double value,
double min,
double max)```
Clamps the value between the min and max values
Parameters:
`value` - to clamp
`min` -
`max` -
Returns:
value, min or max
• #### clamp

```public static float clamp(float value,
float limit)```
Clamps the value between -limit and limit
Parameters:
`value` - to clamp
`limit` -
Returns:
value, -limit or limit
• #### clamp

```public static float clamp(float value,
float min,
float max)```
Clamps the value between the min and max values
Parameters:
`value` - to clamp
`min` -
`max` -
Returns:
value, min or max
• #### clamp

```public static int clamp(int value,
int limit)```
Clamps the value between -limit and limit
Parameters:
`value` - to clamp
`limit` -
Returns:
value, -limit or limit
• #### clamp

```public static int clamp(int value,
int min,
int max)```
Clamps the value between the min and max values
Parameters:
`value` - to clamp
`min` -
`max` -
Returns:
value, min or max
• #### clamp

```public static long clamp(long value,
long limit)```
Clamps the value between -limit and limit
Parameters:
`value` - to clamp
`limit` -
Returns:
value, -limit or limit
• #### clamp

```public static long clamp(long value,
long min,
long max)```
Clamps the value between the min and max values
Parameters:
`value` - to clamp
`min` -
`max` -
Returns:
value, min or max
• #### invert

```public static int invert(int value,
boolean negative)```
Turns a value negative or keeps it positive based on a boolean input
Parameters:
`value` - to work with
`negative` - - True to invert, False to keep the old value
Returns:
the value or inverted (-value)
• #### invert

```public static float invert(float value,
boolean negative)```
Turns a value negative or keeps it positive based on a boolean input
Parameters:
`value` - to work with
`negative` - - True to invert, False to keep the old value
Returns:
the value or inverted (-value)
• #### invert

```public static double invert(double value,
boolean negative)```
Turns a value negative or keeps it positive based on a boolean input
Parameters:
`value` - to work with
`negative` - - True to invert, False to keep the old value
Returns:
the value or inverted (-value)
• #### toLong

```public static long toLong(int msw,
int lsw)```
Merges two ints into a long
Parameters:
`msw` - integer
`lsw` - integer
Returns:
merged long value
• #### longHashToLong

```public static long longHashToLong(int msw,
int lsw)```
• #### longHashMsw

`public static int longHashMsw(long key)`
• #### longHashLsw

`public static int longHashLsw(long key)`

```public static long longHashAdd(long key_a,
long key_b)```
Shorthand equivalent of:
longHashToLong(longHashMsw(a)+longHashMsw(b), longHashLsw(a)+longHashLsw(b))
Parameters:
`key_a` -
`key_b` -
Returns:
key_a + key_b
• #### setVectorLength

```public static void setVectorLength(org.bukkit.util.Vector vector,
double length)```
• #### setVectorLengthSquared

```public static void setVectorLengthSquared(org.bukkit.util.Vector vector,
double lengthsquared)```

```public static boolean isHeadingTo(org.bukkit.block.BlockFace direction,
org.bukkit.util.Vector velocity)```

```public static boolean isHeadingTo(org.bukkit.Location from,
org.bukkit.Location to,
org.bukkit.util.Vector velocity)```

```public static boolean isHeadingTo(org.bukkit.util.Vector offset,
org.bukkit.util.Vector velocity)```
• #### getNormalizationFactor

`public static double getNormalizationFactor(org.bukkit.util.Vector v)`
Calculates the normalization factor for a 3D vector. Multiplying the input vector with this factor will turn it into a vector of unit length. If the input vector is (0,0,0), Infinity is returned.
Parameters:
`v` -
Returns:
normalization factor
• #### getNormalizationFactor

```public static double getNormalizationFactor(double x,
double y,
double z,
double w)```
Calculates the normalization factor for a 4D vector. Multiplying the input vector with this factor will turn it into a vector of unit length. If the input vector is (0,0,0,0), Infinity is returned.
Parameters:
`x` -
`y` -
`z` -
`w` -
Returns:
normalization factor
• #### getNormalizationFactor

```public static double getNormalizationFactor(double x,
double y,
double z)```
Calculates the normalization factor for a 3D vector. Multiplying the input vector with this factor will turn it into a vector of unit length. If the input vector is (0,0,0), Infinity is returned.
Parameters:
`x` -
`y` -
`z` -
Returns:
normalization factor
• #### getNormalizationFactor

```public static double getNormalizationFactor(double x,
double y)```
Calculates the normalization factor for a 2D vector. Multiplying the input vector with this factor will turn it into a vector of unit length. If the input vector is (0,0), Infinity is returned.
Parameters:
`x` -
`y` -
Returns:
normalization factor
• #### getNormalizationFactorLS

`public static double getNormalizationFactorLS(double lengthSquared)`
Calculates the normalization factor for a squared length. Multiplying the input values with this factor will turn it into a vector of unit length. If the squared length is 0, Infinity is returned.
Parameters:
`lengthSquared` -
Returns:
normalization factor
• #### getAngleDifference

```public static double getAngleDifference(org.bukkit.util.Vector v0,
org.bukkit.util.Vector v1)```
Calculates the angle difference between two vectors in degrees
Parameters:
`v0` - first vector
`v1` - second vector
Returns:
absolute angle difference in degrees