before I get into my problem, I want to point out a couple of things: 1)I know there is already an atan2 function in the cmath library, this is purely as an exercise and for my own practice, and 2) I know that the code does not account for 0.
ok, so tan(theta) = y / x, where y and x are coordinates on a plane... that means:
theta = atan(y/x) in Quads I and IV, and theta = atan(y/x) + 180 in Quads II and III
so why when I use the following code:
float atan(float y, float x)
{
float result = 0.0f;
if (x > 0) //quads I and IV if x is positive
{
result = atanf(y/x);
}
else if (x < 0)
{
result = atan(y/x) + 180; //quads II and III if x is negative
}
return result;
}
does it spit me junk? example, for coordinates (-4,4) it gave me the result of: 179.215, when it should be 135:
atan(4/-4) = -45 degrees + 180 degrees = 135 degrees
but what's happening is it is computing
atan(4.0f/-4.0f) = -0.785398 + 180 degrees = 179.215.
am I missing some step here?