danger.util
Class Fixed

java.lang.Object
  extended by danger.util.Fixed


public class Fixed
extends Object

Fixed point math library. Precision is 16 bits whole, 16 bits fractional. Uses 32 bit values to represent the Fixed number.


Field Summary
static int FIXED_ONE
          Positive one as a fixed point value.
static int FIXED_ONE_HALF
          Positive .5 as a fixed point value.
static int FIXED_PI
          Pi as a fixed point value.
static int FIXED_TWO
          Positive two as a fixed point value.
static int MAX_VALUE
          The largest fixed point value.
 
Constructor Summary
Fixed()
           
 
Method Summary
static int abs(int value)
          Returns the absolute value of a fixed point number.
static int acosf(int cosf)
          Returns the arccosine of a fixed point value.
static int add(int fvalue1, int fvalue2)
          Returns the sum of two fixed point values.
static int asinf(int sinf)
          Returns the arcsine of a fixed point value.
static int atanf(int tanf)
          Returns the arctangent of a fixed point value.
static int cos(int angle)
          Returns the cosine of a whole number.
static int cosf(int fAngle)
          Returns the cosine of a fixed point value.
static int divide(int dividend, int divisor)
          Divides a fixed point value by another fixed point value.
static int floor(int fValue)
          Returns the nearest whole number less than or equal to the specified fixed point value.
static int fromInt(int value)
          Given an integer value, return a fixed point value.
static int fromReal(int whole, int dividend, int divisor)
          Create a fixed point value from a real expression.
static int getFraction(int fValue)
          Given a fixed point value, return the fraction.
static int hypot(int x, int y)
          Returns the hypotenuse of a right-angled triangle with sides of length x and y.
static int multiply(int factor1, int factor2)
          Performs signed multiplication of two fixed point values.
static int round(int fValue)
          Returns the whole number nearest to the specified fixed point value.
static int sin(int angle)
          Returns the sine of a whole number.
static int sinf(int fAngle)
          Returns the sine of a fixed point value.
static int sqrt(int fValue)
          Returns the square root of a fixed point value.
static int subtract(int fvalue1, int fvalue2)
          Returns the difference of two fixed point values.
static int tan(int angle)
          Returns the tangent of a whole number.
static int tanf(int fAngle)
          Returns the tangent of a fixed point value.
static int toInt(int fvalue)
          Given a fixed point value, return an integer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FIXED_ONE

public static final int FIXED_ONE
Positive one as a fixed point value.

See Also:
Constant Field Values

FIXED_TWO

public static final int FIXED_TWO
Positive two as a fixed point value.

See Also:
Constant Field Values

FIXED_PI

public static final int FIXED_PI
Pi as a fixed point value.

See Also:
Constant Field Values

MAX_VALUE

public static final int MAX_VALUE
The largest fixed point value.

See Also:
Constant Field Values

FIXED_ONE_HALF

public static final int FIXED_ONE_HALF
Positive .5 as a fixed point value.

See Also:
Constant Field Values
Constructor Detail

Fixed

public Fixed()
Method Detail

fromInt

public static int fromInt(int value)
Given an integer value, return a fixed point value. The value is shifted up 16 bits.

Parameters:
value - the whole number to convert
Returns:
the fixed point value

toInt

public static int toInt(int fvalue)
Given a fixed point value, return an integer. The value is shifted down 16 bits.

Parameters:
fvalue - the fixed point value to convert
Returns:
the whole number

getFraction

public static int getFraction(int fValue)
Given a fixed point value, return the fraction. The high 16 bits of the value are cleared.

Parameters:
fValue - the fixed point value
Returns:
the fraction

fromReal

public static int fromReal(int whole,
                           int dividend,
                           int divisor)
Create a fixed point value from a real expression.

Parameters:
whole - the whole number
dividend - the top of the fraction
divisor - the bottom of the fraction
Returns:
the fixed point value
Throws:
IllegalArgumentException - if either dividend or divisor is negative and whole is non-zero

abs

public static int abs(int value)
Returns the absolute value of a fixed point number.

Parameters:
value - the fixed point value
Returns:
the absolute value of value

divide

public static int divide(int dividend,
                         int divisor)
Divides a fixed point value by another fixed point value.

Parameters:
dividend - value divided into
divisor - value divided by
Returns:
the divided, fixed point value
Throws:
ArithmeticException - if divisor is zero

add

public static int add(int fvalue1,
                      int fvalue2)
Returns the sum of two fixed point values.

Parameters:
fvalue1 - the first fixed point value
fvalue2 - the second fixed point value
Returns:
the sum of fvalue1 and fvalue2, as a fixed point value

subtract

public static int subtract(int fvalue1,
                           int fvalue2)
Returns the difference of two fixed point values.

Parameters:
fvalue1 - the first fixed point value
fvalue2 - the second fixed point value
Returns:
the difference of fvalue1 and fvalue2, as a fixed point value

multiply

public static int multiply(int factor1,
                           int factor2)
Performs signed multiplication of two fixed point values.

Parameters:
factor1 - the first fixed point value
factor2 - the second fixed point value
Returns:
the multiplied, fixed point value

sinf

public static int sinf(int fAngle)
Returns the sine of a fixed point value.

Parameters:
fAngle - the fixed point angle in degrees
Returns:
the sine of fAngle as a fixed point value

cosf

public static int cosf(int fAngle)
Returns the cosine of a fixed point value.

Parameters:
fAngle - the fixed point angle in degrees
Returns:
the cosine of fAngle as a fixed point value

tanf

public static int tanf(int fAngle)
Returns the tangent of a fixed point value. The tangent is equal to the sine divided by the cosine.

Parameters:
fAngle - the fixed point angle in degrees
Returns:
the tangent of fAngle as a fixed point value
Throws:
ArithmeticException - if the cosine of fAngle is zero
Since:
4.0

asinf

public static int asinf(int sinf)
Returns the arcsine of a fixed point value. Arcsine converts a sine back to its original value. Therefore, arcsine is defined only for the range of the sine fuction (i.e., -1 to 1, inclusive).

Parameters:
sinf - the fixed point sine value
Returns:
the arcsine of sinf as a fixed point value
Throws:
ArithmeticException - if arcsine is undefined for sinf
Since:
4.0

acosf

public static int acosf(int cosf)
Returns the arccosine of a fixed point value. Arccosine converts a cosine back to its original value. Therefore, arccosine is defined only for the range of the cosine fuction (i.e., -1 to 1, inclusive).

Parameters:
cosf - the fixed point cosine value
Returns:
the arccosine of cosf as a fixed point value
Throws:
ArithmeticException - if arccosine is undefined for cosf
Since:
4.0

atanf

public static int atanf(int tanf)
Returns the arctangent of a fixed point value. Arctangent converts a tangent back to its original value.

Parameters:
tanf - the fixed point tangent value
Returns:
the arctangent of tanf as a fixed point value
Since:
4.0

sin

public static int sin(int angle)
Returns the sine of a whole number.

Parameters:
angle - the angle in degrees
Returns:
the sine of angle as a fixed point value

cos

public static int cos(int angle)
Returns the cosine of a whole number.

Parameters:
angle - the angle in degrees
Returns:
the cosine of angle as a fixed point value

tan

public static int tan(int angle)
Returns the tangent of a whole number. The tangent is equal to the sine divided by the cosine.

Parameters:
angle - the angle in degrees
Returns:
the tangent of angle as a fixed point value
Throws:
ArithmeticException - if the cosine of angle is zero
Since:
4.0

floor

public static int floor(int fValue)
Returns the nearest whole number less than or equal to the specified fixed point value.

Parameters:
fValue - the fixed point value
Returns:
the nearest whole number less than or equal to fValue, as a whole number

round

public static int round(int fValue)
Returns the whole number nearest to the specified fixed point value.

Parameters:
fValue - the fixed point value
Returns:
the nearest whole number to fValue, as a fixed point value

hypot

public static int hypot(int x,
                        int y)
Returns the hypotenuse of a right-angled triangle with sides of length x and y. By the Pythagorean theorem, this is equal to sqrt(x**2 + y**2).

Parameters:
x - length of side x, as a fixed point value
y - length of side y, as a fixed point value
Returns:
the hypotenuse, as a fixed point value
Throws:
IllegalArgumentException - if ((x|y) < 0) || (x > 0x800000) || (y > 0x800000)

sqrt

public static int sqrt(int fValue)
Returns the square root of a fixed point value. The algorithm is less accurate, but faster, than Math.sqrt().

Parameters:
fValue - the fixed point value
Returns:
the square root of fValue, as a fixed point value
Throws:
ArithmeticException - if fValue is less than zero
See Also:
Math.sqrt(double)