about summary refs log tree commit diff
path: root/poltocart.py
blob: 2db67236c118d48cd10f748250922193878575bf (plain)
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
# 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])