# Use TSQL for Drawing

,

This is an algorithm from the september 1986 issue of Byte Magazine, in an article called Abstract Mathematical Art. It borrows from Tim Conways Game of Life to represent an algorithm by which a series of numerical values can attempt to replicate itself. It is deeply steeped in the philosophy of Caos Theory and Evolution, but I just like it because it is a terrific example for my MOC 2071 and 2073 students, plus it makes cool wallpaper.

```DECLARE @GenA   VARCHAR(80) -- First Generation
DECLARE @GenB   VARCHAR(80) -- Next Generation
DECLARE @I      INT         -- Outer Loop Counter
DECLARE @J      INT         -- Inner Loop Counter
DECLARE @Rule   CHAR(10)    -- Replication Rule
DECLARE @Chars  CHAR(4)     -- ASCII Drawing Characters
DECLARE @Line   VARCHAR(80) -- Line of ASCII Output

SET @GenA = '0'             -- First column of GenA should be zero
SET @I = 1                  -- Set Loop counter to 1
SET @J = 1                  -- Set loop counter to 1
SET @Rule = '0102030201'    -- Initialize the rule
SET @Chars = ' .o+'         -- Numbers in GenA will be mapped through here (n'th character)

WHILE @I < 80               -- Randomly build numbers into the initial state in GenA
BEGIN
SET @I = @I + 1
SET @GenA = @GenA + CAST(CONVERT(INT, RAND() * 3 + .5) AS CHAR(1))
END

SET @GenA = @GenA + '0'     -- Last column of GenA should be zero
SET @I = 0                  -- Reset loop counter

WHILE @I < 25               -- Loop through this many rows of output
BEGIN
SET @J = 1                -- Reset loop counter
SET @Line = ''            -- Reset line of ASCII output

WHILE @J < 80             -- Loop across GenA, populating Line with ASCII symbols
BEGIN
SET @J = @J + 1
SET @Line = @Line + SUBSTRING(@Chars, CAST(SUBSTRING(@GenA, @J, 1) AS INT) + 1, 1)
END

PRINT @Line               -- Print a line of output
SET @I = @I + 1           -- Increment the row we're on
SET @J = 1                -- Reset loop counter
SET @GenB = '0'           -- First column of GenB should be zero

WHILE @J < 79             -- Calculate the next generation from GenA to GenB
BEGIN
SET @J = @J + 1
SET @GenB = @GenB +
SUBSTRING(@Rule, CAST(SUBSTRING(@GenA, @J - 1, 1) AS INT) +
CAST(SUBSTRING(@GenA, @J, 1) AS INT) +
CAST(SUBSTRING(@GenA, @J + 1, 1) AS INT) + 1, 1)
END

SET @GenA = @GenB + '0'   -- Last column of GenB should be zero
END```

1 (1)

1 (1)