|
|
|
< animation |
Simple Loops |
conditions > |
|
|
The essence of much visual art and mathematical investigation is repetition. In Tymless, repeated code is called a loop, because the program runs the repeated section then loops back and does it again. Here is a program which draws a line of squares: // a line of 20 squares The repeated section is the bit which draws a square and then walks forward one unit. Around that we wrap a loop which says repeat this 20 times. I have written the separate elements on different lines and indented them to make it clearer what is happening. This is good style, but quite unnecessary. The whole program could be written on one line. Loops can be 'nested' within one another. For example here is a grid of squares: // a grid of squares, 10 rows by 5 columns Take a little time to work out what is happening. We are drawing one row at a time, and each row consists of five squares. At the end of the first row we turn right, drop down to the row beneath and start drawing the next row 'backwards'. And the end of the second row we turn left instead, because we have flipped left and right with the flip command ~. This is just one way to draw a grid. A similar technique will tile triangles and hexagons, or even more complicated tesselations. Sometimes it's useful to keep a count of how many times the loop has cycled so far. Tymless does this for you. The system variable $# tells you the current loop-count. Here is a program which draws a line of polygons with increasing numbers of sides. // program 3: a line of polygons If you have nested loops, Tymless will even tell you the count for the loop outside the current one, in system variable $## (you can use as many hashes # as you have nested loops, if necessary).
|
|
< animation |
conditions > |