www.pudn.com > j3dme-0.3.0.rar > Math.java
/*
* J3DME Fast 3D software rendering for small devices
* Copyright (C) 2001 Onno Hommes
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package net.jscience.j3dme;
/**
* This class implements a fast 8-bit fixed-point integer library.
* For performance reasons no effort is put into high precision nor
* in the range of the fixed points. As mentiond this library uses
* 8 bits for the fraction. In addition it only uses 16 bits of the
* remaining 24-bits for the non fractional part of a number. The
* other 8 bits are used as an overflow to adjust the fixed point prior
* for divisions and multiplications.
*/
public abstract class Math
{
/**
* The maximum useable fixed point integer
*/
public final static int MAX_VALUE = 0x007fffff;
/**
* The minimum usable fixed point integer
*/
public final static int MIN_VALUE = -MAX_VALUE;
/**
* The representation of Not A Number
*/
public final static int NAN = 0x7fffffff;
/* The SIN lookup table */
private final static int[] SIN =
{0,
6,12,18,25,31,37,43,49,
56,62,68,74,80,86,92,97,
103,109,115,120,126,131,136,142,
147,152,157,162,167,171,176,181,
185,189,193,197,201,205,209,212,
216,219,222,225,228,231,234,236,
238,241,243,244,246,248,249,251,
252,253,254,254,255,255,255,255,
255,255,255,254,254,253,252,251,
249,248,246,244,243,241,238,236,
234,231,228,225,222,219,216,212,
209,205,201,197,193,189,185,181,
176,171,167,162,157,152,147,142,
136,131,126,120,115,109,103,97,
92,86,80,74,68,62,56,49,
43,37,31,25,18,12,6,0
};
/**
* Returns the multiplications of two fixed point integers
* Calculate the multiplication of the fixed point integer x and
* the fixed point integer y and return the result as a fixed point
* integer. No overflow detection exist so anything greater than
* MAX_VALUE or smaller then MIN_VALUE should be ignored and
* interpreted as NAN.
*
* @param x The fixed point to be multiplied
* @param y The fixed point multiplier.
* @return The multiplication of x times y as fixed point
*/
public static int mul(int x, int y){
return x * y >> 8;
}
/**
* Returns the division of two fixed point integers
* Calculate the division of the fixed point integer x divided by
* the fixed point integer y and return the result as a fixed point
* integer. No overflow detection exist so anything greater than
* MAX_VALUE or smaller then MIN_VALUE should be ignored and
* interpreted as NAN.
*
* @param x The fixed point to be divided
* @param y The fixed point dividend
* @return The division of x by y as a fixed point integer
*/
public static int div(int x, int y){
return (x << 8) / y ;
}
/**
* Returns the absolute value of a fixed point integer
* Determine the absolute value of the fixed point integer x.
* the fixed point integer y and return the result as a fixed point.
*
* @param x A fixed point integer
* @return The absolute value x a fixed point integer
*/
public static int abs(int x){
return (x < 0)? -x:x;
}
/**
* Returns the maximum value of two fixed point integers
* Determine the greater of two given fixed point integers
* and return the result as a fixed point integer.
*
* @param x A fixed point integer
* @param y A fixed point integer
* @return The greater of x and y as a fixed point integer
*/
public static int max(int x,int y){
return (y > x)? y:x;
}
/**
* Returns the minimum value of two fixed point integers
* Determine the smallest number of two given fixed point integers
* and return the result as a fixed point integer.
*
* @param x A fixed point integer
* @param y A fixed point integer
* @return The lesser value of x and y as a fixed point integer
*/
public static int min(int x,int y){
return (x > y)? y:x;
}
/**
* Returns the square root of a fixed point integer
* Calculate the square root of a fixed point integer x using
* the Newton method and iterate only r times to close in on the
* result returned as a fixed point integer. The number of iterations
* must be specified as a normal (i.e. non fixed-point ) integer.
*
* @param x The fixed point integer to sqrt
* @param r The number of repetitions in the algorithm
* @return The square root of x as a fixed point.
*/
public static int sqrt(int x,int r){
if (x <= 0) return 0;
int s = (x + 256) >> 1;
for(int i=0;i> 1;
return (s);
}
/**
* Return the sine of a binary angle.
* Determine the sine value of the binary angle b. This algorithm
* uses binary angles. A full circle has 256 sectors each representing
* an angle (e.g. 128 represents PI , 256 2*PI, etc...).
* The advantage of this approach is that normalization can be done
* with a simple AND operation (b AND 256). This provides the
* performance needed for the 3D engine.
* This function returns a fixed point representing the sine of the angle.
*
* @param b The binary angle
* @return The sine of the binary angle as a fixed point integer.
*/
public static int sin(int b){
b &= 0xff;
if ( b > 0x80) return -SIN[b & 0x7f];
return SIN[b];
}
/**
* Return the cosine of a binary angle.
* Determine the cosine value of the binary angle b.
* This function returns a fixed point representing the cosine of
* the binary angle.
*
* @param b The binary angle
* @return The cosine of the binary angle as a fixed point integer.
*/
public static int cos(int b){
return sin(64-b);
}
/**
* Return the tangent of a binary angle.
* Determine the tangent value of the binary angle b.
* This function returns a fixed point representing the tangent of
* the binary angle or returns NAN for angles representing 1/2PI
* (i.e. b=64) or a multiplication thereof.
*
* @param b The binary angle
* @return The cosine of the binary angle as a fixed point integer.
*/
public static int tan(int b){
int c=cos(b);
return (c != 0)?(sin(b) << 8)/c:NAN;
}
}