Part The Sky at Night

To create a simulated image of the night sky, we use the script in Listing 9-10.

Listing 9-10. Creating an Image ofthe Sky at Night,

# create a fictitious night sky from random import randrange as rr execfile('')

# parameters for the simulated night sky image img_size = 800

num_stars = 25

# star parameters: number of pointy edges and radius min_num_points = 5

max_num_points = 11 min_star_radius = 2 max_star_radius = 10

# star parameter 'thinness' is on a scale of 1 to 10 min_thin = 5

# draw the night sky figure(facecolor='k') cur_axes = gca()

# patch stars for i in range(num_stars):

new_star = star(rr(min_star_radius, max_star_radius), rr(o, img_size) , rr(o, img_size), 'w', \

rr(min_num_points, max_num_points), \ rr(min_thin, max_thin)/10.0) cur_axes.add_patch(new_star)

# modify axis behavior axis([0, img_size, 0, img_size])


savefig('../images/nightsky', facecolor='k', edgecolor='k')

I've imported the function randrange() from the module random and decided to rename it to rr(), which I think is clearer to read. I then define a set of values you can tweak and observe the results. The values are self-explanatory and include such values as the image size and number of stars in the image.

The patching of stars is done in the for loop, which creates a new star with random values and adds it to the current figure. I then follow up by updating the image size and removing the axes. Finally, I save the image to file. Figure 9-7 shows random output from the simulated night sky.

Figure 9-7. Simulated (random) night sky

Was this article helpful?

0 0

Post a comment