2015-11-23 LabN01

posted Nov 23, 2015, 4:46 AM by Samuel Konstantinovich   [ updated Nov 23, 2015, 7:59 AM ]

Goal: lab writing functions in netlogo

Homework: Complete the lab (bring a digital copy to school, you need a working copy)

Do Now:

1. Write a function that creates a turtle and then moves the turtle to (3,4) using forward and other directional commands. Before writing the command: What context should this command be / why?

2. Write a function that creates a turtle and then moves it to (-2,1) using the set command. Before writing the command: What context should this command be / why?

3. Write a function that makes all turtles draw a square with side measure 2 units, and returns the turtle to it’s original position and orientation. Before writing the command: What context should this command be / why?




Would you want to write a function for each polygon that same way like hexagon,pentagon? What about all the functions up to a 100-gon?


We could write functions that draw single shapes but it would be so much better to make one function for all regular polygons.

Triangle draws a side and turns 120 degrees. It does this 3 times.

Square draws a side and turns 90 degrees and draws a side. It does this 4 times.


Write a function that would take a parameter n and sideLength. It will make whichever turtle runs the command draw an n-gon with sides that measure sideLength. When the function is done, the only difference in the world is that a polygon is drawn. No turtles should be harmed/created/changed.

n-gon 5 2    would make the turtle draw a pentagon with side 2

n-gon 10 1  would make the turtle draw a decagon with side 1

n-gon 20 .5  would make the turtle draw an icosagon with side .5

Do Not - Create a turtle in this function!


Answers to DoNow

to number1
  crt 1[
    set color white
    set heading 0
    pd
    fd 4
    rt 90
    fd 3
  ]
end

to number2
  crt 1[
    ;set color white
    ;pd
    setxy -2 1
    ;set xcor -2
    ;set ycor 1
  ]
end


;This function makes any turtle that uses the function draw a square
to square[side]
  pd
  repeat 4 [  fd side   rt 90]
end

;This function makes all turtles make a square with the square command
to number3
  ask turtles[ square 5]
end

Comments