2018-03-23 HW

posted Mar 21, 2018, 7:09 PM by Konstantinovich Samuel   [ updated Mar 23, 2018, 10:05 AM ]
Reminder:
Quiz Tuesday ( Lists + Recursion )

Goal: Trees  [ are fractalicious ]

do now:
1. Write a Netlogo procedure square n (non-reporting) to make a turtle draw a square of size n.
2. Write a Netlogo function sum n   that takes an integer n, and calculates the sum of the numbers from 1 to n inclusive. 
remember:
-Functions use to-report
-You can use the repeat x[ ]  command or a while loop




Notes: ( Diagrams and notes from class are on the board only ) sorry!

tree 0 draws nothing.
tree 1 draws a single stick.
tree 2 draws a capital Y

Tree 2-6 are displayed here: 


Classwork/Homework 


Design the overall code structure of a tree[n] function where is is the number of levels to the tree. 
(Do not worry about the branch sizes changing, we can have a tree that has all equal sized branches)


DO NOT WRITE CODE YET, DO NOT WRITE CODE YET. 

Discuss and plan with your neighbors on paper:

for 1 and 2, look at the pictures of trees, and see if you can find a recursive relationship! a draw-tree function is recursive when there is at least one other draw-tree command inside of it!
1a-How would you draw a tree 2 by doing a little bit of work and at least one other tree call? 
1b - What is a simpler tree call than tree 2? 
1c - Can you use that in tree 2?
2-How would you draw a tree 3 by doing a little bit of work and at least one other tree call? 

DO NOT WRITE CODE YET, DO NOT WRITE CODE YET. 

After you find the recursive relationship, think about the following:
3.Draw a turtle on your paper, literally: go through the steps to make it draw the tree, and write the code next to the steps.
3a-Where does your turtle need to be when it is done with each step?

4-What is the base case? Is that the smallest base case? (What is the smallest tree size?)

DO NOT WRITE CODE YET, DO NOT WRITE CODE YET. 

5-How would your code be arranged to accommodate this? Write some pseudocode, not a full function. 
You can write some code and some English. Keep track of where your turtle will be and what direction would it be facing after each command.


Complete: After planning on paper 

Due Tuesday!

Write your tree function in several phases:
Phase 1: Basic
draw-tree [ levels ]   
levels : number of levels

use 2 for the branch length, and 30 degrees for the turn angle* of each branch. 
*30 degrees from the center, so there is a 60-degree angle between the branches.

DANGER: When you rename/recode a recursive function, the first thing you should do: rename the recursive calls, so they call the new function instead of the old.

Phase 2: Flexibility!
draw-tree2 [ levels blen bangle]   
levels : number of levels
blen  : length of each branch
bangle : angle of the branches - 45 would mean that the two branches are 45 degrees off center, and there is a 90-degree angle between them.

Phase 3: Decay! This is the final required phase. 
draw-tree3 [ levels blen bangle]   
levels : number of levels
blen  : length of each branch
bangle : angle of the branches

The branch length should decrease as the function calls itself. This does NOT mean you decrease a variable, it means that just like fib(n-1) or fib(n-2) you decrease the parameter as you pass it to the next copy.
----Your slider is the STARTING value, the parameter can be changed every time your function is called.
Have the branch decay by multiplying by 0.85 before passing it to the next recursive call. 
-When this works, try making the branch decay a slider from 0.1 to 0.9 in increments of 0.01

Optional Bits:
-Change the angle randomly a little bit at each phase.
-Change the branch width to draw thicker lines in the base of the tree, and thinner lines at the top of the tree. (hint: decay, and add a variable to the function)
-Change the color at the last level of the tree.
-Make your own changes to try to make the tree look more organic! 



Comments