Lecture: 11-20-2012

posted Nov 20, 2012, 6:26 AM by Samuel Konstantinovich   [ updated Nov 20, 2012, 7:01 AM ]

Homework:

Watch tutorial videos 4 and 5. 

Make sure you are able to complete the exercises on the bottom of each of the tutorials.


Goal: Writing functions in Netlogo.

Do Now: (On Paper)

0. Log in while you work on your do now. Turn the monitor off after you did this.

1. Write code that creates a turtle and then moves the turtle to (3,4) using forward and other directional commands.

2. Write code that creates a turtle and then moves it to (-2,1) using the set command.

3. Write code that makes a turtle draw a square with side measure 2 units, and returns the turtle to it’s original orientation.

4. How repetitive was that? Would you want to write a decagon that same way? What about a 100-gon?


Section A. Turtle creation options:

Functions that create turtles can be used two ways:

  1. create-turtles NUMBER

  2. create-turtles NUMBER [ commands ]

Examples:

crt 4

crt 3 [set color red]

cro 5 [set size 2 set shape "sheep"]

Section B. Comments/Repeat:

;comments in netlogo start with semicolons

;blah blah...


Repeat command:

repeat N [ commands ]

e.g.

repeat 4 [ fd 2 rt 90]

would make a turtle go in a square pattern.

Section C. Functions / Procedures :

We can define a sequence of commands just like we did in scheme. We write these in the code tab of Netlogo. The format for a procedure is:

to FunName

  command...

  command...

  command...

end

Examples:

;observer context function:

to setup

  ca

  cro 3

end

;turtle context function

to go

  fd 1

  rt 1

end

The context of a function is determined by the first context specific command in the function. So if there is a clear-all in a function the rest of the commands must be observer commands.

Lab Work:

Make two procedures makeTriangle and MakeSquare that makes a turtle draw an equilateral triangle, and a square respectively.

Section D: Parameters.

Some functions need parameters (like when you use crt you have to put a number)

This is how you write those functions:

to functionName [ parameter1 parameter2 … ]

  command...

  command...

end



so if you wrote:

to funkyGo [dist angle]

fd dist

rt angle

end

You can issue a turtle command:

funkyGo 2 30

This would make a turtle go forward 2, and turn right 30. 

LAB WORK:

Write a command drawNgon [N D] that makes a polygon with N sides, that has a side length of D.

-You need to calculate the angle, be careful you don't use the interior angle by accident. 

Comments