]> icculus.org git repositories - theoddone33/hhexen.git/blob - opengl/m_bams.c
25feea4306796d05c7b0af6b61fc6eb454bb5fe4
[theoddone33/hhexen.git] / opengl / m_bams.c
1 // BAMS trigonometric functions.
2
3 #include <math.h>
4 #include "m_bams.h"
5
6 #define BAMS_TABLE_ACCURACY 2000
7
8 static binangle atantable[BAMS_TABLE_ACCURACY];
9
10 void bamsInit() // Fill in the tables.
11 {
12         int     i;      
13         
14         for(i=1; i<BAMS_TABLE_ACCURACY; i++)
15                 atantable[i] = RAD2BANG(atan(i/(float)BAMS_TABLE_ACCURACY));
16 }
17
18 binangle bamsAtan2(int y,int x)
19 {
20         binangle        bang;
21         int                     absy = y, absx = x;
22
23         if(!x && !y) return BANG_0;     // Indeterminate.
24
25         // Make sure the absolute values are absolute.
26         if(absy < 0) absy = -absy;
27         if(absx < 0) absx = -absx;
28         
29         // We'll first determine what the angle is in the first quadrant.
30         // That's what the tables are for.
31         if(!absy) 
32                 bang = BANG_0;
33         else if(absy == absx) 
34                 bang = BANG_45; 
35         else if(!absx) 
36                 bang = BANG_90;
37         else
38         {
39                 // The special cases didn't help. Use the tables.
40                 // absx and absy can't be zero here.
41                 if(absy > absx)
42                         bang = BANG_90-atantable[(int)((float)absx/(float)absy*BAMS_TABLE_ACCURACY)];
43                 else
44                         bang = atantable[(int)((float)absy/(float)absx*BAMS_TABLE_ACCURACY)];
45         }
46                 
47         // Now we know the angle in the first quadrant. Let's look at the signs
48         // and choose the right quadrant.
49         if(x < 0) // Flip horizontally?
50                 bang = BANG_180 - bang; 
51         if(y < 0) // Flip vertically?
52         {
53                 // At the moment bang must be smaller than 180.
54                 bang = BANG_180 + BANG_180-bang;
55         }
56         // This is the final angle.
57         return bang;
58 }