Skip to content

Instantly share code, notes, and snippets.

@agoose77
Last active September 1, 2015 13:10
Show Gist options
  • Save agoose77/22958866365663903552 to your computer and use it in GitHub Desktop.
Save agoose77/22958866365663903552 to your computer and use it in GitHub Desktop.
from math import acos, atan, cos, sin
from mathutils import Vector
def vect_to_polar(vect):
x, y, z = vect
r = vect.length
phi = acos(z / r)
theta = atan(y / x)
return r, phi, theta
def polar_to_vect(polar):
r, phi, theta = polar
x = r * cos(theta) * sin(phi)
y = r * sin(theta) * sin(phi)
z = r * cos(phi)
return Vector((x, y, z))
vect = Vector((0.2, 99.9, 12.7))
assert (vect - polar_to_vect(vect_to_polar(vect))).length_squared < 0.1
print(vect_to_polar(vect))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment