Integer Version (using an arc tangent look-up table). See version 1 for more information.

// N.B. There is a 16 bit version of this routine below this one. long auto_arc_tan( long v_dx, long v_dy ) { // AUTOMATIC ARC TANGENT ROUTINE returning an Signed 32 bit value. // (c) Jon P // // Version 5.0 Speed-up routine with a look-up table rather than a formula. // #define ARC_TANGENT_TABLE_SIZE 129 #define ARC_TANGENT_ONE 512 static unsigned char arc_tangent_table[ ARC_TANGENT_TABLE_SIZE ] = { 0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 28, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36, 37, 37, 38, 39, 39, 40, 40, 41, 41, 42, 42, 43, 43, 44, 44, 44, 45, 45, 45, 45, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 45, 45, 45, 44, 44, 44, 43, 43, 43, 42, 42, 41, 41, 40, 39, 39, 38, 38, 37, 36, 35, 35, 34, 33, 32, 31, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 19, 18, 17, 16, 15, 14, 12, 11, 10, 8, 7, 6, 4, 3, 1, 0 }; // ...This array was made by ATAN_TBL.BAS JonP register long grad, ang, abs_dx, abs_dy, half; abs_dx = v_dx; if ( abs_dx < 0 ) abs_dx = 0 - abs_dx; abs_dy = v_dy; if ( abs_dy < 0 ) abs_dy = 0 - abs_dy; // find the gradient between 0 and "1 in 1" (or 0 and 512 in this impelmentation).... if ( abs_dy <= abs_dx ) { if ( abs_dx == 0 ) { //exact angle can be returned... also this prevents divide by 0 error... if ( v_dy >= 0 ) return 0; else return 2048; } grad = abs_dy; grad <<= 9; grad /= abs_dx; half = 1; } else { if ( abs_dy == 0 ) { //exact angle can be returned... also this prevents divide by 0 error... if ( v_dx >= 0 ) return 1024; else return 3072; } grad = abs_dx; grad <<= 9; grad /= abs_dy; half = 0; } // We now have a gradient between 0 and "1 in 1" (0 to 512). // Access the arc tangent look-up table to convert the gradient to // an angle between 0 and 45 degrees (0 to 512)... ang = arc_tangent_table[grad>>2] + grad; if ( v_dx >= 0 ) { if ( v_dy >= 0 ) { if ( half ) ang = 1024 - ang; } else { if ( half ) ang += 1024; else ang = 2048 - ang; } } else { if ( v_dy >= 0 ) { if ( half ) ang += 3072; else ang = 4096 - ang; } else { if ( half ) ang = 3072 - ang; else ang += 2048; } } // The next line is for testing only.... // printf( "\ndx %d dy %d = ang %d ", v_dx, v_dy, ang ); return ang; } short auto_arc_tan_s16( long v_dx, long v_dy ) { // AUTOMATIC ARC TANGENT ROUTINE returning an Signed 16 bit value. // (c) Jon P // // Version 5.0 Speed-up routine with a look-up table rather than a formula. // #define ARC_TANGENT_TABLE_SIZE 129 #define ARC_TANGENT_ONE 512 static unsigned char arc_tangent_table[ ARC_TANGENT_TABLE_SIZE ] = { 0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 28, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36, 37, 37, 38, 39, 39, 40, 40, 41, 41, 42, 42, 43, 43, 44, 44, 44, 45, 45, 45, 45, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 45, 45, 45, 44, 44, 44, 43, 43, 43, 42, 42, 41, 41, 40, 39, 39, 38, 38, 37, 36, 35, 35, 34, 33, 32, 31, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 19, 18, 17, 16, 15, 14, 12, 11, 10, 8, 7, 6, 4, 3, 1, 0 }; // ...This array was made by ATAN_TBL.BAS JonP register long grad, abs_dx, abs_dy, half; register short ang; abs_dx = v_dx; if ( abs_dx < 0 ) abs_dx = 0 - abs_dx; abs_dy = v_dy; if ( abs_dy < 0 ) abs_dy = 0 - abs_dy; // find the gradient between 0 and "1 in 1" (or 0 and 512 in this impelmentation).... if ( abs_dy <= abs_dx ) { if ( abs_dx == 0 ) { //exact angle can be returned... also this prevents divide by 0 error... if ( v_dy >= 0 ) return 0; else return 2048; } grad = abs_dy; grad <<= 9; grad /= abs_dx; half = 1; } else { if ( abs_dy == 0 ) { //exact angle can be returned... also this prevents divide by 0 error... if ( v_dx >= 0 ) return 1024; else return 3072; } grad = abs_dx; grad <<= 9; grad /= abs_dy; half = 0; } // We now have a gradient between 0 and "1 in 1" (0 to 512). // Access the arc tangent look-up table to convert the gradient to // an angle between 0 and 45 degrees (0 to 512)... ang = arc_tangent_table[grad>>2] + grad; if ( v_dx >= 0 ) { if ( v_dy >= 0 ) { if ( half ) ang = 1024 - ang; } else { if ( half ) ang += 1024; else ang = 2048 - ang; } } else { if ( v_dy >= 0 ) { if ( half ) ang += 3072; else ang = 4096 - ang; } else { if ( half ) ang = 3072 - ang; else ang += 2048; } } // The next line is for testing only.... // printf( "\ndx %d dy %d = ang %d ", v_dx, v_dy, ang ); return ang; }