Background
jLogo Programming
- Commanding a Turtle
- Pseudocode
- Adding New Commands
- Iteration & Animation
- Hierarchical Structure
- Procedure Inputs
- Operators & Expressions
- Defining Operators
- Words & Sentences
- User Interface Events
- What If? (Predicates)
- Recursion
- Local Variables
- Global Variables
- Word/Sentence Iteration
- Mastermind Project
- Turtles As Actors
- Arrays
- File Input/Output
Java
- A Java Program
- What's a Class?
- Extending Existing Classes
- Types
- Turtle Graphics
- Control Flow
- User Interface Events
Appendices
- Jargon
- What Is TG?
- TG Directives
- jLogo Primitives
- TG Editor
- Java Tables
- Example Programs
- Installation Notes
Updates
- December 13, 2008
- January 6, 2012
- March 15, 2013
- January 20, 2014
- February 13, 2014
- July 29, 2014
- January 18, 2016
- January 29, 2016
Lastly
Von Koch Snowflake Program Hint
OK, you need a hint... No big deal... BFOIT's lessons are intended to be challenging, but not frustrating. Here is the skeleton of the heart of the problem, the recursive procedure that does all of the drawing.
|
Notice the extra input? So, what's the purpose of the level input? Well, in all of the programs in the lesson on recursion, the recursive procedures we wrote had three parts to them:
- a STOP rule,
- instructions that did something, and
- an invocation of itself (the recursive piece).
As the comments for the skeleton of the procedure I've given you explain, when the level input is zero, a straight line is drawn and so we are done... So, let's add that code...
|
One of the things I can't repeat enough is that when you are writing a program, first get something that works even if it only does something simple. We've just done that... Our new kochCurve works for the base case - when level is equal to zero. As long as we always invoke it with the first input equal to zero, it is equivilent to the forward primitive command. Type it into the applet below and then invoke it a few times to test it.
Now let's use our kochCurve procedure to draw a snowflake, the leftmost image in Figure 13.6, an equilateral triangle.
|
Type this procedure into the following applet and see what happens.
TG Programming Environment Applet |
Ok, we are off and running; we have a subset of our program working. Now time to expand this by adding code that I have to believe you came up with when you first attempted to solve the puzzle.
|
Ok, by dividing :steps by 3 to get lines of the proper length, and making the proper turns left and right, we now have a procedure that works properly when level is equal to zero or one. Try it out.
We now have a procedure that will draw the kochCurve shown in the bottom half of Figure 13.8. And if you invoke the kochFlake procedure with 1 for the first input (level) and, say, 120 for the second input (steps), you should see the second image in Figure 13.6.
Great... We are getting closer to what we need. But wait... where is our recursive invocation? We have a STOP rule (level equals 0). And we have instructions that draw straight lines.
All we are missing is the recursive invocation. How do we transform kochCurve into a recursive procedure which will get us the remaining images in Figure 13.6.
Think about where it should be. Play around a bit. Hopefully the solution will come to you - you'll have the Ah, Ha! moment...
But, if not and you need an additional hint, here it is.