#Import the math library to handle the calculations. from math import * #Ask for input of system variables: ##Ask for number of years to be simulated. number_string = raw_input("How many years do you want to simulate?") number_of_years = int(number_string) ##Ask for the branching angle of the limbs. angle_string = raw_input("What angle do you want the limbs to branch off at?") angle = float(angle_string) ##Ask for the length of the first branch (i.e. the trunk). initial_length_string = raw_input("What length do you want the initial length to be?") initial_length = int(initial_length_string) ##Ask for the factor by which each successive branch should be shorter than its ##predecessor. factor_string = raw_input("By what factor do you want successive limbs to decrease in length?") factor = float(factor_string) #Create an array of lengths to be used for each year of the simulation. length_by_year = [] for year_number in range(int(number_of_years)): years_limb_length = (factor ** year_number) * initial_length length_by_year.append(years_limb_length) #Set up graphics window. from graphics import * graphics_window = GraphWin("FINAL PROJECT", 900, 900) sky = Rectangle(Point(0, 0), Point(900, 725)) sky.setFill("light blue") sky.setOutline("light blue") sky.draw(graphics_window) water = Rectangle(Point(0, 725), Point(900, 750)) water.setFill("turquoise") water.setOutline("turquoise") water.draw(graphics_window) sand = Rectangle(Point(0, 750), Point(900, 900)) sand.setFill("beige") sand.setOutline("beige") sand.draw(graphics_window) #Create limb class of objects. class limb: def __init__(self, point_1, point_2): self.branch = Line(point_1, point_2) self.branch.setWidth(number_of_years - year_number) #Define methods for tree limb objects. def draw(self, graphics_window): self.branch.draw(graphics_window) #Create an empty array of tree limb objects. tree_limbs = [] #Create an empty array of point_2's (i.e., the location of the tips of a year's new growth). point_2_array = [] #Create an empty array of angles. angle_array = [] #Set system up with initial loop for the tree's first year. for year_number in range(0,1): #Add year number to array of tree limbs, array of point_2's, and array of angles, #and make each year number another array. angle_array.append(year_number) angle_array[year_number] = [] point_2_array.append(year_number) point_2_array[year_number] = [] tree_limbs.append(year_number) tree_limbs[year_number] = [] #Set first point_1 (i.e. the base of the tree). point_1 = Point(450, 800) #Set first angle. new_angle = pi #Add this angle to the angle_array to be used in calculating the next year's angles. angle_array[0].append(new_angle) #Set first point_2 (i.e. the top of the tree trunk), depending on the given initial length. point_2 = Point(450, (800 - initial_length)) #Add this point_2 to the point_2_array so that it can be retrieved #to become the next loop's point_1. point_2_array[0].append(point_2) #From the initial point_1 and point_2, create the first limb, the trunk. tree_limb = limb(point_1, point_2) #Add object tree_limb to array called tree_limbs. tree_limbs[0].append(tree_limb) #Draw the object tree_limb. tree_limb.draw(graphics_window) #Continue forward for subsequent years. for year_number in range(1, number_of_years): #Add year number to the arrays of angles, point_2's, and tree_limbs, #thus setting things up to sort them by year_number. angle_array.append(year_number) angle_array[year_number] = [] point_2_array.append(year_number) point_2_array[year_number] = [] tree_limbs.append(year_number) tree_limbs[year_number] = [] #Draw two limbs coming off of the ends of the previous year's limbs. for j in range(len(tree_limbs[year_number - 1])): #First branch. #Get angle. new_angle = angle_array[year_number - 1][j] + angle angle_array[year_number].append(new_angle) #Get point_2 from previous year and make it new point_1. point_1 = point_2_array[year_number - 1][j] #Pick out x- and y- values of new point_1. point_1_x_value = point_1.getX() point_1_y_value = point_1.getY() #Define new point_2 with respect to new point_1. point_2_x_value = (point_1_x_value + int((length_by_year[year_number]) * (sin(new_angle)))) point_2_y_value = (point_1_y_value + int((length_by_year[year_number]) * (cos(new_angle)))) point_2 = Point(point_2_x_value, point_2_y_value) #Add these point_2's to the point_2_array so that they can be retrieved #to become the next loop's point_1's. point_2_array[year_number].append(point_2) #Create object called tree_limb. tree_limb = limb(point_1, point_2) #Draw the object tree_limb. tree_limb.draw(graphics_window) #Add object tree_limb into array called tree_limbs. tree_limbs[year_number].append(tree_limb) #Second branch. #Get angle. new_angle = angle_array[year_number - 1][j] - angle angle_array[year_number].append(new_angle) #Get point_2 from previous year and make it new point_1. point_1 = point_2_array[year_number - 1][j] #Pick out x- and y- values of new point_1. point_1_x_value = point_1.getX() point_1_y_value = point_1.getY() #Define new point_2 with respect to new point_1. point_2_x_value = (point_1_x_value + int((length_by_year[year_number]) * (sin(new_angle)))) point_2_y_value = (point_1_y_value + int((length_by_year[year_number]) * (cos(new_angle)))) point_2 = Point(point_2_x_value, point_2_y_value) #Add these point_2's to the point_2_array so that they can be retrieved #to become the next loop's point_1's. point_2_array[year_number].append(point_2) #Create object called tree_limb. tree_limb = limb(point_1, point_2) #Draw the object tree_limb. tree_limb.draw(graphics_window) #Add object tree_limb into array called tree_limbs. tree_limbs[year_number].append(tree_limb) #Add a leaf to the end of each of the newest branches. for j in range(len(tree_limbs[number_of_years - 1])): #Create array of leaves. leaf_array = [] leaf_array.append(j) #Recall the angle of the branch from which the leaf will extend. end_angle = angle_array[number_of_years - 1][j] #Recall the point_2 of the branch. This will become the base of the leaf. center = point_2_array[number_of_years - 1][j] center_x_value = center.getX() center_y_value = center.getY() #Create points to form the shape of the leaves. leaf_point_1 = Point(center_x_value + int(10 * sin(end_angle - (pi/3))), center_y_value + int(10 * cos(end_angle - (pi/3)))) leaf_point_2 = Point(center_x_value + int(6 * sin(end_angle - (pi/2))), center_y_value + int(6 * cos(end_angle - (pi/2)))) leaf_point_3 = Point(center_x_value + int(10 * sin(end_angle - 2 * pi/3)), center_y_value + int(10 * cos(end_angle - 2 * pi/3))) leaf_point_7 = Point(center_x_value + int(6 * sin(pi + end_angle - 5 * pi/6)), center_y_value + int(6 * cos(pi+end_angle - 5 * pi/6))) leaf_point_8 = Point(center_x_value + int(10 * sin(pi + end_angle - pi)), center_y_value + int(10 * cos(pi+end_angle - pi))) leaf_point_9 = Point(center_x_value + int(6 * sin(pi + end_angle - 7 * pi/6)), center_y_value + int(6 * cos(pi+end_angle - 7 * pi/6))) leaf_point_4 = Point(center_x_value + int(10 * sin(end_angle - 4 * pi/3)), center_y_value + int(10 * cos(end_angle - 4 * pi/3))) leaf_point_5 = Point(center_x_value + int(6 * sin(end_angle - 3 * pi/2)), center_y_value + int(6 * cos(end_angle - 3 * pi/2))) leaf_point_6 = Point(center_x_value + int(10 * sin(end_angle - 5 * pi/3)), center_y_value + int(10 * cos(end_angle - 5 * pi/3))) #Create polygon from these points. leaf = Polygon(leaf_point_1, leaf_point_2, leaf_point_3, center, leaf_point_4, leaf_point_5, leaf_point_6, leaf_point_7, leaf_point_8, leaf_point_9) #Allow a variety in leaf color. from random import randrange i = randrange(1, 6) if i == 1: leaf.setFill("light green") else: leaf.setFill("green") #Draw each leaf. leaf.draw(graphics_window) #Wait for mouse-click to close. graphics_window.getMouse()