BFOIT

Introduction to Programming

Introductory Logo Commands


*Note*    In 2007, these lessons were significantly modified.    *Note*
This is a link to the new materials.
This is a link to the new introductory commands lesson.


Introduction

In this lesson, you will write your first computer programs in Logo.  After the introduction of a few commands, it's up to you to program a turtle to draw a bunch of figures.  You will instruct the turtle to draw a few simple polygons, then patterns of rectangles.


An Overview of jLogo and TurtleGraphics

Logo and TurtleGraphics go back quite a long time.  They come from MIT's Artificial Intelligence Laboratory; Seymour Papert, a scientist there, invented TurtleGraphics in the 70s.  Let's read Seymour's own description of it:
... "the turtle." You can think of this as a drawing instrument... Imagine that you are looking at a computer screen. On it you see a small turtle, which moves when you type commands in a language called "turtle talk," leaving a line as it goes. The command "Forward 50" causes the turtle to move straight ahead a certain distance. "Forward 100" will make it move in the same direction twice as far. You soon get the idea that the numbers represent the distance it moves; they can be thought of as turtle steps. Now if you want to make it go in a different direction, you give it a command like "Right 90." It stays in the same place but turns on itself, facing east if it had previously been facing north. With this knowledge you should easily be able to make it draw a box. If that's easy for you, you can think about how to draw a circle, and if that's easy you can try a spiral. Somewhere you will meet your level of difficulty, and when you do I'll give you this piece of advice: Put yourself in the place of the turtle. Imagine yourself moving in the outline of a box or a circle or a spiral or whatever it may be.

And the original turtle:

From "The Children's Machine," Seymour Papert, HarperCollins, 1993.

jLogo is a version of Logo that is written in the Java programming language.  It is sort-of a subset of Berkeley Logo extended to have some traits of Java.  Java is just not a good language to start out with.  By starting out with jLogo, you will get a good feel for what programming is all about.  Given this base, transitioning to Java will not be too hard.


The GraphicsCanvas - TurtleSpace - The Turtle's World

You are going to be learning how to write computer programs which draw things on an electronic canvas.  This makes a lot of sense because most things that you interact with that contain a computer have a display.  Think about it; all personal computers have monitors, notebook computers have a flat-panel displays, cellphones have a small displays, MP3 players - check, a VCR/DVD player - yep...

You are going to be instructing an object, a turtle, to move around on a virtual canvas, drawing as it goes.  The program you will use has an area (a subwindow) called the GraphicsCanvas.  Think of it as the world in which the turtle lives.

Here's what the world looks like to the turtle.  The turtle starts out at the coordinates: 0,0 (the center of its world), heading North.  The turtle's world is a bit different from the Cartesian coordinate system that you are familiar with (from your math classes). 

Figure 2.1

If you look closely, you'll notice that the origin for angle measurement is different.  In TurtleGraphics, 0 degrees is aligned with the positive Y axis instead of the positive X axis.  The other difference is that angle measurement in positive amounts measure clockwise rotation, the opposite direction that you've learned in math classes. 


Four Basic Commands

Here is a table of a few of the most common Logo procedures, in this case commands

Command Inputs Description Example
FD
FORWARD
number Moves the turtle forward, in the direction it is facing, by the specified number of turtle steps. FD 100
BK
BACK
number Moves the turtle backward, i.e., exactly opposite to the direction that it's facing, by the specified number of turtle steps. BACK 150
LT
LEFT
number Turns the turtle counterclockwise by the specified angle measured by a number of degrees (1/360 of a circle). LEFT 180
RT
RIGHT
number Turns the turtle clockwise by the specified angle, measured in degrees (1/360 of a circle). RT 90
Table 2.1

Notice that these commands must include a number, an input.  As an example, when you type in a forward command, you must specify some number of turtle steps.  This makes sense; if you didn't specify a distance, how would the turtle know when to stop?  If you forget to provide the number, you will be reminded, e.g.,


    forward
will result in the error message: "Not enough inputs to forward."

I've written a computer program that can be embedded in webpages (it's a Java applet) which understands jLogo.  I've named it TG, an acronym for TurtleGraphics.  Here it is. 

The turtle (the hexagon with head, legs and a tail) is in the middle of the graphics area.  It is heading north.  It has its black pen down, ready to draw a line as it moves.  Click the left mouse button in the box below the turtle, the CommandCenter - an interaction area.  A cursor (small, thick, black vertical bar) to the right of a "? " prompt should start to blink indicating it is ready for you to type.

alt="Your browser understands the <APPLET> tag but isn't running the applet, for some reason." Your browser is completely ignoring the <APPLET> tag!
TurtleGraphics Applet

To get started, try typing the following instructions which will draw a box:


        forward 100
        right 90
        forward 100
        right 90
        fd 100 rt 90 fd 100 rt 90
  


Exploration - Additional Commands

OK.  Here are a few more commands. 

Command Inputs Description Example
HOME   Moves the turtle to the center of the graphics area, i.e., coordinates 0,0.  
PENUP
PU
  Lifts the turtle's pen up so that it leaves no trace when it moves.  
PENDOWN
PD
  Puts the turtle's pen down so that it leaves a trace when it moves.  
CG
CLEAN
  Erases (cleans) everything that the turtle has drawn on the graphics area (ClearGraphics). The turtle's state (position, heading, pen color, etc.) is not changed.  
SETC
SETPC
SETPENCOLOR
number Sets the turtle's color - the color of its pen. Color is expressed as a number.

Number
Color
Number
Color
Number
Color
Number
Color
0
black
4
red
8
brown
12
salmon
1
blue
5
magenta
9
tan
13
violet
2
green
6
yellow
10
forest
14
orange
3
cyan
7
white
11
aqua
15
grey
SETC 10
HT
HIDETURTLE
  Hides the turtle, makes it invisible.  
ST
SHOWTURTLE
  Makes the turtle visible.  
Table 2.2

Experiment with all of the commands.  Draw

Have fun.  Make each of them a different color with the "setpencolor" command.  You will need to make them different sizes so that you can fit them into the graphics area.

Finally, make sure you have tried out all of the commands in Table 2.1.


Summary

   
   Writing software, computer programs, is a lot
   like writing down the steps to do something.
   
You've learned some of the the jLogo language.  If you got the turtle to draw a triangle, a box, etc..., for you, you got experience instructing it to do things in a specific order.  And, the order is very important.  In programming we refer to this order of steps as a sequence.  In our programs, we moved the turtle and then turned it, moved it, then turned it, moved it then turned it,...  If you put all of the movement commands first followed by all the turn commands, you would not get polygons - you would get a straight line.  So, writing a program is all about putting together instructions in a specific order.


Exercises

Write programs that draw collections of boxes similar to the following ones.

         
    
 

                   
   
 

                  
   
   
   

Since by now the TG applet has scrolled off your screen and you probably want to look at the figures while you are trying to get the turtle to draw them... Here is a link to a pop-up web page containing a tiny version the TG applet. Resize the window so it appears in so that it's just big enough to hold the applet.  It you do this, it should only take up a quarter of the screen leaving lots of room for this web page.


The TurtleGraphics Application (TG)

The nice feature of Java applets is that they are part of web pages.  Once you have the browser on the page containing the applet, you have access to the applet.  But, an important security feature of a Java applet is that it can't do anything to the computer on which it's running.  To insure this, applets have no access to stuff on the computer they are executed on. 

But, as our programs get bigger and more complicated, it would be nice if we had a TurtleGraphics program that could read a file containing commands and execute them as it reads the file.  It would even be nicer if you could change the instructions and/or add more source code to your program, and then save what you've done to a file. 

Java applications can provide this functionality.

I've written a Java application I've named TG (the acronym for TurtleGraphics).  It does everything that the applet you've been using does and more.  But, for you to run it, you need to have a copy of it and a Java Virtual Machine (JVM) on your computer.

Hopefully you or your instructor has installed them on your system.  Appendix F covers installation of all of the software used in this series of lessons. 

If you have a JVM and the TG application on your system, you should be able to start it up with a "java" command:


   java -jar TG.jar
There is a "Help->Commands" menu item to get a full list of what you can tell the turtle to do.  Read the Help->Commands stuff and play with all of the new stuff that's documented.

If you don't have Java on your computer or you would just prefer to continue using the TG applet, Here is a link to a pop-up web page containing only the applet and its documentation.


Back to Table of Contents
Back to previous Lesson ( What's Computer Programming? )
On to next Lesson ( Pseudocode )