BFOIT - Introduction to Computer Programming

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.

 `````` ; Draw a Von Koch Curve ; level - count of number of times to break a straight line ; into three parts: left side, middle, and right ; side. When equal to zero, simply draw a straight ; line. ; steps - length of the curve/line drawn in turtle steps to kochCurve :level :steps ; ... end ``````

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...

 ``````to kochCurve :level :steps if equal? :level 0 [forward :steps stop]    ; ... end ``````

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.

 ``````to kochFlake :level :steps setheading 30 repeat 3 [kochCurve :level :steps right 120]    end ``````

Type this procedure into the following applet and see what happens.

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.

 ``````to kochCurve :level :steps if equal? :level 0 [forward :steps stop]    forward (quotient :steps 3) left 60 forward (quotient :steps 3) right 120 forward (quotient :steps 3) left 60 forward (quotient :steps 3) end ``````

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.

This work (BFOIT: Introduction to Computer Programming, by Guy M. Haas),
identified by Berkeley Foundation for Opportunities in IT (BFOIT),
is free of known copyright restrictions.