Polar to Rectangular conversion in C++
In this tutorial, we are going to learn Polar to Rectangular conversion in C++. Here we will learn about the rectangular form, polar form, and Polar to Rectangular conversion. After that, we will see the C++ program for the same.

Let us see how to represent z=a+ib in polar form:-
As, shown in the figure:
r ² = a ² + b ² (Pythagoras theorem)
By using the basic trigonometry:
=> cosθ=a/r and sinθ=b/r
=> a=rcosθ and b=rsinθ
Substitute the values of a and b in z=a+ib, we get
z=a+bi =rcosθ+(rsinθ)i =r(cosθ+isinθ)
This is called as polar form.
Conversion of polar form to rectangular form in C++
To convert polar form to rectangular form i.e.
r(cosθ+isinθ) to a+ib
we have to calculate the value of a and b:-
a = r cos θ
b = r sin θ
and substitute these values in a+ib to get the rectangular form.
C++ program
So, here is the C++ implementation of the above conversion:-
#include<bits/stdc++.h>
using namespace std;
/*===============================================
FUNCTION FOR CONVERSION FROM POLAR TO RECTANGULAR
================================================*/
void convert_to_rect(int r, int angle)
{
int a,b;
//Calculating values of a and b
a=r*cos(angle);
b=r*sin(angle);
//Displaying rectangular form
cout<<"Rectangular form is: "<<a<<" + i("<<b<<")"<<endl;
}
/*======================================
MAIN FUNCTION
=======================================*/
int main()
{
//Initializing r and angle(in radian)
int r,angle;
r=5;
angle=10;
//Displaying Polar form
cout<<"Polar form is: "<<r<<"*(cos("<<angle<<")+isin("
<<angle<<"))"<<endl;
//Passing r and angle to convert_to_rect function
convert_to_rect(r,angle);
return 0;
}
Output:-
Polar form is: 5*(cos(10)+isin(10)) Rectangular form is: -4 + i(-2)
Leave a Reply