C++ PROGRAM TO CONVERT CARTESIAN COORDINATES TO POLAR COORDINATE

In mathematics, a Cartesian coordinate system is a coordinate system that specifies each point uniquely in a plane by a set of numeric points.
Cartesian Coordinates is represented by (x,y).
In mathematics, the polar coordinate system is a two-dimensional coordinate system in which each point on a plane is determined by a distance from a reference point known as radius and an angle from a reference direction known as theta or simply angle.
Polar Coordinates system is represented by (r,θ).
For the negative value of r in polar coordinates, what to do? Watch the given below youtube video to cover it.
Check this website for more knowledge: https://openstax.org/books/calculus-volume-2/pages/7-3-polar-coordinates
C++ PROGRAM :
DEFINE TWO CLASSES POLAR AND RECTANGLE REPRESENT POINTS IN POLAR AND RECTANGULAR SYSTEM. USE SUITABLE MEMBER FUNCTIONS TO CONVERT FROM ONE SYSTEM TO ANOTHER. WRITE A PROGRAM TO TEST ABOVE CLASSES.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
#include <iostream> #include <math.h> // USING STD:: WHEN DIRECTLY IO FUNCTIONS ARE NOT WORKED. class polar; class rectangle { int x,y; public: rectangle(int x=0, int y=0); polar topolar(); void display(); }; class polar{ int r,theta; public: polar(int r=0, int theta=0); rectangle torectangle(); void display(); }; rectangle::rectangle(int x, int y){ this->x=x; this->y=y; } polar rectangle::topolar() { return polar(sqrt(x*x + y*y),tan(y/x)); } void rectangle::display() { std::cout<<"( "<<x<<", "<<y<<")"; } polar::polar(int r, int theta) { this->r=r; this->theta=theta; } rectangle polar::torectangle() { return rectangle(r*cos(theta), r*sin(theta)); } void polar::display() { std::cout<<"( "<<r<<", "<<theta<<")"; } int main() { int x, y, r, theta; std::cout<<"Enter value in Rectangle co ordinates "<<std::endl<<"X= "; std::cin>>x; std::cout<<"Y= "; std::cin>>y; std::cout<<std::endl<<"Enter value in polar coordinates "<<std::endl<<"R= "; std::cin>>r; std::cout<<"Theta= "; std::cin>>theta; rectangle point1(x,y); polar point2(r,theta); std::cout<<"Point 1: \nIn Rectangle co ordinate is: "; point1.display(); std::cout<<"In polar co ordinate is "; point1.topolar().display(); std::cout<<"\n\nPoint 2: \nIn Rectangle co ordinate is: "; point2.torectangle().display(); std::cout<<"\nIn polar co-ordinate is "; point2.display(); } |
INPUT – OUTPUT :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Enter value in Rectangle co ordinates X= 5 Y= 6 Enter value in polar coordinates R= 5 Theta= 45 Point 1: In Rectangle co ordinate is: ( 5, 6)In polar co ordinate is ( 7, 1) Point 2: In Rectangle co ordinate is: ( 2, 4) In polar co-ordinate is ( 5, 45) |
FAQ:
1> How do you convert from Polar to Rectangular?
=> To convert from Polar Coordinates (r,θ) to Cartesian Coordinates (x,y) :
1 2 |
x = r × cos( θ ) y = r × sin( θ ) |
2> How do you convert from rectangular to polar form?
=> Using Pythagoras Theorem, To convert from Cartesian Coordinates (x,y) to Polar Coordinates (r,θ).
Step 1: Square both sides of r = 5 and substitute for r^2.
r^2 = x^2 + y^2
Step 2: Determine the value of tan θ and equate this to y/ x .
tan θ = y/x
3> What is Polar and Cartesian coordinate?
Using Rectangular coordinates, or cartesian coordinates we mark a point by how far along and how far up it is.
Using Polar Coordinates we mark a point by how far away, and what angle it is.
