Package arc.math
Class Mathf
java.lang.Object
arc.math.Mathf
-
Field Summary
Modifier and TypeFieldDescriptionstatic final boolean[]
static final float
static final float
multiply by this to convert from degrees to radiansstatic final double
static final double
static final float
static final float
static final float
static final float
static final float
static final float
static final float
static final float
multiply by this to convert from radians to degreesstatic Rand
static final int[]
static final float
static final float
static final int[]
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic float
absin
(float scl, float mag) static float
absin
(float in, float scl, float mag) static float
angle
(float x, float y) static float
angleExact
(float x, float y) static float
approach
(float from, float to, float speed) Approaches a value at linear speed.static float
approachDelta
(float from, float to, float speed) Approaches a value at linear speed.static Vec2
arrive
(float x, float y, float destX, float destY, Vec2 curVel, float radius, float tolerance, float speed, float accel) static Vec2
arrive
(Position pos, Position target, Vec2 curVel, float radius, float tolerance, float speed, float smoothTime) static float
atan2
(float x, float y) Close approximation of the frequently-used trigonometric method atan2, with higher precision than libGDX's atan2 approximation.static int
ceil
(float value) Returns the smallest integer greater than or equal to the specified float.static int
ceilPositive
(float value) Returns the smallest integer greater than or equal to the specified float.static boolean
chance
(double d) static boolean
chanceDelta
(double d) static double
clamp
(double value, double min, double max) static float
clamp
(float value) Clamps to [0, 1].static float
clamp
(float value, float min, float max) static int
clamp
(int value, int min, int max) static long
clamp
(long value, long min, long max) static float
cos
(float radians) Returns the cosine in radians from a lookup table.static float
cos
(float radians, float scl, float mag) static float
cosDeg
(float degrees) Returns the cosine in radians from a lookup table.static float
curve
(float f, float offset) Converts a 0-1 value to 0-1 when it is in [offset, 1].static float
curve
(float f, float from, float to) Converts a 0-1 value to 0-1 when it is in [offset, to].static float
curveMargin
(float f, float margin) Transforms a 0-1 value to a value with a 0.5 plateau in the middle.static float
curveMargin
(float f, float marginLeft, float marginRight) Transforms a 0-1 value to a value with a 0.5 plateau in the middle.static int
digits
(int n) static int
digits
(long n) static float
dot
(float x1, float y1, float x2, float y2) static float
dst
(float x1, float y1) static float
dst
(float x1, float y1, float x2, float y2) static float
dst2
(float x1, float y1) static float
dst2
(float x1, float y1, float x2, float y2) static float
dstm
(float x1, float y1, float x2, float y2) Manhattan distance.static boolean
equal
(float a, float b) Returns true if a is nearly equal to b.static boolean
equal
(float a, float b, float tolerance) Returns true if a is nearly equal to b.static int
floor
(float value) Returns the largest integer less than or equal to the specified float.static int
floorPositive
(float value) Returns the largest integer less than or equal to the specified float.static boolean
isPowerOfTwo
(int value) static float
len
(float x, float y) static float
len2
(float x, float y) static float
lerp
(float fromValue, float toValue, float progress) Linearly interpolates between fromValue to toValue on progress position.static float
lerpDelta
(float fromValue, float toValue, float progress) Linearly interpolates between fromValue to toValue on progress position.static float
log
(float a, float value) static float
log2
(float value) static int
log2
(int value) static float
map
(float value, float from, float to) Map value from [0, 1].static float
map
(float value, float froma, float toa, float fromb, float tob) static float
maxZero
(float val) static float
mod
(float f, float n) Mod function that works properly for negative numbers.static int
mod
(int x, int n) Mod function that works properly for negative numbers.static int
nextPowerOfTwo
(int value) Returns the next power of two.static int
num
(boolean b) Converts a boolean to an integer: 1 if true, 0, if false.static float
pow
(float a, float b) static int
pow
(int a, int b) static float
random()
Returns random number between 0.0 (inclusive) and 1.0 (exclusive).static float
random
(float range) Returns a random number between 0 (inclusive) and the specified value (exclusive).static float
random
(float start, float end) Returns a random number between start (inclusive) and end (exclusive).static int
random
(int range) Returns a random number between 0 (inclusive) and the specified value (inclusive).static int
random
(int start, int end) Returns a random number between start (inclusive) and end (inclusive).static long
random
(long range) Returns a random number between 0 (inclusive) and the specified value (inclusive).static long
random
(long start, long end) Returns a random number between start (inclusive) and end (inclusive).static boolean
Returns a random boolean value.static boolean
randomBoolean
(float chance) Returns true if a random value between 0 and 1 is less than the specified value.static float
randomSeed
(long seed) static float
randomSeed
(long seed, float max) static float
randomSeed
(long seed, float min, float max) Inclusive.static int
randomSeed
(long seed, int min, int max) Inclusive.static float
randomSeedRange
(long seed, float range) static int
Returns -1 or 1, randomly.static float
Returns a triangularly distributed random number between -1.0 (exclusive) and 1.0 (exclusive), where values around zero are more likely.static float
randomTriangular
(float max) Returns a triangularly distributed random number between-max
(exclusive) andmax
(exclusive), where values around zero are more likely.static float
randomTriangular
(float min, float max) Returns a triangularly distributed random number betweenmin
(inclusive) andmax
(exclusive), where themode
argument defaults to the midpoint between the bounds, giving a symmetric distribution.static float
randomTriangular
(float min, float max, float mode) Returns a triangularly distributed random number betweenmin
(inclusive) andmax
(exclusive), where values aroundmode
are more likely.static float
range
(float range) static float
range
(float min, float max) static int
range
(int range) static int
round
(float value) Returns the closest integer to the specified float.static float
round
(float value, float step) static int
round
(float value, int step) static int
round
(int value, int step) static int
roundPositive
(float value) Returns the closest integer to the specified float.static float
sample
(float[] values, float time) static int
sign
(boolean b) Returns 1 if true, -1 if false.static int
sign
(float f) Returns -1 if f<0, 1 otherwise.static float
sin
(float radians) Returns the sine in radians from a lookup table.static float
sin
(float scl, float mag) static float
sin
(float radians, float scl, float mag) static float
sinDeg
(float degrees) Returns the sine in radians from a lookup table.static float
slerp
(float fromDegrees, float toDegrees, float progress) Linearly interpolates between two angles in degrees.static float
slerpDelta
(float fromDegrees, float toDegrees, float progress) static float
slerpRad
(float fromRadians, float toRadians, float progress) Linearly interpolates between two angles in radians.static float
slope
(float fin) static float
sqr
(float x) static float
sqrt
(float x) static float
tan
(float radians, float scl, float mag) static boolean
within
(float x1, float y1, float dst) static boolean
within
(float x1, float y1, float x2, float y2, float dst) static float
wrapAngleAroundZero
(float a) Wraps the given angle to the range [-PI, PI]static boolean
zero
(double value) Returns true if the value is zero (using the default tolerance as upper bound)static boolean
zero
(float value) Returns true if the value is zero (using the default tolerance as upper bound)static boolean
zero
(float value, float tolerance) Returns true if the value is zero.
-
Field Details
-
signs
public static final int[] signs -
zeroOne
public static final int[] zeroOne -
booleans
public static final boolean[] booleans -
FLOAT_ROUNDING_ERROR
public static final float FLOAT_ROUNDING_ERROR- See Also:
-
PI
public static final float PI- See Also:
-
pi
public static final float pi- See Also:
-
halfPi
public static final float halfPi- See Also:
-
PI2
public static final float PI2- See Also:
-
E
public static final float E- See Also:
-
sqrt2
public static final float sqrt2 -
sqrt3
public static final float sqrt3 -
radiansToDegrees
public static final float radiansToDegreesmultiply by this to convert from radians to degrees- See Also:
-
radDeg
public static final float radDeg- See Also:
-
degreesToRadians
public static final float degreesToRadiansmultiply by this to convert from degrees to radians- See Also:
-
degRad
public static final float degRad- See Also:
-
doubleDegRad
public static final double doubleDegRad- See Also:
-
doubleRadDeg
public static final double doubleRadDeg- See Also:
-
rand
-
-
Constructor Details
-
Mathf
public Mathf()
-
-
Method Details
-
sin
public static float sin(float radians) Returns the sine in radians from a lookup table. -
cos
public static float cos(float radians) Returns the cosine in radians from a lookup table. -
sinDeg
public static float sinDeg(float degrees) Returns the sine in radians from a lookup table. -
cosDeg
public static float cosDeg(float degrees) Returns the cosine in radians from a lookup table. -
absin
public static float absin(float scl, float mag) -
absin
public static float absin(float in, float scl, float mag) -
tan
public static float tan(float radians, float scl, float mag) -
sin
public static float sin(float scl, float mag) -
sin
public static float sin(float radians, float scl, float mag) -
cos
public static float cos(float radians, float scl, float mag) -
angle
public static float angle(float x, float y) -
angleExact
public static float angleExact(float x, float y) -
wrapAngleAroundZero
public static float wrapAngleAroundZero(float a) Wraps the given angle to the range [-PI, PI]- Parameters:
a
- the angle in radians- Returns:
- the given angle wrapped to the range [-PI, PI]
-
atan2
public static float atan2(float x, float y) Close approximation of the frequently-used trigonometric method atan2, with higher precision than libGDX's atan2 approximation. Average error is 1.057E-6 radians; maximum error is 1.922E-6. Takes y and x (in that unusual order) as floats, and returns the angle from the origin to that point in radians. It is about 4 times faster thanMath.atan2(double, double)
(roughly 15 ns instead of roughly 60 ns for Math, on Java 8 HotSpot).
Credit for this goes to the 1955 research study "Approximations for Digital Computers," by RAND Corporation. This is sheet 11's algorithm, which is the fourth-fastest and fourth-least precise. The algorithms on sheets 8-10 are faster, but only by a very small degree, and are considerably less precise. That study provides an atan method, and that cleanly translates to atan2().- Parameters:
y
- y-component of the point to find the angle towards; note the parameter order is unusual by conventionx
- x-component of the point to find the angle towards; note the parameter order is unusual by convention- Returns:
- the angle to the given point, in radians as a float; ranges from -PI to PI
-
digits
public static int digits(int n) -
digits
public static int digits(long n) -
sqrt
public static float sqrt(float x) -
sqr
public static float sqr(float x) -
map
public static float map(float value, float froma, float toa, float fromb, float tob) -
map
public static float map(float value, float from, float to) Map value from [0, 1]. -
sign
public static int sign(float f) Returns -1 if f<0, 1 otherwise. -
sign
public static int sign(boolean b) Returns 1 if true, -1 if false. -
num
public static int num(boolean b) Converts a boolean to an integer: 1 if true, 0, if false. -
pow
public static float pow(float a, float b) -
pow
public static int pow(int a, int b) -
range
public static float range(float range) -
range
public static int range(int range) -
range
public static float range(float min, float max) -
chanceDelta
public static boolean chanceDelta(double d) -
chance
public static boolean chance(double d) -
random
public static int random(int range) Returns a random number between 0 (inclusive) and the specified value (inclusive). -
random
public static int random(int start, int end) Returns a random number between start (inclusive) and end (inclusive). -
random
public static long random(long range) Returns a random number between 0 (inclusive) and the specified value (inclusive). -
random
public static long random(long start, long end) Returns a random number between start (inclusive) and end (inclusive). -
randomBoolean
public static boolean randomBoolean()Returns a random boolean value. -
randomBoolean
public static boolean randomBoolean(float chance) Returns true if a random value between 0 and 1 is less than the specified value. -
random
public static float random()Returns random number between 0.0 (inclusive) and 1.0 (exclusive). -
random
public static float random(float range) Returns a random number between 0 (inclusive) and the specified value (exclusive). -
random
public static float random(float start, float end) Returns a random number between start (inclusive) and end (exclusive). -
randomSign
public static int randomSign()Returns -1 or 1, randomly. -
randomSeed
public static int randomSeed(long seed, int min, int max) Inclusive. -
randomSeed
public static float randomSeed(long seed, float min, float max) Inclusive. -
randomSeed
public static float randomSeed(long seed) -
randomSeed
public static float randomSeed(long seed, float max) -
randomSeedRange
public static float randomSeedRange(long seed, float range) -
randomTriangular
public static float randomTriangular()Returns a triangularly distributed random number between -1.0 (exclusive) and 1.0 (exclusive), where values around zero are more likely.This is an optimized version of
randomTriangular(-1, 1, 0)
-
randomTriangular
public static float randomTriangular(float max) Returns a triangularly distributed random number between-max
(exclusive) andmax
(exclusive), where values around zero are more likely.This is an optimized version of
randomTriangular(-max, max, 0)
- Parameters:
max
- the upper limit
-
randomTriangular
public static float randomTriangular(float min, float max) Returns a triangularly distributed random number betweenmin
(inclusive) andmax
(exclusive), where themode
argument defaults to the midpoint between the bounds, giving a symmetric distribution.This method is equivalent of
randomTriangular(min, max, (min + max) * .5f)
- Parameters:
min
- the lower limitmax
- the upper limit
-
randomTriangular
public static float randomTriangular(float min, float max, float mode) Returns a triangularly distributed random number betweenmin
(inclusive) andmax
(exclusive), where values aroundmode
are more likely.- Parameters:
min
- the lower limitmax
- the upper limitmode
- the point around which the values are more likely
-
nextPowerOfTwo
public static int nextPowerOfTwo(int value) Returns the next power of two. Returns the specified value if the value is already a power of two. -
isPowerOfTwo
public static boolean isPowerOfTwo(int value) -
clamp
public static int clamp(int value, int min, int max) -
clamp
public static long clamp(long value, long min, long max) -
clamp
public static float clamp(float value, float min, float max) -
clamp
public static float clamp(float value) Clamps to [0, 1]. -
clamp
public static double clamp(double value, double min, double max) -
maxZero
public static float maxZero(float val) -
approach
public static float approach(float from, float to, float speed) Approaches a value at linear speed. -
approachDelta
public static float approachDelta(float from, float to, float speed) Approaches a value at linear speed. Multiplied by the delta. -
lerp
public static float lerp(float fromValue, float toValue, float progress) Linearly interpolates between fromValue to toValue on progress position. -
lerpDelta
public static float lerpDelta(float fromValue, float toValue, float progress) Linearly interpolates between fromValue to toValue on progress position. Multiplied by Time.delta(). -
slerpRad
public static float slerpRad(float fromRadians, float toRadians, float progress) Linearly interpolates between two angles in radians. Takes into account that angles wrap at two pi and always takes the direction with the smallest delta angle.- Parameters:
fromRadians
- start angle in radianstoRadians
- target angle in radiansprogress
- interpolation value in the range [0, 1]- Returns:
- the interpolated angle in the range [0, PI2[
-
slerp
public static float slerp(float fromDegrees, float toDegrees, float progress) Linearly interpolates between two angles in degrees. Takes into account that angles wrap at 360 degrees and always takes the direction with the smallest delta angle.- Parameters:
fromDegrees
- start angle in degreestoDegrees
- target angle in degreesprogress
- interpolation value in the range [0, 1]- Returns:
- the interpolated angle in the range [0, 360[
-
slerpDelta
public static float slerpDelta(float fromDegrees, float toDegrees, float progress) -
floor
public static int floor(float value) Returns the largest integer less than or equal to the specified float. This method will only properly floor floats from -(2^14) to (Float.MAX_VALUE - 2^14). -
floorPositive
public static int floorPositive(float value) Returns the largest integer less than or equal to the specified float. This method will only properly floor floats that are positive. Note this method simply casts the float to int. -
ceil
public static int ceil(float value) Returns the smallest integer greater than or equal to the specified float. This method will only properly ceil floats from -(2^14) to (Float.MAX_VALUE - 2^14). -
ceilPositive
public static int ceilPositive(float value) Returns the smallest integer greater than or equal to the specified float. This method will only properly ceil floats that are positive. -
round
public static int round(float value) Returns the closest integer to the specified float. This method will only properly round floats from -(2^14) to (Float.MAX_VALUE - 2^14). -
round
public static int round(int value, int step) -
round
public static float round(float value, float step) -
round
public static int round(float value, int step) -
roundPositive
public static int roundPositive(float value) Returns the closest integer to the specified float. This method will only properly round floats that are positive. -
zero
public static boolean zero(float value) Returns true if the value is zero (using the default tolerance as upper bound) -
zero
public static boolean zero(double value) Returns true if the value is zero (using the default tolerance as upper bound) -
zero
public static boolean zero(float value, float tolerance) Returns true if the value is zero.- Parameters:
tolerance
- represent an upper bound below which the value is considered zero.
-
equal
public static boolean equal(float a, float b) Returns true if a is nearly equal to b. The function uses the default floating error tolerance.- Parameters:
a
- the first value.b
- the second value.
-
equal
public static boolean equal(float a, float b, float tolerance) Returns true if a is nearly equal to b.- Parameters:
a
- the first value.b
- the second value.tolerance
- represent an upper bound below which the two values are considered equal.
-
log
public static float log(float a, float value) - Returns:
- the logarithm of value with base a
-
log2
public static float log2(float value) - Returns:
- the logarithm of value with base 2
-
log2
public static int log2(int value) - Returns:
- base-2 logarithm of the specified integer
-
mod
public static float mod(float f, float n) Mod function that works properly for negative numbers. -
mod
public static int mod(int x, int n) Mod function that works properly for negative numbers. -
sample
public static float sample(float[] values, float time) - Returns:
- a sampled value based on position in an array of float values.
-
slope
public static float slope(float fin) - Returns:
- the input 0-1 value scaled to 0-1-0.
-
curve
public static float curve(float f, float offset) Converts a 0-1 value to 0-1 when it is in [offset, 1]. -
curve
public static float curve(float f, float from, float to) Converts a 0-1 value to 0-1 when it is in [offset, to]. -
curveMargin
public static float curveMargin(float f, float margin) Transforms a 0-1 value to a value with a 0.5 plateau in the middle. When margin = 0.5, this method doesn't do anything. -
curveMargin
public static float curveMargin(float f, float marginLeft, float marginRight) Transforms a 0-1 value to a value with a 0.5 plateau in the middle. When margin = 0.5, this method doesn't do anything. -
len
public static float len(float x, float y) -
len2
public static float len2(float x, float y) -
dot
public static float dot(float x1, float y1, float x2, float y2) -
dst
public static float dst(float x1, float y1) -
dst2
public static float dst2(float x1, float y1) -
dst
public static float dst(float x1, float y1, float x2, float y2) -
dst2
public static float dst2(float x1, float y1, float x2, float y2) -
dstm
public static float dstm(float x1, float y1, float x2, float y2) Manhattan distance. -
arrive
-
arrive
-
within
public static boolean within(float x1, float y1, float x2, float y2, float dst) - Returns:
- whether dst(x1, y1, x2, y2) < dst
-
within
public static boolean within(float x1, float y1, float dst) - Returns:
- whether dst(x, y, 0, 0) < dst
-