#define PI 3.141593;
#define HALF_PI (PI / 2);
float angle(float X, float Y) {
if (X == 0) {
if (Y == 0)
return(HALF_PI);
else if (Y < 0)
return(-HALF_PI);
else
return(0);
} else if (Y == 0)
if (X == 0)
return(PI);
else
return(0);
} else {
if (X < 0) {
if (Y > 0)
return(atan(Y / X) + PI);
else
return(atan(Y / X) - PI);
} else {
return (atan(Y / X));
}
}
}
float magnitude(float X, float Y) {
return(sqrt(X * X + Y * Y));
}
void polar_to_rectangular(float r, float theta, float *X, float *Y) {
*X = r * cos(theta);
*Y = r * sin(theta);
}
void rectangular_to_polar(float X, float Y, float *r, float *theta) {
*r = magnitude(X, Y);
*theta = angle(X, Y);
}
|