1 // BAMS trigonometric functions.
6 #define BAMS_TABLE_ACCURACY 2000
8 static binangle atantable[BAMS_TABLE_ACCURACY];
10 void bamsInit() // Fill in the tables.
14 for(i=1; i<BAMS_TABLE_ACCURACY; i++)
15 atantable[i] = RAD2BANG(atan(i/(float)BAMS_TABLE_ACCURACY));
18 binangle bamsAtan2(int y,int x)
21 int absy = y, absx = x;
23 if(!x && !y) return BANG_0; // Indeterminate.
25 // Make sure the absolute values are absolute.
26 if(absy < 0) absy = -absy;
27 if(absx < 0) absx = -absx;
29 // We'll first determine what the angle is in the first quadrant.
30 // That's what the tables are for.
39 // The special cases didn't help. Use the tables.
40 // absx and absy can't be zero here.
42 bang = BANG_90-atantable[(int)((float)absx/(float)absy*BAMS_TABLE_ACCURACY)];
44 bang = atantable[(int)((float)absy/(float)absx*BAMS_TABLE_ACCURACY)];
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?
53 // At the moment bang must be smaller than 180.
54 bang = BANG_180 + BANG_180-bang;
56 // This is the final angle.