Three Dimension Coordinate Systems Converter Source Code

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

3/1/2019 Three Dimension Coordinate Systems Converter source code - Cprogramming.

com

Three Dimension Coordinate Systems Converter source code


This snippet submitted by Ali Nawkhas Murad on 2012-02-24. It has been viewed 22187 times.
Rating of 5.3 with 112 votes

1 //Three Dimension Coordinate Systems Converter


2 #include <iostream>
3 #include <math.h>
4 #include<cstring>
5 #include<cstdlib>
6
7 #define PI 3.1415926535897932384626433832795
8 #define FAC1 (180.0/PI)
9 #define FAC2 (PI/180.0)
10 using namespace std;
11
12 typedef struct
13 {
14 double d1;
15 double d2;
16 double d3;
17 string str;
18 string str1[2];
19 string str2[2];
20 string str3[2];
21 } Coordinate;
22
23 class convt
24 {
25 private:
26 Coordinate cr;//Cartesian (rectangular)
27 Coordinate cc;//Cylinderical
28 Coordinate cs;//Spherical
29 public:
30 convt(){
31 cr.str = "Cartesian coordinate system";
32 cc.str = "Cylindrical coordinate system";
33 cs.str = "Spherical coordinate system";
34 //Cartesian
35 cr.str1[0] = "x = ";
36 cr.str2[0] = "y = ";
37 cr.str3[0] = "z = ";
38 cr.str1[1] = " Unit";
39 cr.str2[1] = " Unit";
40 cr.str3[1] = " Unit\n";
41
42 //Cylindrical
43 cc.str1[0] = "Rho = ";
44 cc.str2[0] = "Phi = ";
45 cc.str3[0] = "z = ";
46 cc.str1[1] = " Unit";
47 cc.str2[1] = " Degree";
48 cc.str3[1] = " Unit\n";
49 //Spherical
50 cs.str1[0] = "R = ";
51 cs.str2[0] = "Theta = ";
52 cs.str3[0] = "Phi = ";
53 cs.str1[1] = " Unit";
54 cs.str2[1] = " Degree";
55 cs.str3[1] = " Degree\n";
56 }
57 void setVarr(double x,double y , double z){cr.d1 = x;cr.d2 = y; cr.d3 = z;}
58 void setVarc(double x,double y , double z){cc.d1 = x;cc.d2 = y; cc.d3 = z;}
59 void setVars(double x,double y , double z){cs.d1 = x;cs.d2 = y; cs.d3 = z;}
60
61
62 void convertr_c()
63 {
64 //convert from Cartesian to Cylindrical
65 cc.d1 = sqrtl(cr.d1*cr.d1 + cr.d2 * cr.d2);//Rho
66 cc.d2 = atan(cr.d2/cr.d1);//Phi
67 cc.d3 = cr.d3;//z
68 }
69 void convertc_s()
70 {
71 //Convert from cylinderical to spherical
72 cs.d1 = sqrtl(cc.d1 * cc.d1 + cc.d3 * cc.d3);//r
73 cs.d2 = atan(cc.d1/cc.d3);//theta
74 cs.d3 = atan((cc.d1 * sin(cc.d2)) /(cc.d1 * cos(cc.d2)));//phi
75 }
76 void converts_r()
77 {
78 //convert from spherical to cartesian
79 cr.d1 = cs.d1 * sin(cs.d2) * cos(cs.d3);
80 cr.d2 = cs.d1 * sin(cs.d2) * sin(cs.d3);
81 cr.d3 = cs.d1 * cos(cs.d2);
82 }

https://www.cprogramming.com/snippets/source-code/three-dimensional-coordinate-converter 1/3
3/1/2019 Three Dimension Coordinate Systems Converter source code - Cprogramming.com
83 friend void disp(convt&);
84 };
85 void disp(convt &conv)
86 {
87 cout << endl << conv.cr.str << endl;//display rectangular
88 cout <<"-------------------------------\n";
89 cout << conv.cr.str1[0] << conv.cr.d1 << conv.cr.str1[1] << endl;
90 cout << conv.cr.str2[0] << conv.cr.d2 << conv.cr.str2[1] << endl;
91 cout << conv.cr.str3[0] << conv.cr.d3 << conv.cr.str3[1] << endl;
92
93 cout << conv.cc.str << endl;//display cylindrical
94 cout <<"-------------------------------\n";
95 cout << conv.cc.str1[0] << conv.cc.d1 << conv.cc.str1[1] << endl;
96 cout << conv.cc.str2[0] << FAC1 * conv.cc.d2 << conv.cc.str2[1] << endl;
97 cout << conv.cc.str3[0] << conv.cc.d3 << conv.cc.str3[1] << endl;
98
99 cout << conv.cs.str << endl;//display spherical
100 cout <<"-------------------------------\n";
101 cout << conv.cs.str1[0] << conv.cs.d1 << conv.cs.str1[1] << endl;
102 cout << conv.cs.str2[0] << FAC1 * conv.cs.d2 << conv.cs.str2[1] << endl;
103 cout << conv.cs.str3[0] << FAC1 * conv.cs.d3 << conv.cs.str3[1] << endl << endl;
104 }
105 int main()
106 {
107 convt con;
108 double dx1,dx2,dx3;
109 char ch;
110
111 for(;;)
112 {
113 cout << "\nThree Dimension Coordinate Systems Converter" << endl;
114 cout << "\n\t1) Rectangular Coordinate System (x, y, z) " << endl;
115 cout << "\t2) Cylinderical Coordinate System (Rho, Phi, z)" << endl;
116 cout << "\t3) Spherical Coordinate System (R, Theta, Phi)" << endl;
117 cout << "\t4) Exit " << endl << endl;
118 cout << "Hint: The used angles are in degrees" << endl << endl;
119 cout << "Choose one option : ";
120 cin >> ch;
121
122 if(ch == '1' || ch == '2' || ch == '3' )
123 {
124 cout << "\nEnter coordinates of the chosen system:";
125 cout << "\nEnter 1st axis :";
126 cin >> dx1;
127 cout << "Enter 2nd axis :";
128 cin >> dx2;
129 cout << "Enter 3rd axis :";
130 cin >> dx3;
131 switch(ch)
132 {
133 case '1':
134 {
135 con.setVarr(dx1,dx2,dx3);
136 con.convertr_c();
137 con.convertc_s();
138 disp(con);
139 break;
140 }
141 case '2':
142 {
143 dx2 = dx2 * FAC2;
144 con.setVarc(dx1,dx2,dx3);
145 con.convertc_s();
146 con.converts_r();
147 disp(con);
148 break;
149 }
150 case '3':
151 {
152 dx2 = dx2 * FAC2;
153 dx3 = dx3 * FAC2;
154 con.setVars(dx1,dx2,dx3);
155 con.converts_r();
156 con.convertr_c();
157 disp(con);
158 break;
159 }
160 }
161 }
162 else if( ch == '4')
163 break;
164 else
165 cout << "Wrong number choosen. Try to choose right option :" << endl << endl;
166 }
167 return 0;
168 }

10 Rate it

More C and C++ source code snippets

https://www.cprogramming.com/snippets/source-code/three-dimensional-coordinate-converter 2/3
3/1/2019 Three Dimension Coordinate Systems Converter source code - Cprogramming.com

https://www.cprogramming.com/snippets/source-code/three-dimensional-coordinate-converter 3/3

You might also like