<p>The stars should fit <span class="math-container">$r = \sqrt{x^2 + y^2 + z^2}$</span> but seem to be plotted in the <span class="math-container">$x = r$</span> plane instead.</p>

<p><a href="https://en.wikipedia.org/wiki/Equatorial_coordinate_system#Rectangular_coordinates" rel="nofollow noreferrer">Conventionally</a> the <em>x</em> axis is at (<em>&alpha;</em>=0<sup>h</sup>, <em>&delta;</em>=0&deg;),
the <em>y</em> axis is at (<em>&alpha;</em>=6<sup>h</sup>, <em>&delta;</em>=0&deg;),
and the <em>z</em> axis is at <em>&delta;</em>=+90&deg;.</p>

<p>Using right triangles instead of isosceles triangles,</p>

<p><img src="https://i.sstatic.net/YMLqo.png" alt="right triangles"></p>

<p>Then <span class="math-container">$h = r \cos \delta$</span>, and
<span class="math-container">$$\begin{align}
x &amp;= r \cos \delta \cos \alpha \\
y &amp;= r \cos \delta \sin \alpha \\
z &amp;= r \sin \delta
\end{align}$$</span></p>

<p>Most math libraries use radians for angles.
Passing angles in other units to trig functions may result in a celestial sphere covered somewhat evenly but incorrectly.</p>

CC BY-SA 4.0
2019-12-21T13:00:26.490Z
Used corrected the maths, moved from degrees to radians and now the stars are spread out even better an much more evenly, thanks!
SidS
CC BY-SA 4.0
2019-12-21T03:55:50.507Z
2019-12-21T03:55:50.507Z
Mike G
<p>Using the Right Ascension and Declination values from the HYG Database from <a href="http://www.astronexus.com/hyg" rel="nofollow noreferrer">The Astronomy Nexus</a>:</p>

<p>Right ascension is measured in hours and declination is measured in degrees.</p>

<p>1 - Convert right ascension and declination into degrees</p>

<pre><code>float getAngle(float value, char units)
{
    if (units == 'h')
    {
        // 15 degrees = 1 hour
        // 360/24 = 15

        return value * 15;
    }
    else
    {
        return value;
    }
}
</code></pre>

<p>When right ascension is inputted, the input will get multiplied by 15, because RA is measured in hours.</p>

<p>When declination is entered, the input value is returned, this is because Dec is already measured in degrees.</p>

<p>2 - Work out the vectors</p>

<p><a href="https://i.sstatic.net/P6dYg.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/P6dYg.jpg" alt="Trigonometry diagram"></a></p>

<p>Two isosceles triangles can be derived, one for the horizontal plane and another for the vertical plane. The angles are alpha and delta, right ascension and declination respectively. Using these angles and knowing that some of the distances are equal to the distance between the centre of the celestial sphere to the star, the following code should work.</p>

<pre><code>Vector3 getVectors(float RA, float Dec)
{
    float x = distanceStars;
    float z = (2 * distanceStars * (Mathf.Sin((getAngle(RA, 'h'))/ 2)));
    float y = (2 * distanceStars * (Mathf.Sin((getAngle(Dec, 'd'))/ 2)));

    return new Vector3(x, y, z);
}
</code></pre>

<p>Unfortunately, my code doesn't seem to work as this is the output that I am getting:</p>

<p><a href="https://i.sstatic.net/e6ilG.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/e6ilG.png" alt="Graphical output"></a></p>

<p>As you can see, the stars (white dots) are not spreading out evenly around the Earth (which is the black sphere in the middle).</p>

<p>It would be great if someone could help me with this.</p>

CC BY-SA 4.0
2019-12-20T21:31:36.410Z
What Barry said. Also, your formulae don't look like the usual [polar to cartesian](https://en.m.wikipedia.org/wiki/Spherical_coordinate_system#Cartesian_coordinates) formulae. And you probably should be using `double`, not `float`.
PM 2Ring
CC BY-SA 4.0
2019-12-20T21:34:27.640Z
I followed the link that @ebv commented, and now my program works perfectly
SidS
CC BY-SA 4.0
2019-12-20T22:14:22.760Z
@barrycarter nope, not using radians
SidS
<positional-astronomy><declination><right-ascension>
Help with Converting Right Ascension and Declination into 3D Vectors
SidS
# Help with Converting Right Ascension and Declination into 3D Vectors

(Asked by: username_0 on 2019-12-20)

Using the Right Ascension and Declination values from the HYG Database from [The Astronomy Nexus](http://www.astronexus.com/hyg):

Right ascension is measured in hours and declination is measured in degrees.

1 - Convert right ascension and declination into degrees

```
float getAngle(float value, char units)
{
    if (units == 'h')
    {
        // 15 degrees = 1 hour
        // 360/24 = 15

        return value * 15;
    }
    else
    {
        return value;
    }
}

```

When right ascension is inputted, the input will get multiplied by 15, because RA is measured in hours.

When declination is entered, the input value is returned, this is because Dec is already measured in degrees.

2 - Work out the vectors

[![Trigonometry diagram](https://i.sstatic.net/P6dYg.jpg)](https://i.sstatic.net/P6dYg.jpg)

Two isosceles triangles can be derived, one for the horizontal plane and another for the vertical plane. The angles are alpha and delta, right ascension and declination respectively. Using these angles and knowing that some of the distances are equal to the distance between the centre of the celestial sphere to the star, the following code should work.

```
Vector3 getVectors(float RA, float Dec)
{
    float x = distanceStars;
    float z = (2 * distanceStars * (Mathf.Sin((getAngle(RA, 'h'))/ 2)));
    float y = (2 * distanceStars * (Mathf.Sin((getAngle(Dec, 'd'))/ 2)));

    return new Vector3(x, y, z);
}

```

Unfortunately, my code doesn't seem to work as this is the output that I am getting:

[![Graphical output](https://i.sstatic.net/e6ilG.png)](https://i.sstatic.net/e6ilG.png)

As you can see, the stars (white dots) are not spreading out evenly around the Earth (which is the black sphere in the middle).

It would be great if someone could help me with this.

**Tags:** positional-astronomy, declination, right-ascension

> **Comments:**
> **username_1**: What Barry said. Also, your formulae don't look like the usual [polar to cartesian](https://en.m.wikipedia.org/wiki/Spherical_coordinate_system#Cartesian_coordinates) formulae. And you probably should be using `double`, not `float`.  
> **username_0**: I followed the link that @ebv commented, and now my program works perfectly  
> **username_0**: @barrycarter nope, not using radians  

---

## 1 answers

### Answer by username_2 on 2019-12-21. Score: 3 | Accepted answer

The stars should fit $r = \sqrt{x^2 + y^2 + z^2}$ but seem to be plotted in the $x = r$ plane instead.

[Conventionally](https://en.wikipedia.org/wiki/Equatorial_coordinate_system#Rectangular_coordinates) the *x* axis is at (*α*=0h, *δ*=0°),
the *y* axis is at (*α*=6h, *δ*=0°),
and the *z* axis is at *δ*=+90°.

Using right triangles instead of isosceles triangles,

![right triangles](https://i.sstatic.net/YMLqo.png)

Then $h = r \cos \delta$, and
$$\begin{align}
x &= r \cos \delta \cos \alpha \\
y &= r \cos \delta \sin \alpha \\
z &= r \sin \delta
\end{align}$$

Most math libraries use radians for angles.
Passing angles in other units to trig functions may result in a celestial sphere covered somewhat evenly but incorrectly.

> **Comments:**
> **username_0**: Used corrected the maths, moved from degrees to radians and now the stars are spread out even better an much more evenly, thanks!  

---

