The magazine of the Melbourne PC User Group

Some call it home: The spiral galaxy
Ken Holmes

The title was just meant to get your attention. We are not specifically going to look at the Milky Way but will try to depict a typical spiral galaxy for stereo viewing by using a fairly simple QBasic program.

After the Big Bang, the universe was not absolutely uniform and regions of higher density responded to gravity by contracting and pulling in matter from the surrounding less-dense regions. This happened at all scales, forming stars, galaxies, clusters of galaxies, superclusters of same and ginormous bubbles of emptyish spaces bordered by walls of galaxies. The shape assumed by any congregation of matter is greatly influenced by the net angular momentum that congregation happens to have.

If there is little angular momentum, the components mill about in a fuzzy ball with many near misses or collisions that transfer kinetic energy between units, occasionally whipping some of them out of the ball. This is seen in spherical galaxies and in the so-called globular clusters, of perhaps a mere million stars, in the haloes of spiral galaxies. With somewhat more net angular momentum, the sphere is flattened, as in ellipsoidal galaxies, with the shortest axis being the axis of nett rotation--somewhat akin to the earth's shape.

With greater rotation, a disk is formed with a concentration at the galaxy centre, and the disk thinning further out. A similar mechanism applies in the formation of galaxies or planetary systems. Nature does all of this as a matter of course, having had lots of practice with probably a couple of hundred billion galaxies, each containing perhaps a couple of hundred billion stars. The statistical mathematics to describe these processes are best left to those who devote their lives to such pursuits. Our aims are much less ambitious and we will use some easy maths to get a shape which looks something like that which books and magazines publish. I haven't seen an estimate of the proportion of such galaxies; perhaps it is only a few tens of billions, but, whatever we produce, there are certainly a few out that resemble it.

As the disk thins down, higher density regions in it will also tend to gather material. Processes, which we might refer to vaguely as resonances, lead to the forming of rings, as with Saturn, or the arms of spiral galaxies. Planets and moons result from the bunching up of the rings. The gravitational attraction of adjacent parts of an arm obviously favours retention of a continuous arm; a fundamental fact about orbits is that bodies further out move slower, both in velocity and in angular velocity, which leads to the arms being swept back into a spiral.

It is not, however, a simple case of individual stars obeying the orbital laws about the galaxy centre; stars in the tapering arms are towed along by the greater density of the stars ahead of them so that, at some sweep angle, they maintain the same angular velocity and the whole pattern rotates. Perhaps some stars trail off the end or are ejected after a close encounter with another.

Our Sun orbits our galaxy centre in about 250 million years but stars closer in must be going faster or they would be drawn closer to the centre. The pattern is not completely static, as not every star is necessarily in a perfect circular orbit in the central disk plane. Some, like the globular clusters, are well out of the plane, which they must pass through twice during each orbit, no doubt causing considerable upset to the locals. Hope one doesn't come our way. Just as we can see reasonable numbers of spiral galaxies we should expect that there are significant numbers of stars, perhaps billions in our own galaxy alone, with sufficient rotation to form disks and planets and, surely, some of the latter would be, like earth, user-friendly to life.

In the QBasic listing, we make much use of the random number generator, RND, which returns numbers between 0 and 1 that we can manipulate to position individual stars in the galaxy. Using the 640 x 480 screen, we put the origin three quarters of the way across the screen and draw the right eye view on the right, between x = -160 and x = 159. The left eye view is calculated on the right but then mirror flipped about x = -160 before plotting to lie between -160 and -480. The 5000 stars might not cut the mustard for your average galaxy, but it'll do for your average screen.

By raising the first RND to power 2.5 to allot the radius, we concentrate stars towards the centre but this gives a very dense central axle, so we add 0.4 to RND first to remove this. You may vary the concentration by varying the exponent. The angle, a, is 1.26 radians (2II/5) multiplied by a random integer, 0 to 4, to define 5 arms. Now, we want to smear stars circumferentially in each arm; in the centre, "sa", the smear angle, is uniform over the range +/- 0.63 radians (2II/10), but in the outer arms, stars are concentrated near the arm, by squaring RND, and "sa" falls off by the inverse square of the radius, to give thin arms at the rim, smearing into a more continuous disk towards the centre. RND greater or less than 0.5 is used to distribute stars fore or aft.

We now sweep the arms back, proportional to radius, to get the spiral effect. The axis of the galaxy is, initially, the x axis so we resolve the radius into its y and z co-ordinates, using the calculated angle. We now need to distribute stars each side of the disk. Near the centre, we have a flat ellipsoid so the x co-ordinate is randomly allotted within the appropriately calculated range. In the outer disk, the x range tapers off inversely as the radius. With the galaxy in this position, we would get an edge-on view of the shape, but will rotate it about the y axis for a better view of the whole.

To transpose each star's position to screen pixels for each eye, we first calculate a "z factor" that is the ratio of the screen-to-star distance to the eye-to-star distance. Using similar triangles, this is used to transpose the x and y co-ordinates. The left eye pixel has to be flipped across the centreline so that you can use a mirror on the centreline, facing left, to flip it right back to give you a true stereo picture on the right. Stars are randomly coloured as this can assist the eyes to marry up the correct pairs to make individual stars easier to see stereoscopically; also we avoid copying over earlier star plots by checking for both clear black pixels before plotting. Note that the code uses a black background which is more realistic and effective; for publication we have to use a white background since printing coloured dots on a black background is bleedin' unsatisfactory. Also added are a few globular clusters, four pixels apiece, near the galaxy and a couple of distant galaxies; these are about two metres beyond the page and you need to consciously focus on them to see them in stereo.

By changing the variables and the exponents you can get a wide assortment of shapes, but a common feature will be that they are much too regular for a natural object--but this is what you get from simple calculations; if you want to re-create the variability of nature you need to do a bit more work. The turbulence equations used in ray-tracing programs for clouds and marble patterns are quite elegant--or you can deliberately build in variations by code which might not quite earn that description.


Place mirror edge up centreline, mirror facing left. Right eye looks at right screen, left eye at reflection in mirror.



The .BAS and .EXE files that produced Figure 1 are on the BBS in AGALAXY.LZH. By the way, if you are a more recent member and interested in graphics, ray tracing, programming, stereo and/or animation, you may like to also get the file STEREOS3.TXT, which lists 21 files on the BBS relating to articles in PC Update over the past six years. Hopefully, these could give you ideas for lines of enquiry in your own dabblings on the infernal machine.

Program listing of GALAXY.BAS

'galaxy.bas Tap it into your QBasic
'VGA screen. Move origin to mid right half
SCREEN 12: WINDOW (-480, -240)-(159, 239)
FOR n = 1 TO 5000 '5000 stars
'Random radius, 14 to 330
r = 140 + (RND + .4) ^ 2.5
'To get 5 distinct arms around 2 pi
a = INT(RND * 5) * 1.26 'angle of arm
IF r < 100 THEN 'centrally
sa = 0.63 * RND 'smear angle
ELSE 'outer disk
sa = RND ^ 2 * (150 / r) ^2 'smear angle
END IF
'Now smear arm, fore and aft
IF RND > .5 THEN sa = -sa
a = a + sa 'gives smeared, straight arms
'Sweep arms by 1 radian at r=100 and...
'... by 3.3 radians at r=330
a = a - r / 100
y = r * SIN(a): z = r * COS(a) 'resolve
IF r < 50 THEN 'centre is flat ellipsoid
x = 0.7 * RND * SQR(2500 - r * r)
ELSE 'outer galaxy thickness tapers off
x = RND * 500 / r
END IF
'Now distribute either side of disk
IF RND > .5 THEN x = -x
x = x - z / 4 'rotate about y axis
'Now for the stereo bit. Eyes at z=-1000
zf = z / (z + 1000) 'z factor
xr = x - zf*(x + 80) 'right eye at x=-80
'xl= x - zf*(x + 240) left eye at x=-240
'but, mirror flipped about x=-160
'ie. xl = -160 - (xl + 160) , gives...
xl = -320 - x + zf * (x + 240)
yb = y + zf*(240 - y) 'both eyes at y=240
c = 1 + INT(RND * 7) 'colours 1 to 7
IF POINT(xr,yb)=0 AND POINT(xl,yb)=0 THEN
PSET (xr, yb), c: PSET (xl, yb), c
END IF
NEXT n 'Let's have another one, do.
DO: LOOP WHILE INKEY$ = "" 'Look at that!


Reprinted from the June 1999 issue of PC Update, the magazine of Melbourne PC User Group, Australia

 

[About Melbourne PC User Group]