1 Replies - 34294 Views - Last Post: 03 November 2007 - 10:57 PM Rate Topic: -----

#1 naash   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 115
  • Joined: 26-August 07

POLAR & CARTESIAN

Posted 03 November 2007 - 10:13 PM

1) Implement teh conversian of the followng 2 classes into one another

POLAR (r,a) <-------> CARTESIAN (x,y)


r=sqrt(x*x+y*y)
a=atan(x/y)


x=r*cos(a);
y=r*sin(a);

#include<iostream.h>
#include<conio.h>
#include<math.h>

class cartesian;

class polar
{
  float radius,angle;
  public :
	 polar()
	{
	  radius=0.0;
	  angle=0.0;
	}
	 polar(float r,float a)
	{
	   radius=r;
	   angle=a;
	}
	 polar(cartesian p);
	 float get_radius();
	 float get_angle();
	 friend ostream & operator << (ostream & dout, polar x)
	{
	  dout<<"\nRADIUS : "<<x.radius;
	  dout<<"\nANGLE  : "<<x.angle;
	  return dout;
	}

};

class cartesian
{
  float x,y;
  public :
	 cartesian()
	{
	  x=0.0;
	  y=0.0;
	}
	 cartesian(float X,float Y)
	{
	   x=X;
	   y=Y;
	}
	 cartesian(polar p);
	 float get_x();
	 float get_y();
	 friend ostream & operator << (ostream & dout, cartesian c)
	{
	  dout<<"\nX : "<<c.x;
	  dout<<"\nY  : "<<c.y;
	  return dout;
	}

};


float polar :: get_radius()	{ return radius; }
float polar :: get_angle()	{ return angle; }

float cartesian :: get_x()	{ return x; }
float cartesian :: get_y()	{ return y; }

polar :: polar(cartesian p)
{
  float X=p.get_x();
  float Y=p.get_y();
  radius=sqrt(X*X+Y*Y);
  angle=atan(X/Y);
}

cartesian :: cartesian(polar p)
{
  float r=p.get_radius();
  float a=p.get_angle();
  x=r*cos(a);
  y=r*sin(a);
}

void main()
{
  clrscr();
  polar ob1,ob2(2,1);
  cartesian obj1,obj2(2,1);
  cout<<"\nPOLAR\n"<<ob2;
  obj1=ob1;
  cout<<endl<<"\nPOLAR -> CARTESIAN\n"<<obj1;
  cout<<"\n\n\nCARTESIAN\n"<<obj2;
  ob1=obj1;
  cout<<endl<<"\nCARTESIAN -> POLAR\n"<<ob1;
  getch();
}





The above code is compiled.
But doesn't give right output . All wrong answers ! :mellow

POLAR
radius=2
angle=1
POLAR -> CARTESIAN
X : 1.080605
Y : 1.682942

CARTESIAN
X=2
Y=1
CARTESIAN -> POLAR
RADIUS : 2.236068
ANGLE : 1.107149

Is This A Good Question/Topic? 0
  • +

Replies To: POLAR & CARTESIAN

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5612
  • View blog
  • Posts: 14,686
  • Joined: 18-April 07

Re: POLAR & CARTESIAN

Posted 03 November 2007 - 10:57 PM

Hi naash,

I cleaned up your program a bit, put related functions close to the classes they belong with and rewrote your main function. Remember you have to create instances of the class and then pass the instance to another instance through its constructor as it is being created.

In the program below I create 1 polar and 1 cartesian object using our default float values. I then create another polar object using our cartesian object we already defined and I created one cartesian object using the polar object we already defined.

I then cleaned up the objects. The results came out right on the money and everything is working great.

I also took out your functions dealing with file streams since they were a bit buggy and not really related to your immediate problems. You can now put them back in and debug them knowing the program is correct up to this point.

#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;

// Forward declaration of cartesian object
class cartesian;

// Define a polar object class
class polar
{
	float radius,angle;

public :
	polar()
	{
		radius=0.0;
		angle=0.0;
	}

	polar(float r,float a)
	{
		radius=r;
		angle=a;
	}

	polar(cartesian p);
	float get_radius();
	float get_angle();

};

// Methods that belong to polar objects
float polar :: get_radius()   { return radius; }
float polar :: get_angle()	{ return angle; }

// Polar constructor taking a cartesian object
polar :: polar(cartesian p)
{
  float X=p.get_x();
  float Y=p.get_y();
  radius=sqrt(X*X+Y*Y);
  angle=atan(X/Y);
}


// Define a cartesian object
class cartesian
{
	float x,y;

public:
	cartesian()
	{
		x=0.0;
		y=0.0;
	}

	cartesian(float X,float Y)
	{
		x=X;
		y=Y;
	}

	cartesian(polar p);
	float get_x();
	float get_y();

};

// Methods that belong to cartesian objects
float cartesian :: get_x()	{ return x; }
float cartesian :: get_y()	{ return y; }

// Cartesian constructor taking a polar object.
cartesian :: cartesian(polar p)
{
  float r=p.get_radius();
  float a=p.get_angle();
  x=r*cos(a);
  y=r*sin(a);
}

// Main driver function
void main()
{
  // Create a polar object. Notice that we use new to create an instance.
  // We also print out its angle and radius to make sure they are set.
  polar *polarobj1 = new polar(2.0,1.0);
  cout << "Polar angle: " << polarobj1->get_angle() << endl;
  cout << "Polar radius: " << polarobj1->get_radius() << endl;

  // Create a cartesian object, and again print out its coordinates.
  cartesian *cartesianobj1 = new cartesian(2.0,1.0);
  cout << "Cartesian x: " << cartesianobj1->get_x() << endl;
  cout << "Cartesian y: " << cartesianobj1->get_y() << endl;

  // Create a cartesian object using polar object defined above
  // Print out results of new cartesian object.
  cartesian *cartesianconvert = new cartesian(*polarobj1);
  cout << "Cartesian x: " << cartesianconvert->get_x() << endl;
  cout << "Cartesian y: " << cartesianconvert->get_y() << endl;

  // Create a polar object using the cartesian object defined above
  // Print out the results of the new polar object.
  polar *polarconvert = new polar(*cartesianobj1);
  cout << "Polar angle: " << polarconvert->get_angle() << endl;
  cout << "Polar radius: " << polarconvert->get_radius() << endl;

  // Got to clean up our objects.
  delete(polarobj1);
  delete(cartesianobj1);
  delete(cartesianconvert);
  delete(polarconvert);
}



As usual you can read through the comments to see how I am doing things. As you will see on the output, they should be right along your figures.

Enjoy!

"At DIC we be polar creating, cartesian object loving coding ninjas!" :snap:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1