The Computer Graphics Companion Online Edition

Damian Scattergood 1999

Index
Home

Download HelpFile

RECURSIVE IMAGES (TURTLE GRAPHICS)

Turtle Graphics is a system for drawing images that was first introduced in the Language LOGO. The basic premise for Turtle Graphics is quite simple. It is based on the idea of a turtle, which appears mid screen and can be moved by ordering it to move about by using a few basic directional commands.

When the turtle moves it leaves a trail behind it, creating the displayed image. One of the features of Turtle graphics is that through recursive calls to functions quite stunning graphic images can be created. Your basic turtle can be moved forward by set distances and may be turned by a given angle.

To experience the power of Turtle graphics you would require a full working version of LOGO, but the program below is simple enough to allow you to see how recursion works, and still creates some quite stunning results.

cx = 160; // Horizontal CenterX of screen.
cy = 100; // Vertical CenterY of screen.
repeats = 700 // The number of repeations to do
forward = 10 // The number of steps forward to move each time
incstep = 10 // The incremental step for the angle
turn = 200 // The angle to turn after each move
PI = 3.141 //The value of PI

Pixel (cx, cy) //Plot first point at centre of screen

angle = 0 //Initial angle at 0 degrees

For a = 1 To repeats
angle = angle + turn // Turn to new angle
ra = (angle * PI) / 180 // Radians of new angle
dx = Sin(ra) * forward // Delta X for new move
dy = Cos(ra) * forward // Delta Y for new move
LineRel (dx, dy) // Draw a line relative from (cx,cy) to (cx+dx,cy+dy)
forward = forward + incstep // Increase step size.
Next a

Simply changing some of the basic settings at the start of the program can change the basic image quite dramatically. The following are two examples of other images created by using alternate values.

repeats = 100

forward = 2

incstep = 1

angle = 88

repeats = 55

forward = 1

incstep = 5

angle = 181

The main advantage of this type of system is that it is extremely easy to store a variety of images in quite a small amount of space. With the above examples it requires only four variables to create each. So you can see that within any computer program it is possible to store a large number of graphic images in a small space. This simple idea of recursion is found again in the area of fractal geometry, and can be used in some very powerful ways. It is possible for instance to create quite complex patterns using only some very simple algorithms. The algorithm above can be altered to create virtually an infinite numbers of patterns.

To take the recursive program a bit further you can add color to your images and create more interesting results. Using color cycling on the images can create quite stunning effects. There are many different variations to this theme. Its really up to you and how much time you have to work and alter the basic algorithm.

  1