
Learn how to write Stellarium scripts with Typescript.
Spiral galaxy M51. The galaxy has a clearly visible spiral structure with an abundance of H II
regions shining red due to the radiation released by newly born stars.
Spiral galaxies are among the most beautiful observable objects in the night sky. Take, for instance, the Whirlpool Galaxy shown in Image ?. The picture was taken with the NASA/ESA Hubble Space Telescope. It's one of my favorite images as it features all the components of a spiral galaxy in great detail. One can see a clear spiral structure with a lot of bright red H II regions. In addition, the spiral arms show a fine pattern of darker lines which are formed by interstellar dust clouds. These clouds absorb the light of the stars behind them and therefore appear dark. They also provide gas and heavy elements required to form new stars and planetary systems. The spiral arms are the places where new stars are born.
Pretty as it is, it's just an image. What might a rotating galaxy look like? To find out, one might use N-body simulations but they are quite complicated and require more computational time than desirable. What we need is a "shortcut", a way to spare ourselves the complexity of calculating the interactions between thousands of stars without oversimplifying the problem. For this simulation the "shortcut" is the density wave theory. It will enable us to render a realistic-looking galaxy without having to compute the complex N-body interactions of a real galaxy.
The interactive view requires JavaScript and WebGL 2. The video shows the simulated rotation.
The source code of the galaxy renderer is available on GitHub: as a C++ program and as the TypeScript version that runs in this article.
As early as 1925, the Swedish astronomer Bertil Lindblad recognized that our Milky Way was rotating. He also recognized that the angular velocity of this rotation decreases outwards. Therefore the spiral arms could not be a mere static accumulation of stars and dust. Due to the differential rotation of the Milky Way, they would wind up and dissolve within a short time (the "winding problem"; Animation 2). To solve this problem, he developed a first version of density wave theory. It is based on the assumption that a density wave rotates through the galaxy at a constant angular speed, the so-called pattern speed. This pattern speed generally differs from the angular speed at which the stars and gas orbit. In the mid-1960s, Lindblad's ideas were developed further by the mathematician C. C. Lin and the astrophysicist Frank Shu [1].
In order to classify spiral galaxies, a classification scheme was devised by Edwin Hubble in 1926. This classification scheme is still in use today. Hubble distinguished between elliptical galaxies and spiral galaxies. Elliptical galaxies were labeled E0 to E7, with their apparent flattening increasing towards E7.
Type S0 (lenticular galaxies) is an intermediate form of galaxy that marks the transition to the spiral galaxies. Spiral galaxies themselves are divided into two categories: normal spirals (Sa to Sc) and barred spirals (SBa to SBc).
Classification of galaxies according to Edwin Hubble; Courtesy of: Ville Koistinen (CC BY-SA 3.0)
For the rest of this article only spiral galaxies will matter. According to the classification scheme the following observations can be made for the different types of spiral galaxies:
A series of ellipses, each rotated slightly relative to the previous one as their size increases.
Orbits of a galaxy similar to those predicted by the density wave theory. The bulge area is marked
in yellow. The innermost spiral structure slightly resembles a bar. The green circle marks the edge
of the galaxy.
The first challenge in modeling a realistic-looking galaxy is getting its shape right. One theory for explaining the spiral structure is the density wave theory. It explains the typical spiral arm structure as a result of regions with higher stellar density, caused by slight variations in the stars' orbits. These regions are not static. Stars will move in and out of them as they orbit the galaxy. The spiral arms can be modeled with elliptical orbits that are rotated slightly as their radii grow. Although this is a simplification of the orbits, it will serve us well for getting a realistic-looking shape of the galaxy. I will use the following simplified assumptions for the orbits in the galaxy:
To be clear: in reality, orbits look different! They are not closed like an ellipse but open, like a rosette, because the mass of a galaxy is not concentrated at its center. It is spread over the bulge and the disk and, above all, over a spherical halo of dark matter that makes up most of the total mass. However, the goal here is not to calculate the orbits as accurately as possible but only to get the visual impression right, and for that this approximation is good enough.
Every spiral galaxy probably has a supermassive black hole at its center, but even at a billion solar masses it accounts for only a tiny fraction of the total mass. Image ? shows elliptical orbits whose orientation shifts slightly as their size increases. Even this simple construction clearly shows a spiral structure. The spiral arms start in the center of the galaxy and extend to the edge. No distinct bulge is visible.
Since observations show that most spiral galaxies have a distinct bulge area, the visual model needs to be tweaked to represent this fact properly. The bulge area is often circular in shape and so the overall model should allow such shapes. For this reason, the model is extended to always have a round core.
The eccentricities then increase towards the bulge-disk boundary (yellow circle in Image ?) and from there they change again towards the edge of the galactic disk. Beyond the disk, the eccentricities decrease again until the orbits become circular at twice the radius of the galactic disk. This represents a galactic halo, but the simulation renders only a few stars there. Real objects in the halo of a galaxy, such as globular clusters, move on highly eccentric orbits that are inclined at arbitrary angles to the disk. For simplicity, these orbits are omitted from Image ? because they don't contribute much to the overall shape of the galaxy.
In real galaxies, stars do not move on elliptical orbits, because the mass is spread across the entire galaxy. Stellar orbits are not closed. During each revolution, a star completes between one and two radial oscillations between its innermost and outermost points (pericenter and apocenter), so its orbit traces out a rosette [6]. Simulations also show that spiral arms continually change the stars' orbits, so that stars can migrate far inwards or outwards (radial migration) [7], [8]. Finally, ESA's Gaia mission, which has measured the positions and velocities of millions of stars, has shown that stellar orbits in the Galactic disk are significantly perturbed [9].
To create more than two spiral arms in the simulation, we have to leave physics behind. The simplest way is to give up pure ellipses and add artificial sinusoidal perturbations to the orbits. The galaxy renderer therefore superimposes a small circular motion on each orbit. This motion runs opposite to the orbital motion and completes \(2N\) cycles per revolution, where \(N\) is the number of perturbations per ellipse. As a result, the distance to the center oscillates \((2N+1)\) times per revolution, and \((2N+1)\) additional arms are superimposed on the two arms produced by the ellipses (Image ?).
Orbits (top) and rendered galaxy (bottom) without perturbations (\(N = 0\)) and with \(N = 1\) and \(N = 2\). The perturbations add three and five arms, respectively, to the two arms of the ellipses. The galaxy at the top of this article uses \(N = 2\).
The perturbation of the ellipses is my own construction. It is merely a tool for creating more spiral arms, and I do not claim that real stellar orbits look like this. Only the ellipses themselves have a physical basis: Lindblad and Kalnajs explained two-armed spirals as nested elliptical orbits, each rotated slightly relative to the next, a picture known as kinematic density waves [4]. According to current understanding, the number of arms is set mainly by the self-gravity of the disk [5].
Once the orbits are known, the galaxy can be populated with stars. This section will provide some background information on the distribution of stars in a spiral galaxy. It leads to a model that distributes the stars in a way similar to what is observed in real galaxies. The basic assumption is that stars should be distributed in such a way that the surface brightness distribution of a real galaxy is matched. The surface brightness distribution of the bulge is similar to that of an elliptical galaxy. It can be estimated using the de Vaucouleurs law:
\begin{equation} \nonumber I_B(R)=I_e\, e^{-7.669\left[\left(R/R_e\right)^{1/4}-1\right]} \end{equation}Here \(R_e\) is the radius that contains half of the bulge light, and \(I_e\) is the surface brightness at that radius. According to Freeman [2] the surface brightness distribution of the galactic disk can be described using an exponential law:
\begin{equation} \nonumber I(R)=I_0 e^{\frac{-R}{R_D}} \end{equation}Here \(I_0\) is the disk surface brightness extrapolated to the center and \(R_D\) is the disk scale length. At \(R=R_D\), the disk brightness is \(1/e\), or about 37 percent of its central value. The red curve in the surface brightness diagram adds the light from the disk and the bulge at each radius.
Model of a galaxy featuring a star distribution that closely resembles the surface
brightness distribution of observed galaxies.

The previous section discussed the galaxy and its distribution of stars. For a realistic rendering it is crucial to apply a realistic coloring scheme as stars in a galaxy can only have specific colors. The color of a star greatly depends on its surface temperature. The underlying physical principle is black-body radiation.
Color temperature according to Planck's law of radiation.
A realistic color scheme should be restricted to colors which are plausible according to Planck's law. In the model, the surface temperatures of the simulated stars range from 4,000 to 8,000 K. In reality, a star's surface temperature changes over its lifetime as it moves along its evolutionary track in the Hertzsprung–Russell diagram.
Calculating the color based on a given temperature is a pretty complex process which I won't discuss here in detail. Instead, I will use a program called specrend.c which was written by John Walker [3]. For those interested in the physical background of his program, I recommend reading his website where he explains the algorithm in depth. For all others it will suffice to say that his program computes the RGB values for a given temperature.
Image ? shows the result of applying coloring based on Planck's law to the galaxy shown in Image ?. The exponential increase in star density towards the center is now easier to see because the stars differ in color. However, the galaxy as a whole still appears to consist of more or less unrelated points. At this point, the simulation already uses about 40,000 stars. Simply rendering more stars doesn't seem to be a viable option due to performance constraints.
In reality, galaxies consist of billions of stars. Even in a photographic image of a spiral galaxy, the resolution isn't high enough to display distinct stars (unless they are extraordinarily bright). What we see in images is a diffuse glow that would resolve into individual stars only if we could zoom in much further. For this reason, the model needs an additional feature representing all those stars that are too small to be seen individually. In addition to the stars, nebulae will be added to the galaxy. When using OpenGL, one can use the point sprite extension to get the desired effect. Half of the nebulae follow the distribution of the stars; the other half are distributed evenly over the galactic disk. The result is shown in Image ?.
Image ? is already a pretty good approximation of the galaxy, but one crucial thing is still missing: H II regions. If you look carefully at Image ? you will see an abundance of bright red nebulae distributed across the spiral arms. These nebulae are so-called H II regions, huge interstellar clouds composed of hydrogen. They are galactic nurseries full of newly born bright hot stars whose emissions make the surrounding hydrogen glow in a characteristic red color.
To match the appearance of a real galaxy, the model also needs H II regions, and they should be located in the spiral arms just like in reality. This is achieved with a trick: first, H II regions are distributed evenly across the galactic disk, much like ordinary stars, except that they are rendered as red nebulae. To restrict their visibility to the spiral arms, the farther apart the neighboring orbits are at the position of an H II region, the smaller it is drawn, and beyond a certain distance it is not drawn at all. Since the orbits are close together only in the spiral arms, H II regions appear only there, but they move exactly like ordinary stars.

So far, the motion of the galaxy has been ignored. This section will finally discuss the motion of a galaxy. So let's look at a galaxy's rotation curve. A rotation curve shows how fast the stars and gas components of a galaxy move as a function of their distance from the galactic center. In NGC 3198 the measured speed initially rises. From 20 to 44 kpc it stays between 146 and 154 km/s (Image ?, curve B).
Interestingly, this contradicts models based on the distribution of a galaxy's visible mass (Image ?; curve A). This discrepancy led to the hypothesis of dark matter, whose mass could explain the flat shape of curve B.
The only thing left to do is to make sure that the rotation curve is similar to the one displayed in Image ?. The final result is shown in Animation 4. It features a galaxy with several spiral arms and H II regions. The galaxy is of Hubble type Sb.