// While the domain of arctangent is all the // real numbers, the range is only from // -pi/2 to pi/2, excluding the end-points, // as tan(pi/2) and tan(-pi/2) are undefined. // Additionally, however, this is because y/x is // equal to (-y)/(-x), and therefore from the // quotient alone it is impossible to determine // whether 'x' and 'y' were both positive or both // negative. In the image below, if the point // in the first quadrant has an angle of θ, // then the point in third quadrant has an // angle of θ - π. We subtract because we want // the angle to be in (-π, π]: // // | * // | /: // | / : // | / : y // | / : // -x |/ θ : // --------------+-----*-------- // : /| x // : / | // -y : / | // : / | For positive slopes, // :/ | 0 < θ < π/2 // * | // -1 // You will note that tan does give the angle // corresponding to the slope of the extended // line, but this angle is always between // -π/2 and π/2: // // | // * | For negative slopes, // |\ | -π/2 < θ < 0 // -y | \ | // | \| x // --------------+-----*-------- // -x |\θ : // | \ : y // | \: // | * // | // // If x = 0 and y = 0, then we simply define // the "angle" to be 0 radians. // // Otherwise, we must be careful about dividing // by zero, so if 'x' is zero, the sign of 'y' // determines whether we return 'pi/2' or '-pi/2'. // // Otherwise, if 'y' is zero, we return 0 or 'pi', // depending on the sign of 'x'. // - While we could also legitimately return // '-pi' for x < 0, by convention the range // is (-pi, pi]. // -1 // Otherwise, we calculate tan (y/x) and then // if the coordinate (x, y) is in the second // or third quadrant, we must add or subtract // 'pi' to the result returned by 'atan', // respectively. #include #include #include // Function declarations int main(); // Function definitions int main() { double x{}; std::cout << "Enter the x-coordinate: "; std::cin >> x; double y{}; std::cout << "Enter the y-coordinate: "; std::cin >> y; // The local variable 'pi' is initialized // with the value 3.141592653589793 double pi{ std::numbers::pi }; if ( x == 0.0 ) { } else { // -1 / y \ // θ = tan | --- | // \ x / double theta{ std::atan( y/x ) }; } return 0; }