Writing with the Turtle, part 1

Although the ultimate aim is to develop a writing robot, it would be unwise to start working with a real robot writing on real paper because we would probably waste a huge amount of paper and ink as we slowly developed our solution. So why not first have the Small Basic turtle drawing the characters for us inside its graphics window? That way we can work out what moves are needed to draw each character, and we can learn some new Small Basic skills on the way.

Here is the finished result, so you can know what we are aiming for:

Note that the characters are stylised in order to match a grid in which each cell is 100x100 pixels. The height of each character is thus 400 pixels and the width is 200 pixels. 

Note that the turtle has a standard position before and after drawing each character. It always starts in the bottom left corner, pointing right, and it always ends in the bottom right corner, facing right, ready to draw the next letter. A horizontal movement is included to move the turtle into the correct position to begin drawing the next character. The reason for this standardisation is so that we can later draw characters in any order, knowing that the turtle will always end each character in a predictable location with a predictable orientation, ready to draw the next character.

Note that the turtle sometimes moves backwards, to reduce the amount of turning needed. Finally, know that Small Basic does not have any command for drawing arcs, so each semicircle is made by making 90 tiny straight line movements, with a 2 degree turn between each movement.

Here is the code I used for this program. As usual, you can copy and paste it into Small Basic so that you can test it and modify it. Notice that the main program is at the end and that it includes the lines E()    V()  and   three() which call the subprograms that draw each character. (I had to call the subroutine 'three()' because '3()' is not allowed.)

sub E
  Turtle.Turn(-90)
  Turtle.Move(200) 
  Turtle.Turn(90)
  Turtle.PenDown()
  Turtle.Move(100)      'draw middle line
  Turtle.PenUp()
  Turtle.Turn(-63.434)
  Turtle.Move(223.6)
  Turtle.Turn(-26.565)
  Turtle.Turn(-90)
  Turtle.PenDown()
  Turtle.Move(200)      'draw top line
  Turtle.Turn(-90)
  Turtle.Move(400)      'draw vertical line down
  Turtle.Turn(-90)
  Turtle.Move(200)      'draw bottom line
  Turtle.PenUp()
  Turtle.Move(100)      'get in position for for next letter
Endsub

Sub V
  Turtle.Turn(-90)
  Turtle.Move(400)
  Turtle.Turn(90+75.963)
  Turtle.PenDown()
  Turtle.Move(412.3)    'draw down stroke
  Turtle.Turn(-2*75.963)
  Turtle.Move(412.3)    'draw up stroke
  Turtle.PenUp()  
  Turtle.Turn(75.964+90)
  Turtle.Move(400)
  Turtle.Turn(-90)
  Turtle.Move(100)
EndSub

Sub three
  Turtle.PenDown()
  Turtle.Move(100)
  For j=1 to 90         'draw semicircle
    Turtle.Move(3.49)   '100*pi/90
    Turtle.Turn(-2)
  EndFor
  For j=1 to 90         'draw semicircle
    Turtle.Move(-3.49)
    Turtle.Turn(-2)
  EndFor
  Turtle.Move(-100)
  Turtle.PenUp()
  Turtle.Turn(63.434)
  Turtle.Move(447.2)
  Turtle.Turn(-63.434)
  Turtle.Move(100)
EndSub

'Main program starts here
GraphicsWindow.penColor="yellow"
For y=50 To 450 Step 100  'draw horizontal grid lines
  GraphicsWindow.DrawLine(50,y,950,y)
EndFor

For x=50 To 950 Step 100  'draw vertical grid lines
  GraphicsWindow.DrawLine(x,50,x,450)
EndFor

GraphicsWindow.Width=1000
GraphicsWindow.Height=500
GraphicsWindow.PenColor="black"
GraphicsWindow.PenWidth=4
Turtle.Speed=8  'fast
Turtle.x=50
Turtle.y=450
Turtle.Angle=90   'point right
Turtle.PenUp()

E()
V()
three()

For the movements that are neither purely horizontal nor purely vertical, it is necessary to know the angles and distances to be used. This could be calculated within the program but that would make the code much harder to read so I prefer instead to present you with this diagram which will help you make sense of the code above:

I could have used Turtle.TurnLeft() and Turtle.TurnRight() at certain points in the code but for the sake of consistency I preferred the longer versions Turtle.Turn(-90) and Turtle.Turn(90).

In the program above, look how many lines we needed just to define the movements for these three characters E,V,3. Imagine how many lines we would need in order to deal with 26 capital letters and 10 digits - it would make the program very hard to read! Can the program be greatly condensed? Yes, it can, so CLICK HERE to see how.
Comments