Review Polygon
Want to draw a hexagon withh circuference 400:
function main() for 0 <= i < 6 do ♻turtle → forward(66.67) ♻turtle → left turn(60) end for end function
Arithmetic
- Now, what if we wnat a pentagon instead
- Need to change the iterations, the forward distance, and the turn angle turning the same amount 5 times
But there is a better way, make the program do the math
function main() for 0 <= i < 6 do ♻turtle → forward(400 / 6) ♻turtle → left turn(360 / 6) end for end function
- Now we can change the shape just by changing all of the sixes to fives
Anwhere you can put a number, you can put a mathmatical equations as well
Variables
- But changing all three of those variables is a bit of pain, and prone to making mistakes in a large program
- Again, there is a better way, named values, also called variables
- We can name the 5, according to what it represents, in this case the number of sides
- var number of side := 5
Replace all of the fives with the variable name
function main() for 0 <= i < number of sides do ♻turtle → forward(400 / number of sides) ♻turtle → left turn(360 / number of sides) end for end function
- And changing the shape is as simple as changing one number in the program
- Added bonus, it’s easier to read
- We can make it even more easy to read by using another variable
- Replace 400 with the name circumference
Note, the order matters; must declare the variable’s value before using the variable name
Reusing Variables
- It is also possible to change the value of a variable after it has been declared
Let’s say we want to draw an N
function main() ♻turtle → forward(100) ♻turtle → right turn(135) ♻turtle → forward(141.42) ♻turtle → left turn(135) ♻turtle → forward(100) end function
- can use a varible for the distance it moves
but that distance changes each time, so update the variable
function main() var distance = 100 ♻turtle → forward(distance) ♻turtle → right turn(135) distance := 141.42 ♻turtle → forward(141.42) ♻turtle → left turn(135) distance := 100 ♻turtle → forward(100) end function
- Note, when changing the value, don’t use
var
Also note, order still matters
Updating Variables
- But what if we want to change the size of the N?
- Need to change two variables, one dependent on the other
- We can do better
Can update a variable using its own value
function main() var distance = 100 ♻turtle → forward(distance) ♻turtle → right turn(135) distance := distance * math → sqrt(2) ♻turtle → forward(141.42) ♻turtle → left turn(135) distance := distance / math → sqrt(2) ♻turtle → forward(100) end function
So now can change the distance variable and it will change the size
Accumulating Variables
Here is a neat drawing that uses a loop
function main() var distance = 100 var angle = 5 for 0 <= i < 360 / angle do ♻turtle → forward(distance) ♻turtle → back(distance) ♻turtle → left turn(angle) end for end function
- Note the use of variables that make it easier to change the drawing
- By updating variables inside a loop, we can change what it does each iteration of the loop.
For example, changing the distance each iteration
function main() var distance = 5 var angle = 5 for 0 <= i < 360 / angle do ♻turtle → forward(distance) ♻turtle → back(distance) ♻turtle → left turn(angle) distance := distance + 5 end for end function
Quiz
- m.socrative.com
- Room name ‘INQ241’