Package org.openpatch.scratch
Class Operators
java.lang.Object
org.openpatch.scratch.Operators
The Operators class provides a collection of static methods for performing various mathematical
operations. These operations include interpolation, constraint, finding minimum and maximum
values, mapping ranges, rounding, modulo, absolute value, floor, ceiling, square root,
trigonometric functions, inverse trigonometric functions, logarithms, and exponentiation.
Each method is designed to be a utility function that can be used without instantiating the Operators class. The methods cover operations for both double and int data types where applicable.
-
Method Summary
Modifier and TypeMethodDescriptionstatic final double
absOf
(double x) "abs" is an abbreviation for "absolute value." The absolute value of a number is its distance from 0.static final int
absOf
(int x) "abs" is an abbreviation for "absolute value." The absolute value of a number is its distance from 0.static final double
acosOf
(double x) "acos" is the abbreviation for "arccosine" and is also sometimes written as cos−1.static final double
asinOf
(double x) "asin" is the abbreviation for "arcsine" and is also sometimes written as sin−1.static final double
atanOf
(double x) "atan" is the abbreviation for "arctangent" and is also sometimes written as tan−1.static final double
ceilingOf
(double x) This always rounds the number up to the least whole number greater than or equal to the number.static final double
constrain
(double amt, double low, double high) Constrains a value to not exceed a maximum and minimum value.static final int
constrain
(int amt, int low, int high) Constrains a value to not exceed a maximum and minimum value.static final double
cosOf
(double x) "cos" is the abbreviation for "cosine." The cosine of an angle is the ratio between the length of the side adjacent (next to) it on the triangle and the length of the hypotenuse.static final double
eToThePowerOf
(double x) "e" is an abbreviation for "Euler's number", which is about 2.718.static final double
floorOf
(double x) This always rounds the number down to the greatest whole number less than or equal to the number.static final double
lerp
(double start, double stop, double amt) Calculates a number between two numbers at a specific increment.static final double
lnOf
(double x) ln is "natural log." It figures out how many times e would have to be multiplied by itself to get the value.static final double
logOf
(double x) "Log" is short for "logarithm." The log function figures out how many times 10 must be multiplied by itself to get the value.static final double
map
(double value, double start1, double stop1, double start2, double stop2) Re-maps a number from one range to another.
In the first example above, the number 25 is converted from a value in the range of 0 to 100 into a value that ranges from the left edge of the window (0) to the right edge (width).
As shown in the second example, numbers outside the range are not clamped to the minimum and maximum parameters values, because out-of-range values are often intentional and useful.static final double
max
(double... v) Determines the largest value in a sequence of numbers, and then returns that value.static final int
max
(int... v) Determines the largest value in a sequence of numbers, and then returns that value.static final double
min
(double... v) Determines the smallest value in a sequence of numbers, and then returns that value.static final int
min
(int... v) Determines the smallest value in a sequence of numbers, and then returns that value.static final double
mod
(double x, double y) "mod" is an abbreviation for "modulo".static final int
mod
(int x, int y) "mod" is an abbreviation for "modulo".static final int
round
(double x) The standard rules of rounding are followed; decimals that are .5 or higher are rounded up, whereas decimals less than .5 are rounded down.static final double
sinOf
(double x) "sin" is an abbreviation for "sine." The sine of an angle is the ratio between the length of the side that is opposite (across) the triangle from it and the length of the hypotenuse (the side that is across from the right angle).static final double
sqrtOf
(double x) "sqrt" is an abbreviation for "square root." A number that is squared is multiplied by itself.static final double
tanOf
(double x) "tan" is the abbreviation for "tangent." The tangent of an angle is the ratio between the length of the side adjacent to it and the side opposite of it.static final double
tenToThePowerOf
(double x) The 10^ function multiplies 10 times itself the value number of times.
-
Method Details
-
lerp
public static final double lerp(double start, double stop, double amt) Calculates a number between two numbers at a specific increment. The amt parameter is the amount to interpolate between the two values where 0.0 equal to the first point, 0.1 is very near the first point, 0.5 is half-way in between, etc. The lerp function is convenient for creating motion along a straight path and for drawing dotted lines.- Parameters:
start
- first valuestop
- second valueamt
- double between 0.0 and 1.0- Returns:
- the interpolated value
-
constrain
public static final double constrain(double amt, double low, double high) Constrains a value to not exceed a maximum and minimum value.- Parameters:
amt
- the value to constrainlow
- minimum limithigh
- maximum limit
-
constrain
public static final int constrain(int amt, int low, int high) Constrains a value to not exceed a maximum and minimum value.- Parameters:
amt
- the value to constrainlow
- minimum limithigh
- maximum limit
-
min
public static final double min(double... v) Determines the smallest value in a sequence of numbers, and then returns that value. min() accepts either two or three double or int values as parameters, or an array of any length.- Parameters:
v
- values- Returns:
- the minimum
-
max
public static final double max(double... v) Determines the largest value in a sequence of numbers, and then returns that value. max() accepts either two or three double or int values as parameters, or an array of any length.- Parameters:
v
- values- Returns:
- the maximum
-
min
public static final int min(int... v) Determines the smallest value in a sequence of numbers, and then returns that value. min() accepts either two or three double or int values as parameters, or an array of any length.- Parameters:
v
- values- Returns:
- the minimum
-
max
public static final int max(int... v) Determines the largest value in a sequence of numbers, and then returns that value. max() accepts either two or three double or int values as parameters, or an array of any length.- Parameters:
v
- values- Returns:
- the maximum
-
map
public static final double map(double value, double start1, double stop1, double start2, double stop2) Re-maps a number from one range to another.
In the first example above, the number 25 is converted from a value in the range of 0 to 100 into a value that ranges from the left edge of the window (0) to the right edge (width).
As shown in the second example, numbers outside the range are not clamped to the minimum and maximum parameters values, because out-of-range values are often intentional and useful.- Parameters:
value
- the incoming value to be convertedstart1
- lower bound of the value's current rangestop1
- upper bound of the value's current rangestart2
- lower bound of the value's target rangestop2
- upper bound of the value's target range- Returns:
- the value mapped to the new range
-
round
public static final int round(double x) The standard rules of rounding are followed; decimals that are .5 or higher are rounded up, whereas decimals less than .5 are rounded down.- Parameters:
x
- a value- Returns:
- the rounded value
-
mod
public static final double mod(double x, double y) "mod" is an abbreviation for "modulo". Modulo returns the remainder when the first input is divided by the second input.- Parameters:
x
- first valuey
- second value- Returns:
- reminder of first value divided by second value
-
mod
public static final int mod(int x, int y) "mod" is an abbreviation for "modulo". Modulo returns the remainder when the first input is divided by the second input.- Parameters:
x
- first valuey
- second value- Returns:
- reminder of first value divided by second value
-
absOf
public static final int absOf(int x) "abs" is an abbreviation for "absolute value." The absolute value of a number is its distance from 0. A simpler way to describe an absolute value is that it makes any number positive. If it is negative, it becomes positive, and if it is positive, it stays the same.- Parameters:
x
- a value- Returns:
- the absolute value
-
absOf
public static final double absOf(double x) "abs" is an abbreviation for "absolute value." The absolute value of a number is its distance from 0. A simpler way to describe an absolute value is that it makes any number positive. If it is negative, it becomes positive, and if it is positive, it stays the same.- Parameters:
x
- a value- Returns:
- the absolute value
-
floorOf
public static final double floorOf(double x) This always rounds the number down to the greatest whole number less than or equal to the number. For example, floor(1.73) = 1 and floor(-2.74) = -3.- Parameters:
x
- a value- Returns:
- the floor of that value
-
ceilingOf
public static final double ceilingOf(double x) This always rounds the number up to the least whole number greater than or equal to the number. For example, ceiling(3.14) = 4 and ceiling(7.68) = 8.- Parameters:
x
- a value- Returns:
- the ceiling of that value
-
sqrtOf
public static final double sqrtOf(double x) "sqrt" is an abbreviation for "square root." A number that is squared is multiplied by itself. For example, when 2 is squared, the answer is 2×2, which is 4. When a number is square rooted, the answer is the number that was squared to get it. So, the square root of 4 is 2. Similarly, the square root of 2 is about 1.414213562373095 because 1.4142135623730952 (1.414213562373095 × 1.414213562373095) is close to 2.- Parameters:
x
- a value- Returns:
- the square root of the value
-
sinOf
public static final double sinOf(double x) "sin" is an abbreviation for "sine." The sine of an angle is the ratio between the length of the side that is opposite (across) the triangle from it and the length of the hypotenuse (the side that is across from the right angle). In the picture above, the sine of angle A is equal to side "opposite" divided by side "hypotenuse".- Parameters:
x
- an angle between [0,...,360]- Returns:
- the sin of the angle
-
cosOf
public static final double cosOf(double x) "cos" is the abbreviation for "cosine." The cosine of an angle is the ratio between the length of the side adjacent (next to) it on the triangle and the length of the hypotenuse. In the picture above, the cosine of angle A is equal to side "adjacent" divided by side "hypotenuse".- Parameters:
x
- an angle between [0, ..., 360]- Returns:
- the cos of the angle
-
tanOf
public static final double tanOf(double x) "tan" is the abbreviation for "tangent." The tangent of an angle is the ratio between the length of the side adjacent to it and the side opposite of it. In the picture above, the tangent of angle A is equal to side "opposite" divided by side "adjacent".- Parameters:
x
- an angle between [0, ..., 360]- Returns:
- the tan of the angle
-
asinOf
public static final double asinOf(double x) "asin" is the abbreviation for "arcsine" and is also sometimes written as sin−1. When given the ratio (in decimal form) of the length of the opposite side and hypotenuse of a right triangle, it finds the angle.- Parameters:
x
- a value- Returns:
- the angle of that value
-
acosOf
public static final double acosOf(double x) "acos" is the abbreviation for "arccosine" and is also sometimes written as cos−1. When given the ratio (in decimal form) of the length of the adjacent side and hypotenuse of a right triangle, it finds the angle.- Parameters:
x
- a value- Returns:
- the angle of that value
-
atanOf
public static final double atanOf(double x) "atan" is the abbreviation for "arctangent" and is also sometimes written as tan−1. When given the ratio (in decimal form) of the length of the opposite side and adjacent side of a right triangle, it finds the angle.- Parameters:
x
- a value- Returns:
- the angle of that value
-
lnOf
public static final double lnOf(double x) ln is "natural log." It figures out how many times e would have to be multiplied by itself to get the value. For example, if the value was 148.4, the answer would be about 5 because e5 (e×e×e×e×e) is around 148.4.- Parameters:
x
- a value- Returns:
- the ln of that value
-
logOf
public static final double logOf(double x) "Log" is short for "logarithm." The log function figures out how many times 10 must be multiplied by itself to get the value. For example, if the value is 100, the answer is 2 because 10×10 is 100.- Parameters:
x
- a value- Returns:
- the log to base 10 of that value
-
eToThePowerOf
public static final double eToThePowerOf(double x) "e" is an abbreviation for "Euler's number", which is about 2.718. With the e^ function, e is multiplied by itself the value number of times. For example, if the value is 3, the answer would be e3, or e×e×e, which is about 20.086.- Parameters:
x
- a value- Returns:
- e to the power of that value
-
tenToThePowerOf
public static final double tenToThePowerOf(double x) The 10^ function multiplies 10 times itself the value number of times. For example, if the value was 6, the answer would be 106 (that is 10×10×10×10×10×10), which is 1,000,000.- Parameters:
x
- a value- Returns:
- 10 to the power of that value
-