Tymless Tutorial: Drawing



   
  < turtles

Lines

 
flipping >
 

Drawing

Control

Data

Reference

 

Here is a Tymless program which draws two descending steps:

|>|<|>|

This program uses only three elements, 'draw' |, 'right' > and 'left' <, so lets look at exactly what they do:

The draw command tells the turtle to walk forward, drawing a line as it goes. How long is the line? Well that depends upon the size of the window, because Tymless fits the image to the screen. Nevertheless, the turtle thinks it is drawing a line of 1 unit.

The left and right commands tell the turtle to turn on the spot. If you don't tell it otherwise it will turn through a right-angle.

The turtle can also walk forwards without drawing a line:

// two lines (this is comment)
| ; |

The 'walk' command ; (which looks vaguely like turtle footprints!) creates a gap of 1 unit between the two lines. These lines are shorter than in the previous program as Tymless attempts to fit them into the same size image. Nevertheless, in each case the turtle thinks it is drawing lines of 1 unit length.

Of course you don't always want to draw lines of the same length. This program draws a rectangle, turning left for a change:

// a rectangle
|*2 < | < |*2 < |

Two of the lines are drawn with the command |*2, which tells the turtle to draw twice as far as usual. The two characters |* combine to form a single Tymless keyword. Tymless provides a lot of such combination keywords which allow you to accomplish tasks with minimal code. This is not only easier to type, it also runs faster. They are generally as intuitive as this example, and are worth becoming familliar with.

How about a hexagon? How do we tell the turtle to turn 60 degrees not 90 degrees? Well the turtle might not be very clever, but it has a good memory for angles, lengths and many other useful things. If you tell it to start using a different angle-of-turn it will do so.

// a hexagon
| >= $pi/3 | > | > | > | > |

The first 'turn right' command >= is another combination keyword which says first change the angle-of-turn to pi/3 radians, and then turn right ($pi is a system constant giving pi). From then on all left and right commands will be based on 60 degrees.

For those not familiar with radians they are very simple to use. To convert from degrees just remember that pi radians is 180 degrees:

  • 360 degrees = 2*pi radians
  • 180 degrees = pi radians
  • 90 degrees = pi/2 radians
  • 60 degrees = pi/3 radians
  • 45 degrees = pi/4 radians
   
  < turtles
 
flipping >