inline double atan2(const double &y, const double &x) { if( fabs(x) >= fabs(y) ) { // Sector to the right if( x > 0 ) { const double phi = y/x; return atan(phi); } // Sector to the left else if( x < 0 ) { const double phi = y/x; if( y >= 0) { return atan(phi) + M_PI; } else { return atan(phi) - M_PI; } } // Hence, x and y are zero else { return 0.0; } } else if( fabs(y) >= fabs(x) ) { // Sector to the top if( y > 0 ) { const double phi = x/y; return M_PI_2 - atan(phi); } // Sector to the bottom if( y < 0 ) { const double phi = x/y; return - M_PI_2 - atan(phi); } // Actually, the impossible case, x=y=0. Should be catched above. else { assert(false); return 0.0; } } // This can only happen, if x or y is NaN. else { return 0.0; } }