Skip to content

Instantly share code, notes, and snippets.

@bhachauk
Created September 15, 2018 04:43
Show Gist options
  • Save bhachauk/7fc9aee06acc4d35a2d2cc6a36d0136d to your computer and use it in GitHub Desktop.
Save bhachauk/7fc9aee06acc4d35a2d2cc6a36d0136d to your computer and use it in GitHub Desktop.
spherical <--> cart
def sph2cart1(r, th, phi):
x = r * cos(phi) * sin(th)
y = r * sin(phi) * sin(th)
z = r * cos(th)
return x, y, z
def cart2sph1(x, y, z):
r = sqrt(x**2 + y**2 + z**2) + 1e-15
th = acos(z / r)
phi = atan2(y, x)
return r, th, phi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment