0

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.

Sim Son
  • 310
  • 1
  • 10
  • `(1, α, β)` already is a vector in spherical coordinates. Do you mean you want to convert it to another coordinate system? If so, which one? What programming language are you using? What have you done? Please consult the [help] articles, especially "[ask]". See also "[How much research effort is expected of Stack Overflow users?](//meta.stackoverflow.com/q/261592/90527)". Note you should [search](/help/searching) SO, as there's likely already an answer. – outis Sep 18 '22 at 08:27
  • [Converting spherical coordinates into Cartesian and then converting back into Cartesian isn't giving the desired output](https://stackoverflow.com/questions/69462888/converting-spherical-coordinates-into-cartesian-and-then-converting-back-into-ca) is related to this, and could be of help to solve this. – Luuk Sep 18 '22 at 08:34
  • @outis well, it's not quite like spherical coordinates are defined. The angles in spherical coordinates have a different reference (not both angles are with respect to the same axis). – Sim Son Sep 18 '22 at 09:00
  • @Luuk radians vs degrees is not the issue here. I've updated the question, please see my code for reference. – Sim Son Sep 18 '22 at 09:09
  • @SimSon: ok, now you have posted your code I see that the link, which is not only about radians vs. degrees, but also shows how to do the conversion might be give too quick. I think the problem lies in the fact that `sqrt(-1*-1)` = 1, and not `-1` – Luuk Sep 18 '22 at 09:40
  • @Luuk at a first glance it looks like I loose information to that square root, but as `l=sqrt(...)` is only used to normalize the components, this should have no effect on the signedness. – Sim Son Sep 18 '22 at 09:46
  • When changing the caluclation of l to: `l=sqrt(x**2+y**2+z**2) * (1,-1)[x*y*z<0]` the result of `(180, 180)` seems to be OK, but problems arise with other results. (It's been too long since I did this kind of math) – Luuk Sep 18 '22 at 09:52
  • A vector pointing to `(0,0,1)`, with a length of -1, might be the same as `(0,0,-1)` ? – Luuk Sep 18 '22 at 09:54
  • @Luuk that's certainly true as the length is just a factor that could be multiplied with each component (so all components become negative for negative length). But as you pointed out, that's not a universal solution. It's getting even more disgusting for arbitrary angles between those special cases I posted :D – Sim Son Sep 18 '22 at 09:58
  • I see the difference. A major issue with this system is that there aren't unique coordinates for most points in the X-Y plane (only points on the X-Y axes can be specified). Instead, coordinates only specify quadrants (as arcs of angle π/2). Consider (α, β) = (-π/2, -π/2): this could be any point on (cos(t+π), sin(-t), 0), where t∈(0…π/2). To resolve, the system needs to include an angle of the vector projected onto the X-Y plane (relative to either the X or Y axis). – outis Sep 18 '22 at 19:16
  • The question that Luuk linked to is basically this question. `α` here is `π/2 - α` in the other, and the other is for an unspecified length (making this question a clearer statement of the issue). The answer to the other has a math error in its calculation of `y` (consider (π/4, π/4)). – outis Sep 18 '22 at 20:41

0 Answers0