Year 7: Crystal Rainforest and Logo

1. Introduction: Crystal Rain Forest

You will solve the Crystal Rain Forest puzzle/game. In Crystal Rain Forest choose Teacher Options and make sure that Level 2 and Activity Mode are selected. Remove the selections from the first four puzzles, leaving the last 8. Complete these as a preliminary to using Logo.

2. Basic Logo

You should now appreciate the meaning and use of the basic Logo commands:

FD 50 BK 50 LT 90 RT 90

Use these with REPEAT n to draw a square. Clear the screen and draw some more polygons, a pentagon, a hexagon and an octagon.

What about a circle? Can you draw a true circle in Logo on a computer screen?

Assignment: In Word show your code and screen shots for regular polygons. Show the code for drawing a circle and explain why it is not possible to draw a true circle on a computer screen.

3. Functions

Writing out the same code over and over again to do the same thing is no fun and computer scientists have long ago found ways to avoid this. They say "Don't reinvent the wheel". One solution to this is to put code inside procedures or functions. Another solution is to save code for later use so you don't have to keep typing it. Another solution would be to use copy and paste from a suitable source.

A function in Logo can be called by name so you can draw a SQUARE or any other shape by defining it with a suitable name. Functions in Logo begin with 'TO' (they are going TO DO something) and end with END.

TO SQUARE
  code goes here
END

Enter your code from the previous exercise for drawing a SQUARE. Use the REPEAT version. You could write out 8 separate commands, FD, LT, FD LT, etc. and it wouldn't matter very much because they are in a function and you won't need to type them again. However, computer scientists like keeping effort to a minimum (they call it 'elegant'). When you press the Enter key after 'END' Logo will tell you that the function has been defined. You can now type 'SQUARE' and Logo will carry out the instructions.

Define some extra functions for PENTAGON, HEXAGON and OCTAGON.

Now you have defined SQUARE etc. as separate functions you can call (did I say that's the word we use when we want to make a function do something) them repeatedly to make patterns. Write a new function called PATTERN1 that calls SQUARE 12 times with an appropriate turn before each one.

TO PATTERN1
code - add it yourself
SQUARE
code - add it yourself
END

Write new functions with more or fewer squares. Write new functions that use different shapes or shapes in combination.

Assignment: Copy the code for your functions into Word along with some screen shots of the graphical outputs in Logo. Look up 'Islamic Art' in Google images. What do you notice? Add a section to your document with title 'Islamic Art', include some images and comment on how you might use Logo to create abstract art like this. This site is particularly interesting as it is a maths site and includes the lines and stages of development.

4. Generalising Functions

Another trick computer scientists like to play is to generalise a function to all sorts of different possibilities. One way of doing this is to use variables inside functions. A variable is simply something with a name that varies (the name is often called an identifier because it identifies something).

Another technique is to pass the variable to a function as a parameter or argument (all these new words!) To draw a square with sides of a specified length we do something like this:

TO SQUARE :LENGTH
REPEAT 4 [ FD :LENGTH LT 90]
END

Instead of a fixed number such as 50 we can now call SQUARE with any number we like:

SQUARE 40
SQUARE 20
SQUARE 100
etc.

Note that you now have to call the function with a value for the parameter. Logo won't run the function without the parameter.

But that is not enough, there is still a fixed number in the function (90). Generalise the SQUARE function further to draw a polygon with any number of sides. To do this you will have to think about the angle the turtle turns through to make a particular polygon. For a square it's 90 and 4x90 = 360, for a hexagon its 6x60 = 360, and so on. The formula for the angle for a square is 360/4, for a hexagon 360/6 and so on. We need to incorporate this information in our new function.

Your POLYGON function should allow you to draw a polygon with any number of sides and any length of side.

Some ideas here.

Assignment: Copy your code for a generalised polygon function into Word and explain how it works.

5. Some More Shapes

Try these additional problems.

6. Recursion, Spirals and Squirals

Here's another of those computer science words. Recursion is where a function is called from inside itself. That's not too hard. The first example on this page illustrates the idea. This does not produce anything interesting, mathematically or aesthetically. We can use recursion to make more interesting things than this.

The trick is to call the function from inside itself with a new value, for example by adding something to the starting value multiplying the starting value by a factor. For example:

TO SQUARE :LENGTH
REPEAT 4 [FD :LENGTH RT 90]
SQUARE :LENGTH + 5
END

Press F12 to stop the program. This is not very interesting either but hold on, we should see something better soon.

To stop the screen filling up like before you will need a line like this:

IF :LENGTH > 100 [STOP]

This looks at the current value of :LENGTH and checks to see if it is more than 100: if it is then the procedure stops.

So how does :LENGTH get to be greater than 100 unless we give it such a value? Look at the code above and notice how we start with a small value and increase it each time we call the function.

Polygons are closed shapes, spirals make open patterns. A squiral is a spiral pattern that looks like a square (words formed from two words like this are called portmanteau words). Write a function that draws a squiral. The length of the line will have to increase after (how many?) sides. You can do this in a recursive call by adding a small value to the length of the side.

Now try making a spiral hexagon (no made up word for this! squexal?)

Now try changing the angles inside the spiral so that they do not multiply out to 180, for example 89 or 91 for a square spiral or 57 or 65 for a hexagonal spiral. Great!

Making a curved spiral is more tricky. Try using multiplication by something like 1.001 to increase the length of the side.

Assignment: As usual, copy your best code into Word and copy the output results to go with them.

7. Trees

You probably (definitely?) could not write this code yourselves but we can use existing code to investigate some intricate shapes that a few lines of Logo can generate. Rather than try to write this code yourselves you should try to figure out how it works and amend it to produce new results. Pick up the basic code for a tree here. Experiment with some trees of different sizes.

Look at the numbers in the code. The TREE function is called recursively with a parameter of LENGTH/2. What happens to each branch as it is drawn? How can you get more branches in your trees? (Change the divisor. What happens if you make the divisor equal to 1? What happens if you make the divisor less than 1?)

Look at the numbers in the code, this time 45 and 90. These are the angles through which the turtle turns to draw the tree. Their relationship is clear. Try replacing these values with other pairs of numbers. This will change the shape of the trees. Try replacing the values with triples of numbers like 60, 40, 20 (the relationship is?)

That's all folks!

Back