Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.

MathUtils

public final class MathUtils
extends Object

java.lang.Object
   ↳ com.google.android.material.math.MathUtils


A class that contains utility methods related to numbers.

Summary

Constants

float DEFAULT_EPSILON

Default epsilon value for fuzzy float comparisons.

Public methods

static float abs(float v)
static float acos(float value)
static float asin(float value)
static float atan(float value)
static float atan2(float a, float b)
static float constrain(float amount, float low, float high)
static int constrain(int amount, int low, int high)
static long constrain(long amount, long low, long high)
static float cross(float v1x, float v1y, float v2x, float v2y)
static float degrees(float radians)
static float dist(float x1, float y1, float z1, float x2, float y2, float z2)
static float dist(float x1, float y1, float x2, float y2)
static float distanceToFurthestCorner(float pointX, float pointY, float rectLeft, float rectTop, float rectRight, float rectBottom)

Returns the furthest distance from the point defined by pointX and pointY to the four corners of the rectangle defined by rectLeft, rectTop, rectRight, and rectBottom.

static float dot(float v1x, float v1y, float v2x, float v2y)
static boolean eq(float a, float b, float epsilon)

Fuzzy equal to for floats.

static float exp(float a)
static boolean geq(float a, float b, float epsilon)

Fuzzy greater than or equal to for floats.

static boolean leq(float a, float b, float epsilon)

Fuzzy less than or equal to for floats.

static float lerp(float start, float stop, float amount)
static float log(float a)
static float mag(float a, float b, float c)
static float mag(float a, float b)
static float map(float minStart, float minStop, float maxStart, float maxStop, float value)
static float max(float a, float b, float c, float d)
static float max(int a, int b)
static float max(int a, int b, int c, int d)
static float max(int a, int b, int c)
static float max(float a, float b, float c)
static float max(float a, float b)
static float min(float a, float b, float c)
static float min(float a, float b)
static float min(int a, int b)
static float min(int a, int b, int c)
static int nearest(int amount, int low, int high)
static float nearest(float amount, float low, float high)
static boolean neq(float a, float b, float epsilon)

Fuzzy not equal to for floats.

static float norm(float start, float stop, float value)
static float pow(float a, float b)
static float radians(float degrees)
static float sq(float v)
static float sqrt(float value)
static float tan(float angle)

Inherited methods

From class java.lang.Object

Constants

DEFAULT_EPSILON

float DEFAULT_EPSILON

Default epsilon value for fuzzy float comparisons.

Constant Value: 1.0E-4

Public methods

abs

float abs (float v)

Parameters
v float

Returns
float

acos

float acos (float value)

Parameters
value float

Returns
float

asin

float asin (float value)

Parameters
value float

Returns
float

atan

float atan (float value)

Parameters
value float

Returns
float

atan2

float atan2 (float a, 
                float b)

Parameters
a float

b float

Returns
float

constrain

float constrain (float amount, 
                float low, 
                float high)

Parameters
amount float

low float

high float

Returns
float

constrain

int constrain (int amount, 
                int low, 
                int high)

Parameters
amount int

low int

high int

Returns
int

constrain

long constrain (long amount, 
                long low, 
                long high)

Parameters
amount long

low long

high long

Returns
long

cross

float cross (float v1x, 
                float v1y, 
                float v2x, 
                float v2y)

Parameters
v1x float

v1y float

v2x float

v2y float

Returns
float

degrees

float degrees (float radians)

Parameters
radians float

Returns
float

dist

float dist (float x1, 
                float y1, 
                float z1, 
                float x2, 
                float y2, 
                float z2)

Parameters
x1 float

y1 float

z1 float

x2 float

y2 float

z2 float

Returns
float

dist

float dist (float x1, 
                float y1, 
                float x2, 
                float y2)

Parameters
x1 float

y1 float

x2 float

y2 float

Returns
float

distanceToFurthestCorner

float distanceToFurthestCorner (float pointX, 
                float pointY, 
                float rectLeft, 
                float rectTop, 
                float rectRight, 
                float rectBottom)

Returns the furthest distance from the point defined by pointX and pointY to the four corners of the rectangle defined by rectLeft, rectTop, rectRight, and rectBottom.

The caller should ensure that the point and rectangle share the same coordinate space.

Parameters
pointX float

pointY float

rectLeft float

rectTop float

rectRight float

rectBottom float

Returns
float

dot

float dot (float v1x, 
                float v1y, 
                float v2x, 
                float v2y)

Parameters
v1x float

v1y float

v2x float

v2y float

Returns
float

eq

boolean eq (float a, 
                float b, 
                float epsilon)

Fuzzy equal to for floats.

Returns true if a is equal to b, allowing for epsilon error due to limitations in floating point accuracy.

Does not handle overflow, underflow, infinity, or NaN.

Parameters
a float

b float

epsilon float

Returns
boolean

exp

float exp (float a)

Parameters
a float

Returns
float

geq

boolean geq (float a, 
                float b, 
                float epsilon)

Fuzzy greater than or equal to for floats.

Returns true if a is greater than or equal to b, allowing for epsilon error due to limitations in floating point accuracy.

Does not handle overflow, underflow, infinity, or NaN.

Parameters
a float

b float

epsilon float

Returns
boolean

leq

boolean leq (float a, 
                float b, 
                float epsilon)

Fuzzy less than or equal to for floats.

Returns true if a is less than or equal to b, allowing for epsilon error due to limitations in floating point accuracy.

Does not handle overflow, underflow, infinity, or NaN.

Parameters
a float

b float

epsilon float

Returns
boolean

lerp

float lerp (float start, 
                float stop, 
                float amount)

Parameters
start float

stop float

amount float

Returns
float

log

float log (float a)

Parameters
a float

Returns
float

mag

float mag (float a, 
                float b, 
                float c)

Parameters
a float

b float

c float

Returns
float

mag

float mag (float a, 
                float b)

Parameters
a float

b float

Returns
float

map

float map (float minStart, 
                float minStop, 
                float maxStart, 
                float maxStop, 
                float value)

Parameters
minStart float

minStop float

maxStart float

maxStop float

value float

Returns
float

max

float max (float a, 
                float b, 
                float c, 
                float d)

Parameters
a float

b float

c float

d float

Returns
float

max

float max (int a, 
                int b)

Parameters
a int

b int

Returns
float

max

float max (int a, 
                int b, 
                int c, 
                int d)

Parameters
a int

b int

c int

d int

Returns
float

max

float max (int a, 
                int b, 
                int c)

Parameters
a int

b int

c int

Returns
float

max

float max (float a, 
                float b, 
                float c)

Parameters
a float

b float

c float

Returns
float

max

float max (float a, 
                float b)

Parameters
a float

b float

Returns
float

min

float min (float a, 
                float b, 
                float c)

Parameters
a float

b float

c float

Returns
float

min

float min (float a, 
                float b)

Parameters
a float

b float

Returns
float

min

float min (int a, 
                int b)

Parameters
a int

b int

Returns
float

min

float min (int a, 
                int b, 
                int c)

Parameters
a int

b int

c int

Returns
float

nearest

int nearest (int amount, 
                int low, 
                int high)

Parameters
amount int

low int

high int

Returns
int

nearest

float nearest (float amount, 
                float low, 
                float high)

Parameters
amount float

low float

high float

Returns
float

neq

boolean neq (float a, 
                float b, 
                float epsilon)

Fuzzy not equal to for floats.

Returns false if a is equal to b, allowing for epsilon error due to limitations in floating point accuracy.

Does not handle overflow, underflow, infinity, or NaN.

Parameters
a float

b float

epsilon float

Returns
boolean

norm

float norm (float start, 
                float stop, 
                float value)

Parameters
start float

stop float

value float

Returns
float

pow

float pow (float a, 
                float b)

Parameters
a float

b float

Returns
float

radians

float radians (float degrees)

Parameters
degrees float

Returns
float

sq

float sq (float v)

Parameters
v float

Returns
float

sqrt

float sqrt (float value)

Parameters
value float

Returns
float

tan

float tan (float angle)

Parameters
angle float

Returns
float