From e568c202455b93ccebfacc8ab4e032f12f7b3233 Mon Sep 17 00:00:00 2001 From: HanEmile Date: Sat, 11 Mar 2017 01:15:35 +0100 Subject: poltocart.py working :D --- poltocart.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 poltocart.py (limited to 'poltocart.py') 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]) -- cgit 1.4.1