title: Coordinate conversion in c++
body: <p>I am trying to convert different coordinate systems. From polar to rectangular and vice versa. My <code>pol_to_rect()</code>function is not working properly. It is giving very small values(~10^(-44)) after converting and also before converting. There might be some problem while using the sin() and cos() functions. The <code>rect_to_pol()</code> is working fine for positive values. 
  Edit - When I changed <code>atan()</code> to <code>atan2()</code> how can I incorporate other values of x and y.  </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cmath&gt;
  
  using namespace std;
  #define PI 3.1415926
  class Polar; // Forward declaration
  
  class Rectangular {
      private:
          float x, y;
  
      public:
          Rectangular() {} // default constructor
          Rectangular(float mv_x, float mv_y) {
              x = mv_x;
              y = mv_y;
          }
  
          void showData() const;
          Polar rect_to_pol();
          float&amp; get_x() {
              return x;
          }
  
          float&amp; get_y() {
              return y;
          }
  
  };
  
  void Rectangular::showData() const {
      cout &lt;&lt; "--Rectangular--" &lt;&lt; endl;
      cout &lt;&lt; "x: " &lt;&lt; x &lt;&lt; "\t" &lt;&lt;"y: " &lt;&lt; y &lt;&lt; endl;
  }
  
  
  
  
  class Polar {
      private:
          float r;
          float theta;
      public:
          Polar() {} // default constructor
          Polar(float mv_r, float mv_theta) {
              r = mv_r;
              theta = mv_theta;
          }
          void showData();
          Rectangular pol_to_rect();
          float&amp; get_r(){
              return r;
          }
          float&amp; get_theta() {
              return theta;
          }
  
  };
  
  void Polar::showData() {
      cout &lt;&lt; "--Polar--" &lt;&lt; endl;
      cout &lt;&lt; "r:" &lt;&lt; r &lt;&lt; "\t" &lt;&lt; "Theta(Radians):" &lt;&lt; theta &lt;&lt; endl;
  }
  
  Rectangular Polar::pol_to_rect() {
      Rectangular temp;
      temp.get_x() = r * cos(theta*(PI/180.0)); // in degrees
      temp.get_y() = r * sin(theta*(PI/180.0));
      return temp;
  
  }
  
  Polar Rectangular::rect_to_pol() {
      Polar temp;
  
      temp.get_r() = sqrt(pow(x, 2) + pow(y, 2));
      temp.get_theta() = atan2(y, x);
      return temp;
  }
  
  int main()
  {
      Rectangular r1(-1, -1), r2;
      Polar p1(12.0, 30.0), p2;
      r1.showData();
      p2 = r1.rect_to_pol();
      cout &lt;&lt; "After Conversion (RECT TO POLAR)-&gt;" &lt;&lt; endl;
      p2.showData();
  
      p1.showData();
      r2 = p1.pol_to_rect();
      cout &lt;&lt; "After Conversion (POLAR TO RECT)" &lt;&lt; endl;
      r2.showData();
      return 0;
  }
  </code></pre>