Turn x,y angles into vector

Viewed 34

I have a direction defined by two angles alpha and beta which I need to convert to a vector of length 1.

alpha and beta define the angle between the z-axis and the projection of the desired vector on the x,z-plane (or y,z-plane respectively). Please see the below image for illustration.

enter image description here

Note the difference to ordinary spherical coordinates, where alpha is the angle between the desired vector and the z-axis and beta describes the rotation of the vector around the z-axis.

My python implementation below is pretty close to the solution posted here, but it does not quite return the expected results:

from math import sin,cos,sqrt

def get_vector(alpha,beta):
    alpha=alpha/180*pi
    beta=beta/180*pi
    x=sin(alpha)*cos(beta)
    z=cos(alpha)*cos(beta)
    y=sin(beta)
    l=sqrt(x**2+y**2+z**2)
    target_vector=[round(x/l,3),round(y/l,3),round(z/l,3)]
    return target_vector

print((90,0)," -> ",get_vector(90,0))
print((-90,0)," -> ",get_vector(-90,0))
print((0,90)," -> ",get_vector(0,90))
print((0,-90)," -> ",get_vector(0,-90))
print((0,0)," -> ",get_vector(0,0))
print((180,180)," -> ",get_vector(180,180))
print((90,90)," -> ",get_vector(90,90))
print((45,0)," -> ",get_vector(45,0))

Here are the expected results for some angles:

- alpha=90, beta=0 (pointing to x): (1,0,0)
- alpha=-90, beta=0 (pointing to -x): (-1,0,0)
- alpha=0, beta=90 (pointing to y): (0,1,0)
- alpha=0, beta=-90 (pointing to -y): (0,-1,0)
- alpha=0, beta=0 (pointing upwards): (0,0,1)
- alpha=180, beta=180 (pointing downwards): (0,0,-1)
- alpha=90, beta=90 (pointing diagonal on x,y-plane): (0.707,0.707,0)
- alpha=45, beta=0 (pointing diagonal on x,z-plane): (0.707,0,0.707)

What my solution returns:

- alpha=90, beta=0 (pointing to x): (1,0,0)
- alpha=-90, beta=0 (pointing to -x): (-1,0,0)
- alpha=0, beta=90 (pointing to y): (0,1,0)
- alpha=0, beta=-90 (pointing to -y): (0,-1,0)
- alpha=0, beta=0 (pointing upwards): (0,0,1)
- alpha=180, beta=180 (pointing downwards): (0,0,1) -> WRONG
- alpha=90, beta=90 (pointing diagonal on x,y-plane): (0,1,0) -> WRONG
- alpha=45, beta=0 (pointing diagonal on x,z-plane): (0.707,0,0.707)

This is as close I could get until now, but the result is still wrong for certain angles.

0 Answers
Related