# File Name: Exercise3_3_alt.py
# Purpose: generate a "scatter plot" of (100, 50), (72, 10), (30, 27), (50, 13), (90, 43).
# Author: Emily G. Allen

# solution that uses functions

# import graphics library
from graphics import *

# ================================================
# Function: Create and format the graphics window
# Inputs: None
# Outputs: the window instance
# ================================================
def Window():
    mywin = GraphWin('My Scatter Plot', 300, 300)    # create the window
    mywin.setCoords(0,0, 200, 200)   # adjust coordinates to make plotting easier
    return mywin # return the window

# ================================================
# Function: Draw  and label the axes
# Inputs: the window, and the offset of each axis
#         from the edge of the window
# Outputs: None
# ================================================
def DrawAxes(offset, win):
    # draw axes
    xaxis = Line(Point(offset, offset), Point(150, offset))
    xaxis.draw(win)

    yaxis = Line(Point(offset,offset), Point(offset, 150))
    yaxis.draw(win)

    # create the text labels
    xlabel = Text(Point(75, offset/2), 'X')
    ylabel = Text(Point(offset/2, 75), 'Y')

    # draw the labels
    xlabel.draw(win)
    ylabel.draw(win)

# ================================================
# Function: Draw a point as a circle
# Inputs: the point and the window
# Outputs: None
# ================================================
def DrawPoint(point, win):
    c = Circle(point, 2)  # use point to create a circle
    c.setFill('red') # make it look better
    c.draw(win) # draw the point
    
# ================================================
# Function: Draw a line between two points
# Inputs: the two points, 
#         the window
# Outputs: None
# ================================================
def DrawLine(a, b, win):
    l = Line(a, b) # create a line from a to b
    l.draw(win) # draw the line

# ================================================
# Function: Create and format the graphics window
# ================================================
def main():
    mywin = Window() # create and format the window

    offset = 20 # offset of axes from edge of window
    DrawAxes(offset, mywin) # draw axes and label

    # create the points
    p1 = Point(100, 50) 
    p2 = Point(72, 10)
    p3 = Point(30, 27)
    p4 = Point(50, 13)
    p5 = Point(90, 43)

    # adjust by offset
    p1.move(offset, offset)
    p2.move(offset, offset)
    p3.move(offset, offset)
    p4.move(offset, offset)
    p5.move(offset, offset)

    # draw the lines between points
    DrawLine(p3, p4, mywin)
    DrawLine(p4, p2, mywin)
    DrawLine(p2, p5, mywin)
    DrawLine(p5, p1, mywin)
    
    # draw the circles (scatter part)
    DrawPoint(p1, mywin)
    DrawPoint(p2, mywin)
    DrawPoint(p3, mywin)
    DrawPoint(p4, mywin)
    DrawPoint(p5, mywin)

    # wait for mouse click to close
    mywin.getMouse()
    mywin.close()
    
main() # invoke the program
