solution_018.py

#!/usr/bin/python3
# ==================================================================
# Who can hear us?
#
# Draw a circle for the galaxy and draw a circle for how far
# our radio broadcasts have reached. Draw the radio circle
# relative to Earth's position in the galaxy.
# ==================================================================
# Started radio broadcasting commercially around 1920 to 1923.
#
# Radio waves travel at approximately at the speed of light
# (186,000 miles a second).
#
# The estimated diameter of the Milky Way is 105,700 light years.
#
# The earth is located about 25,000 light-years from the center
# of the Milky Way in one of the spiral arms.
# ==================================================================

import graphics as gr
import user_interface as ui
import coordinate_conversion as cc
import draw_axes as ax

galaxy_pixel_radius = 300                        # pixels
window_pixel_width  = 801                        # pixels
window_pixel_height = 801                        # pixels

radio_broadcast_years = 2021 - 1920              # years

galaxy_diameter          = 105700                # light years
galaxy_raidus            = galaxy_diameter / 2   # light years
earth_location_in_galaxy = 25000                 # light years

# ---- given the galaxy radius in pixels and the size of
# ---- the galaxy in light years, calculate the number of pixels
# ---- per light year.

pixels_per_light_year = galaxy_pixel_radius / galaxy_raidus


# -------------------------------------------------------------------
# ---- draw a circle
# -------------------------------------------------------------------

def draw_circle(win,x,y,r,color):

    wx,wy = cc.center_to_win_coords(x,y,
               window_pixel_width,window_pixel_height)
    c = gr.Circle(gr.Point(wx,wy),r)
    c.setWidth(1)
    c.setOutline(color)
    c.draw(win)


# -------------------------------------------------------------------
# ---- main
# -------------------------------------------------------------------

# ---- display stats

print()
print(f'galaxy_pixel_radius     : {galaxy_pixel_radius}  (pixels)')
print(f'window_pixel_width      : {window_pixel_width}  (pixels)')
print(f'window_pixel_height     : {window_pixel_height}  (pixels)')
print(f'radio_broadcast_years   : {radio_broadcast_years}  (years)')
print(f'galaxy_diameter         : {galaxy_diameter}  (light years)')
print(f'galaxy_radius           : {galaxy_radius}  (light years)')
print(f'earth_location_in_galaxy: {earth_location_in_galaxy} (light years)')
print(f'pixels_per_lightyear    : {pixels_per_light_year}  (pixels)')
print()


# ---- create window

win = gr.GraphWin("Who Can Hear Us?",
                  window_pixel_width,window_pixel_height)
win.setBackground("white")

# ---- draw axes

ax.draw_xy_axes(win,True)

# ---- draw galaxy

draw_circle(win,0,0,galaxy_pixel_radius,"red")

# ---- draw earth radio broadcast area

draw_circle(win,
            25000 * pixels_per_light_year,
            0,
            pixels_per_light_year * radio_broadcast_years,
            "black")

ui.pause()

win.close()