COSC 1 P 02 Cosc 1 P 02

  • Slides: 12
Download presentation
COSC 1 P 02 Cosc 1 P 02 Week 2 Lecture slides You can

COSC 1 P 02 Cosc 1 P 02 Week 2 Lecture slides You can pretend to be serious; you can't pretend to be witty. - Sacha Guitry Introduction to Computer Science 2. 1

COSC 1 P 02 Turtle Graphics · Seymour Papert late ’ 60 s -

COSC 1 P 02 Turtle Graphics · Seymour Papert late ’ 60 s - teaching children to reason about processes - Logo programming language · Vector graphics - Turtle has a position and direction and travels a distance - direction measured in radians (2 = full circle) - distance in drawing units (pixels) · Brock libraries - Turtle. Displayer provides a canvas for a turtle to draw on ° canvas is 300 x 300 pixels ° turtle starts in center facing right (i. e. 0 radians is facing right) ° can have any number of Turtles draw on the same canvas ° methods - Turtle represents a turtle ° methods Introduction to Computer Science 2. 2

COSC 1 P 02 Turtle Graphics. · Analogy Time !!! - We take a

COSC 1 P 02 Turtle Graphics. · Analogy Time !!! - We take a piece of paper and place it on the desk. - We select a drawing tool, A pencil - We put the pencil onto the paper - We control the pencil by moving it forward, backward, right, left etc. · Turtle Graphics are controlled much the same way. - Use resources (pencil and paper) and instructions (methods) provided by Basic. IO and Media. Introduction to Computer Science 2. 3

COSC 1 P 02 Two-by-Two · Two turtles on different canvasses · 2 Turtle.

COSC 1 P 02 Two-by-Two · Two turtles on different canvasses · 2 Turtle. Displayers & 2 Turtles · 4 variables - 2 for the displays - 2 for the turtles · Where’s yertle? - move displays ° wait. For. User() · Analogy - 2 pieces of paper - 2 pencils, each draws on a dedicated surface. Introduction to Computer Science 2. 6

COSC 1 P 02 Two-on-One · Two (or more) turtles drawing on the same

COSC 1 P 02 Two-on-One · Two (or more) turtles drawing on the same canvas · 1 Turtle. Displayer & 2 Turtles - 3 variables · Where are yertle’s lines - move yertle ° move. To(x, y) ° coordinates (cartesian) × (o, o) is center × x-coordinate is right(+)/left(-) × y-coordinate is up(+)/down(-) - Relative coordinates, move. To repositions the relative location for the 1 st turtle. Introduction to Computer Science 2. 7

COSC 1 P 02 Variables, Objects, References & Assignment · Variables correspond to memory

COSC 1 P 02 Variables, Objects, References & Assignment · Variables correspond to memory cells - allocated when declared · Objects are represented by some number of memory cells - allocated when object created by new · Objects have attributes - Turtle ° location, direction, pen state (up/down), pen color, pen width ° display it is drawing on - Turtle. Displayer ° current turtle doing drawing · Assignment - new produces a reference to (location in memory of) an object - = replaces the current value stored in the variable with the value produced by the right hand side · Example Introduction to Computer Science 2. 8

COSC 1 P 02 Drawing A Hexagon · Geometry - 6 sides - exterior

COSC 1 P 02 Drawing A Hexagon · Geometry - 6 sides - exterior angle /3 (6 times around = 2 ) · Often sequence repeated - in Square - tedious to rewrite - large number of times? · Repetition (loop) - for statement - index variable · Square rewritten Introduction to Computer Science 2. 10

COSC 1 P 02 Poly-Spiral · A spiral has lines of increasing length drawn

COSC 1 P 02 Poly-Spiral · A spiral has lines of increasing length drawn at same angle - smaller the angle the tighter the spiral · Essentially a loop - repeatedly draw a line and turn · Line length changes? - previously length was constant (used a constant: 40) - now length is variable (use a variable) · Variable declaration - line length is a number of drawing units (countable) - in Mathematics, the countable numbers are called integers - in Java integer is represented by the type int · Assignment - = replaces value of variable on left with value computed on right · Increase len each time a line drawn (i. e. in loop) · Initial value? - set before loop · Variables & assignment revisited Introduction to Computer Science 2. 15

COSC 1 P 02 Nesting · Mathematical expressions use composition ° 2(4+1) - We

COSC 1 P 02 Nesting · Mathematical expressions use composition ° 2(4+1) - We can expand this to ° 8+2 - This can be viewed as (4+1) done twice. - Serves as a way of simplification. · Code behaves much the same way - If we can do 1 thing ° Draw a square - We could do this multiple times - 8(Draw a square) · 8 Squares Example · Tip: Factor code like you would a mathematical expression. - This can simplify code by using composition. - Fewer lines of code usually means easier to understand. Introduction to Computer Science 2. 19

COSC 1 P 02 Repeating A Pattern · Draw 8 squares in pattern ·

COSC 1 P 02 Repeating A Pattern · Draw 8 squares in pattern · Repetition - 8 squares - rotate between - for loop · Composition - we already know how to draw a square - write program using composition by inserting code for square in code for repetition of pattern - also called nesting - different index variables - inner (nested) loop (i) done completely for each time through outer loop (j) Introduction to Computer Science 2. 20

COSC 1 P 02 Repeated Patterns · Complex (repeating) patterns can be created using

COSC 1 P 02 Repeated Patterns · Complex (repeating) patterns can be created using composition · Code for pattern nested (composed) inside of code for repetition · Some sort of transition between patterns - e. g. rotating turtle in Eight. Squares - e. g. moving over to next position · General algorithm - starting position - repetition - pattern - transition · Tiling - pattern for one line nested in repetition for many lines Introduction to Computer Science 2. 26

COSC 1 P 02 The end Introduction to Computer Science 2. 29

COSC 1 P 02 The end Introduction to Computer Science 2. 29