Turtles that Climb Trees

(Solutions to additional problems)

Sierpinksi Triangles (Fig 16-9):

In any graphical recursion, there is a basic shape which is redrawn during recursive calls. The recursive procedure decides where and how it is redrawn.

 

In this recursion:

- The basic shape is an upright equilateral triangle,

- In every recursive call every upright triangle is filled with 3 smaller upright equilateral triangles.

 

;===== Utility: Sitting triangle ========

ERASE "striangle

TO striangle :size

RT 30

REPEAT 3 [FD :size RT 360/3]

LT 30

END

 

Now, drawing 3 smaller triangles would be straightforward as shown below.

Erase "Sierpinski.triangle

To Sierpinski.triangle :size :level

IF (:level < 1) [STOP]

;left triangle

striangle :size/2

Sierpinski.triangle :size/2 :level-1

 

;right triangle

RT 90 FD :size/2 LT 90

striangle :size/2

Sierpinski.triangle :size/2 :level-1

 

;top triangle

LT 30 FD :size/2 RT 30 striangle :size/2

Sierpinski.triangle :size/2 :level-1

 

RT 30 BK :size/2 LT 30

End

 

Now try it out:

? CS Sierpinski.triangle 300 3

? CS Sierpinski.triangle 300 5

 

It would be much more efficient (faster) to simply draw the central upside-down triangle by connecting the mid-points of each edge. Our recursive procedure will draw the central upside-down triangle and it will recurse by going to each of the 3 sub-triangles. Try this yourself.

 

Snowflakes (Fig 16-10):

If you see carefully, the basic shape is a regular (equilateral) triangle, and the edges of the triangle progressively (recursively) become more and more wrinkly. If we get the recursive wrinkle pattern for a single line, that same pattern is repeated 3 times for the triangle. So, we will first write a recursive procedure for the edge of the triangle.

 

If we inspect carefully, we see that at every level of recursion, the middle 1/3rd portion of every straight line segment is pulled up into a spike.

 

So, here is our recursive procedure for the triangle’s edge.

Erase "flake.line

To flake.line :x :level

if [:level=0] [FD :x stop]

flake.line :x/3 :level-1

LT 60

flake.line :x/3 :level-1

RT 120

flake.line :x/3 :level-1

LT 60

flake.line :x/3 :level-1

End

 

And now, the triangle itself which is our snowflake:

Erase "snowflake

TO snowflake :size :level

REPEAT 3 [

  Flake.line :size :level RT 120

]

END

 

The following commands will reproduce the snowflake images shown in the book.

? cs snowflake 100 0

? cs snowflake 100 1

? cs snowflake 100 2

? cs snowflake 100 3

 

Christmas tree (Figure 16-16):

The level 1 figure shows the basic pattern, which is quite easy to get. We can even parameterize the angle at which the branches come out. And we plant the recursive calls at 3 places: the tips of the three branches at the top. We will also supply thickness as a parameter which will go on reducing through the recursion.

 

Erase "ctree

TO ctree :s :ang :l :t

IF (:l<1) [STOP]

setpensize round :t

FD :s

LT :ang FD :s/20 ctree .4*:s :ang-12 :l-1 :t*.7 BK :s/20

RT :ang FD :s/20 ctree .7*:s :ang-12 :l-1 :t*.7 BK :s/20

RT :ang FD :s/20 ctree .4*:s :ang-12 :l-1 :t*.7 BK :s/20

LT :ang

BK :s

setpensize round :t

End

 

Now, try it out. The following instructions will reproduce the images shown in the book:

 

? cs ctree 300 70 1 3

? cs ctree 150 70 3 5

? cs ctree 120 70 6 12