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 dist(float x1, float y1, float x2, float y2)

Returns the distance between two points.

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 floorMod(float x, int y)

This returns as similar as Math.floorMod(int, int).

static int floorMod(int x, int y)

This is same as Math.floorMod(int, int).

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

Fuzzy greater than or equal to for floats.

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

Returns the linear interpolation of amount between start and stop.

Inherited methods

Constants

DEFAULT_EPSILON

public static final float DEFAULT_EPSILON

Default epsilon value for fuzzy float comparisons.

Constant Value: 1.0E-4

Public methods

dist

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

Returns the distance between two points.

Parameters
x1 float

y1 float

x2 float

y2 float

Returns
float

distanceToFurthestCorner

public 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.

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

floorMod

public static float floorMod (float x, 
                int y)

This returns as similar as Math.floorMod(int, int). Instead it works for float type values. And the re-implementation makes it back compatible to API<24.

Parameters
x float

y int

Returns
float

floorMod

public static int floorMod (int x, 
                int y)

This is same as Math.floorMod(int, int). Re-implementation makes it back compatible to API<24.

Parameters
x int

y int

Returns
int

geq

public static 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

lerp

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

Returns the linear interpolation of amount between start and stop.

Parameters
start float

stop float

amount float

Returns
float