# L-9 MCS 275 Mon 30 Jan 2017 : multigon.py

"""
In prepartion of the making of the Koch curve,
we first draw a regular n-gon on Canvas.
"""

from tkinter import Tk, IntVar, Scale, Canvas, ALL
from math import cos, sin, pi

class MultiGon(object):
    """
    GUI to draw a regular n-gon on canvas.
    """
    def __init__(self, wdw):
        """
        Determines the layout of the GUI.
        """
        wdw.title('regular n-gon')
        self.dim = 400
        self.ngn = IntVar()
        self.scl = Scale(wdw, orient='horizontal', \
            from_=1, to=20, tickinterval=1, \
            length=self.dim, variable=self.ngn, \
            command=self.draw_gon)
        self.scl.set(10)
        self.scl.grid(row=0, column=0)
        self.cnv = Canvas(wdw, width=self.dim, \
            height=self.dim, bg='white')
        self.cnv.grid(row=1, column=0)

    def draw_gon(self, val):
        """
        Draws a regular n-gon.
        """
        xctr = self.dim/2
        yctr = self.dim/2
        radius = 0.4*self.dim
        self.cnv.delete(ALL)
        self.cnv.create_text(xctr, yctr, text=val, tags="text")
        ngn = int(val)
        pts = []
        for i in range(0, ngn):
            xpt = xctr + radius*cos(2*i*pi/ngn)
            ypt = yctr + radius*sin(2*i*pi/ngn)
            self.cnv.create_oval(xpt-6, ypt-6, xpt+6, ypt+6, width=1, \
                outline='black', fill='SkyBlue2', tags="dot")
            pts.append((xpt, ypt))
        for i in range(0, ngn-1):
            self.cnv.create_line(pts[i][0], pts[i][1], \
                pts[i+1][0], pts[i+1][1], width=2)
        self.cnv.create_line(pts[ngn-1][0], pts[ngn-1][1], \
            pts[0][0], pts[0][1], width=2)

def main():
    """
    Instantiates the GUI object
    and launches the main event loop.
    """
    top = Tk()
    MultiGon(top)
    top.mainloop()

if __name__ == "__main__":
    main()
