# Polar to Cartesian in C++

This tutorial will help us to learn how to convert Polar to Cartesian in C++. This refers to the conversion of given Polar coordinates to the equivalent Cartesian coordinates. The post explains the formula and the program to carry out this task in C++. Feel free to ask questions or drop comments in the comments section at the end of the post.

## How to convert Polar to Cartesian in C++

We will see what the coordinate formats are in each of the systems and the conversion formulas. After that, we will see the actual code for the same. Let’s learn to convert Polar to Cartesian in C++!

#### 1. Libraries

Code :

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

iostream : input and output functions.

math.h : mathematical functions

#### 2. Polar Coordinate system

The Polar Coordinate system uses two dimensions to represent the point data. Polar coordinates are points labeled (r,θ) and plotted on a polar grid using the same.

So we will be getting input in the form of ‘r’ and ‘θ’ (theta).

Code :

```class polar_coordinate
{
public:
float r,th;
polar_coordinate(){}

polar_coordinate(int val1,int val2)
{
r=val1;
th=val2;
}
void show()
{
cout<<"Polar form : \nr="<<r<<" and θ = "<<th;

}
};```

A class named polar_coordinates is defined. ‘r’ and theta (θ) are the member variables. The values for the member variables are passed at the time of object declaring using constructor.

#### 3. Cartesian Coordinate System

Cartesian coordinate system represents each point using numeric values. These values are the perpendicular distances of the point from the considered axes in three or two dimensions. The coordinates, therefore, are represented in the form of (x,y) where x is the distance from the y axis and y is the distance from the x axis.

#### 4. Conversion from Polar to Cartesian format

The conversion is simply done using 2 formulas to get the values of ‘x’ and ‘y’ using ‘r’ and ‘θ’.

x = r * cos(θ)

y = r * sin(θ)

The values are calculated by substituting values for ‘r’ and ‘θ’.

Code :

```class cartesian
{
float x,y;
public:
cartesian(){}

cartesian(polar p)
{
x = p.r * cos(p.th);
y = p.r * sin(p.th);
}
void show()
{
cout<<"\n\nIn Cartesian form : \nx="<<x<<" and y="<<y;

}
};
```

The Polar class variable consisting the values of ‘r’ and ‘θ’ is passed as an argument to the object of class Cartesian and the values for ‘x’ and ‘y’ are initialized with the formula using constructor.

```int main()
{
polar p(5.5,3.14/2);
p.show();

cartesian c(p);
c.show();

return 0;
}```

In the main function, the Polar class object is declared and then the same is passed to the Cartesian object while declaring it. The values for both the coordinates are displayed.

Output :

```In polar form :
r=5 and theta = 1

In Cartesian form :
x=2.70151 and y=4.20735```

#### Complete Code

```//Polar to Cartesian in C++

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

class polar_coordinate
{
public: float r,th;

polar_coordinate(){}
polar_coordinate(int val1,int val2)
{
r=val1;
th=val2;
}

void show() {
cout<<"Polar form : \nr="<<r<<" and θ = "<<th;
}
};

class cartesian
{
float x,y;
public:
cartesian(){}

cartesian(polar p)
{
x = p.r * cos(p.th);
y = p.r * sin(p.th);
}
void show()
{
cout<<"\n\nIn Cartesian form : \nx="<<x<<" and y="<<y;

}
};

int main()
{
polar p(5.5,3.14/2);
p.show();

cartesian c(p);
c.show();

return 0;
}
```

Output :

```In polar form :
r=5 and theta = 1

In Cartesian form :
x=2.70151and y=4.20735```

Therefore, we have converted the Polar coordinates to Cartesian coordinates in C++.

You may also learn,

Draw a line in C++ graphics