logo programming book preview

37

Upload: sandyg31794

Post on 22-Oct-2014

447 views

Category:

Documents


0 download

DESCRIPTION

An ideal gift for children of age 10 to 16! A creative and fun way for children to learn Mathematics. * The Logo programming language is easy to learn and fun to work with. * Children learn to draw geometric shapes, design intricate patterns, and explore exciting ideas such as randomness and recursion. * Children build 21st century skills through programming. * Written by Software Industry veterans, who have taught Logo programming to numerous school children.For more details, please visit www.spark-institute.com/logobook/

TRANSCRIPT

Page 1: Logo Programming Book Preview
Page 2: Logo Programming Book Preview

Contents

Section I: Basics of Logo ....................................................... 11 1.  Getting Started with Logo .......................................................................... 13 

2.  Drawing with the Logo Turtle ................................................................... 23 

3.  Instructions and Procedures....................................................................... 38 

4.  Teaching New Words.................................................................................. 45 

5.  Designs with Repeating Patterns............................................................... 61 

Section II: Powerful Ideas..................................................... 73 6.  Control through Input................................................................................. 75 

7.  Drawing Polygons and Circles .................................................................. 87 

8.  Painting Our Designs ................................................................................ 105 

9.  Mirror Images, Flowers, and other Designs........................................... 115 

10.  Problem‐solving using Divide and Conquer ......................................... 133 

Section III: Appendices....................................................... 153 Appendix A: Logo Primitives (Built‐in Procedures) .................................... 155 

Appendix B: Installing Logo ............................................................................ 160 

Page 3: Logo Programming Book Preview

   

Contents

Section IV: Advanced Exploration........................................ 7 

11.  Drawing Curvy Objects ................................................................................ 9 

12.  Power of Polygons ....................................................................................... 35 

13.  Perspectives and Concentric Shapes ......................................................... 57 

Section V: Programming Adventures................................. 77 

14.  Fooled by RANDOM................................................................................... 79 

15.  Adding Life ‐ Introduction to Animation................................................. 91 

16.  Turtles that Climb Trees............................................................................ 105 

17.  Looking Back and Final Project................................................................ 129 

Section VI: Appendices....................................................... 133 

Appendix C: Additional Logo Primitives (Built‐in Procedures) ................ 135 

Page 4: Logo Programming Book Preview

 

  Series on Learning through Computer Programming  5 

Preface  The other day, while working on this book, one of the authors was struggling to get a feature in his word processor to work. His 8‐year old son was watching curiously from behind  to  see what his Dad was up  to on his  shining new  laptop. When he noticed Dadʹs hesitation, he leaped over his Dadʹs shoulders, clicked a few places on the screen and exclaimed, ʺThere you go, Dad! Is that what you wanted?ʺ  Dad  was  naturally  very  pleased  with  his  son,  and  also  amazed  at  his  sonʹs knowledge of this not‐so‐common feature. 

The truth is: Children have a natural fascination for computers. We see young children ‐ some of them barely out of preschool ‐ handling with ease computers  and  gadgets  such  as  video‐game  consoles  and  mobile  devices.  They browse the Internet, connect with friends using social networks, play games, or use ready‐made software like word processors.   Our schools have surely begun to realize the promises of the digital revolution. Yet, the best attempts in many schools to introduce the computer to their students do not go beyond treating it as a productivity tool, i.e. as an assistant for routine work.  What really needs to happen is that our children take the driverʹs seat, feel the real power  of  computers,  do  creative  work,  and  learn  in  the  process.  This  book demonstrates a novel idea that can achieve this exact goal. 

The new idea is to use Programming as a medium for learning. Programming means tapping into the computerʹs immense power by talking with it directly. Through programming, children use the computerʹs terrific power to draw graphics,  design  animation,  solve mathematical  or word  puzzles,  and  even  build robots.  This  idea was first proposed  in  the famous book  ʺMindstormsʺ by Seymour Papert and  has  subsequently  been  appreciated  and  praised  by  educators  and  parents  all over the world.  

Page 5: Logo Programming Book Preview

Fun with Logo Programming – Part 1 

6  Series on Learning through Computer Programming 

Children see the computer as a powerful assistant. Through  programming,  students  discover  that  the  computer  is  a  powerful  and flexible  tool.  Using  interesting  ideas  embedded  in  programming  environments, students  solve  problems  in  their  favorite  subjects,  and  also  develop  interest  in ʺdifficultʺ subjects like Math and Physics.   

Children learn through programming. • Children become active learners, because they explore ideas through a hands‐

on activity. • They learn to think about and analyze their own thinking, because that is the 

only way to program computers. • They  learn  the  technique  of  dealing with  complex  problems  by  breaking 

them into smaller sub‐problems. • They learn a new way of thinking (called ʺcomputationalʺ thinking). • In the world of programming, answers are not simply ʺrightʺ or ʺwrongʺ; this 

prepares childrenʹs mindset for real life problems. • Childrenʹs overall  learning process transforms from acquiring  facts to creative 

and analytical thinking.  

References 1. Mindstorms: Children, Computers, and Powerful Ideas by Seymour Papert, 

Professor at MIT. (Also http://papert.org/: Papertʹs website) 2. Computer Science Logo Style by Brian Harvey, Professor at UC Berkeley. 3. Apple Logo for Teachers by Earl Babbie (Wadsworth Publishers, 1984) 4. http://www.cs.berkeley.edu/~bh/: Articles by Prof. Brian Harvey, UC 

Berkeley. 5. http://el.media.mit.edu/logo-foundation/: Website of Logo Foundation 6. http://scratch.mit.edu: Website of the Scratch project 

   

Page 6: Logo Programming Book Preview

 

  Series on Learning through Computer Programming   13 

 

1. Getting Started with Logo

Why Learn Logo Programming? Computer  programming  is  one  of  the most  exciting  and  powerful ways  to  use  a computer. And Logo is a fascinating programming language that practically anyone ‐ children and adults alike ‐ can learn to explore the art of computer programming.   You may ask: what is so special about programming?  Let us look at the picture below. 

 Figure 1‐1 

 The following Logo program draws this figure:  REPEAT 6 [FORWARD 100 RIGHT 60]

Page 7: Logo Programming Book Preview

Fun with Logo Programming – Part 1 

14  Series on Learning through Computer Programming 

 You can also easily draw it using drawing software like Microsoft Paint.   So, whatʹs  the difference? Why should anyone bother  to  learn programming when readymade software can do the job?  Well, now, see the picture below. By modifying your Logo program just a little you can create this new picture.   REPEAT 6 [ REPEAT 6 [FORWARD 50 RIGHT 60] RIGHT 60 ]

 Figure 1‐2 

 Now, try drawing that using Microsoft Paint!!  This  is  just one  example  of how Programming gives you  a  special power  that no ready‐made software can. Programming gives you a direct hotline to the brain of the computer. It allows you to try out your creative ideas. And most of all, it is a lot of fun!  

Page 8: Logo Programming Book Preview

 

  Series on Learning through Computer Programming   23 

2. Drawing with the Logo Turtle

Introduction In  the previous chapter, we got a glimpse of  the power of Logo programming. We started talking with the computer using words like PRINT.  Can we get Logo to draw things on the screen, like the ones shown below?   

 Figure 2‐1 

 The answer is Yes! We can create these and many other exciting drawings with the help of the Logo Turtle! Let us get introduced to this new friend. 

Page 9: Logo Programming Book Preview

 Drawing with the Logo Turtle 

  Series on Learning through Computer Programming   27 

? RIGHT 90

 Figure 2‐4 

 Using the command RIGHT 90, we told the Turtle to turn right by 90 degrees.  Now, we can ask  the Turtle  to move forward  again  by  typing  the following.  ? FORWARD 100    

Figure 2‐5  This is getting interesting, isnʹt it! Can you complete the picture and draw a square?   Here  is how we  can draw  the  remaining  sides of  the  square  and bring  the Turtle back to its original position.  ? RIGHT 90 ? FORWARD 100 ? RIGHT 90 ? FORWARD 100 ? RIGHT 90  

 Figure 2‐6 

 What if we wanted to erase everything on the screen and start all over? Simple! We have done it once already. Type:  ? CLEARSCREEN  

Page 10: Logo Programming Book Preview

Teaching New Words 

  Series on Learning through Computer Programming   45 

 

4. Teaching New Words

Introduction We are now able to create interesting shapes and designs using words (procedures) that Logo knows. These procedures are called Logo primitives.   Wouldn’t it be fun if we could teach Logo new words?  For example, it would be nice if Logo could draw the blocks (as shown below), when we simply type blocks:  ? blocks

 Figure 4‐1 

 In this chapter, we will see how we can teach Logo such new words.     Right now, of course, Logo will just give us the error message: I don’t know how to blocks. 

Page 11: Logo Programming Book Preview

Teaching New Words 

  Series on Learning through Computer Programming   55 

The Concept Called “Abstraction” (Just FYI) Abstraction means  capturing an  idea  into a word  (or phrase). The new procedure “Square”  is  an  abstraction  of  the  idea  of  drawing  a  shape  consisting  of  4  sides. Defining a new word that has a specific meaning is called abstraction. No big deal! 

Play Time: Define the following new Logo procedures. Verify that these new procedures work as expected. 1. A procedure called “Myinfo”  that clears  the screen and shows your  full name, 

school name, and city. 2. Procedures for geometric shapes like rectangle and triangle. 3. Procedures for English letters (stick or block) like i, H, E, or A. 4. Procedure for rectangle wave.  

Logo Challenge  Can you write a Logo procedure to draw this design? 

 Figure 4‐6 

New words and old words: Once defined, a new procedure  is  treated  (by Logo)  just  like any other procedure that  Logo  already  knows.  There  is  no  special  treatment  for  new  procedures.  For example, if you want to define yet another new procedure, you can make use of both old (primitives) and new procedures.  

Page 12: Logo Programming Book Preview

 

  Series on Learning through Computer Programming   61 

 

5. Designs with Repeating Patterns

Introduction Take a look at the following design. It appears like a powerful multi‐color star.   

  

Figure 5‐1  

Do we know  enough of Logo  to draw  this  star? Probably not. There  is  actually  a powerful new way to draw this star (and many other exciting designs) quite easily. Read on to learn about it. 

Page 13: Logo Programming Book Preview

Designs with Repeating Patterns 

  Series on Learning through Computer Programming   63 

How about drawing a rectangle wave?   

 Figure 5‐4 

 Do you see a repeating pattern in each of these?  Just like the staircase, there is a basic pattern (that is repeated) in each of the above pictures.  If we  can work  out  the Logo  instructions  for  each basic pattern, we  can simply type these instructions a number of times to get the final picture.   So, for each of these pictures a certain set of instructions needs to be repeated.  Fortunately,  instead  of  us  having  to  type  these  instructions  repeatedly,  we  can simply use a procedure that Logo provides to perform the repetition for us. Can you guess the name of that Logo procedure?  Yes! You guessed correctly. It is called REPEAT. 

How to Use the REPEAT Procedure Ask Logo to say “I am an idiot” 4 times!  ? REPEAT 4 [PRINT [I am an idiot]] I am an idiot I am an idiot I am an idiot I am an idiot  The basic format of REPEAT procedure is as follows: REPEAT N [List of Instructions]  N  is  the  number  of  times  you  want  some  actions  to  be  repeated,  and  List of Instructions gives the actions that you want to be repeated.   

Page 14: Logo Programming Book Preview

Designs with Repeating Patterns 

  Series on Learning through Computer Programming   65 

Drawing a star using REPEAT: Ok, you  think  that REPEAT saves  typing effort. And you are right. But, REPEAT  is much more interesting than just being a ʺtyping assistantʺ. Drawing a star (as below) gives us a perfect opportunity to glimpse the power of REPEAT.  Letʹs  say  we  want  to  draw  the  ʺpoint starʺ as shown here.  It  is  basically  a  bunch  of  rays  (line segments) coming out of a point. Right? This particular star has 20 rays. And  the gap (angle) between each adjacent pair of rays is constant. 

 Figure 5‐9 

 Letʹs see how we would go about drawing such a star in Logo.  To simplify the problem, let us first try a star with fewer rays.   This star has 8 rays. We will assume that the Turtle  is  initially at  the center. After drawing each ray (FD and BK), the Turtle has to take a right turn. Since there are 8 equal  turns, each of  these  turns  (angles) must be 45 (360/8). 

 Figure 5‐10 

     

Page 15: Logo Programming Book Preview

Designs with Repeating Patterns 

  Series on Learning through Computer Programming   69 

What We Learnt in this Chapter 1. When we draw designs that have a repeating pattern, it gets tedious to type the 

same instructions again and again. REPEAT comes to rescue. 2. Introduction to REPEAT: 

a. REPEAT is a labor‐saving idea ‐ not for the computer, but for us. b. It is also a powerful idea ‐ something that opens up new possibilities. 

3. We begin to see the value of using a text editor (like Notepad) ‐ it keeps a record of programs AND helps in debugging (trouble‐shooting). 

4. The key sequence CNTL-Q helps you get the ʺ?ʺ prompt (ALT-S on UCB Logo 6.0)  if you want to terminate a program, or  if you don’t see the ʺ?ʺ prompt for any reason. 

5. It  is  important  to  first  figure out  (in your mind or notebook) what  exactly  is getting repeated before using REPEAT. 

  

Suggestions to Teachers/Parents REPEAT is an extremely powerful idea. It will help to practice it to really appreciate its power and to understand how best to utilize  it. Think of repetitive patterns and designs which can be drawn using the basic Turtle commands, and give them to the children for practice. 

Page 16: Logo Programming Book Preview

 

  Series on Learning through Computer Programming   75 

 

6. Control through Input

Introduction With  the  ideas we have  learnt so  far  ‐ Turtle commands, creating new procedures, and the REPEAT command ‐ can we draw some of the designs shown below?  

 Figure 6‐1 

 We probably can. We can use our  ‘Point.Star’ procedure  to draw  the stars. But, we will have to constantly modify the procedure to draw the variety shown above. Logo offers an  idea  that allows a  single  star procedure  to draw any and all of  the  stars above. Won’t that be interesting? So, read on this chapter to learn this new idea. 

Page 17: Logo Programming Book Preview

Control through Input 

  Series on Learning through Computer Programming   79 

Step 4 is to put this name at the end of the TO line as shown below. This tells Logo that size is the input of Hexagon.  TO Hexagon :size  Step 5  (and  the  final step)  is  to put  the new name wherever we refer  to size of  the hexagon.  After making all these changes this is what we get:  ERASE "Hexagon TO Hexagon :size REPEAT 6 [FD :size RT 60] END  Note: Donʹt  forget  the  colon  ‐  it  tells Logo  that  the word  that  follows  is  an  input. Without ʺ:ʺ Logo will treat the word as procedure name.  Bingo! Our hexagon procedure will now take input just like FD or RT.  ? Hexagon not enough inputs to hexagon With inputs we get: 

Hexagon 100

 Figure 6‐3 

Hexagon 50  

 Figure 6‐4 

  

Page 18: Logo Programming Book Preview

 

  Series on Learning through Computer Programming   105 

 

8. Painting Our Designs

Introduction Drawing figures (outlines) usually goes hand in hand with painting, i.e. filling areas with color. And of course, color makes our creative ideas more lively and exciting.  

 Figure 8‐1 

 In this chapter, we will learn how to paint shapes in Logo, i.e. how to fill color in our designs to make them look beautiful. And  it  is quite straightforward. You can then draw pretty designs as shown above. 

Page 19: Logo Programming Book Preview

Fun with Logo Programming – Part 1 

108  Series on Learning through Computer Programming 

And then the jump and paint job:  

PU RT 45 FD 10 PD

SETPC 2 FILL

 

Since  the  nearest  closed  shape  for  the Turtle  (when FILL  is called)  is  the space between the two squares, thatʹs what got painted. 

 Figure 8‐8 

 

Play Time: 1. Write  a procedure  called fsquare  that  takes  2  inputs: size  and color,  and 

draws a painted square. For example, ʺfsquare 100 2" will draw a square of size  100  painted  green.  ʺfsquare 300 13" will  draw  a  square  of  size  300 painted purple (Solution explained in the following section). 

2. Figure  out how you  can paint  a  triangle. Write  a procedure ftriangle with size and color inputs. 

The FSQUARE Procedure We know that the following set of instructions draws a square of size 100 and paints it with the red color. ; Draw a square REPEAT 4 [FD 100 RT 90] ; Jump inside PU RT 45 FD 50 PD ; Set color to red and paint SETPC 4 FILL  To finish the job, we should get the Turtle back to its original position. ; Jump back outside PU BK 50 LT 45 PD  We can put all these instructions into a procedure called FSQUARE as below:  

Page 20: Logo Programming Book Preview

 

  Series on Learning through Computer Programming   115 

 

9. Mirror Images, Flowers, and other Designs

Introduction Using a basic  shape  (such as a  simple  square), we  can  create a  lot of  entertaining designs by moving it in different ways. Imagine that you have cut out a shape drawn on paper. You could draw its outline by placing it anywhere on a drawing paper. Or, you could  flip  it along an edge  to get a mirror  image. Or, you can hold one of  its corners with a real pin and rotate it around this pin, while drawing outlines all along to get a flowery pattern.  We will learn how to do these cool things using simple techniques in Logo. 

Hopping Around This is the simplest of the possible movements. See below: 

 Figure 9‐1 

 

Page 21: Logo Programming Book Preview

Fun with Logo Programming – Part 1 

122  Series on Learning through Computer Programming 

Nested REPEAT (Just FYI): Instead of using  the square procedure,  if we had simply used Logo  instructions, our program to draw a flower of 18 squares would look as follows:  REPEAT 18 [ REPEAT 4 [FD 100 RT 90] RT 20 ]  We  have  used  a  REPEAT  inside  another  REPEAT.  In  programming  lingo,  this  is called nested REPEAT. Just for your information! 

 Since we are calling our designs as flowers, we will call each of its squares as a petal. 

Play Time: 1. Write  a  procedure  called  sqflower  (short  for  flower  of  squares)  that  takes  2 

inputs: size and a number N: size is for the size of each square, and N specifies the number of petals. So, for example: sqflower 100 5 should draw a flower of 5 squares each of size 100 sqflower 50 20 should draw a flower of 20 squares each of size 50  

2. Replace  the  square with  a  triangle  and  draw  various  types  of  triangle  flowers: dumb‐bell (2 petals), fan (4 petals), and so on. 

 Figure 9‐14 

 Figure 9‐15 

 Figure 9‐16 

 3. Draw flowers of other polygon shapes: pentagon, hexagon, etc. 

Page 22: Logo Programming Book Preview

Fun with Logo Programming – Part 1 

126  Series on Learning through Computer Programming 

Key Observations • REPEAT  is not  just a handy  little command  that saves  typing; amazing new 

possibilities  open  up  that would  be  impossible without  REPEAT.  (Imagine drawing a flower of circles without the help of REPEAT). 

• Before using REPEAT, you must  first  figure out  the  repeated basic pattern, which may consist of a visible pattern (lines) and an invisible pattern (angles and gaps). 

• In order to figure out the basic pattern in a complicated design, simply trace the motion of Turtle  from  the central point of  the design and note all  lines and turns. Remember, in a regular polygon, all lines and turns must be equal. 

• Programming allows  infinite experimentation  ‐ so donʹt be afraid  to  try out your  ideas  and  hunches.  If  they  turn  out  to  be  incorrect  (weird  stuff  gets drawn  on  the  screen),  either  pat  yourself  on  the  back  for  amazing  new discoveries  (if  the weird  stuff  looks  pretty)  or  go  back  to  your  notebook, make changes, and try again. 

• Do not forget to solve the problem in your mind (and notebook) first, before writing Logo programs. Remember that Logo is not going to help you solve the  problem;  it will  simply  replicate  all  the mistakes  that  you  have made yourself in your design. 

• The geometry you  learnt  in school does come handy  in drawing designs. In turn,  Logo  programming  will  help  you  understand  and  appreciate  your school geometry. 

• When you define  a new Logo procedure, define  it without  any  input  first. Make sure  it works as expected. And  then add  inputs one at a time,  testing the procedure at every step. 

  

Page 23: Logo Programming Book Preview

 

  Series on Learning through Computer Programming   133 

 

10. Problem-solving Using Divide and Conquer

Introduction We have been learning a lot of little tricks of programming so far and applying them to write  lots of  little programs. Now, we are  ready  to write  some  ʺseriously  coolʺ programs. See the collage below.  

 Figure 10‐1 

 But, before you start churning out Logo programs for these fancy objects, letʹs take a couple  of  simple  examples  and  understand  the  process  of  how  to  go  about developing such programs. 

Page 24: Logo Programming Book Preview

Fun with Logo Programming – Part 1 

138  Series on Learning through Computer Programming 

that there is very little room for making mistakes, and the components can be used for other designs also.  

Self‐study: Using the above procedure, how will you draw block arrows pointing in other directions, such as, North or South? 

Play Time: Draw a collage of block arrows of different sizes, pointing in different directions, and of different colors and thickness. 

House Example   Letʹs  go  through  another  interesting example to make sure we understand the Divide‐and‐conquer  method.  We  will attempt  to  draw  the  house  as  shown here.  First, we take a step back and take a keen look at  the design and ask  the  standard question:   

Figure 10‐8  ʺWhat  is  this design made up of? Can we break  it apart  into meaningful  reusable components or parts?ʺ  We come up with the answer: ʺYes, we see three parts: a wall, a roof, and a window.ʺ  

 Figure 10‐9 

Page 25: Logo Programming Book Preview

 

  Series on Learning through Computer Programming   9 

 

11. Drawing Curvy Objects

Introduction With  the  exception of  circles, we have  so  far only  seen how  to draw designs  that contain straight lines. We don’t know yet how to draw nice curvy objects like a lotus flower, a flying bird, or a fish. Or a beautiful garden as shown below:  

 Figure 11‐1 

 In this chapter, we will discover that we can draw curves without needing to  learn anything new; not even new Logo commands. We will see that itʹs all just the magic of simple geometry. 

Page 26: Logo Programming Book Preview

Drawing Curvy Objects 

  Series on Learning through Computer Programming   15 

The median of the petal would be at half the angle of the right turn (i.e. 45). So,  in order to get a symmetric petal around the Turtleʹs present orientation, we should tilt the Turtle to the left by 45 before drawing the petal.  ERASE "petal2 TO petal2 :size ; size is diameter (of the full circle) LT 45 REPEAT 2 [ q.dcircle :size RT 90 ] RT 45 END  Petal2 150  

 Figure 11‐11 

 

Play Time: 1. Draw a candle as shown below.  

 Figure 11‐12 

 

Page 27: Logo Programming Book Preview

Drawing Curvy Objects 

  Series on Learning through Computer Programming   21 

;Quarter circle anti-clockwise. Size is diameter. ERASE "Q.dcircleL TO Q.dcircleL :size REPEAT 90 [FD :size*3.14159/360 LT 1] END Using a combination of right quarter circle and left quarter circle we can create a nice looking wave:  ERASE "waves TO waves :n :size REPEAT :n [ q.dcircle :size q.dcircleL :size ] END waves 3 40

 Figure 11‐26 

  Self‐study: Make the wave above appear horizontal. 

Logo Challenges

Chestnut leaves:

 Figure 11‐27 

The figure shows the  leaf pattern of a Chestnut  tree. The amazing  thing is that all Chestnut leaves appear in the exact  same pattern as  shown  in Figure 11‐27: always  in groups of 7 with  one  large  leaf  at  the  top,  and remaining  6  appearing  as  mirror images.  Hint:  First  write  a  procedure  to draw an  individual  leaf  (use a  ʹsizeʹ input to control the size of the leaf). Then, design the pattern above. 

 

Page 28: Logo Programming Book Preview

 

  Series on Learning through Computer Programming   35 

 

12. Power of Polygons

Introduction Earlier, we used our knowledge of geometry to draw interesting but simple designs using regular polygons. We can explore this idea further and develop some expertise in this very exciting field of “Geometric Designs”.  You must  have  seen  patterns  ‐  like  the  one  shown  here  ‐  used  for  tiles,  cutlery, architectures,  and  even paintings. All  that we  really need  to  know  to  create  such designs  is polygons, Turtle Round Trip principle, and the Logo commands that we already know. Are you interested? Then, read on. 

 Figure 12‐1 

Page 29: Logo Programming Book Preview

Fun with Logo Programming – Part 2 

40  Series on Learning through Computer Programming 

Reversing Sides What will we need to do to draw a figure shown below? 

 Figure 12‐9 

 What  if you were  told  that  this  figure  is related  to  the next  figure  in  some way? Clearly,  the  outside  octagon  has  been made to hide.  But, how do we get  the following figure in the first place?  

 Here is the code that will produce the design.  REPEAT 8 [ ;Draw the inside triangle Polygon 100 3 ;Draw side of octagon FD 100 RT 360/8 ] It’s really the same idea as before, except this  time,  the  triangle  is  drawn  on  the inside of the main octagon.  

Figure 12‐10  You could get rid of the octagon in two ways: (1) avoid drawing it in the first place – this will require you to modify the triangle procedure, or (2) Use PENERASE to erase the Octagon. The following code shows  the second approach  to get  the star shown above.  PENERASE REPEAT 8 [FD 100 RT 360/8] PENPAINT

Page 30: Logo Programming Book Preview

 

  Series on Learning through Computer Programming   57 

13. Perspectives and Concentric Shapes

Introduction Do you like the design shown below?  

 Figure 13‐1 

 Can you guess how it can be drawn in Logo?   Yes! You can give a series of circle commands and increase the size slightly every time. But, that would be tedious, wonʹt it?     

Page 31: Logo Programming Book Preview

Fun with Logo Programming – Part 2 

58  Series on Learning through Computer Programming 

How about drawing this beautiful design shown next?  

 Figure 13‐2 

 Can you guess how this design can be drawn in Logo?   If you  look  carefully, you will  see a  series of  squares drawn around a point, with each  square  slightly  bigger  than  the  previous  one. Once  again,  you  could  give  a series  of  square  commands  and  increase  the  size  slightly  every  time.  But,  that would be horribly tedious, wouldnʹt it!   Continue reading this chapter,  if you would  like to  learn a much more elegant and simpler way of drawing this sort of designs in which there is repetition for sure, but there is a twist after each repetition. 

Counting Repetitions Imagine  that  you  want  to  run  around  a  tree,  and  your  friend  is  watching  you standing aside. You could ask him to count 1, 2, 3… as you go around the tree. So, you could ask him any time, “Hey, what round am I doing now?” And he would tell you, ʺItʹs the 9th roundʺ, or something like that. In a sense, your friend is your counter.  

Page 32: Logo Programming Book Preview

 

  Series on Learning through Computer Programming   79 

 

14. Fooled by RANDOM

Introduction One of the most exciting  ideas  in programming, and even  in real  life,  is the  idea of randomness. We often say  things  like,  ʺOh,  that person  is so random!ʺ Philosophers often muse  about  the  randomness of  events. The  industry  of gambling  is  entirely based on the idea of random outcomes. Take a look at the picture below:   

 Figure 14‐1 

 

Page 33: Logo Programming Book Preview

Fooled by RANDOM 

  Series on Learning through Computer Programming   83 

 Figure 14‐4 

 As  you  can  see,  we  get  this  weird‐looking window.  In  fact,  your window will most  likely  look quite different! Get it? It’s the RANDOM thing!

 Now, if we simply put this in a loop and run  it  a  number  of  times,  we  get something  that  looks  like  a  shattered window!  REPEAT 50 [ REPEAT 4 [ square (RANDOM 150) RT 90 ] ]  

 Figure 14‐5 

 If you increase the outer loop count to a really large number (like 10000) you will just get a  large painted square. That’s because, when run 10000 times, RANDOM ends up giving practically all possible numbers  in the available range (0 to 100), and so, we get squares of all possible sizes drawn very close  to each other giving  the effect of FILL.  

Insight: If called a large number of times, RANDOM usually ends up giving every value  in  the given range – although  this  is not guaranteed, nor  is  it predictable how often it will give a particular value. 

 So, as you see, the application of RANDOM really depends on our creativity.  

Page 34: Logo Programming Book Preview

 

  Series on Learning through Computer Programming   91 

15. Adding Life - Introduction to Animation

Introduction Animation means life. Animation means motion and action. Animation means fun!  The good news is that we can do animation in Logo. So far, our Logo programs have produced nice‐looking graphics and  images. We are now  ready  to make our Logo drawings come alive and perform actions. As you might know already, animation is an  illusion of motion  (or  action)  created by persistence of vision  and  a  successive display of slowly changing pictures.   For a quick demo of animation, just run the following program and watch the screen. Press Ctrl‐Q (Alt‐S in UCB Logo 6.0) when you have seen enough!  REPEAT FOREVER [ FD 100 BK 100 ; Draw a line WAIT 5 ; Wait for some time PENERASE ; Turn the Eraser mode on FD 100 BK 100 ; Erase the line drawn earlier PENPAINT ; Restore the Pen RT 5 ; Turn right by 5 degrees ]  Isnʹt  it  interesting?  Can  you  figure  out  how  the  program works  by  reading  the comments  in  the  code?  (Note: REPEAT FOREVER  is  a  special  form of REPEAT in which things go on, well, forever!)  Read further to learn how to write animation programs in Logo. 

Page 35: Logo Programming Book Preview

Fun with Logo Programming – Part 2 

96  Series on Learning through Computer Programming 

ERASE "Bcwheel TO Bcwheel SETPENSIZE 5 REPEAT 100 [ FD 50 BK 50 RT 360/100 ] SETPENSIZE 20 REPEAT 10 [ PU FD 50 FD 20 PD FD 50 BK 50 PU BK 20 BK 50 PD RT 360/10 ] END CS SETBG 7 SETPC 0 Bcwheel

 Figure 15‐7 

Shapes with Wings and Legs Well, we donʹt mean  literally, but  in  the sense  that our stationary shapes will start moving around the screen as if they were living or motorized objects.   In a previous chapter, we wrote a procedure to draw a block arrow as shown below. Now, we will see how we can make it move around the screen.  

 Figure 15‐8 

 To create the moving arrow effect, we could try the following steps: 1. Draw a block arrow. 2. Wait for a short time. 3. Clear the screen. 

Page 36: Logo Programming Book Preview

 

  Series on Learning through Computer Programming   105 

 

16. Turtles that Climb Trees

Introduction You probably noticed the Fern branch and Tropical tree drawn on the very first page of this book. Here they are once more:  

   Figure 16‐1 

 Believe  it or not,  these  figures have been drawn using Logo programs – programs containing not more than 15 instructions each! They are based on an interesting idea called Recursion.  Continue reading this chapter to learn about recursion and how to draw these sort of designs in Logo. 

Page 37: Logo Programming Book Preview

Turtles that Climb Trees 

  Series on Learning through Computer Programming   121 

Hint: The following instructions will draw a single curved line and return the Turtle to its base (this code would actually be level 1 of the recursion):   REPEAT 10 [FD 25 RT 5] REPEAT 10 [LT 5 BK 25]  You  just have to recursively implant the same curved line at a number of places on both sides.

Logo Challenge The trees shown next have the following new features: - The tree looks different every time you call the instruction (even if all inputs are 

the same) - The  tree shows  fruit at some places. The  fruit shows up at random places, and 

the amount of fruit is controlled through an input.  Hints: (1) Obviously, we have used RANDOM to make things happen differently every time. (2) To draw the fruit, add code  just before the STOP command, because that’s where the branches end.   

   Figure 16‐18