Skip to content

Instantly share code, notes, and snippets.

@yaodong
Created February 6, 2017 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaodong/788b4d46dedb51408740acc17c501e53 to your computer and use it in GitHub Desktop.
Save yaodong/788b4d46dedb51408740acc17c501e53 to your computer and use it in GitHub Desktop.
def rotate(row, angle, dim1, dim2):
radian = angle * pi / 180.
cos_alpha, sin_alpha = cos(radian), sin(radian)
x, y = row[dim1], row[dim2]
row[dim1] = cos_alpha * x - sin_alpha * y
row[dim2] = sin_alpha * x + cos_alpha * y
return row
def cartesian_to_spherical(x, y, z):
r = sqrt(pow(x, 2) + pow(y, 2) + pow(z, 3))
if z == 0:
phi = pi / 2
else:
phi = arctan(sqrt(pow(x, 2) + pow(y, 2)) / z)
if x == 0:
theta = 0
else:
theta = arctan(y / x)
return r, phi, theta
def spherical_to_cartesian(r, phi, theta):
x = r * sin(phi) * cos(theta)
y = r * sin(phi) * sin(theta)
z = r * cos(phi)
return x, y, z
def spherical_rotate_to_cartesian(coord, phi_inc=0, theta_inc=0):
r, phi, theta = coord
return spherical_to_cartesian(r, phi + phi_inc, theta + theta_inc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment