home *** CD-ROM | disk | FTP | other *** search
/ com!online 2002 April / comcd0402.iso / homepage / javaspecial / 03_01 / kubik / FastMath.java < prev    next >
Encoding:
Java Source  |  1999-05-18  |  2.3 KB  |  93 lines

  1. /**
  2.  * This class provides really fast sine and cosine functions. In fact, it
  3.  * uses a precalculated table of sine and cosine values. The number format
  4.  * is NOT floating point, but fixed point. The public attribute TWOPI_BITS
  5.  * indicates the number of bits used to represent the value of 2*pi,
  6.  * i.e. 6.283185... Also, the public attribute TWOPI represents the value
  7.  * of 2*pi in this number format. The methods sin and cos take their
  8.  * arguments in this number format and their return values are also in
  9.  * this special format.
  10.  * <P>
  11.  * author: Michael Kraus<BR>
  12.  * version: 1.2<BR>
  13.  * date: 1999/5/17<BR>
  14.  * environment: JDK 1.0.2<BR>
  15.  * email: <A href="michael.kraus@informatik.uni-muenchen.de">michael.kraus@informatik.uni-muenchen.de</A><BR>
  16.  * homepage: <A href="www.informatik.uni-muenchen.de/~michael.kraus">www.informatik.uni-muenchen.de/~michael.kraus</A>
  17.  */
  18.  
  19. public class FastMath
  20. {
  21.     /**
  22.      * number of bits used to represent the value of 2*pi
  23.      * (6.283185...) in the special fixed point number format
  24.      * used in this class
  25.      */
  26.  
  27.     public static final int TWOPI_BITS=8;
  28.  
  29.     /**
  30.      * value of 2*pi (6.283185...) in the special fixed
  31.      * point number format used in this class
  32.      */
  33.  
  34.     public static final int TWOPI=1<<TWOPI_BITS;
  35.  
  36.     /**
  37.      * value of pi/2 (1.570796...) in the special fixed
  38.      * point number format used in this class
  39.      */
  40.  
  41.     public static final int PIHALF=TWOPI/4;
  42.  
  43.     /**
  44.      * bitmask of 2*pi (6.283185...) in the special fixed
  45.      * point number format used in this class
  46.      */
  47.  
  48.     public static final int TWOPI_MASK=TWOPI-1;
  49.  
  50.     /**
  51.      * here the precalculated cosine values are stored
  52.      */
  53.  
  54.     private static int costable[];
  55.  
  56.     /*
  57.      * precalculate the cosine values in the interval [0;2*pi[
  58.      */
  59.  
  60.     static
  61.     {
  62.         int n;
  63.  
  64.         costable=new int[TWOPI];
  65.  
  66.         for(n=0;n<TWOPI;n++)
  67.             costable[n]=(int)(Math.cos(2*Math.PI*n/TWOPI)*TWOPI);
  68.     }
  69.  
  70.     /**
  71.      * get the sine value of the argument. Both the argument
  72.      * and the result are in a special fixed point number
  73.       * format.
  74.      */
  75.  
  76.     public static int sin(int value)
  77.     {
  78.         return costable[Math.abs(PIHALF-value)&TWOPI_MASK];
  79.     }
  80.  
  81.     /**
  82.      * get the cosine value of the argument. Both the argument
  83.      * and the result are in a special fixed point number
  84.       * format.
  85.      */
  86.  
  87.     public static int cos(int value)
  88.     {
  89.         return costable[Math.abs(value)&TWOPI_MASK];
  90.     }
  91. }
  92.  
  93.