Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cosc 1P02 Week 2 Lecture slides

Similar presentations


Presentation on theme: "Cosc 1P02 Week 2 Lecture slides"— Presentation transcript:

1 Cosc 1P02 Week 2 Lecture slides
You can pretend to be serious; you can't pretend to be witty. - Sacha Guitry

2 Turtle Graphics Seymour Papert late ’60s
COSC 1P02 2008/09 Turtle Graphics Seymour Papert late ’60s 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 TurtleDisplayer provides a canvas for a turtle to draw on canvas is 300x300 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

3 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 BasicIO and Media.

4 TurtleDisplayer Methods
COSC 1P02 2008/09 TurtleDisplayer Methods

5 COSC 1P02 2008/09 Turtle Methods

6 Two-by-Two Two turtles on different canvasses
COSC 1P02 2008/09 Two-by-Two Two turtles on different canvasses 2 TurtleDisplayers & 2 Turtles 4 variables 2 for the displays 2 for the turtles Where’s yertle? move displays waitForUser() Analogy 2 pieces of paper 2 pencils, each draws on a dedicated surface.

7 Two-on-One Two (or more) turtles drawing on the same canvas
COSC 1P02 2008/09 Two-on-One Two (or more) turtles drawing on the same canvas 1 TurtleDisplayer & 2 Turtles 3 variables Where are yertle’s lines move yertle moveTo(x,y) coordinates (cartesian) (o,o) is center x-coordinate is right(+)/left(-) y-coordinate is up(+)/down(-) Relative coordinates, moveTo repositions the relative location for the 1st turtle.

8 Variables, Objects, References & Assignment
COSC 1P02 2008/09 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 TurtleDisplayer 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

9 This reference in assigned to a variable.
COSC 1P02 2008/09 This reference in assigned to a variable. Declare variables TurtleDisplayer is created in memory, returns a reference (address) A Turtle object is created the same way. Each Object has memory which can be modified. Memory or Attributes can be set or accessed. An object can be assigned to multiple variables. Here mertle will refer to the original yertle object.

10 Drawing A Hexagon Geometry 6 sides
COSC 1P02 2008/09 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

11 COSC 1P02 2008/09

12 COSC 1P02 2008/09

13 Square 2 The result is the same
package Example_2_2; import BasicIO.*; import Media.*; import static java.lang.Math.*; public class DrawSquare { private TurtleDisplayer display; // display to draw on private Turtle yertle; // turtle to do drawing /** The constructor draws a square using a loop */ public DrawSquare ( ) { display = new TurtleDisplayer(); yertle = new Turtle(); display.setTurtle(yertle); yertle.penDown(); for ( int i=1 ; i<=4 ; i++ ) { yertle.forward(40); yertle.right(PI/2); }; yertle.penUp(); display.close(); }; // constructor public static void main ( String[] args ) { new DrawSquare(); }; } The result is the same The for loop allows us to do this 4 times, once for each side of the square One side of the square is drawn, and the turtle rotated 90o display

14 COSC 1P02 2008/09

15 COSC 1P02 2008/09 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

16 COSC 1P02 2008/09

17 COSC 1P02 2008/09

18 PolySpiral

19 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.

20 Repeating A Pattern Draw 8 squares in pattern Repetition 8 squares
COSC 1P02 2008/09 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)

21 8 Squares public class Draw8Squares { private TurtleDisplayer display; // display to draw on private Turtle yertle; // turtle to do drawing // The constructor draws 8 squares in a pattern. public Draw8Squares ( ) { display = new TurtleDisplayer(); yertle = new Turtle(); display.setTurtle(yertle); yertle.penDown(); for ( int j=1 ; j<=8 ; j++ ) { for ( int i=1 ; i<=4 ; i++ ) { yertle.forward(40); yertle.right(PI/2); }; yertle.right(PI/4); yertle.penUp(); display.close(); }; // constructor public static void main ( String[] args ) { new Draw8Squares(); }; The variables i & j are counters used to control the loops. Each must be unique. A single square is drawn This for loop controls how many squares will be drawn. In our case 8(squares). The turtle is rotated 45 degrees, or PI/4. This sets up the turtle to draw the next square

22 8 Squares.

23 COSC 1P02 2008/09

24 COSC 1P02 2008/09

25 COSC 1P02 2008/09

26 COSC 1P02 2008/09 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 EightSquares 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

27 COSC 1P02 2008/09

28 Tiled Image Create a square, using composition Start of tiling
move to top left repeat for as many lines as needed repeat for as many patterns as needed draw pattern (tile) transition to next pattern on line move to left side Create a square, using composition

29 The end


Download ppt "Cosc 1P02 Week 2 Lecture slides"

Similar presentations


Ads by Google