Turtle Graphics and Abstract Data Types Denison University
Turtle Graphics and Abstract Data Types Denison University, CS 111: Foundations of Computing Guest Lecture 01/29/2014 Hernando Bermudez
Objectives • Introduce Turtle Graphics as a way of visualizing data. • Describe the concept of an Abstract Data Type(ADT) and illustrate the main ideas via the turtle.
Turtle Graphics • Turtle Graphics are created by moving an abstract “turtle” with directional commands. • As the turtle moves, its “tail” leaves behind a trail. If we lift the turtle’s tail it can move without leaving a trace. • The turtle moniker comes from a type of robot which could be used to trace out on paper the picture which is drawn by the “turtle” on the screen.
The Valiant Turtle Robot Picture from Wikipedia
Abstract Data Types • The turtle is an example of an Abstract Data Type(ADT). • An ADT is an implementation independent description of some abstract object. • The ADT consists of the information about the entity and the functions required to change said information.
Example of ADT: A Graph Information • • Vertices Edges Functions • • • Edge: Is there an edge between two vertices? Add vertex Add edge Remove vertex Remove edge
Adjacency Matrix Implementation 0 1 0 0 0 0 1 0 1 0 0 1 0 0 0 1 1 0 0 0 0 1 1 0 0 0 1 0 1 1 0 0 0 0 1 1 0 0 Vertex Pair Implementation The Petersen Graph (1, 2), (1, 5), (1, 6), (2, 3), (2, 7), (3, 4), (3, 8), (4, 9), (4, 5), (5, 10), (6, 8), (6, 9), (7, 10), (8, 10)
The Turtle as an ADT Information • • Position Orientation Color Tail status (Up or Down) Functions • • • Move Forward Turn Right Turn Left Change Color Lift Tail
Classes in Python • ADT’s are implemented in Python with a class. • A class holds the information in instance variables which can be changed via methods. • The instance variables are typically interacted with via the methods and not directly.
The Turtle Class Instance Variables • • Methods _position • _pencolor • • _orient etc • • • left(angle): modify _orient by rotating angle degrees to the left right(angle) forward(distance): change _position by moving distance units in _orient direction pencolor(‘color’): set _pencolor to ‘color’ up(): lift the tail etc
Instances • To utilize the turtle class we begin by creating an instance of the class. • The class is the description of a type of object, each actual object is an instance of the class. • This allows us to have more than one object of the same type in a program.
A Quick Overview of Polynomial Geometry • • • For a regular n-gon: The inner angles measure 180*(n-2)/n degrees. If d is the diameter of the circumscribed circle then the length of the d_k diagonal is d*sin(180*k/n)
- Slides: 13