1 building your own turtle functions for making really cool pictures!

21
1 Building Your Own Turtle Functions For making really cool pictures!

Upload: evan-patrick

Post on 13-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Building Your Own Turtle Functions For making really cool pictures!

1

Building Your Own Turtle Functions

For making really cool pictures!

Page 2: 1 Building Your Own Turtle Functions For making really cool pictures!

2

5 Cool Things…

1. Variables and Arithmetic

2. Branches for variety

3. Using Functions

4. Building your own Functions (Today)

5. Loops to repeat (Next class)

Page 3: 1 Building Your Own Turtle Functions For making really cool pictures!

3

Review:1) Variables and Arithmetic

var n = 4;

var y = 2;

var x = n + y;

y = n * 2;

document.write(“x=” + x + “, y=” + y);

Page 4: 1 Building Your Own Turtle Functions For making really cool pictures!

4

2) Using Functions

Math Functions:x = Math.sqrt(81);y = Math.sqrt(x);document.write(“x=” + x + “, y=” + y);

Turtle Functions:forward(20);left(90);forward(20);right(135);backward(40);

Identify the Function calls and the arguments

Page 5: 1 Building Your Own Turtle Functions For making really cool pictures!

5

Which command(s) uses “relative” positioning, and which “absolute”

forward -- move turtle forward some number of pixels

left – turn left some number of degrees

moveTo -- move to an x,y coordinate

turnTo – turn to a particular degree heading

home – send turtle back to center of screen, facing up

Page 6: 1 Building Your Own Turtle Functions For making really cool pictures!

6

Today—3) Building your own functions

Lets you “abstract” a collection of moves

For example, these lines make a square:forward(20); right(90);

forward(20); right(90);

forward(20); right(90);

forward(20); right(90);

Page 7: 1 Building Your Own Turtle Functions For making really cool pictures!

7

If you want to draw a lot of squares, put this Function Definition at the top of your script…

function square( )

{

forward(40); right(90);

forward(40); right(90);

forward(40); right(90);

forward(40); right(90);

}

This is a Function Definition

Page 8: 1 Building Your Own Turtle Functions For making really cool pictures!

8

Now you can ‘call’ your square function

square();

--------------------

left(30); square();

left(180); square();

moveTo(-300, -100);

left(30); square();

left(180); square();

Page 9: 1 Building Your Own Turtle Functions For making really cool pictures!

9

Functions help manage complexity

You can do interesting patterns without a lot of repetition of code

They save time and effort

Functions can use other functions

Page 10: 1 Building Your Own Turtle Functions For making really cool pictures!

10

What if you want different sizes of squares?

function square( n )

{

forward(n); right(90);

forward(n); right(90);

forward(n); right(90);

forward(n); right(90);

}

n is called a parameter

It’s a variable that receives

the size of the square

(given as the argument in the function call)

Page 11: 1 Building Your Own Turtle Functions For making really cool pictures!

11

Now when you call the square function, just say how big you want it to be

square(100);

square(50);

square(25);

Page 12: 1 Building Your Own Turtle Functions For making really cool pictures!

12

You can generate random sized squares…

var size, angle;size=rand(50,100); square(size);angle=rand(0,180); turnTo(angle);size=rand(50,100); square(size);

rand(low, high) gives random number between low and high

Page 13: 1 Building Your Own Turtle Functions For making really cool pictures!

13

Every time you run the code from last slide, it gives a different result

It’s interesting to observe the variations and similarities

Page 14: 1 Building Your Own Turtle Functions For making really cool pictures!

14

ProgramStructure

Function Definitions

(in <head>)

Main program block

(in <body>)

Page 15: 1 Building Your Own Turtle Functions For making really cool pictures!

15

Functions Give “Vertical Structure”How to draw

this complicated mosaic of 120 triangles?

Page 16: 1 Building Your Own Turtle Functions For making really cool pictures!

16

“Bottom Up” Problem SolvingSolve this sequence of ever more complex problems using functions at each level.

triangle pattern line mosaic

Page 17: 1 Building Your Own Turtle Functions For making really cool pictures!

17

Solutions

function triangle(n)

{

forward(n); right(120);

forward(n); right(120);

forward(n); right(120);

}

function pattern(n)

{

turnTo(30);

triangle(n); right(90);

triangle(n); right(90);

triangle(n); right(90);

triangle(n); right(90);

turnTo(90);

}

Page 18: 1 Building Your Own Turtle Functions For making really cool pictures!

18

That’s all!

function line(n){pattern(n);jumpFwd(n*1.7);pattern(n); jumpFwd(n*1.7);pattern(n); jumpFwd(n*1.7);pattern(n); jumpFwd(n*1.7);pattern(n); }

function mosaic(n){jumpTo(-200,-100);line(n);jumpTo(-200+30*1.7, -100);line(n);jumpTo(-200+30*2*1.7, -100);line(n);jumpTo(-200+30*3*1.7, -100);line(n);jumpTo(-200+30*4*1.7, -100);line(n);jumpTo(-200+30*5*1.7, -100);line(n);}

Page 19: 1 Building Your Own Turtle Functions For making really cool pictures!

19

Not Quite!We need an initial function call to mosaic to kick it off:

mosaic(30);

Notice we give a size of 30…fills in for ‘n’ in all the other functions.

So…using functions we can draw 120 triangles (or 360 forward commands) with about 30 lines of JavaScript—powerful!

Page 20: 1 Building Your Own Turtle Functions For making really cool pictures!

20

Capture your images in a “screen shot”

Press Alt and PrtScr at same time

Open Paint

Edit/Paste

You can chop out image using select tool Dotted line box

Then paste into Microsoft Word

Or save as a .jpg file (project 2)

Page 21: 1 Building Your Own Turtle Functions For making really cool pictures!

21

That’s it! Have fun in lab

SUMMARY—Functions let you create your own building blocks to use in more complex programming problems

Functions abstract pieces of code—self documenting

Functions can call other functions

Functions are re-usable—save time and money in software development

Next week…branches and loops