






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Lecture 1: Turtle Graphics the turtle and the crane and the swallow observe the time of their coming; Jeremiah 8:
1. Turtle Graphics Motion generates geometry. The turtle is a handy paradigm for investigating curves generated by motion. Imagine a turtle that after each step leaves a trail connecting her previous location to her new location. As this turtle crawls around on a flat surface, the turtle may traverse paths that form intriguing geometric patterns (see Figure 1). Figure 1: Some interesting geometric patterns generated by turtle paths. Some of you may have encountered this mythical turtle before in the programming language LOGO. LOGO has been used in primary schools to introduce young children to programming, but the turtle is more than just a toy for young children. In their book Turtle Geometry , Abelson and DiSessa use turtles to investigate many advanced topics, ranging from elementary differential geometry and topology to Einstein's General Theory of Relativity. The study of geometry using programming is the branch of Computer Science called Computational Geometry. We shall begin our investigation of Computer Graphics with Turtle Graphics. After developing a simple variant of LOGO, we will see that Turtle Graphics can be used to generate many diverse shapes, ranging from simple polygons to complex fractals. 2. Turtle Commands To help understand the ideas behind Turtle Graphics, we introduce a virtual turtle. The virtual turtle is a simple creature: she knows only where she is, in which direction she is facing, and her step size. The virtual turtle obeys solely simple commands to change either her location, or her heading, or her notion of scale. Consider such a virtual turtle living on a virtual plane. The turtle’s location can be represented
by a point P (a dot) given by a pair of coordinates € ( x , y ); similarly the turtle’s heading can be represented by a vector w (an arrow) given by another pair of coordinates € ( u , v ) (see Figure 2). The step size of the turtle is simply the length of the vector w. Thus the step size of the turtle is € | w |= u 2
€
€ w Figure 2: The virtual turtle is represented by a point P (dot) and a vector w (arrow). Location is a point; direction is a vector. Points are represented by dots; vectors are represented by arrows. Points have position, but no direction or length; vectors have direction and length, but no fixed position. In many branches of science and engineering, the distinction between points and vectors is often overlooked, but this distinction is very important in Computer Graphics. We shall see shortly that computationally points and vectors are treated quite differently in LOGO. The pair € ( P , w ) is called the turtle’s state. Although internally the computer stores the coordinates € ( x , y ) and € ( u , v ) , the turtle (and the turtle programmer) has no access to these global coordinates; the turtle knows only the local information € ( P , w ) , not the global information € ( x , y ) and € ( u , v ). That is, the turtle knows only that she is here at P facing there in the direction w ; she does not know how P and w are related to some global origin or coordinate axes, to other heres and theres. This state model for the turtle is similar to the model of a billiard ball in classical mechanics, where physicists keep track of a ball's position and momentum. Turtle location is analogous to the position of the billiard ball, and turtle direction is analogous to the momentum of the billiard ball. The main difference between these two models is that for billiard balls, the laws of physics (differential equations) govern the position and momentum of the ball; in contrast, we shall write our own programs to change the location and direction of the turtle. The turtle responds to four basic commands: FORWARD, MOVE, TURN, and RESIZE. These commands affect the turtle in the following ways:
€ wnew
w ⊥ sin( α ) € w cos( α ) €
€ w € w ⊥ €
Figure 4: The command TURN € α changes the turtle’s heading, but leaves her position and step size unchanged. To derive the turtle’s new heading, we work in the turtle’s local coordinate system. Let € ( P , w ) be the turtle’s current state, and let € w ⊥ denote the vector perpendicular to € w of the same length as € w. Then € wnew = w cos( α) + w ⊥ sin( α ). But if € w = ( u , v ), then € w ⊥ = (− v , u ) (see Exercise 11). Therefore in terms of coordinates € unew = u cos( α ) − v sin(α) vnew = v cos( α ) + u sin(α) or in matrix notation €
cos( α ) sin(α) − sin( α) cos( α)
€ S w € w Figure 5: The command RESIZE S changes the turtle’s step size by a factor of S , but leaves her position and heading unchanged. The turtle’s new direction vector is given by € wnew = S w. Thus in terms of coordinates € unew = S u vnew = S v or in matrix notation €
The turtle state € ( P , w ) is a complete description of what the turtle knows, and the four turtle commands FORWARD, MOVE, TURN, RESIZE are the only way that a programmer can communicate with the turtle. Yet with this simple setup, we can use the turtle to draw an amazing variety of interesting patterns in the plane.
3. Turtle Programs Once the four turtle commands are implemented, we can start to write turtle programs to generate a wide assortment of shapes. The simplest programs just iterate various combinations of the FORWARD, TURN, and RESIZE commands. For example, by iterating the FORWARD and TURN commands, we can create polygons and stars (see Table 1). Notice that the angle in the TURN command is the exterior angle, not the interior angle, of the polygon. Circles can be generated by building polygons with lots of sides. Iterating FORWARD and RESIZE, the turtle walks along a straight line, and iterating TURN and RESIZE, the turtle simply spins in place. But by iterating FORWARD, TURN, and RESIZE, the turtle can generate spiral curves (see Figure 6). POLYGON N STAR N SPIRAL N, A, S REPEAT N TIMES REPEAT N TIMES REPEAT N TIMES FORWARD 1 FORWARD 1 FORWARD 1 TURN € 2 π / N TURN € 4 π / N TURN A RESIZE S Table 1: Simple turtle programs for generating polygons, stars, and spirals by iterating the basic turtle commands. For the spiral program, A is a fixed angle and S is a fixed scalar. Figure 6: A pentagon, a five pointed star, and a spiral generated by the programs in Table 1. Here the spiral angle € A = 4 π / 5 and the scale factor for the spiral is €
With a bit more ingenuity (and with some help from the Law of Cosines), we can program the turtle to generate more complicated shapes such as the wheel and the rosette (see Figure 7). 5
Figure 10: The effect of composing SPIN with SCALE: starting with a triangle (left), a star (center), and a pentagon (right). Shapes built by iteration are cute to visualize and fun to build, but by far the most interesting and exciting shapes that can be created using Turtle Graphics are generated by recursion. In our next lecture we shall show how to design fractal curves using recursive turtle programs.
4. Summary Turtle programming is the main theme of this lecture, but this chapter also includes several important leitmotifs. In addition to gaining some facility with turtle programming, you should also pick up on the following distinctions that will be featured throughout this course. First, there is an important difference between points and vectors. Although in the plane both points and vectors are typically represented by pairs of rectangular coordinates, conceptually points and vectors are different types of objects with different kinds of behaviors. Points are affected by translation; vectors are not. Vectors can be rotated and scaled; points cannot. Thus, if the turtle is in state € ( P , w ) , then the FORWARD command translates the point P , but leaves the vector w unchanged. Similarly, the TURN and RESIZE commands, rotate and scale the vector w , but leave the point P unchanged. Second, there are two ways of approaching geometry: conceptual and computational. On the conceptual level, when we think about Euclidean geometry, we think about lengths and angles. Thus when we program the turtle, we use the FORWARD, TURN, and RESIZE commands to affect distances and directions. Only when we need to communicate with a computer -- only when we need to perform actual computations -- do we need to descend to the level of coordinates. Coordinates are a low level computational tool, not a high level conceptual device. Coordinates are akin to an assembly language for geometry. Assembly languages are effective tools for communicating efficiently with a computer, but generally we do not want to think or write programs in an assembly language; rather we want to work in a high level language. We shall try to keep 7
these two modes -- concepts and computations -- distinct throughout this course by developing novel ways to think about and to represent geometry. The turtle is only the first of many high level devices we shall employ for this purpose. Exercises:
w ⊥ €
Figure 12: The vectors € w = ( u , v ) and € w ⊥ = (− v , u ) form a right angle at the origin O. 10