Truchet Tilings
~~~~~~~~~~~~~~~

<PROGRAM: TRUCHET.BAS>
What you need to run the program:  MS-DOS, VGA, QBASIC or QuickBASIC

"Computerized patterns: before fractals, there were Truchet tiles"

                          TRUCHET TILINGS
                          by Scott Edwards

   In the 1700s, when European monk Sebastien Truchet was exploring tile 
patterns, your PC would have seemed like magic. Truchet would be happy to 
know that in 1994 his tile patterns, rendered in vivid color on a VGA 
monitor, are still magical.

   What Truchet discovered was that square tiles, decorated with lines or 
arcs connecting the midpoints of two adjacent sides, could be laid out 
randomly to form intriguing patterns. The accompanying program, 
TRUCHET.BAS, generates these tiles and then animates them by randomly 
flooding them with color.

   The program begins by declaring the subroutines that generate the 
tiles. Next it dimensions a pair of integer variables to hold the 
coordinates of the upper-left corner of each tile to be drawn. The DIM 
SHARED statement allows all subroutines access to these variables. Without 
it, each subroutine must contain its own SHARED statement.

   The constants (CONST) set the color and size of the tiles. Feel free to 
change these values to your taste. Valid entries for theColor are O to 15. 
The display looks best when TileSize is in the range of 3 to 3O. Because 
all the routines that depend on these values adjust themselves relative to 
the constants, your experimentation won't introduce bugs. That's the 
benefit of using CONST to declare certain universal values at the 
beginning of the program. Constants are accessible to all subroutines in 
QBASIC.

   The program uses SCREEN 12, the 16-color VGA mode with 640-by-480 
resolution. After clearing the screen, the program seeds the random-number 
generator with the contents of the timer to ensure that you see new 
patterns every time you run the program.

   Next a pair of FOR/NEXT loops lay tile outlines across the 640-by-480 
screen. Tiles are chosen with the QBASIC equivalent of a coin toss. RND 
returns a value between O and 1. If it's less than O.5, Tile1 is drawn; 
more than O.5, and Tile2 is drawn. This is repeated until the screen is 
filled.

   Note that you can substitute an alternative pair of tile routines, 
Tile3 and Tile4, in the IF/THEN/ELSE line. Tile3 and Tile4 generate 
straight, maze-like patterns. You can even mix routines, such as Tile1 and 
Tile3, to produce odd hybrid patterns.

   The next section of the program is a DO/WHILE loop that begins by 
selecting a random point on the screen and a random color. If the random 
color is the same as the tile outline, the program throws it back.

   The PAINT command colors between boundaries set by the tile outlines. 
If the screen were flooded with this color, it would obliterate the 
outlines. Eventually the entire screen would become filled with the 
outline color.

   If the user presses any key while the program is running, execution 
stops as soon as the PAINT command has finished.

   At the end of the program listing are the tile subroutines. These 
connect the mid-points of adjacent sides of an imaginary square with arcs 
(Tile1 and Tile2) or straight lines (Tile3 and Tile4). Tile2 is Tile1 
rotated 90 degrees; Tile4 is Tile3 rotated 90 degrees.

   One aspect of the CIRCLE command (used in Tile1 and Tile2) is worth a 
little explanation. CIRCLE can draw circles, arcs, pie slices or ellipses. 
In this case it draws arcs. To do so, it requires the center point of the 
arc, radius, color, and starting and ending angles. These angles are in 
radians instead of the more familiar degrees. Degrees are generally 
expressed in compass terms, with zero degrees corresponding to north. A 
compass marked in radians would have zero due east, pi/2 (1.57) north, pi 
(3.14) west, and 3*pi/2 (4.71) south. The CIRCLE command also draws an arc 
for zero to pi/2 different from that for pi/2 to zero. In the first case 
the belly of the arc points up and to the right; in the second it's down 
and to the left.

   The program's ripe for further experimentation. Try different values 
for TileSize. How about using SELECT CASE to pick from all four tile 
subroutines based on the value of RND? Can you predict how that would 
look? If you're really feeling adventurous, you might even write your own 
tile subroutines.

   If you'd like to read more about Truchet tilings and other graphical 
curiosities, see the book *Computers, Patterns, Chaos and Beauty* by 
Clifford Pickover. It's full of inspiring ideas for BASIC programmers, 
although it presents only concepts and algorithms -- not actual programs.

A designer of electronic kits, toys and educational devices, Scott Edwards 
programs in BASIC to simulate projects under development -- and just for 
fun.

                                  -=*=-

               A designer of electronic kits, toys and 
            educational devices, Scott Edwards programs
            in BASIC to simulate projects under develop-
            ment -- and just for fun.

-=--------------        -=*=-     -=*=-     -=*=-        --------------=-
