We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
atan(x, y) (Read 1123 times)
atan(x, y)
Jun 28th, 2005, 2:48am
 
If the x and y coordinates are available, you can get a more "exact" arctangent through a function like this. It might be helpful to implement as a built-in function so others don't have to code it.

I think the atan2() function is the same thing, but why have a different name?

I'd like to put in a request for acos(x, y) and asin(x, y), too. What about sec(), csc(), and cot()? I think it would be best to define these if for nothing other than code readability. (Java's math library probably does these already...)

Code:
float atan(float tx, float ty) {
 float theta = abs(atan( ty / tx ));
 
 if(tx < 0 && ty > 0) // Quadrant II
   theta = PI - theta;
 else if(tx < 0 && ty < 0) // Quadrant III
   theta = PI + theta;
 else if(tx > 0 && ty < 0) // Quadrant IV
   theta = TWO_PI - theta;
   
 if(ty == 0 && tx > 0)
   theta = 0;
 else if(tx == 0 && ty > 0)
   theta = PI / 2;
 else if(tx == 0 && ty < 0)
   theta = (3.0 / 2.0) * PI;
 else if(ty == 0 && tx < 0)
   theta = PI;
   
 return theta;
}
Page Index Toggle Pages: 1