17.3: Exercises
- Page ID
- 15371
The following is a series of exercises using TurtleWorld. They are meant to be fun, but they have a point, too. While you are working on them, think about what the point is.
The following sections have solutions to the exercises, so don’t look until you have finished (or at least tried).
- Write a function called
square
that takes a parameter namedt
, which is a turtle. It should use the turtle to draw a square.Write a function call that passes
bob
as an argument tosquare
, and then run the program again. - Add another parameter, named
length
, tosquare
. Modify the body so length of the sides islength
, and then modify the function call to provide a second argument. Run the program again. Test your program with a range of values forlength
. - The functions
lt
andrt
make 90-degree turns by default, but you can provide a second argument that specifies the number of degrees. For example,lt(bob, 45)
turnsbob
45 degrees to the left.Make a copy of
square
and change the name topolygon
. Add another parameter namedn
and modify the body so it draws an n-sided regular polygon. Hint: The exterior angles of an n-sided regular polygon are 360/n degrees. - Write a function called
circle
that takes a turtle,t
, and radius,r
, as parameters and that draws an approximate circle by invokingpolygon
with an appropriate length and number of sides. Test your function with a range of values ofr
.Hint: figure out the circumference of the circle and make sure that
length * n = circumference
.Another hint: if
bob
is too slow for you, you can speed him up by changingbob.delay
, which is the time between moves, in seconds.bob.delay = 0.01
ought to get him moving. - Make a more general version of
circle
calledarc
that takes an additional parameterangle
, which determines what fraction of a circle to draw.angle
is in units of degrees, so whenangle=360
,arc
should draw a complete circle.