public class MathUtils

  1. Object
  2. MathUtils
A few math methods that don’t fit very well anywhere else.

Fields

public static final float PI = 3.1415927f
public static final float TWOPI = 6.2831855f
public static final float INV_PI = 0.31830987f
public static final float HALF_PI = 1.5707964f
public static final float QUARTER_PI = 0.7853982f
public static final float THREE_HALVES_PI = 4.712389f
public static final float DEG2RAD = 0.017453292fDegrees to radians conversion factor
public static final float RAD2DEG = 57.295776fRadians to degrees conversion factor
public static final float[] sinLUT

Constructors

public MathUtils()

Methods

public static final float sin(float x)
public static final float sinLUT(float x)
public static final float cos(float x)
public static final float abs(float x)
public static final int abs(int x)
public static final int floor(float x)
public static final int ceil(float x)
public static final int round(float x)
public static final int ceilPowerOf2(int x)Rounds up the value to the nearest higher power^2 value.
public static final float max(float a, float b)
public static final int max(int a, int b)
public static final float min(float a, float b)
public static final int min(int a, int b)
public static final float map(float val, float fromMin, float fromMax, float toMin, float toMax)
public static final float clamp(float a, float low, float high)Returns the closest value to ‘a’ that is in between ’low’ and ‘high’
public static final Vec2 clamp(Vec2 a, Vec2 low, Vec2 high)
public static final void clampToOut(Vec2 a, Vec2 low, Vec2 high, Vec2 dest)
public static final int nextPowerOfTwo(int x)Next Largest Power of 2: Given a binary integer value x, the next largest power of 2 can be computed by a SWAR algorithm that recursively “folds” the upper bits into the lower bits.
public static final boolean isPowerOfTwo(int x)
public static final float atan2(float y, float x)
public static final float fastAtan2(float y, float x)
public static final float reduceAngle(float theta)
public static final float randomFloat(float argLow, float argHigh)
public static final float randomFloat(Random r, float argLow, float argHigh)
public static final float sqrt(float x)
public static final float distanceSquared(Vec2 v1, Vec2 v2)
public static final float distance(Vec2 v1, Vec2 v2)
public static final float fastPow(float a, float b)

Inherited methods

Field details

PI

public static final float PI = 3.1415927f

TWOPI

public static final float TWOPI = 6.2831855f

INV_PI

public static final float INV_PI = 0.31830987f

HALF_PI

public static final float HALF_PI = 1.5707964f

QUARTER_PI

public static final float QUARTER_PI = 0.7853982f

THREE_HALVES_PI

public static final float THREE_HALVES_PI = 4.712389f

DEG2RAD

public static final float DEG2RAD = 0.017453292f
Degrees to radians conversion factor

RAD2DEG

public static final float RAD2DEG = 57.295776f
Radians to degrees conversion factor

sinLUT

public static final float[] sinLUT

Constructor details

MathUtils

public MathUtils()

Method details

sin

public static final float sin(float x)

sinLUT

public static final float sinLUT(float x)

cos

public static final float cos(float x)

abs

public static final float abs(float x)

abs

public static final int abs(int x)

floor

public static final int floor(float x)

ceil

public static final int ceil(float x)

round

public static final int round(float x)

ceilPowerOf2

public static final int ceilPowerOf2(int x)
Rounds up the value to the nearest higher power^2 value.

Returns

power^2 value

max

public static final float max(float a, float b)

max

public static final int max(int a, int b)

min

public static final float min(float a, float b)

min

public static final int min(int a, int b)

map

public static final float map(float val, float fromMin, float fromMax, float toMin, float toMax)

clamp

public static final float clamp(float a, float low, float high)
Returns the closest value to ‘a’ that is in between ’low’ and ‘high’

clamp

public static final Vec2 clamp(Vec2 a, Vec2 low, Vec2 high)

clampToOut

public static final void clampToOut(Vec2 a, Vec2 low, Vec2 high, Vec2 dest)

nextPowerOfTwo

public static final int nextPowerOfTwo(int x)
Next Largest Power of 2: Given a binary integer value x, the next largest power of 2 can be computed by a SWAR algorithm that recursively “folds” the upper bits into the lower bits. This process yields a bit vector with the same most significant 1 as x, but all 1’s below it. Adding 1 to that value yields the next largest power of 2.

isPowerOfTwo

public static final boolean isPowerOfTwo(int x)

atan2

public static final float atan2(float y, float x)

fastAtan2

public static final float fastAtan2(float y, float x)

reduceAngle

public static final float reduceAngle(float theta)

randomFloat

public static final float randomFloat(float argLow, float argHigh)

randomFloat

public static final float randomFloat(Random r, float argLow, float argHigh)

sqrt

public static final float sqrt(float x)

distanceSquared

public static final float distanceSquared(Vec2 v1, Vec2 v2)

distance

public static final float distance(Vec2 v1, Vec2 v2)

fastPow

public static final float fastPow(float a, float b)