Introduction To Algorithms: Resonance September 1996
Introduction To Algorithms: Resonance September 1996
net/publication/225852424
Introduction to algorithms
CITATIONS READS
30 6,868
1 author:
R. K. Shyamasundar
Indian Institute of Technology Bombay
290 PUBLICATIONS 1,568 CITATIONS
SEE PROFILE
Some of the authors of this publication are also working on these related projects:
All content following this page was uploaded by R. K. Shyamasundar on 31 May 2014.
Introduction to Algorithms
4. Turtle Graphics
R K Shyamasundar
In the previous articles of this series, we have looked at the basic A good
control structures of languages. In this article, we shall take a look programming
at an integration of such structures with simple graphic language should
commands and illustrate how such an integration aids the help express how
programmer to solve problems in turtle-geometry. Finally, we the program
provide a glimpse of a few of the programming languages that should run, and
support different styles of programming. what it intends to
accomplish.
A Simple Programming Language
Capabilities of Turtle
line n Move n units and draw a straight line in the direction of the Turtle
skip n Move n units in the direction of the Turtle without drawing a line
Anti-clock n Rotate the Turtle by n degrees in the anti-clockwise direction
Clockwise n Rotate the Turtle by n degrees in the clockwise direction
Programming Notation
As elaborated in the earlier articles, algorithms must be written in an unambiguous formal way.
Algorithms intended for automatic execution by computers are called programs and the formal
notations used to write programs are called programming languages. The concept of a
programming language has been around since the mid-fifties. In 1945, the German mathe-
matician Konrad Zuse invented a notation called Plankalkül. Statements in the language had a
two-dimensional format: variables and their subscripts were aligned vertically and operations
on them were laid out along the horizontal axis. Zuse wrote Plankalkül programs on paper
including one that made simple chess moves. Even though the language was not implemented, many
of the ideas developed by Zuse have been introduced in subsequent programming languages.
Drawing a Square
Drawing a Circle
Turing Award
The Turing Award — highest award in Computer Science — is presented annually “to an individual
selected for contributions of a technical nature to the computing community that are judged to be of
lasting and major importance to the field of computing science.” The award commemorates Alan M
Turing, an English mathematician whose work “captured the imagination and mobilized the thoughts
of a generation of scientists” to quote Alan J Perlis, the first recipient of the award.
Programming is fundamental to the computing field. Programs play a dual role both as the notations
that influence thought and also the directions for an abstract computing machine. The influence can be
seen by the fact that in the first 20 years half of the Turing awards have recognized contributions to
programming languages, programming methodology, and programming systems. The award lectures
by Alan J Perlis, Edsgar W Dijkstra, Donald E Knuth, Dana Scott, Michael Rabin, John Backus, Robert
Floyd, C A R Hoare, Ken Thompson, Dennis Ritchie and Niklaus Wirth provide insights into the various
computational aspects as seen by the pioneers.
Complex Patterns
Drawing a Petal
We have shown above how we can draw circles and arcs. Using
the same technique, it should be easy to arrive at a procedure to
draw a petal. For simplicity, the reader can think of a petal as
made up of two quadrants of a circle with some radius say size.
Since the orientation of the turtle is important, let us enforce the
condition that the turtle returns to its original position and
orientation after the petal is drawn. Let us denote the procedure
by
procedure PETAL (SIZE: integer)
The code for this procedure is left as an exercise to the reader.
Now, a command of PETAL (5) will draw a petal similar to that
shown in Figure 2(a) with 5 units as the radius. Now, we can
program a simple flower using PETAL as a procedure. Let us
One can draw assume that a flower has a head, with several petals in a circle, and
spirals of various a stem with a leaf, which looks just like a petal. A program to draw
shapes and sizes a flower with N petals of size S is shown in Table 6.
Using the above procedures, the user can create further patterns
such as a bunch of flowers etc.
One can draw spirals of various shapes and sizes. Consider the
shape shown in Figure 2(c). The growing spiral could be considered
as made up of lines and rotations on the turtle position which is
captured in the program shown in Table 7. Now a program to
draw a spiral is shown in Table 8 which uses the procedure STEP
given in Table 7. Size 1 and size 2 indicate the lengths of the
least and the maximum sides in the spiral respectively.
Exercise
1. Try and trace the above procedure with some values for the
parameters; this will give you a good understanding of the recursive
program structure and the correspondencebetween formal and
actual parameters.
It may be noted
2. The interested reader may write a program to generate
that the program
logarithmic spirals.
for drawing a spiral
is recursive.
It may be noted that the program for drawing a spiral is recursive;
in a sense, it is natural to treat it as a recursive program; by
removing the part in the beginning or at the end, the pattern
remains a spiral. In fact, the program reflects a growing spiral.
That is, it starts from a basic step and adds to it in stages. It is also
possible to write it in the reverse manner. An iterative program
for the spiral is shown in Table 9. Iteration and recursion are two
powerful techniques of programming; a comparative view would
be presented in the forthcoming articles.
Fractals
Table 9 An Iterative
procedure SPIRAL (size 1, size 2: integer, angle:integer); Program for Drawing a
i:=1; (∗ used for the offset from side to side ∗) Spiral.
(∗ assumed to be 1 here ∗)
while size 1 < size 2 do
call step (size1, angle);
size1:= size1+i; (∗ increment the side of the spiral ∗)
end
endprocedure
Figure 3 H – Fractal
Suggested Reading