Code and screenshot of recursive pythagoras tree

236 views
Skip to first unread message

Raj

unread,
Aug 20, 2009, 12:02:41 PM8/20/09
to KTurtle; an educational programming environment
Hi,

Please feel free to include these on the project webpage
if you think they would be useful.
The code is quite highly commented.

http://www.nmr.mgh.harvard.edu/~raj/Logo/kturtle06_pythagoras_tree.logo
http://www.nmr.mgh.harvard.edu/~raj/Logo/kturtle06_pythagoras_tree_recursion.png

This was with kturtle v0.6 on Mac OS X,
installed via fink.

Raj

Sly

unread,
Aug 20, 2009, 4:20:09 PM8/20/09
to KTurtle; an educational programming environment
Hi!
And there is update of code in the Kturtle v0.8:

# First, define subfunctions to draw the squares
# and the triangle. Make all the drawing functions end up
# in their starting position and direction. That return-to-start
# is crucial for the recursive drawing to work
learn square $length {
repeat 4 {
forward $length
turnleft 90
}
}
learn triangle_from_hypotenuse $length {
turnright 36.87 # 90-acos(5/3) # Later versions of
forward 3*$length/5 # kturtle have trig funcs
turnright 90
forward 4*$length/5
turnleft 36.87
backward $length
turnleft 90
}
learn triangle_and_three_squares_recurse $length {
$stopping_length = 3
# First draw the big square
square $length
# Now move to starting point for triangle
forward $length
turnleft 90
forward $length
turnright 90
# Now draw the triangle
triangle_from_hypotenuse $length
# Now move to start of side3 square
turnright 36.87
forward 3*$length/5
turnleft 90
#########################
# Branch-off point 1. Either start a new set
# of drawings where the side3 square would be,
# or simply draw the side3 square
if $length > $stopping_length {
triangle_and_three_squares_recurse 3*$length/5
} else {
# Draw the side3 square
square 3*$length/5
}
# Now position for start of side4 square
backward 4*$length/5
turnright 90
#########################
# Branch-off point 2
if $length > $stopping_length {
triangle_and_three_squares_recurse 4*$length/5
} else {
square 4*$length/5
}
# Finally, return to starting position
turnleft 36.87
backward $length
}
reset
canvassize 800,700
go 400,650
triangle_and_three_squares_recurse 100

I was happy as a little child on the Christmas eve when the turtle
finally finished the job :-)
Enjoy!

Silvia

On 20 kol, 18:02, Raj <rajeev.raiz...@gmail.com> wrote:
> Hi,
>
> Please feel free to include these on the project webpage
> if you think they would be useful.
> The code is quite highly commented.
>
> http://www.nmr.mgh.harvard.edu/~raj/Logo/kturtle06_pythagoras_tree.logohttp://www.nmr.mgh.harvard.edu/~raj/Logo/kturtle06_pythagoras_tree_re...

Raj

unread,
Aug 22, 2009, 1:28:00 PM8/22/09
to KTurtle; an educational programming environment
On Aug 20, 4:20 pm, Sly <silvia.kola...@gmail.com> wrote:
> Hi!
> And there is update of code in the Kturtle v0.8:

Thanks for posting this!

> I was happy as a little child on the Christmas eve when the turtle
> finally finished the job :-)
> Enjoy!
>
> Silvia

Great!

Raj
Reply all
Reply to author
Forward
0 new messages