Riding the Turtle with Ruby

15 Feb 2008

Turtle graphics is all about making a 'turtle' to move around on the computer screen. The turtle leaves a trail while moving around. So if the Turtle is made to move in a straight line, it draws a straight line on the screen, if the turtle moves in a circle, it results in drawing a circle on the screen. As simple as that :)

Ruby has a lightweight turtle graphics library TkTurtle, which uses Tk. It can be downloaded from here. You need to have Tk and its Ruby bindings installed.

Getting started

Once tkturtle is installed, try the following in in irb. (Use the command 'irb' to open the interactive Ruby console) :

require 'tkturtle' include TurtleGraphics

t = Turtle.new

A window pops up, with a small white triangle (arrow shaped), at the center. This is the 'turtle' !


turtle turns to the left.


turtle turns to the right.

t.forward 10

turtle moves 10 pixes forward (in the current direction)

t.turn 45

turtle turns 45 degrees in the anticlockwise direction.

These are the only basic commands needed for doing Turtle graphics.

Looping to draw some complex shapes

def one_line forward(300); end

This ruby method simply draws a straight line.

def twoperpendicularlines 2.times { one_line ; turn 90 } end

This method draws a pair of mutually perpendicular lines.

2.times {twoperpendicularlines }

This will give a square.

10.times {twoperpendicularlines}

This will also give the same square because lines go on overlapping. To avoid this, I introduced a small angle in beween consecutive "twoperpendicularlines" like this :

def draw(a,b) a.times { twoperpendicularlines ; turn(b) ; wait } end

wait is just to introduce a small delay in between, so that we can watch the turtle draw the lines.

Yea, thats it. Now lets go ahead and see the magic.

I have the code put into a file, for the sake of convenience, turtle1.rb

ruby turtle1.rb 0.1 1 20

0.1 is the time delay in to be introduced while drawing, 1 is is the number of times our basic building block (two perpendicualr lines) is to be repeated, and 20 is the angle in degrees between two consecuive building blocks.

This just gives a pair of perpendicular lines.

Now try

ruby turtle1.rb 0.1 3 20

Nothig much even now - 3 pairs of perpendicular lines, with an extra angle of 20 between them.

Now lets increases the number of times they are repeated.

ruby turtle1.rb 0.1 100 20

Wow.. See what i got..!


Its more interesting to see the drawing process, how a pairs of perpendicular lines join together one by one to produce this figure, than watching this static image!

Now lets us try changing the parameters

ruby turtle1.rb 0.1 50 63


Try various combinations of parameters and observe the patterns produced.

Drawing with Multiple Turtles simultaneously

Idea is to make one turtle draw a circle. Then make six other turtles draw the same circle simultaneosly, but in six different orientations. Its great to see the drawing process, six turtles starting off from one single point, then travel in 6 different directions, do their work and come back to where they started.

Here is my program turtle2.rb

ruby turtle2.rb 6


Lets employ more number of turtles now

ruby turtle2.rb 15


Fractals , Recursion ad Turtles

Fractals when translated to computer language, to me it is Recursion..! I am trying to plot Koch curve which is a classical fractal curve.