Lecture: 11-26-2012

posted Nov 25, 2012, 1:14 PM by Samuel Konstantinovich   [ updated Nov 26, 2012, 8:23 AM ]
Goal: Making Fractal Trees!

Homework: Watch tutorials 9,10,13. Make sure you can complete the exercises that follow. 

Do Now: Read the go command below. Right now the turtles on the right side will eventually cross over to the right side of the screen and draw on it.  QUESTION: How can you fix the code so the left side of the drawing stays clean? Come up with at least two different ways. Discuss with your neighbors, and write out your idea, and any code changes you would want to make.

;This go command makes turtles put their pen down on the right side of the screen (x>0 is the right side of the y axis) but it doesn't keep turtles from drawing on the left side... as you will see.

to go
  if xcor > 0 

to wiggle
  fd .5
  lt random 20
  rt random 20

to setup
  cro 10 [  fd 5 set heading random 360]


So an if statement is as follows:

if BOOLEAN [ command block]


ifelse BOOLEAN

[true command block]

[false command block]

Some boolean examples:

color = brown

size < 10

xcor > ycor

Fractal Trees: 

♦1. We will learn two ways to make a simple fractal tree. We can use recursion to draw a tree OR we can use hatch to help us draw a tree. First we will look at hatch. Later we will look at how to do this recursively.

♦2. Lets agree on the form of a simple fractal tree: 
Every branch of the tree is a line segment. A tree has a size, lets say the size is N:
-When N=0 there is no tree.
-When N=1 the tree is a stick:


-When N is greater than 1, we draw N-1 trees at the end of the branch:

\/   <- A '2' tree  has two '1' trees on the end.  

-So a 3 tree, is a stick that has two '2' trees at the end of the branch, and a 4 tree is a stick with two '3' trees at the end of the branch... and so forth. 

Lets look at a more complex tree:
Notice on the left and right side of the main branch or trunk, there are two symmetrical trees attached. How big are they? How big is the whole tree?

♦3. If we wanted to make a "tree n" procedure, we need an algorithm. Imagine one turtle draws the single trunk, then he stops.

Stop and Think: How do you make a hatched turtle draw the rest? What command would you use to draw a tree? How do you make it draw a smaller tree?

More generally: How can you have the turtle use hatch to solve this problem? There are two ways to do this:
        1. By hatching two then having them each draw the next pair of trees (RECURSION!) then the original must die.
        2. By hatching one, and having the one turtle one side(RECURSION), while the original draws the other(RECURSION). 

Either way, the hatched turtle needs a few commands inside the command block of : hatch n [ command], and either way you will need an if or ifelse command.