#polygon3.py
"""Interactively chose polygon vertices, wasting no clicks.
Illustrates use of Text, Line, Entry, Polygon, getMouse.
"""

from graphics import *

def main():
    width = 300
    height = 300
    infoHeight = 15         
    lineHeight = 2* infoHeight
    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)
    # note no variable is assigned to the Line - it is never referred to again
    
    pt = win.getMouse()  
    sizeStr = sizeEntry.getText() #don't know size is set until the mouse click
    size = int(sizeStr)
    instructions.setText("Click more polygon vertices.")
    vertices = [pt]  # use the first point!
    poly = Polygon(vertices)  
    poly.draw(win)
    for i in range(size-1): # already have one point
        pt = win.getMouse()
        poly.undraw()
        vertices.append(pt) # append method adds to the list
        poly = Polygon(vertices)  
        poly.draw(win)

    poly.setFill("yellow")
    instructions.setText("Click anywhere to quit.")
    win.getMouse()
    win.close()

main()
