Handout 5B

The purpose of this exercise is to draw a picture using different pen colors, some elementary geometry and some of LOGO's numeric operators.

Use a piece of blank paper and some colored pens to draw a picture. You are going to draw this picture on the computer using the turtle, so try to restrict yourself (at least at first) to using arcs, straight lines, and simple geometric figures. A sample design is shown below.

geometric line drawing using several colors


Run the following recursive procedure with an argument of 7 to discover what colors LOGO knows. (Feel free to experiment with other arguments, but save your work first!) Colors in LOGO are numbered from 0 to 7, 0 to 15, or higher. Color 0 is black and color 7 is white. The command SETPENCOLOR (abbreviated SETPC) changes the turtle color as demonstrated in the procedure.

TO SHOWCOLORS :NUMCOLOR
  SETPENCOLOR :NUMCOLOR            ; This sets the pen color.
  FORWARD (15 * :NUMCOLOR)         ; This will make a nice spiral pattern.
  RIGHT 90
  IF :NUMCOLOR = 0 [SETPC 7 STOP]  ; Stop when we finish color number 0.
  SHOWCOLORS :NUMCOLOR - 1         ; Otherwise repeat for next color.
END
On your drawing, mark down the numbers of the colors that you will use for each line or figure.

(NOTE: This procedure isn't truly state transparent because it always ends with the pen color set to 7. It also assumes that the input :NUMCOLOR is a positive integer. How would you fix these problems?)


Write procedures for the different parts of your drawing. For example, you might write one procedure to draw a house, another to draw a tree, and a third to draw a moon. These procedures might be built up using EQUILATERALTRIANGLE or HALFCIRCLE procedures, which should be the first things you write. The sample figure is made up of three squares, two half-circles and four other lines. The code to draw it can be found at the end of this page. Some other examples are discussed on pages 235-242 of the text.

If you are lucky, you have some right triangles in your drawing. Use the Pythagorean theorem, LOGO's SQRT command, and lots of parentheses to draw the hypotenuse of each of those triangles as accurately as possible. Here is an example:

TO HALFSQUARE :SIDE
  FORWARD :SIDE
  RIGHT 90
  FORWARD :SIDE
  RIGHT 135
  FORWARD ((SQRT 2) * :SIDE)
  RIGHT 135
END
Write a procedure to reproduce your drawing on the computer screen. If you don't want your drawing to appear on a black background, change the background color using the command SETBACKGROUND (or SETBG) with a color number as its argument. When you're done writing your procedure, use the HIDETURTLE (HT) command to remove the turtle from your drawing. You can always ask the turtle to show itself again using SHOWTURTLE (ST).


Here are the LOGO commands needed to draw the picture at the top of the page.

TO SQUARE :SIDELENGTH
 REPEAT 4 [FORWARD :SIDELENGTH RIGHT 90]
END

TO HALFCIRCLE :RADIUS
 REPEAT 60 ~
  [FORWARD (:RADIUS * (SIN 3/2)) RIGHT 3 FORWARD (:RADIUS * (SIN 3/2))]
END

TO BLUELINES :SIZE
 REPEAT 4 [RIGHT 45 FORWARD (:SIZE / (SQRT 8)) ; Draw short diagonal
           PENUP BACK (:SIZE / (SQRT 8)) LT 45 ; Return to start
           FD :SIZE RT 90 PENDOWN]             ; Move to next corner
END

TO TWINSQUARES :SIZE
 PU RT 90 FORWARD (:SIZE / 2) LT 135 ; Move to starting position
 PD SQUARE (:SIZE / (SQRT 8))        ; Draw square
 PU RT 45 FORWARD (:SIZE / 2) LT 45  ; Move to starting position
 PD SQUARE (:SIZE / (SQRT 8))        ; Draw square
 PU RT 45 BK (:SIZE/2) RT 90         ; Return to start
    BK (:SIZE/2) LT 90 PD
END

TO HALFCIRCLES :SIZE
 PU RT 45 FD (:SIZE / (SQRT 8)) LT 135 ; Move to starting position
 PD HALFCIRCLE (:SIZE / 4)             ; Draw half circle
 PU FD (:SIZE / 2)                     ; Move to next starting position
 PD HALFCIRCLE (:SIZE / 4)             ; Draw half circle
 PU FD (3 * :SIZE / 4) LT 90           ; Return to start
    FD (:SIZE / 4) RT 180 PD
END

TO DRAWPICT :SIZE
 SETBACKGROUND 7                     ; Change to a white background

 SETPENCOLOR 0                       ; Start with a black pen
 SQUARE :SIZE                        ; Draw the outer square

 SETPENCOLOR 1                       ; Change to a blue pen
 BLUELINES :SIZE                     ; Draw four short blue lines

 SETPENCOLOR 4                       ; Change to a red pen
 TWINSQUARES :SIZE                   ; Draw two squares point to point

 SETPENCOLOR 2                       ; Change to a green pen
 HALFCIRCLES :SIZE                   ; Draw two separated half circles

 SETPENCOLOR 0                       ; Change to a black pen
END


Mtht420