next up previous contents index Search
Next: 0.5.10 Soundex English word-sounding Up: 0.5.9 Cartesian and Polar Previous: 0.5.9 Cartesian and Polar

0.5.9.1 Source Code


#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);
}

Scott Gasch
1999-07-09