Stay organized with collections
Save and categorize content based on your preferences.
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 |
From class
java.lang.Object
Object
|
clone()
|
boolean
|
equals(Object arg0)
|
void
|
finalize()
|
final
Class<?>
|
getClass()
|
int
|
hashCode()
|
final
void
|
notify()
|
final
void
|
notifyAll()
|
String
|
toString()
|
final
void
|
wait(long arg0, int arg1)
|
final
void
|
wait(long arg0)
|
final
void
|
wait()
|
|
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 |
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 |
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.
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.
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 |
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 |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[],null,["# MathUtils\n\nSummary: [Constants](#constants) \\| [Methods](#pubmethods) \\| [Inherited Methods](#inhmethods) \n\nMathUtils\n=========\n\n\n`\npublic\n\nfinal\n\nclass\nMathUtils\n`\n\n\n`\n\nextends Object\n\n\n`\n\n`\n\n\n`\n\n|---|--------------------------------------------|\n| java.lang.Object ||\n| ↳ | com.google.android.material.math.MathUtils |\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\nA class that contains utility methods related to numbers.\n\nSummary\n-------\n\n| ### Constants ||\n|---------|---------------------------------------------------------------------------------------------------------------------------------------------|\n| `float` | [DEFAULT_EPSILON](/reference/com/google/android/material/math/MathUtils#DEFAULT_EPSILON) Default epsilon value for fuzzy float comparisons. |\n\n| ### Public methods ||\n|-------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| ` static float` | ` `[dist](/reference/com/google/android/material/math/MathUtils#dist(float,%20float,%20float,%20float))`(float x1, float y1, float x2, float y2) ` Returns the distance between two points. |\n| ` static float` | ` `[distanceToFurthestCorner](/reference/com/google/android/material/math/MathUtils#distanceToFurthestCorner(float,%20float,%20float,%20float,%20float,%20float))`(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. |\n| ` static float` | ` `[floorMod](/reference/com/google/android/material/math/MathUtils#floorMod(float,%20int))`(float x, int y) ` This returns as similar as [Math.floorMod(int, int)](/reference/java/lang/Math#floorMod(int,%20int)). |\n| ` static int` | ` `[floorMod](/reference/com/google/android/material/math/MathUtils#floorMod(int,%20int))`(int x, int y) ` This is same as [Math.floorMod(int, int)](/reference/java/lang/Math#floorMod(int,%20int)). |\n| ` static boolean` | ` `[geq](/reference/com/google/android/material/math/MathUtils#geq(float,%20float,%20float))`(float a, float b, float epsilon) ` Fuzzy greater than or equal to for floats. |\n| ` static float` | ` `[lerp](/reference/com/google/android/material/math/MathUtils#lerp(float,%20float,%20float))`(float start, float stop, float amount) ` Returns the linear interpolation of `amount` between `start` and `stop`. |\n\n| ### Inherited methods |\n|-----------------------|---|\n| From class ` java.lang.Object ` |-------------------|-------------------------------| | ` Object` | ` clone() ` | | ` boolean` | ` equals(Object arg0) ` | | ` void` | ` finalize() ` | | ` final Class\u003c?\u003e` | ` getClass() ` | | ` int` | ` hashCode() ` | | ` final void` | ` notify() ` | | ` final void` | ` notifyAll() ` | | ` String` | ` toString() ` | | ` final void` | ` wait(long arg0, int arg1) ` | | ` final void` | ` wait(long arg0) ` | | ` final void` | ` wait() ` | ||\n\nConstants\n---------\n\n### DEFAULT_EPSILON\n\n```\npublic static final float DEFAULT_EPSILON\n```\n\nDefault epsilon value for fuzzy float comparisons.\n\n\u003cbr /\u003e\n\nConstant Value:\n\n1.0E-4\n\n\nPublic methods\n--------------\n\n### dist\n\n```\npublic static float dist (float x1, \n float y1, \n float x2, \n float y2)\n```\n\nReturns the distance between two points.\n\n\u003cbr /\u003e\n\n| Parameters ||\n|------|----------------|\n| `x1` | `float` \u003cbr /\u003e |\n| `y1` | `float` \u003cbr /\u003e |\n| `x2` | `float` \u003cbr /\u003e |\n| `y2` | `float` \u003cbr /\u003e |\n\n| Returns ||\n|---------|--------|\n| `float` | \u003cbr /\u003e |\n\n### distanceToFurthestCorner\n\n```\npublic static float distanceToFurthestCorner (float pointX, \n float pointY, \n float rectLeft, \n float rectTop, \n float rectRight, \n float rectBottom)\n```\n\nReturns the furthest distance from the point defined by pointX and pointY to the four corners\nof the rectangle defined by rectLeft, rectTop, rectRight, and rectBottom.\n\nThe caller should ensure that the point and rectangle share the same coordinate space.\n\n\u003cbr /\u003e\n\n| Parameters ||\n|--------------|----------------|\n| `pointX` | `float` \u003cbr /\u003e |\n| `pointY` | `float` \u003cbr /\u003e |\n| `rectLeft` | `float` \u003cbr /\u003e |\n| `rectTop` | `float` \u003cbr /\u003e |\n| `rectRight` | `float` \u003cbr /\u003e |\n| `rectBottom` | `float` \u003cbr /\u003e |\n\n| Returns ||\n|---------|--------|\n| `float` | \u003cbr /\u003e |\n\n### floorMod\n\n```\npublic static float floorMod (float x, \n int y)\n```\n\nThis returns as similar as [Math.floorMod(int, int)](/reference/java/lang/Math#floorMod(int,%20int)). Instead it works for float type\nvalues. And the re-implementation makes it back compatible to API\\\u003c24.\n\n\u003cbr /\u003e\n\n| Parameters ||\n|-----|----------------|\n| `x` | `float` \u003cbr /\u003e |\n| `y` | `int` \u003cbr /\u003e |\n\n| Returns ||\n|---------|--------|\n| `float` | \u003cbr /\u003e |\n\n### floorMod\n\n```\npublic static int floorMod (int x, \n int y)\n```\n\nThis is same as [Math.floorMod(int, int)](/reference/java/lang/Math#floorMod(int,%20int)). Re-implementation makes it back compatible to\nAPI\\\u003c24.\n\n\u003cbr /\u003e\n\n| Parameters ||\n|-----|--------------|\n| `x` | `int` \u003cbr /\u003e |\n| `y` | `int` \u003cbr /\u003e |\n\n| Returns ||\n|-------|--------|\n| `int` | \u003cbr /\u003e |\n\n### geq\n\n```\npublic static boolean geq (float a, \n float b, \n float epsilon)\n```\n\nFuzzy greater than or equal to for floats.\n\nReturns true if `a` is greater than or equal to `b`, allowing for `epsilon` error due to limitations in floating point accuracy.\n\nDoes not handle overflow, underflow, infinity, or NaN.\n\n\u003cbr /\u003e\n\n| Parameters ||\n|-----------|----------------|\n| `a` | `float` \u003cbr /\u003e |\n| `b` | `float` \u003cbr /\u003e |\n| `epsilon` | `float` \u003cbr /\u003e |\n\n| Returns ||\n|-----------|--------|\n| `boolean` | \u003cbr /\u003e |\n\n### lerp\n\n```\npublic static float lerp (float start, \n float stop, \n float amount)\n```\n\nReturns the linear interpolation of `amount` between `start` and `stop`.\n\n\u003cbr /\u003e\n\n| Parameters ||\n|----------|----------------|\n| `start` | `float` \u003cbr /\u003e |\n| `stop` | `float` \u003cbr /\u003e |\n| `amount` | `float` \u003cbr /\u003e |\n\n| Returns ||\n|---------|--------|\n| `float` | \u003cbr /\u003e |"]]