title: Turn x,y angles into vector
body: <p>I have a direction defined by two angles <code>alpha</code> and <code>beta</code> which I need to convert to a vector of length 1.</p>
  <p><code>alpha</code> and <code>beta</code> 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.</p>
  <p><a href="https://i.stack.imgur.com/dVZdr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dVZdr.png" alt="enter image description here" /></a></p>
  <p>Note the difference to ordinary spherical coordinates, where <code>alpha</code> is the angle between the desired vector and the z-axis and <code>beta</code> describes the rotation of the vector <em>around</em> the z-axis.</p>
  <p>My python implementation below is pretty close to the solution posted <a href="https://stackoverflow.com/questions/30011741/3d-vector-defined-by-2-angles">here</a>, but it does not quite return the expected results:</p>
  <pre><code>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),&quot; -&gt; &quot;,get_vector(90,0))
  print((-90,0),&quot; -&gt; &quot;,get_vector(-90,0))
  print((0,90),&quot; -&gt; &quot;,get_vector(0,90))
  print((0,-90),&quot; -&gt; &quot;,get_vector(0,-90))
  print((0,0),&quot; -&gt; &quot;,get_vector(0,0))
  print((180,180),&quot; -&gt; &quot;,get_vector(180,180))
  print((90,90),&quot; -&gt; &quot;,get_vector(90,90))
  print((45,0),&quot; -&gt; &quot;,get_vector(45,0))
  </code></pre>
  <p>Here are the expected results for some angles:</p>
  <pre><code>- 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)
  </code></pre>
  <p>What my solution returns:</p>
  <pre><code>- 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) -&gt; WRONG
  - alpha=90, beta=90 (pointing diagonal on x,y-plane): (0,1,0) -&gt; WRONG
  - alpha=45, beta=0 (pointing diagonal on x,z-plane): (0.707,0,0.707)
  </code></pre>
  <p>This is as close I could get until now, but the result is still wrong for certain angles.</p>