PulpCore

pulpcore.math
Class CoreMath

java.lang.Object
  extended by pulpcore.math.CoreMath

public class CoreMath
extends Object

The CoreMath class contains fixed-point arithmetic functions and other useful math functions.

Fixed-point numbers can be used in place of floating-point numbers. Regarding fixed-point numbers:

Addition and subtraction of two fixed-point numbers is done normally, using + and - operators.

Multiplying a fixed-point number by an integer is done normally, using the * operator. The result is a fixed-point number.

Dividing a fixed-point number by an integer (the fixed-point number is the numerator, and the integer is the denominator) is done normally, using the / operator. The result is a fixed-point number.


Field Summary
static int E
           
static int FRACTION_BITS
           
static int FRACTION_MASK
           
static double MAX_DOUBLE_VALUE
          The maximum 64-bit floating-point value that a 32-bit fixed-point value can represent.
static float MAX_FLOAT_VALUE
          The maximum 32-bit floating-point value that a 32-bit fixed-point value can represent.
static int MAX_INT_VALUE
          The maximum integer value that a 32-bit fixed-point value can represent.
static int MAX_VALUE
           
static double MIN_DOUBLE_VALUE
          The minumum 64-bit floating-point value that a 32-bit fixed-point value can represent.
static float MIN_FLOAT_VALUE
          The minumum 32-bit floating-point value that a 32-bit fixed-point value can represent.
static int MIN_INT_VALUE
          The minumum integer value that a 32-bit fixed-point value can represent.
static int MIN_VALUE
           
static int ONE
           
static int ONE_HALF
           
static int ONE_HALF_PI
           
static int PI
           
static int TWO_PI
           
 
Method Summary
static int abs(int n)
          Returns the absolute value of a number.
static int acos(int fx)
          Returns the arccosine of the specified fixed-point value.
static int asin(int fx)
          Returns the arcsine of the specified fixed-point value.
static int atan(int fx)
          Returns the arctangent of the specified fixed-point value.
static int atan2(int fy, int fx)
          Returns in the range from -pi to pi.
static int ceil(int f)
          Returns the ceil of a fixed-point value.
static double clamp(double n, double min, double max)
          Clamps a number between two values.
static float clamp(float n, float min, float max)
          Clamps a number between two values.
static int clamp(int n, int min, int max)
          Clamps a number between two values.
static int cos(int fx)
          Returns the cosine of the specified fixed-point radian value.
static int cosineInterpolate(int n1, int n2, int f)
          Performs a 1-dimensional cosine interpolation between values n1 and n2.
static int cot(int fx)
          Returns the cotangent of the specified fixed-point radian value.
static int countBits(int n)
          Counts the number of "on" bits in an integer.
static int cubicInterpolate(int n0, int n1, int n2, int n3, int f)
          Performs a 1-dimensional cubic interpolation between values n1 and n2.
static long dist(int x1, int y1, int x2, int y2)
           
static int div(int f1, int f2)
          Divides the first fixed-point number by the second fixed-point number.
static long div(long f1, long f2)
          Divides the first fixed-point number by the second fixed-point number.
static int floor(int f)
          Returns the floor of a fixed-point value (removes the fractional part).
static int fracPart(int f)
          Returns the fractional part of a fixed-point value (removes the integer part).
static int intDivCeil(int n, int d)
          Divides the number, n, by the divisor, d, returning the nearest integer greater than or equal to the result.
static int intDivFloor(int n, int d)
          Divides the number, n, by the divisor, d, returning the nearest integer less than or equal to the result.
static int intDivRound(int n, int d)
          Divides the number, n, by the divisor, d, rounding the result to the nearest integer.
static int interpolate(int n1, int n2, int f)
          Performs a 1-dimensional linear interpolation between values n1 and n2.
static int interpolatedNoise(int fx)
           
static int interpolatedNoise(int fx, int fy)
           
static String intToString(int n)
          Converts an integer to a base-10 string representation.
static String intToString(int n, int numFractionalDigits)
          Converts a integer to a base-10 string representation using the specified number of fractional digits.
static String intToString(int n, int minFracDigits, int maxFracDigits, boolean grouping)
          Converts an integer to a base-10 string representation.
static boolean isPowerOfTwo(int n)
          Returns true if the number (greater than 1) is a power of two.
static int log2(int n)
          Returns the log base 2 of an integer greater than 0.
static int mul(int f1, int f2)
          Multiplies two fixed-point numbers together.
static long mul(long f1, long f2)
          Multiplies two fixed-point numbers together.
static int mulDiv(int f1, int f2, int f3)
          Multiplies the first two fixed-point numbers together, then divides by the third fixed-point number.
static long mulDiv(long f1, long f2, long f3)
          Multiplies the first two fixed-point numbers together, then divides by the third fixed-point number.
static int noise(int n)
           
static int noise(int x, int y)
           
static int perlinNoise(int fx, int persistence, int numOctaves)
           
static int perlinNoise(int fx, int fy, int persistence, int numOctaves)
           
static int quickCurveInterpolate(int n1, int n2, int f)
           
static boolean rand()
          Returns a random boolean.
static double rand(double max)
          Returns a random double from 0 to max, inclusive
static double rand(double min, double max)
          Returns a random double from min to max, inclusive
static int rand(int max)
          Returns a random integer from 0 to max, inclusive
static int rand(int min, int max)
          Returns a random integer from min to max, inclusive
static boolean randChance(int percent)
          Returns true if a random event occurs.
static int round(int f)
          Returns the fixed-point value rounded to the nearest integer location.
static int sign(double n)
          Returns the sign of a number.
static int sign(int n)
          Returns the sign of a number.
static int sin(int fx)
          Returns the sine of the specified fixed-point radian value.
static int smoothNoise(int x)
           
static int smoothNoise(int x, int y)
           
static int sqrt(int fx)
           
static long sqrt(long fx)
           
static int tan(int fx)
          Returns the tangent of the specified fixed-point radian value.
static double toDouble(int f)
          Converts a fixed-point value to a double.
static int toFixed(double n)
          Converts a double-percsion float to a fixed-point value.
static int toFixed(float n)
          Converts a float to a fixed-point value.
static int toFixed(int n)
          Converts an integer to a fixed-point value.
static int toFixed(String n)
          Converts a String representing a double-percsion float into a fixed-point value.
static float toFloat(int f)
          Converts a fixed-point value to a float.
static int toInt(int f)
          Converts a fixed-point value to an integer.
static int toIntCeil(int f)
          Converts a fixed-point value to an integer.
static int toIntFloor(int f)
          Converts a fixed-point value to an integer.
static int toIntRound(int f)
          Converts a fixed-point value to an integer.
static String toString(int f)
          Converts a fixed-point number to a base-10 string representation.
static String toString(int f, int numFractionalDigits)
          Converts a fixed-point number to a base-10 string representation using the specified number of fractional digits.
static String toString(int f, int minFracDigits, int maxFracDigits, boolean grouping)
          Converts a fixed-point number to a base-10 string representation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FRACTION_BITS

public static final int FRACTION_BITS
See Also:
Constant Field Values

FRACTION_MASK

public static final int FRACTION_MASK
See Also:
Constant Field Values

ONE

public static final int ONE
See Also:
Constant Field Values

ONE_HALF

public static final int ONE_HALF
See Also:
Constant Field Values

PI

public static final int PI

TWO_PI

public static final int TWO_PI

ONE_HALF_PI

public static final int ONE_HALF_PI

E

public static final int E

MAX_VALUE

public static final int MAX_VALUE
See Also:
Constant Field Values

MIN_VALUE

public static final int MIN_VALUE
See Also:
Constant Field Values

MAX_INT_VALUE

public static final int MAX_INT_VALUE
The maximum integer value that a 32-bit fixed-point value can represent.

See Also:
Constant Field Values

MIN_INT_VALUE

public static final int MIN_INT_VALUE
The minumum integer value that a 32-bit fixed-point value can represent.

See Also:
Constant Field Values

MAX_FLOAT_VALUE

public static final float MAX_FLOAT_VALUE
The maximum 32-bit floating-point value that a 32-bit fixed-point value can represent.

See Also:
Constant Field Values

MIN_FLOAT_VALUE

public static final float MIN_FLOAT_VALUE
The minumum 32-bit floating-point value that a 32-bit fixed-point value can represent.

See Also:
Constant Field Values

MAX_DOUBLE_VALUE

public static final double MAX_DOUBLE_VALUE
The maximum 64-bit floating-point value that a 32-bit fixed-point value can represent.

See Also:
Constant Field Values

MIN_DOUBLE_VALUE

public static final double MIN_DOUBLE_VALUE
The minumum 64-bit floating-point value that a 32-bit fixed-point value can represent.

See Also:
Constant Field Values
Method Detail

toFixed

public static final int toFixed(int n)
Converts an integer to a fixed-point value.


toFixed

public static final int toFixed(float n)
Converts a float to a fixed-point value.


toFixed

public static final int toFixed(double n)
Converts a double-percsion float to a fixed-point value.


toFixed

public static final int toFixed(String n)
                         throws NumberFormatException
Converts a String representing a double-percsion float into a fixed-point value.

Throws:
NumberFormatException - if the string does not contain a parsable number.

toFloat

public static final float toFloat(int f)
Converts a fixed-point value to a float.


toDouble

public static final double toDouble(int f)
Converts a fixed-point value to a double.


toInt

public static final int toInt(int f)
Converts a fixed-point value to an integer. Same behavior as casting a float to an int.


toIntFloor

public static final int toIntFloor(int f)
Converts a fixed-point value to an integer.


toIntRound

public static final int toIntRound(int f)
Converts a fixed-point value to an integer.


toIntCeil

public static final int toIntCeil(int f)
Converts a fixed-point value to an integer.


fracPart

public static final int fracPart(int f)
Returns the fractional part of a fixed-point value (removes the integer part).


floor

public static final int floor(int f)
Returns the floor of a fixed-point value (removes the fractional part).


ceil

public static final int ceil(int f)
Returns the ceil of a fixed-point value.


round

public static final int round(int f)
Returns the fixed-point value rounded to the nearest integer location.


toString

public static final String toString(int f)
Converts a fixed-point number to a base-10 string representation.


toString

public static final String toString(int f,
                                    int numFractionalDigits)
Converts a fixed-point number to a base-10 string representation using the specified number of fractional digits.


toString

public static String toString(int f,
                              int minFracDigits,
                              int maxFracDigits,
                              boolean grouping)
Converts a fixed-point number to a base-10 string representation.

Parameters:
f - the fixed-point number
minFracDigits - the minimum number of digits to show after the decimal point.
maxFracDigits - the maximum number of digits to show after the decimal point.
grouping - if (true, uses the grouping character (',') to seperate groups in the integer portion of the number.

intToString

public static final String intToString(int n)
Converts an integer to a base-10 string representation.


intToString

public static final String intToString(int n,
                                       int numFractionalDigits)
Converts a integer to a base-10 string representation using the specified number of fractional digits.


intToString

public static String intToString(int n,
                                 int minFracDigits,
                                 int maxFracDigits,
                                 boolean grouping)
Converts an integer to a base-10 string representation.

Parameters:
n - the integer
minFracDigits - the minimum number of digits to show after the decimal point.
maxFracDigits - the maximum number of digits to show after the decimal point.
grouping - if (true, uses the grouping character (',') to seperate groups in the integer portion of the number.

isPowerOfTwo

public static final boolean isPowerOfTwo(int n)
Returns true if the number (greater than 1) is a power of two.


countBits

public static final int countBits(int n)
Counts the number of "on" bits in an integer.


log2

public static final int log2(int n)
Returns the log base 2 of an integer greater than 0. The returned value is equal to Math.floor(Math.log(n) / Math.log(2)).


clamp

public static final int clamp(int n,
                              int min,
                              int max)
Clamps a number between two values. If the number <= min returns min; if the number >= max returns max; otherwise returns the number.


clamp

public static final float clamp(float n,
                                float min,
                                float max)
Clamps a number between two values. If the number <= min returns min; if the number >= max returns max; otherwise returns the number.


clamp

public static final double clamp(double n,
                                 double min,
                                 double max)
Clamps a number between two values. If the number <= min returns min; if the number >= max returns max; otherwise returns the number.


sign

public static final int sign(int n)
Returns the sign of a number.


sign

public static final int sign(double n)
Returns the sign of a number.


abs

public static final int abs(int n)
Returns the absolute value of a number.


intDivRound

public static final int intDivRound(int n,
                                    int d)
Divides the number, n, by the divisor, d, rounding the result to the nearest integer.


intDivFloor

public static final int intDivFloor(int n,
                                    int d)
Divides the number, n, by the divisor, d, returning the nearest integer less than or equal to the result.


intDivCeil

public static final int intDivCeil(int n,
                                   int d)
Divides the number, n, by the divisor, d, returning the nearest integer greater than or equal to the result.


mul

public static final int mul(int f1,
                            int f2)
Multiplies two fixed-point numbers together.


mul

public static final long mul(long f1,
                             long f2)
Multiplies two fixed-point numbers together.


div

public static final int div(int f1,
                            int f2)
Divides the first fixed-point number by the second fixed-point number.


div

public static final long div(long f1,
                             long f2)
Divides the first fixed-point number by the second fixed-point number.


mulDiv

public static final int mulDiv(int f1,
                               int f2,
                               int f3)
Multiplies the first two fixed-point numbers together, then divides by the third fixed-point number.


mulDiv

public static final long mulDiv(long f1,
                                long f2,
                                long f3)
Multiplies the first two fixed-point numbers together, then divides by the third fixed-point number.


sqrt

public static final int sqrt(int fx)

sqrt

public static final long sqrt(long fx)

dist

public static long dist(int x1,
                        int y1,
                        int x2,
                        int y2)

sin

public static final int sin(int fx)
Returns the sine of the specified fixed-point radian value.


cos

public static final int cos(int fx)
Returns the cosine of the specified fixed-point radian value.


tan

public static final int tan(int fx)
Returns the tangent of the specified fixed-point radian value.


cot

public static final int cot(int fx)
Returns the cotangent of the specified fixed-point radian value.


asin

public static final int asin(int fx)
Returns the arcsine of the specified fixed-point value.


acos

public static final int acos(int fx)
Returns the arccosine of the specified fixed-point value.


atan

public static final int atan(int fx)
Returns the arctangent of the specified fixed-point value.


atan2

public static final int atan2(int fy,
                              int fx)
Returns in the range from -pi to pi.


rand

public static final int rand(int max)
Returns a random integer from 0 to max, inclusive


rand

public static final int rand(int min,
                             int max)
Returns a random integer from min to max, inclusive


rand

public static final double rand(double max)
Returns a random double from 0 to max, inclusive


rand

public static final double rand(double min,
                                double max)
Returns a random double from min to max, inclusive


rand

public static final boolean rand()
Returns a random boolean.


randChance

public static final boolean randChance(int percent)
Returns true if a random event occurs.

Parameters:
percent - The probability of the event occuring, from 0 (never) to 100 (always).

noise

public static final int noise(int n)
Returns:
a 32-bit signed integer

noise

public static final int noise(int x,
                              int y)
Returns:
a 32-bit signed integer

smoothNoise

public static final int smoothNoise(int x)
Returns:
a 32-bit signed integer

smoothNoise

public static final int smoothNoise(int x,
                                    int y)
Returns:
a 32-bit signed integer

interpolatedNoise

public static final int interpolatedNoise(int fx)
Returns:
a 32-bit signed integer

interpolatedNoise

public static final int interpolatedNoise(int fx,
                                          int fy)
Returns:
a 32-bit signed integer

perlinNoise

public static final int perlinNoise(int fx,
                                    int persistence,
                                    int numOctaves)
Parameters:
fx - fixed-point value
persistence - fixed-point value <= 1.
Returns:
a 32-bit signed integer From http://freespace.virgin.net/hugo.elias/models/m_perlin.htm

perlinNoise

public static final int perlinNoise(int fx,
                                    int fy,
                                    int persistence,
                                    int numOctaves)
Parameters:
fx - fixed-point value
fy - fixed-point value
persistence - fixed-point value <= 1.
Returns:
a 32-bit signed integer From http://freespace.virgin.net/hugo.elias/models/m_perlin.htm

interpolate

public static final int interpolate(int n1,
                                    int n2,
                                    int f)
Performs a 1-dimensional linear interpolation between values n1 and n2. The f parameter is a fixed-point value from 0.0 to 1.0. If f is less than 0 or greater than 1, extrapolation is calculated.


cosineInterpolate

public static final int cosineInterpolate(int n1,
                                          int n2,
                                          int f)
Performs a 1-dimensional cosine interpolation between values n1 and n2. The f parameter is a fixed-point value from 0.0 to 1.0. If f is less than 0 or greater than 1, extrapolation is calculated.


quickCurveInterpolate

public static final int quickCurveInterpolate(int n1,
                                              int n2,
                                              int f)

cubicInterpolate

public static final int cubicInterpolate(int n0,
                                         int n1,
                                         int n2,
                                         int n3,
                                         int f)
Performs a 1-dimensional cubic interpolation between values n1 and n2. The f parameter is a fixed-point value from 0.0 to 1.0. If f is less than 0 or greater than 1, extrapolation is calculated.


PulpCore

Copyright © 2007-2009 Interactive Pulp, LLC.