210 likes | 372 Views
COMPSCI 101 S1 2014 Principles of Programming. 31 Turtle Graphics. Learning outcomes. At the end of this lecture, students should be able to : Use the Turtle class to draw a picture Develop algorithms that use simple graphics operations to draw two-dimensional shapes
E N D
COMPSCI 101 S1 2014Principles of Programming 31 Turtle Graphics
Learning outcomes • At the end of this lecture, students should be able to: • Use the Turtle class to draw a picture • Develop algorithms that use simple graphics operations to draw two-dimensional shapes • Examples and Exercises: • Example 1: Drawing a Triangle • Example 2: Drawing three circles • Case Study 1: Drawing a Square • Case Study 2: Drawing a Polygon • Case Study 3: Taking a Random Walk • Show Case: Repeating Patterns COMPSCI101
Overview of Turtle Graphics • Turtle graphics originally developed as part of the children’s programming language Logo • Created by Seymour Papert and his colleagues at MIT in the late 1960s • Analogy: Turtle crawling on a piece of paper, with a pen tied to its tail • Sheet of paper is a window on a display screen • Position specified with (x, y) coordinates • Cartesian coordinate system, with origin (0, 0) at the centre of a window COMPSCI101
Turtle Graphics • The turtle is an object that has a position in a drawing window • This object can be told to turn a number of degrees, move a given distance, move to a new position, and change its colourand line width • If the turtle’s pen is down, it draws a line; otherwise, it just moves without drawing • The turtle Module • Includes a Turtle type with methods for getting turtle objects to do things • Add an import statement in your py file. import turtle COMPSCI101
Instantiating a Turtle • Instantiate a Turtle object: • The turtle appears as an icon • Initial position: (0, 0) • Initial direction: East (0°) • Colour: black • Line width: 1 pixel • Pen: down (ready to draw) t = turtle.Turtle() 90° x-axis (0,0) (0,0) 180° 0° y-axis 270° COMPSCI101
Properties of a Turtle • The Turtle class provides many methods for setting the properties of a turtle: • penup(): pull pen up, no drawing when moving • pendown(): put pen down, drawing when moving • pensize(width): set the pen line thickness to width • pencolor(color): set the pen colour COMPSCI101
Moving a Turtle • The Turtle class provides many methods for moving the turtle: • forward(distance) • Move turtle in the direction the turtle is headed by distance pixels • left(degrees) • Rotate turtle counter-clockwise by angle degrees • right(degrees) • Rotate turtle clockwise by angle degrees • goto(x,y) • Move turtle to coordinates defined by x, and y; if pen is down, draw line COMPSCI101
Example 1: Drawing a Triangle • Move a Given Distance • Move 50 pixels in the current direction, which is 0° (east) • Drawing a line when moving • Move to a Given Position • Move to location (0, 50) • Drawing a line when moving t = turtle.Turtle() t.forward(50) t.goto(0, 50) COMPSCI101
Example 1: Drawing a Triangle • Set the Direction • 270° is due south • Note: the direction of the turtle’s icon has been updated • Initial heading is east i.e. 0 • Change the Pen’s Colour • Change the RGB value to the brightest red • Note: the icon’s outline colour has been changed t.setheading(270) t.pencolor('red') COMPSCI101
Example 1: Drawing a Triangle Demo 1 • Move a Given Distance • Move 50 pixels in the current direction • Returns to the origin, drawing a red line • Move Without Drawing • Won’t draw when moved now t.forward(50) t.penup() t.forward(50) COMPSCI101
Some More Turtle Methods • The Turtle class provides many methods for drawing: • circle(radius) • Draw a circle with given radius. The centreis radius units left of the turtle • dot(diameter, colour) • Draw a dot with given diameter and colour 90° t = Turtle() t.circle(100) x-axis (0,0) (0,0) 180° 0° y-axis 270° COMPSCI101
Example 2: Drawing three circles Demo 2 • Given the following code: t = turtle.Turtle() t.circle(100) t.penup() t.goto(-50, 50) t.pendown() t.circle(50) t.penup() t.goto(50, 50) t.pendown() t.circle(50) (50,50) (-50,50) (0,0) COMPSCI101
Case Study 1Drawing a square • Task: • Complete the draw_square() function which draws a square with corner point (x, y) and length • Arguments: a turtle, x-coordinate, y-coordinate, length • Draw a square • Case: 4 (0,50) t = turtle.Turtle() drawSquare(t, 0, 50, 100) 1 3 2 COMPSCI101
Case Study 1Drawing a square Demo 3 • Algorithm: 4 1 3 2 COMPSCI101
Case Study 2Drawing a Polygon • Task: • Complete the draw_polygon() function which draws a polygon from a list of vertices. • Arguments: a turtle, a list of vertices • Draw a polygon • Example: t = turtle.Turtle() drawPolygon(t, [(0,0), (0, 60), (60, 0)]) (0, 0) COMPSCI101
Case Study 2Drawing a Polygon Demo 3 • Algorithm: 2 1 3 COMPSCI101
Exercise 1:Drawing … • For each of the three figures below, write a sequence of turtle graphics function calls which draws that figure COMPSCI101
Case Study 3Taking a Random Walk • Animals often appear to wander about randomly, but they are often searching for food, shelter, etc. • Task: • Complete the random_walk() function which takes a turtle, num_steps and distance as parameters and takes a random walk for a step of num_steps. • A random walk is a path in which each step is in a randomly chosen direction • Arguments: a turtle, num_steps, distance • Example: t = turtle.Turtle() randomWalk(t, 30, 50) COMPSCI101
Case Study 3Taking a Random Walk Demo 4 • Repeat the following by a step of num_steps: COMPSCI101
Show Case Demo 5 • Interesting shapes can be created by repeating drawing squares: • Repeat the following 36 times COMPSCI101
Summary • "Turtle graphics" were developed for the language LOGO as a tool for helping beginning programmers visualize the effects of their actions. • In a turtle graphics environment, the programmer draws figures by controlling a "turtle" which leaves a trail on the screen as it moves. • The turtle can be made to move relative to its current position using forward and backward. It can be made to turn relative to its current heading using left and right. • You can control whether the turtle leaves a trail as it moves by calling penup and pendown. COMPSCI101