Last active
September 28, 2018 04:03
-
-
Save theArjun/733e295c0349bf67e0dcc8df7f35cb45 to your computer and use it in GitHub Desktop.
Polar Coordinates to Cartesian Coordinates and Vice Versa
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<iostream> | |
| #include<cmath> | |
| #include<math.h> | |
| using namespace std; | |
| class Cartesian{ | |
| private: | |
| float xCo, yCo; | |
| public: | |
| Cartesian():xCo(0),yCo(0){} | |
| Cartesian(float x, float y):xCo(x),yCo(y){} | |
| float radius(){ | |
| return sqrt(pow(xCo,2)+pow(yCo,2)); | |
| } | |
| float angle(){ | |
| return atan(yCo/xCo); | |
| } | |
| void convertToPolar(){ | |
| cout << "In Polar Co-ordinates : " << endl; | |
| cout << "(" << xCo << "," << yCo << ")" << ":" << "(" << radius() << "," << angle()*180/3.1415 << ")" << endl; | |
| } | |
| }; | |
| class Polar{ | |
| private: | |
| float radius, angle; | |
| public: | |
| Polar():radius(0),angle(0){} | |
| Polar(float r, float a):radius(r),angle(a){} | |
| float xCo(){ | |
| return radius * cos(angle*3.1415/180); | |
| } | |
| float yCo(){ | |
| return radius * cos(angle*3.1415/180); | |
| } | |
| void convertToCartesian(){ | |
| cout << "In Cartesian Co-ordinates : " << endl; | |
| cout << "(" << radius << "," << angle << ")" << ":" << "(" << xCo() << "," << yCo() << ")" << endl; | |
| } | |
| }; | |
| int main(){ | |
| Cartesian c1(10,10); | |
| c1.convertToPolar(); | |
| Polar p1(14.141,45); | |
| p1.convertToCartesian(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment