# File: tbox.py # program to draw a box with a hat using turtle # # Matt Bishop, MHI 289I, Fall 2023 # import turtle # allows us to use turtles import math # need this for the hat :-) # # set up the drawing # wn = turtle.Screen() # draw the window for the picture box = turtle.Turtle() # create a turtle, called "box" # # draw the box # box.forward(30) # tell box to move forward some box.right(90) # tell box to turn right box.forward(30) # tell box to move forward some box.right(90) # tell box to turn right box.forward(30) # tell box to move forward some box.right(90) # tell box to turn right box.forward(30) # tell box to move forward some # # now let's draw the hat # the length of each side of the hat is calculated using # the Pythagorean theorem for a right triangle; the hat # side lengths x are equal, and form the legs of the triange; # the box length is the hypotenuse c, so x**2 + x**2 = c**2; # solving for x gives sqrt((c**2)/2) # box.right(45) # tell box to turn right half a turn box.forward(math.sqrt((30**2)/2)) # tell box to move forward some box. right(90) # tell box to turn right box.forward(math.sqrt((30**2)/2)) # tell box to move forward some # # now display the result # wn.mainloop() # wait until the user closes the window