Updated 2011-04-16 03:11:29 by RLE

Arjen Markus 2004-02-17 - Elaborating on RS's algorithm to calculate a row in Pascal's triangle (see Pascal) I created the script below. It is a one-dimensional cellular automaton with a few very simple rules.

Variations: the number of colours (variable mod) and the size of the squares (variable size)
 # Turn Pascal's triangle into a cellular automaton
 #
 # RS's original:
 #
 proc pascal {{lastrow ""}} {
    set res 1
    foreach a [lrange $lastrow 1 end] b $lastrow {
       lappend res [expr $a+$b]
    }
    set res
 } ;# RS

 #
 # AM's modulo version
 #
 proc pascalam {mod {lastrow ""}} {
    set res 1
    foreach a [lrange $lastrow 1 end] b $lastrow {
       lappend res [expr ($a+$b)%$mod]
    }
    set res
 }

 #
 # Translate the result into colours
 #
 proc tocolours {row} {
    set rowcols {}
    foreach a $row {
       lappend rowcols [lindex {white black blue green yellow orange red} $a]
    }
    set rowcols
 }

 #
 # Show a row
 #
 proc showrow {rowcols xc yc} {
    global size

    set xoff [expr {$xc-(([llength $rowcols]+1)/2)*$size}]
    set x1   $xoff
    set y1   $yc
    set y2   [expr {$yc+$size-1}]
    foreach a $rowcols {
       set x2 [expr {$x1+$size-1}]
       .c create rectangle $x1 $y1 $x2 $y2 -fill $a -outline $a
       set x1 [expr {$x1+$size}]
    }
 }

 #
 # Create the canvas and show the rows
 #

 pack [canvas .c -bg white -width 400 -height 400] -fill both

 set row  1
 set mod  5
 set size 2

 set xc  200
 set yc    0

 for { set i 0 } { $i < [expr {400/$size}] } { incr i } {
    showrow [tocolours [set row [pascalam $mod $row]]] $xc $yc

    incr yc $size
 }