Class MathUtils
java.lang.Object
com.blackrook.gui.struct.MathUtils
A class with static methods that perform "other" types of mathematics.
A bunch of the collision/intersection algorithms found in this class are adapted from Real-Time Collision Detection by Christer Ericson (ISBN-13: 978-1-55860-732-3).
- Author:
- Matthew Tropiano
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final doublePI over Two.static final doubleThree PI over Two.static final doubleTwo PI. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic doubleclampValue(double val, double lo, double hi) Coerces a double to the range bounded by lo and hi.static floatclampValue(float val, float lo, float hi) Coerces a float to the range bounded by lo and hi.static intclampValue(int val, int lo, int hi) Coerces an integer to the range bounded by lo and hi.static shortclampValue(short val, short lo, short hi) Coerces a short to the range bounded by lo and hi.static doubledegToRad(double degrees) Converts degrees to radians.static doublegetInterpolationFactor(double value, double lo, double hi) Gets a scalar factor that equals how "far along" a value is along an interval.static booleangetIntersectionBox(double spx, double spy, double shw, double shh, double tpx, double tpy, double thw, double thh) Tests if two boxes intersect.static booleangetIntersectionCircleBox(double ccx, double ccy, double crad, double bcx, double bcy, double bhw, double bhh) Returns if a circle and box intersect.static doublegetLineLength(double x0, double y0, double x1, double y1) Returns the length of a line by the coordinates of the two points that comprise it.static doublegetLineLengthSquared(double x0, double y0, double x1, double y1) Returns the squared length of a line by the coordinates of the two points that comprise it.static doublegetVectorLengthSquared(double x, double y) Returns the squared length of a vector by its components.static doublelinearInterpolate(double factor, double x, double y) Gives a value that is the result of a linear interpolation between two values.static doubleradToDeg(double radians) Converts radians to degrees.
-
Field Details
-
PI_OVER_TWO
-
TWO_PI
-
THREE_PI_OVER_TWO
public static final double THREE_PI_OVER_TWOThree PI over Two. Equivalent to3.0 *.Math.PI/ 2.0- See Also:
-
-
Constructor Details
-
MathUtils
public MathUtils()
-
-
Method Details
-
radToDeg
public static double radToDeg(double radians) Converts radians to degrees.- Parameters:
radians- the input angle in radians.- Returns:
- the resultant angle in degrees.
-
degToRad
public static double degToRad(double degrees) Converts degrees to radians.- Parameters:
degrees- the input angle in degrees.- Returns:
- the resultant angle in radians.
-
clampValue
public static int clampValue(int val, int lo, int hi) Coerces an integer to the range bounded by lo and hi.
Example: clampValue(32,-16,16) returns 16.
Example: clampValue(4,-16,16) returns 4.
Example: clampValue(-1000,-16,16) returns -16.- Parameters:
val- the integer.lo- the lower bound.hi- the upper bound.- Returns:
- the value after being "forced" into the range.
-
clampValue
public static short clampValue(short val, short lo, short hi) Coerces a short to the range bounded by lo and hi.
Example: clampValue(32,-16,16) returns 16.
Example: clampValue(4,-16,16) returns 4.
Example: clampValue(-1000,-16,16) returns -16.- Parameters:
val- the short.lo- the lower bound.hi- the upper bound.- Returns:
- the value after being "forced" into the range.
-
clampValue
public static float clampValue(float val, float lo, float hi) Coerces a float to the range bounded by lo and hi.
Example: clampValue(32,-16,16) returns 16.
Example: clampValue(4,-16,16) returns 4.
Example: clampValue(-1000,-16,16) returns -16.- Parameters:
val- the float.lo- the lower bound.hi- the upper bound.- Returns:
- the value after being "forced" into the range.
-
clampValue
public static double clampValue(double val, double lo, double hi) Coerces a double to the range bounded by lo and hi.
Example: clampValue(32,-16,16) returns 16.
Example: clampValue(4,-16,16) returns 4.
Example: clampValue(-1000,-16,16) returns -16.- Parameters:
val- the double.lo- the lower bound.hi- the upper bound.- Returns:
- the value after being "forced" into the range.
-
linearInterpolate
public static double linearInterpolate(double factor, double x, double y) Gives a value that is the result of a linear interpolation between two values.- Parameters:
factor- the interpolation factor.x- the first value.y- the second value.- Returns:
- the interpolated value.
-
getInterpolationFactor
public static double getInterpolationFactor(double value, double lo, double hi) Gets a scalar factor that equals how "far along" a value is along an interval.- Parameters:
value- the value to test.lo- the lower value of the interval.hi- the higher value of the interval.- Returns:
- a value between 0 and 1 describing this distance (0 = beginning or less, 1 = end or greater), or 0 if lo and hi are equal.
-
getLineLength
public static double getLineLength(double x0, double y0, double x1, double y1) Returns the length of a line by the coordinates of the two points that comprise it.- Parameters:
x0- the first point's x-component.y0- the first point's y-component.x1- the second point's x-component.y1- the second point's y-component.- Returns:
- the length of the line.
-
getLineLengthSquared
public static double getLineLengthSquared(double x0, double y0, double x1, double y1) Returns the squared length of a line by the coordinates of the two points that comprise it.- Parameters:
x0- the first point's x-component.y0- the first point's y-component.x1- the second point's x-component.y1- the second point's y-component.- Returns:
- the length of the line.
-
getIntersectionCircleBox
public static boolean getIntersectionCircleBox(double ccx, double ccy, double crad, double bcx, double bcy, double bhw, double bhh) Returns if a circle and box intersect.- Parameters:
ccx- the circle center, x-coordinate.ccy- the circle center, y-coordinate.crad- the circle radius.bcx- the box center, x-coordinate.bcy- the box center, y-coordinate.bhw- the box half width.bhh- the box half height.- Returns:
- if an intersection occurred.
-
getIntersectionBox
public static boolean getIntersectionBox(double spx, double spy, double shw, double shh, double tpx, double tpy, double thw, double thh) Tests if two boxes intersect.- Parameters:
spx- the first box center, x-coordinate.spy- the first box center, y-coordinate.shw- the first box half width.shh- the first box half height.tpx- the second box center, x-coordinate.tpy- the second box center, y-coordinate.thw- the second box half width.thh- the second box half height.- Returns:
- true if an intersection occurred, false if not.
-
getVectorLengthSquared
public static double getVectorLengthSquared(double x, double y) Returns the squared length of a vector by its components.- Parameters:
x- the x-component.y- the y-component.- Returns:
- the length of the vector.
-