--- ray/src/common/tcos.c 2013/01/17 22:48:21 3.6 +++ ray/src/common/tcos.c 2013/02/07 20:19:52 3.7 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: tcos.c,v 3.6 2013/01/17 22:48:21 greg Exp $"; +static const char RCSid[] = "$Id: tcos.c,v 3.7 2013/02/07 20:19:52 greg Exp $"; #endif /* * Table-based cosine approximation. @@ -49,4 +49,24 @@ tcos(double x) /* approximate cosine */ return(costab[(4*NCOSENTRY)-i]); } return(0.); /* should never be reached */ +} + + +/* Fast arctangent approximation due to Rajan et al. 2006 */ +double +aatan2(double y, double x) +{ + double ratio, aratio, val; + + if (x == 0) + return (y > 0) ? PI/2. : 3./2.*PI; + + aratio = (ratio = y/x) >= 0 ? ratio : -ratio; + + if (aratio > 1.01) + return PI/2. - aatan2(x, y); + + val = PI/4.*ratio - ratio*(aratio - 1.)*(0.2447 + 0.0663*aratio); + + return val + PI*(x < 0); }