Visualizing Asteroid 2012 DA14

This week’s middle school programming class takes a slight detour into astronomy, using Python to visualize the path of asteroid 2012 DA14 as it buzzes Earth this Friday 15 February, approaching within less than 28,000 kilometers (about 17,000 miles) of our planet, “the closest ever predicted Earth approach for an object this large.”

How close is this?  What I found interesting during discussion about this fly-by was the sense of scale when comparing the “close shave” of the asteroid’s path to the orbits of various satellites, natural and artificial.  With just a few dozen lines of code, we can visually compare the size of these orbits with the path of the asteroid.  A still screenshot of the result is shown below, showing the asteroid passing inside of geostationary orbit, with a view of the orbits of GPS satellites and the International Space Station for comparison.

The asteroid's path compared with various satellite orbits.  The asteroid itself is shown at 20,000 times its actual size ("only" about half a football field in diameter).

The asteroid’s path compared with various satellite orbits. The asteroid itself is shown at 20,000 times its actual size (“only” about half a football field in diameter).

Following is the source code, which animates the rotating earth and the very simplified motion of the asteroid.

from visual import *

# Draw earth.
earth = sphere(radius=6378, material=materials.BlueMarble)

# Draw DA14 asteroid at 20000x size.
da14 = sphere(pos=[34100, -30000, 0], radius=450)

# Draw geostationary orbit.
r_geo = 42164
ring(radius=r_geo, thickness=200, axis=[0, 1, 0], color=color.yellow)
label(pos=[r_geo, 0, 0], text='GEO')

# Draw GPS orbits.
r_gps = 26600
for angle in range(0, 360, 60):
    axis = rotate(
        rotate([0, 1, 0], radians(55), [0, 0, 1]), radians(angle), [0, 1, 0])
    ring(radius=r_gps, thickness=200, axis=axis, color=color.yellow)
label(pos=[r_gps, 0, 0], text='GPS')

# Draw ISS orbit.
r_iss = 6378 + 410
axis = rotate([0, 1, 0], radians(51.6), [0, 0, 1])
ring(radius=r_iss, thickness=200, axis=axis, color=color.yellow)
label(pos=[r_iss, r_iss, 0], text='ISS')

# Animate asteroid and rotating earth at 500x speed.
fps = 10
ff = 500
while True:
    rate(fps)
    da14.pos.y = da14.pos.y + 7.82 / fps * ff
    earth.rotate(angle=radians(360.0 / 86400 / fps * ff), axis=[0, 1, 0])
    if da14.pos.y > 30000:
        da14.pos.y = -30000

Hunt the Wumpus

<nostalgia>I had way too much fun with this.  After a discussion with students about mazes and various ways to represent and display them in computer programs, I ended up writing a Python version of Wumpus.  I don’t mean a 3D graphics version using Visual Python, or even the 1980 TI version with the Wumpus graphic that I see on t-shirts.  I mean the original text version by Gregory Yob.  I remember being fascinated by this game as a kid– it was very likely my first introduction to graph theory, with the rooms of the Wumpus’s cave connected like the vertices of a squashed dodecahedron.  (I love this helpful suggestion from the game’s instructions: “If you don’t know what a dodecahedron is, ask someone.”)

You can download the game at the usual location here.  Although there are many versions of Wumpus out there, I had a few specific requirements that none of them met.  This version still works in Python 2.7, and is a shot-for-shot re-make of the game, behaving exactly as it appeared where I first encountered it, re-printed in the 1979 book More BASIC Computer Games.  My only tweak was to use friendlier lower case text instead of the original all-upper case, and to fix a few typos in the re-print.</nostalgia>