# L-33 MCS 260 Monday 6 April 2015 : class_showline.py
"""
Inheriting from the class Line, we extend a point with a canvas
data attribute and with a method to draw a line on the canvas.
The main program pops a new window with a canvas and draws
eleven random lines on the canvas.
"""

from tkinter import Tk, Canvas
from math import cos, pi
from random import uniform, randint
from class_line import Line
from class_showpoint import ShowPoint

class ShowLine(Line, ShowPoint):
    """
    Extends the Line class with a draw method.
    """
    def __init__(self, c, d, x=0, y=0, a=0):
        """
        Defines the line through the point
        (x, y), with angle a and stores the
        canvas c of dimension d.
        """
        Line.__init__(self, x, y, a)
        ShowPoint.__init__(self, c, x, y)
        self.dimension = d

    def draw(self):
        """
        Draws the line on canvas.
        """
        ShowPoint.draw(self)
        cnv = self.canvas
        (xpt, ypt) = (self.xpt, self.ypt)
        dmx = self.dimension - xpt
        csa = cos(self.angle)
        if csa + 1.0 != 1.0:
            pt1 = Line.__call__(self, dmx/csa)
            cnv.create_line(xpt, ypt, pt1.xpt, pt1.ypt)
            pt2 = Line.__call__(self, -xpt/csa)
            cnv.create_line(pt2.xpt, pt2.ypt, xpt, ypt)
        else: # vertical line
            cnv.create_line(xpt, 0, xpt, self.dimension)

def main():
    """
    Shows 11 lines on canvas.
    """
    top = Tk()
    dim = 400
    cnv = Canvas(top, width=dim, height=dim)
    cnv.pack()
    lines = [ShowLine(cnv, dim, 100, 300, pi/2)]
    for _ in range(10):
        xrd = randint(6, dim-6)
        yrd = randint(6, dim-6)
        rnd = uniform(0, 2*pi)
        lines.append(ShowLine(cnv, dim, xrd, yrd, rnd))
    for line in lines:
        line.draw()
    top.mainloop()

if __name__ == "__main__":
    main()
