Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Draws 3 sets of shapes of related sizes # Draw a rectangle # Draw a triangle with sides the same size as rectangle

Draws 3 sets of shapes of related sizes

#   Draw a rectangle

#   Draw a triangle with sides the same size as rectangle length

#   Draw a filled circle with the same area as the rectangle

#   Draw a triangle with the same area as the rectangle

#   Draw a filled circle with the same diameter as the previous triangle side

import turtle

import math

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)

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.bgcolor("yellow")

    wn.setup(screen_size, screen_size, screen_startx, 0)

    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, tri_side)

       

        # Put turtle to left side of screen at correct height

        axel.up()

        axel.goto(xpos,ypos)

        axel.down()

       

    # Close window nicely after loop finishes

    wn.exitonclick()        

# Run the main function. This should be the last statement in the file.

main()

 

1-Use the non-fruitful function drawRect as a model.  another non-fruitful function that draws a filled circle. The function is defined as

def drawFilledCircle(t, rad):

    """ Draws a filled circle of radius rad using turtle t """

and called with a statement like

drawFilledCircle(axel, radius)

The contents of the function are to begin filling, draw a circle, end filling.

Make sure you call your function in two different places in the program, using a different actual parameter each time.

 

2. another non-fruitful function drawEquilateralTriangle , which is defined as

def drawEquilateralTriangle(t, side):

    """ Draws an equilateral triangle using turtle t with sides of length side """

and is called with

drawEquilateralTriangle(axel, tri_side)

 

Make sure to use the parameters side in the body of this function. Make sure you call your function in two different places in the program, using a different actual parameter each time.

 

3.Next, you'll work with fruitful functions. Look at the function to calculate the next y-position for drawing a set of shapes. This function is called near the bottom of the program:

ypos = next_y_position(ypos, width)

Using this function as a model, add a fruitful function to calculate the length of a side of an equilateral triangle with a given area, which is math.sqrt(length*width*4/math.sqrt(3)). The function is defined with

def getSide(len,wid):     """ Returns the length of a triangle side with an area of len * wid """                              

and  it is called with the statement

tri_side = getSide(length,width)

 

Make sure you use the parameters len and wid in the body of the function.

 

4.Again, using this function as a model, add a fruitful function to calculate the value math.sqrt(length*width/math.pi) which appears near the end of the program. The function should be defined as

def getRad(len,wid):                

    """ Returns the radius of a circle with an area of len * wid """        

and  it is called with the statement

 

radius = getRad(length,width)Make sure you use the parameters  len and  wid in the body of the function.

 

5.Make sure your functions have the same parameters and the same comment as in the assignment description. Write a complete header comment.

6.make  own function and call it from main. Your function doesn't have to do a lot, but it does have to do something.

Step by Step Solution

3.45 Rating (164 Votes )

There are 3 Steps involved in it

Step: 1

1 function to draw a circle import turtle def drawFilledCirclet rad Draws a filled circle of radius ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

A Pathway To Introductory Statistics

Authors: Jay Lehmann

1st Edition

0134107179, 978-0134107172

More Books

Students also viewed these Programming questions