Handout 6

The purpose of this exercise is to play with the commands RANDOM, SETX, SETY and SETHEADING.

Chapter 4 of the text describes Lynn's writing a LOGO program to draw a garden. In this exercise we will draw a garden in which the colors and placements of the flowers are randomly chosen.

First, use this procedure to explore the workings of the RANDOM command.

TO PRINTLESSTHAN :NUMBER
  PRINT (SENTENCE [A number less than] :NUMBER [is] RANDOM :NUMBER)
END
Run the PRINTLESSTHAN procedure with large and small arguments. Use the same argument several times. See if you can "trick" the procedure by giving it arguments that are too big, too small or not numbers.


Next, write a short procedure FLOWER that draws a flower or other small pattern. Don't change the pen color in your procedure; we'll do that later. At first, draw something like a five pointed star that you can type in quickly; if you have extra time later you can add improvements.

Below is a modified version of the GARDEN procedure on page 101 of Teaching with LOGO. You can test your FLOWER procedure with this, or you can move on to the next step.

TO GARDEN1
  FLOWER
  RIGHT 33
  PENUP
  FORWARD 100
  PENDOWN
  SETHEADING 0
  WAIT 30
  GARDEN1
END

The garden we produced by GARDEN1 is attractive, but the arrangement of the flowers seems artificial. Our goal is to use the RANDOM command to distribute the flowers in a more natural seeming manner. We could use screen wrapping, FORWARD and SETHEADING to do this, but instead we will take this opportunity to introduce the SETX and SETY commands.

Compare this procedure with the previous one (or with the output of GARDEN, shown on page 102 of the text.)

TO GARDEN2
  FLOWER
  PENUP
  SETX (XCOR + 54)
  SETY (YCOR + 84)
  PENDOWN
  WAIT 30
  GARDEN2
END
The SETX and SETY commands change the turtle's position using Cartesian coordinates centered at the origin. The XCOR and YCOR commands give the turtle's current position, and the numbers 54 and 84 were chosen so that GARDEN2 would behave similarly (although not identically) to GARDEN1.


Finally, write a procedure that uses SETX and SETY to randomly place flowers in a garden on your screen. Experiment with the turtle to choose a rectangular region on the screen in which you want your garden to grow. Then use SETX, SETY and RANDOM to move the turtle to different locations in that garden. Have the turtle draw a FLOWER at each location.


If you have time left over, try randomly selecting a new color for each flower. (Do you want to draw black flowers (color 0)? If not, how can you avoid doing so?)

You might also want to have random sizes of flowers in your garden, change the shapes of your flowers or plant flowers in rows with slight random shifts in their position.


Mtht420