home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Peter's Final Project / src / my_math.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-10  |  2.4 KB  |  126 lines  |  [TEXT/KAHL]

  1. /*
  2.  *  Peter's Final Project -- A texture mapping demonstration
  3.  *  © 1995, Peter Mattis
  4.  *
  5.  *  E-mail:
  6.  *  petm@soda.csua.berkeley.edu
  7.  *
  8.  *  Snail-mail:
  9.  *   Peter Mattis
  10.  *   557 Fort Laramie Dr.
  11.  *   Sunnyvale, CA 94087
  12.  *
  13.  *  Avaible from:
  14.  *  http://www.csua.berkeley.edu/~petm/final.html
  15.  *
  16.  *  This program is free software; you can redistribute it and/or modify
  17.  *  it under the terms of the GNU General Public License as published by
  18.  *  the Free Software Foundation; either version 2 of the License, or
  19.  *  (at your option) any later version.
  20.  *
  21.  *  This program is distributed in the hope that it will be useful,
  22.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  23.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24.  *  GNU General Public License for more details.
  25.  *
  26.  *  You should have received a copy of the GNU General Public License
  27.  *  along with this program; if not, write to the Free Software
  28.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29.  */
  30.  
  31. #include <assert.h>
  32. #include <math.h>
  33. #include "my_math.h"
  34. #include "sys.stuff.h"
  35.  
  36. /*
  37.  * Define the trig table size.
  38.  */
  39. #define TRIG_TABLE_SIZE    360
  40.  
  41. /*
  42.  * Declare the sine and cosine tables.
  43.  */
  44. static NUM *sin_table;
  45. static NUM *cos_table;
  46.  
  47. /*
  48.  * Declare the pi constant.
  49.  */
  50. static const double pi = 3.1459265359;
  51.  
  52. /*
  53.  * Initialize the trig tables used for fast trig calculations.
  54.  *  Not the most memory efficient versions, but easy to program
  55.  *  and simple to understand.
  56.  */
  57. void 
  58. my_math_init ()
  59. {
  60.     register short i;
  61.     double angle;
  62.     double increment;
  63.  
  64.     sin_table = (NUM*) ALLOC (sizeof(NUM) * TRIG_TABLE_SIZE);
  65.     cos_table = (NUM*) ALLOC (sizeof(NUM) * TRIG_TABLE_SIZE);
  66.  
  67.     angle = 0.0;
  68.     increment = 2.0 * pi /TRIG_TABLE_SIZE;
  69.     for (i = 0; i < TRIG_TABLE_SIZE; i++)
  70.     {
  71.         sin_table[i] = my_float_to_num (sin (angle));
  72.         cos_table[i] = my_float_to_num (cos (angle));
  73.  
  74.         angle += increment;
  75.     }
  76. }
  77.  
  78. /*
  79.  * Delete the trig tables.
  80.  */
  81. void 
  82. my_math_quit ()
  83. {
  84.     FREE (sin_table);
  85.     FREE (cos_table);
  86. }
  87.  
  88. /*
  89.  * Normalize "theta" to be between 0 and
  90.  *  TRIG_TABLE_SIZE.
  91.  */
  92.  
  93. ANGLE 
  94. my_normalize_angle (theta)
  95.     ANGLE theta;
  96. {
  97.     while (theta < 0)
  98.         theta += TRIG_TABLE_SIZE;
  99.     while (theta >= TRIG_TABLE_SIZE)
  100.         theta -= TRIG_TABLE_SIZE;
  101.  
  102.     return theta;
  103. }
  104.  
  105. /*
  106.  * Return the sin of "theta".
  107.  */
  108.  
  109. NUM 
  110. my_sin (theta)
  111.     ANGLE theta;
  112. {
  113.     return sin_table[my_normalize_angle (theta)];
  114. }
  115.  
  116. /*
  117.  * Return the cos of "theta".
  118.  */
  119.  
  120. NUM 
  121. my_cos (theta)
  122.     ANGLE theta;
  123. {
  124.     return cos_table[my_normalize_angle (theta)];
  125. }
  126.