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 #2
Still stuck?
Here is where we stand. We have the following procedures
|
Let's take a step back. If we extract the code that draws the level 1 kochCurve into its own procedure, kochCurve1, and fix kochCurve to invoke it, we have the following.
|
Now let's write kochCurve2. Just to refresh you memory, here are Koch snowflakes for level 1 and level 2. I've also annotated the level 2 snowflake with one kochCurve from level 1.
For level 2, we need to replace what were straight lines in level 1 with what look like kochCurve1s. So here is where we stand with a new kochCurve2 and a modified kochCurve.
|
So writing kochCurve2 wasn't that difficult. Just as the figure above shows, it simply invokes kochCurve1 in place of the forward command. We are looking for a pattern that has the appearance of a recursive process and we are on to something here.
Is writing a kochCurve3 procedure just as simple? Do we just copy/paste the definition of kochCurver2, change its name and replace kochCurve1 invocations in it with invocations of kochCurve2? If so, then adding another if instruction to kochCurve would get us a level 3 snowflake!
Try it out... Here is a modified kochCurve and a new kochCurve3.
|
It works... doesn't it? Well, we have a pattern begging for a recursive solution.
We discovered the pattern by...
There is still one exception to the pattern we've uncovered, kochCurve1 does not invoke kochCurve. It is using forward to draw a straight line. If you think about it, we could remove the forward commands from kochCurve1. We know that if we invoke kochCurve with its first input (level) equal to zero, it does the same thing as forward. Here we go.
|
We are still looking for a way to simplify our program with recursion. But now you should see the emergence of a pattern.
Can you figure out how to eliminate kochCurve1 and kochCurve2, and kochCurve3, leaving only kochCurve? Remember... kochCurve1 came out of kochCurve.