import turtle import math import random def next_y_position(y,ht): Returns the next y-position, given current position
Question:
import turtle
import math
import random
def next_y_position(y,ht):
""" Returns the next y-position, given current position y and height ht """
next = y + ht + 30
return next
def drawRect(t, len, wid):
""" Draws a rectangle using turtle t with sides len and wid """
for side in [len, wid, len, wid]:
t.forward(side)
t.left(90)
# define the function to draw a filled circle
def drawFilledCircle(t, radius):
""" Draws a filled circle of radius rad using turtle t """
t.begin_fill()
t.circle(rad)
t.end_fill()
# define the function to draw an equilateral triangle
def drawEquilateralTriangle(t, side):
""" Draws an equilateral triangle using turtle t with sides of length side """
for i in range(3):
t.forward(side)
t.left(120)
# define the function to calculate the length of a side of an equilateral triangle with a given area
def getSide(len, wid):
""" Returns the length of a triangle side with an area of len * wid """
return math.sqrt(len * wid * 4 / math.sqrt(3))
# define the function to calculate the radius of a circle with a given area
def getRad(len, wid):
""" Returns the radius of a circle with an area of len * wid """
return math.sqrt(len * wid / math.pi)
# a function that generates a random number between 1 and 10 and prints it out
def print_random_number():
num = random.randrange(1, 10)
print("Random number:", num)
# Call the function
print_random_number()
def main():
#named constants
screen_size = 500
screen_startx = 60 # x coordinate of the left edge of the graphics window
# Set up the window and its attributes
wn = turtle.Screen()
wn.setup(screen_size, screen_size, screen_startx, 0)
wn.bgcolor("yellow")
axel = turtle.Turtle()
axel.speed(7)
# Initial turtle position near left edge, toward the bottom
xpos = -screen_size/2 + 20
ypos = -screen_size/2 + 50
axel.up()
axel.goto(xpos,ypos)
axel.down()
# y-dimension of each rectangle
width = 50
# draw three sets of shapes - same width(y-dimension) but different lengths
for length in [25, 55, 75]:
# Draw the rectangle
drawRect(axel, length, width)
# Here's code to draw a rectangle - before putting in a function
#for side in [length, width, length, width]:
#axel.forward(side)
#axel.left(90)
# Draw an equilateral triangle with sides the same as rectangle length
for _ in range(3):
axel.forward(length)
axel.left(120)
# Move a little to the right of the rectangle
axel.up()
axel.forward(2*length)
axel.down()
# Draw a circle with the same area as the rectangle
radius = math.sqrt(length*width/math.pi)
axel.begin_fill()
axel.circle(radius)
axel.end_fill()
# Move a little to the right of the circle
axel.up()
axel.forward(2*radius)
axel.down()
# Draw an equilateral triangle with same area as rectangle
tri_side = math.sqrt(length*width*4/math.sqrt(3))
for _ in range(3):
axel.forward(tri_side)
axel.left(120)
# Move a little to the right of the triangle
axel.up()
axel.forward(2*tri_side)
axel.down()
# Draw a circle with the same diameter as the triangle side
axel.begin_fill()
axel.circle(tri_side/2)
axel.end_fill()
# Calculate the next vertical position for a set of shapes
ypos = next_y_position(ypos,width)
# Put turtle to left side of screen at correct height
axel.up()
axel.goto(xpos,ypos)
axel.down()
def drawFilledCircle(t, rad):
""" Draws a filled circle of radius rad using turtle t """
t.begin_fill()
t.circle(rad)
t.end_fill()
def drawEquilateralTriangle(t, side):
""" Draws an equilateral triangle using turtle t with sides of length side """
for i in range(3):
t.forward(side)
t.left(120)
# Close window nicely after loop finishes
wn.exitonclick()
# Run the main function. This should be the last statement in the file.
main()
1- Run the main function. This should be the last statement in the file
Data Structures and Algorithms in Python
ISBN: 978-1118290279
1st edition
Authors: Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser