#polygon4.py
"""Interactively chose polygon vertices, wasting no clicks,
avoiding repeated code with an if statement.
You are invited in Lab 9 to modify the code to make horizontal and vertical
lines easier to draw.
"""

from graphics import *

def main():
    width = 300
    height = 300
    infoHeight = 15         
    lineHeight = 2* infoHeight
    closeEnough = 10  # parameter added for Lab 9
    win = GraphWin("Polygon Test", width, height)
    instructions = Text(Point(width/2 - 15, infoHeight),
                     "Enter # of points, then click a vertex.")
    instructions.draw(win)
    sizeEntry = Entry(Point(width - 15, infoHeight),2)
    sizeEntry.setText("3")
    sizeEntry.draw(win)
    Line(Point(0, lineHeight), Point(width, lineHeight)).draw(win)
    
    pt = win.getMouse()  
    vertices = [pt]  
    poly = Polygon(vertices)  
    poly.draw(win)
    sizeStr = sizeEntry.getText() #don't know size is set until the mouse click
    size = int(sizeStr)
    for vertNum in range(2, size+1): # have #1, need 2, 3, 4 ... size
        instructions.setText("Click vertex %d" % vertNum)
        lastestPt = win.getMouse()
        poly.undraw()

        # extract coordinates for Lab 9:
        previousPt = vertices[-1] #previous Point is at the end of the list.
        x = lastestPt.getX()
        y = lastestPt.getY()
        prevX = previousPt.getX()
        prevY = previousPt.getY()

        # Code 4 lines here for Lab 9  !
        # modify x and y as needed to make revisedPt correct below
        
        revisedPt = Point (x, y)  
        vertices.append(revisedPt) 
        poly = Polygon(vertices)  
        poly.draw(win)

    poly.setFill("yellow")
    instructions.setText("Click anywhere to quit.")
    win.getMouse()
    win.close()

main()
