Package arc.math
Class Mathf
java.lang.Object
arc.math.Mathf
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final boolean[]static final floatstatic final floatmultiply by this to convert from degrees to radiansstatic final doublestatic final doublestatic final floatstatic final floatstatic final floatstatic final floatstatic final floatstatic final floatstatic final floatstatic final floatmultiply by this to convert from radians to degreesstatic Randstatic final int[]static final floatstatic final floatstatic final int[] -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic floatabsin(float scl, float mag) static floatabsin(float in, float scl, float mag) static floatangle(float x, float y) static floatangleExact(float x, float y) static floatapproach(float from, float to, float speed) Approaches a value at linear speed.static floatapproachDelta(float from, float to, float speed) Approaches a value at linear speed.static Vec2arrive(float x, float y, float destX, float destY, Vec2 curVel, float radius, float tolerance, float speed, float accel) static Vec2arrive(Position pos, Position target, Vec2 curVel, float radius, float tolerance, float speed, float smoothTime) static floatatan2(float x, float y) Close approximation of the frequently-used trigonometric method atan2, with higher precision than libGDX's atan2 approximation.static intceil(float value) Returns the smallest integer greater than or equal to the specified float.static intceilPositive(float value) Returns the smallest integer greater than or equal to the specified float.static booleanchance(double d) static booleanchanceDelta(double d) static doubleclamp(double value, double min, double max) static floatclamp(float value) Clamps to [0, 1].static floatclamp(float value, float min, float max) static intclamp(int value, int min, int max) static longclamp(long value, long min, long max) static floatcos(float radians) Returns the cosine in radians from a lookup table.static floatcos(float radians, float scl, float mag) static floatcosDeg(float degrees) Returns the cosine in radians from a lookup table.static floatcurve(float f, float offset) Converts a 0-1 value to 0-1 when it is in [offset, 1].static floatcurve(float f, float from, float to) Converts a 0-1 value to 0-1 when it is in [offset, to].static floatcurveMargin(float f, float margin) Transforms a 0-1 value to a value with a 0.5 plateau in the middle.static floatcurveMargin(float f, float marginLeft, float marginRight) Transforms a 0-1 value to a value with a 0.5 plateau in the middle.static intdigits(int n) static intdigits(long n) static floatdot(float x1, float y1, float x2, float y2) static floatdst(float x1, float y1) static floatdst(float x1, float y1, float x2, float y2) static floatdst2(float x1, float y1) static floatdst2(float x1, float y1, float x2, float y2) static floatdstm(float x1, float y1, float x2, float y2) Manhattan distance.static booleanequal(float a, float b) Returns true if a is nearly equal to b.static booleanequal(float a, float b, float tolerance) Returns true if a is nearly equal to b.static intfloor(float value) Returns the largest integer less than or equal to the specified float.static intfloorPositive(float value) Returns the largest integer less than or equal to the specified float.static booleanisPowerOfTwo(int value) static floatlen(float x, float y) static floatlen2(float x, float y) static floatlerp(float fromValue, float toValue, float progress) Linearly interpolates between fromValue to toValue on progress position.static floatlerpDelta(float fromValue, float toValue, float progress) Linearly interpolates between fromValue to toValue on progress position.static floatlog(float a, float value) static floatlog2(float value) static intlog2(int value) static floatmap(float value, float from, float to) Map value from [0, 1].static floatmap(float value, float froma, float toa, float fromb, float tob) static floatmaxZero(float val) static floatmod(float f, float n) Mod function that works properly for negative numbers.static intmod(int x, int n) Mod function that works properly for negative numbers.static intnextPowerOfTwo(int value) Returns the next power of two.static intnum(boolean b) Converts a boolean to an integer: 1 if true, 0, if false.static floatpow(float a, float b) static intpow(int a, int b) static floatrandom()Returns random number between 0.0 (inclusive) and 1.0 (exclusive).static floatrandom(float range) Returns a random number between 0 (inclusive) and the specified value (exclusive).static floatrandom(float start, float end) Returns a random number between start (inclusive) and end (exclusive).static intrandom(int range) Returns a random number between 0 (inclusive) and the specified value (inclusive).static intrandom(int start, int end) Returns a random number between start (inclusive) and end (inclusive).static longrandom(long range) Returns a random number between 0 (inclusive) and the specified value (inclusive).static longrandom(long start, long end) Returns a random number between start (inclusive) and end (inclusive).static booleanReturns a random boolean value.static booleanrandomBoolean(float chance) Returns true if a random value between 0 and 1 is less than the specified value.static floatrandomSeed(long seed) static floatrandomSeed(long seed, float max) static floatrandomSeed(long seed, float min, float max) Inclusive.static intrandomSeed(long seed, int min, int max) Inclusive.static floatrandomSeedRange(long seed, float range) static intReturns -1 or 1, randomly.static floatReturns a triangularly distributed random number between -1.0 (exclusive) and 1.0 (exclusive), where values around zero are more likely.static floatrandomTriangular(float max) Returns a triangularly distributed random number between-max(exclusive) andmax(exclusive), where values around zero are more likely.static floatrandomTriangular(float min, float max) Returns a triangularly distributed random number betweenmin(inclusive) andmax(exclusive), where themodeargument defaults to the midpoint between the bounds, giving a symmetric distribution.static floatrandomTriangular(float min, float max, float mode) Returns a triangularly distributed random number betweenmin(inclusive) andmax(exclusive), where values aroundmodeare more likely.static floatrange(float range) static floatrange(float min, float max) static intrange(int range) static intround(float value) Returns the closest integer to the specified float.static floatround(float value, float step) static intround(float value, int step) static intround(int value, int step) static introundPositive(float value) Returns the closest integer to the specified float.static floatsample(float[] values, float time) static intsign(boolean b) Returns 1 if true, -1 if false.static intsign(float f) Returns -1 if f<0, 1 otherwise.static floatsin(float radians) Returns the sine in radians from a lookup table.static floatsin(float scl, float mag) static floatsin(float radians, float scl, float mag) static floatsinDeg(float degrees) Returns the sine in radians from a lookup table.static floatslerp(float fromDegrees, float toDegrees, float progress) Linearly interpolates between two angles in degrees.static floatslerpDelta(float fromDegrees, float toDegrees, float progress) static floatslerpRad(float fromRadians, float toRadians, float progress) Linearly interpolates between two angles in radians.static floatslope(float fin) static floatsqr(float x) static floatsqrt(float x) static floattan(float radians, float scl, float mag) static booleanwithin(float x1, float y1, float dst) static booleanwithin(float x1, float y1, float x2, float y2, float dst) static floatwrapAngleAroundZero(float a) Wraps the given angle to the range [-PI, PI]static booleanzero(double value) Returns true if the value is zero (using the default tolerance as upper bound)static booleanzero(float value) Returns true if the value is zero (using the default tolerance as upper bound)static booleanzero(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 themodeargument 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 aroundmodeare 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
-