#!/usr/bin/python

import math
import random
import getopt,sys
from tkinter import *

(x,y) = (400,600)
theta = 90
master = Tk()
w = Canvas(master, width=800, height=800)

def forward(dist,color='red'):
    global x, y
    newx = x - dist * math.cos(math.radians(theta))
    newy = y - dist * math.sin(math.radians(theta))
    w.create_line(int(x), int(y), int(newx), int(newy), fill=color)
    (x,y) = (newx, newy)

def turn(angle):
    global theta
    theta = theta + angle

def square():
    turn(-45)
    for i in range(4):
        forward(10)
        turn(90)
    turn(45)

def hexagon():
    turn(-60)
    for i in range(6):
        forward(7)
        turn(60)
    turn(60)

def star():
    dist = 8
    turn(-90-45/2)
    for i in range(4):
        forward(dist)
        turn(135)
        forward(dist)
        turn(-45)
    turn(90+45/2)

def circle(color='red'):
    global x, y
    outerRadius = 8
    newx = x - outerRadius*math.cos(math.radians(theta))
    newy = y - outerRadius*math.sin(math.radians(theta))
    # The  first four arguments to create_arc are the
    # axis-aligned bounding box of the arc.
    w.create_arc(int(newx-outerRadius), int(newy+outerRadius),
                 int(newx+outerRadius), int(newy-outerRadius),
                 start=0, extent=359.999, style=ARC, outline=color)
    innerRadius = 4
    w.create_arc(int(newx-innerRadius), int(newy+innerRadius),
                 int(newx+innerRadius), int(newy-innerRadius),
                 start=0, extent=359.999, style=ARC, outline=color)

def vee():
    choices = [hexagon, square, star, circle, vee, vee, vee]
    branchAngle = 30
    turn(-branchAngle)
    forward(30)
    random.choice(choices)()
    forward(-30)
    turn(2*branchAngle)
    forward(30)
    random.choice(choices)()
    forward(-30)
    turn(-branchAngle)

def drawbase(standtype):
    print("Draw base", standtype)
    if standtype == "A":
        forward(40,'blue')
    else:
        forward(60,'blue')

def main(argv):
    standtype = 'A'
    try:
        opts, args = getopt.getopt(argv,"hs:",["standtype="])
    except getopt.GetoptError:
        print('Usage: garcia.py -s [A|B]')
        sys.exit(2)
        
    for opt, arg in opts:
        if opt == '-h':
            print('Usage: garcia.py -s [A|B]')
            sys.exit()
        elif opt in ("-s", "--standtype"):
            if arg != "A" and arg != "B":
                print('Standtype must be "A" or "B"')
                sys.exit(2)
            else:
                standtype = arg
            
    print('Making a tree with stand type', standtype)

    w.pack()
    drawbase(standtype)    
    vee()



if __name__ == "__main__":
    main(sys.argv[1:])
    mainloop()
