Is it possible to convert from Cartesian (x,y,z) to Spherical with 3 points in Python?

Viewed 35

I am actually trying to get rotation(x,y,z) with 3 points in Python. I think converting from Cartesian (x,y,z) to Spherical is the best way to solve it. However, I do not know how to calculate with 3 points.

import numpy as np

#3 vertex points
p1=[x1,y1,z1]
p2=[x2,y2,z2]
p3=[x3,y3,z3]

def cart2sph(x,y,z): #Converting from Cartesian to Spherical
    X_Y = x**2 + y**2
    r = m.sqrt(X_Y + z**2)         
    v = m.atan2(m.sqrt(X_Y),z)
    z = m.atan2(y,x) 
    return r,z ,v
1 Answers

This is more a math stack exchange type of question but nonetheless here is the code, for the explanation you are going to have to search it up or ask on math stackexchange.

Resource used for formula: Cartesian to Spherical coordinates Calculator

import math

def cart2sph(x, y, z):
    r = math.hypot(x, y, z)
    t = math.atan2(y, x)
    z = math.atan2(math.hypot(x, y), z)
    return r, t, z
Related