about summary refs log tree commit diff
path: root/poltocart.py
diff options
context:
space:
mode:
Diffstat (limited to 'poltocart.py')
-rw-r--r--poltocart.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/poltocart.py b/poltocart.py
new file mode 100644
index 0000000..2db6723
--- /dev/null
+++ b/poltocart.py
@@ -0,0 +1,41 @@
+# import
+import numpy as np
+import matplotlib.pyplot as plt
+
+# define r, theta, phi
+r = 0
+theta = 0
+phi = 0
+cart = [r, theta, phi]
+
+def poltocart(pol):
+    print(pol)
+
+    x = pol[0]
+    y = pol[1]
+    z = pol[2]
+
+    # radius
+    r = np.sqrt(np.power(x, 2) + np.power(y, 2) + np.power(z, 2))
+
+    # theta
+    theta = np.arccos( z / r )
+
+    # phi
+    if x > 0:
+        phi = np.arctan(y/x)
+    elif x == 0:
+        phi = np.sign(y)*(np.pi/2)
+    elif x < 0 and y >= 0:
+        phi = np.arctan(y/x) + math.pi
+    elif x < 0 and y < 0:
+        phi = np.arctan(y/x) - math.pi
+
+    # write to cartesian list
+    cart[0] = r
+    cart[1] = theta
+    cart[2] = phi
+
+    return cart
+
+poltocart([4, 3, 5])