Package gov.nasa.worldwind.util
Class WWMath
- java.lang.Object
-
- gov.nasa.worldwind.util.WWMath
-
public class WWMath extends java.lang.Object
Collection of static methods for performing common WorldWind computations.
-
-
Constructor Summary
Constructors Constructor Description WWMath()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Viewport
boundingRectForUnitSquare(Matrix4 unitSquareTransform, Viewport result)
Computes the bounding rectangle for a unit square after applying a transformation matrix to the square's four corners.static double
clamp(double value, double min, double max)
Restricts a value to the range [min, max] degrees, clamping values outside the range.static double
clampAngle180(double degrees)
Restricts an angle to the range [-180, +180] degrees, clamping angles outside the range.static double
clampAngle360(double degrees)
Restricts an angle to the range [0, 360] degrees, clamping angles outside the range.static double
fract(double value)
Returns the fractional part of a specified numberstatic double
interpolate(double amount, double value1, double value2)
Computes the linear interpolation of two values according to a specified fractional amount.static double
interpolateAngle180(double amount, double degrees1, double degrees2)
Computes the linear interpolation of two angles in the range [-180, +180] degrees according to a specified fractional amount.static double
interpolateAngle360(double amount, double degrees1, double degrees2)
Computes the linear interpolation of two angles in the range [0, 360] degrees according to a specified fractional amount.static boolean
isPowerOfTwo(int value)
Indicates whether a specified value is a power of two.static int
mod(int value, int modulus)
Returns the integer modulus of a specified number.static double
normalizeAngle180(double degrees)
Restricts an angle to the range [-180, +180] degrees, wrapping angles outside the range.static double
normalizeAngle360(double degrees)
Restricts an angle to the range [0, 360] degrees, wrapping angles outside the range.static int
powerOfTwoCeiling(int value)
Returns the value that is the nearest power of 2 greater than or equal to the given value.
-
-
-
Method Detail
-
clamp
public static double clamp(double value, double min, double max)
Restricts a value to the range [min, max] degrees, clamping values outside the range. Values less than min are returned as min, and values greater than max are returned as max. Values within the range are returned unmodified.
The result of this method is undefined if min is greater than max.- Parameters:
value
- the values to clampmin
- the minimum valuemax
- the maximum value- Returns:
- the specified values clamped to the range [min, max] degrees
-
clampAngle180
public static double clampAngle180(double degrees)
Restricts an angle to the range [-180, +180] degrees, clamping angles outside the range. Angles less than -180 are returned as -180, and angles greater than +180 are returned as +180. Angles within the range are returned unmodified.- Parameters:
degrees
- the angle to clamp in degrees- Returns:
- the specified angle clamped to the range [-180, +180] degrees
-
clampAngle360
public static double clampAngle360(double degrees)
Restricts an angle to the range [0, 360] degrees, clamping angles outside the range. Angles less than 0 are returned as 0, and angles greater than 360 are returned as 360. Angles within the range are returned unmodified.- Parameters:
degrees
- the angle to clamp in degrees- Returns:
- the specified angle clamped to the range [0, 360] degrees
-
fract
public static double fract(double value)
Returns the fractional part of a specified number- Parameters:
value
- the number whose fractional part to compute- Returns:
- The fractional part of the specified number: value - floor(value)
-
interpolate
public static double interpolate(double amount, double value1, double value2)
Computes the linear interpolation of two values according to a specified fractional amount. The fractional amount is interpreted as a relative proportion of the two values, where 0.0 indicates the first value, 0.5 indicates a 50/50 mix of the two values, and 1.0 indicates the second value.
The result of this method is undefined if the amount is outside the range [0, 1].- Parameters:
amount
- the fractional proportion of the two values in the range [0, 1]value1
- the first valuevalue2
- the second value- Returns:
- the interpolated value
-
interpolateAngle180
public static double interpolateAngle180(double amount, double degrees1, double degrees2)
Computes the linear interpolation of two angles in the range [-180, +180] degrees according to a specified fractional amount. The fractional amount is interpreted as a relative proportion of the two angles, where 0.0 indicates the first angle, 0.5 indicates an angle half way between the two angles, and 1.0 indicates the second angle.
The result of this method is undefined if the amount is outside the range [0, 1].- Parameters:
amount
- the fractional proportion of the two angles in the range [0, 1]degrees1
- the first angle in degreesdegrees2
- the second angle in degrees- Returns:
- the interpolated angle in the range [-180, +180] degrees
-
interpolateAngle360
public static double interpolateAngle360(double amount, double degrees1, double degrees2)
Computes the linear interpolation of two angles in the range [0, 360] degrees according to a specified fractional amount. The fractional amount is interpreted as a relative proportion of the two angles, where 0.0 indicates the first angle, 0.5 indicates an angle half way between the two angles, and 1.0 indicates the second angle.
The result of this method is undefined if the amount is outside the range [0, 1].- Parameters:
amount
- the fractional proportion of the two angles in the range [0, 1]degrees1
- the first angle in degreesdegrees2
- the second angle in degrees- Returns:
- the interpolated angle in the range [0, 360] degrees
-
mod
public static int mod(int value, int modulus)
Returns the integer modulus of a specified number. This differs from the % operator in that the result is always positive when the modulus is positive. For example -1 % 10 = -1, whereas mod(-1, 10) = 1.- Parameters:
value
- the integer number whose modulus to computemodulus
- the modulus- Returns:
- the remainder after dividing the number by the modulus
-
normalizeAngle180
public static double normalizeAngle180(double degrees)
Restricts an angle to the range [-180, +180] degrees, wrapping angles outside the range. Wrapping takes place as though traversing the edge of a unit circle; angles less than -180 wrap back to +180, while angles greater than +180 wrap back to -180.- Parameters:
degrees
- the angle to wrap in degrees- Returns:
- the specified angle wrapped to [-180, +180] degrees
-
normalizeAngle360
public static double normalizeAngle360(double degrees)
Restricts an angle to the range [0, 360] degrees, wrapping angles outside the range. Wrapping takes place as though traversing the edge of a unit circle; angles less than 0 wrap back to 360, while angles greater than 360 wrap back to 0.- Parameters:
degrees
- the angle to wrap in degrees- Returns:
- the specified angle wrapped to [0, 360] degrees
-
boundingRectForUnitSquare
public static Viewport boundingRectForUnitSquare(Matrix4 unitSquareTransform, Viewport result)
Computes the bounding rectangle for a unit square after applying a transformation matrix to the square's four corners.- Parameters:
unitSquareTransform
- the matrix to apply to the unit squareresult
- a pre-allocated Viewport in which to return the computed bounding rectangle- Returns:
- the result argument set to the computed bounding rectangle
-
isPowerOfTwo
public static boolean isPowerOfTwo(int value)
Indicates whether a specified value is a power of two.- Parameters:
value
- the value to test- Returns:
- true if the specified value is a power of two, false othwerwise
-
powerOfTwoCeiling
public static int powerOfTwoCeiling(int value)
Returns the value that is the nearest power of 2 greater than or equal to the given value.- Parameters:
value
- the reference value. The power of 2 returned is greater than or equal to this value.- Returns:
- the value that is the nearest power of 2 greater than or equal to the reference value
-
-