ASCII Mandelbrot

Inspired by the some very short lisp code (http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&c2coff=1&selm=ceef5p%24cch%241%40newsreader2.netcologne.de ) which itself is based on some Java code, I gave a 10 minute try to implement the mandelbrot set in the shortest amount of Tcl (one cheat: use tcllib):

package require math::complexnumbers
 
proc norm {c} { return [expr {pow([math::complexnumbers::real $c],2) + pow([math::complexnumbers::imag $c],2)}] }
proc abs {c} { return [expr {sqrt([norm $c])}] }
proc mandel {} {
    for {set y -1} {$y < 1.1} {set y [expr {$y + 0.1}]} {
        for {set x -2} {$x < 1.0} {set x [expr {$x + 0.04}]} {
            set c 126
            set z [math::complexnumbers::complex $x $y]
            set a $z
            while {([abs [set z [math::complexnumbers::+ [math::complexnumbers::* $z $z] $a]]] < 2) && ([incr c -1] > 32)} {}
            puts -nonewline [format %c $c]
        }
        puts ""
    }
}
mandel

This can be greatly improved on, so please do so! -- Todd Coram

See Mandelbrot for background on this fractal.

Here is a translation of the original Java code (which inspired the lisp version) to Tcl. No tcllib dependencies:

proc mandel {} {
    for {set e 1.1} {$e > -1.2} {set e [expr {$e - .1}]} {
        for {set b -2.0} {$b < 1.0} {set b [expr {$b + 0.04}];
            puts -nonewline [expr {($b > 1) ?  "\n":[format %c $h]}]} {
            for {set r 0; set n 0; set h 127} \
                {[expr {$r*$r+$n*$n}] < 4 && [incr h -1] > 32} \
                {set d $r; set r [expr {$r*$r-$n*$n+$b}];
                 set n [expr {2*$d*$n+$e}]} {}
        }
    }
}
mandel

A whole lot of ugliness there ;-) Todd Coram

Output

~~~~~~~~~~~~~}}}}}}}}}}}}}}}}}}}}||||||||{{{zyvrwuW{|||||}}}}}}~~~~~~~~~~~~
~~~~~~~~~~}}}}}}}}}}}}}}}}}}}}|||||||||{{{zyxwoaqwxz{{{|||||}}}}}}~~~~~~~~~
~~~~~~~~}}}}}}}}}}}}}}}}}}}|||||||||{{zzzyxvn    Knwyz{{{{||||}}}}}}~~~~~~~
~~~~~~}}}}}}}}}}}}}}}}}}||||||||{{zyxuxxxwvuq     svwwyzzzyr{||}}}}}}}~~~~~
~~~~}}}}}}}}}}}}}}}}}|||||{{{{{zzzxt>  qf             pttfqeqz{|}}}}}}}}~~~
~~~}}}}}}}}}}}}}}|||{{{{{{{{{zzzywotn                     atyz{||}}}}}}}}~~
~~}}}}}}}}}||||{{zwvyyyyyyyyyyyxvsP                        swvz{||}}}}}}}}~
~}}}}|||||||{{{{zyxvpN[ur]spvwwvi                           qxz{|||}}}}}}}}
~}||||||||{{{{{zyytun         qq                            avz{|||}}}}}}}}
~||||||{zzzzyyxtroqb           a                            xz{{|||}}}}}}}}
~@G::# 6# (                                              pvxyz{{||||}}}}}}}
~||||||{zzzzyyxtroqb           a                            xz{{|||}}}}}}}}
~}||||||||{{{{{zyytun         qq                            avz{|||}}}}}}}}
~}}}}|||||||{{{{zyxvpN[ur]spvwwvi                           qxz{|||}}}}}}}}
~~}}}}}}}}}||||{{zwvyyyyyyyyyyyxvsP                        swvz{||}}}}}}}}~
~~~}}}}}}}}}}}}}}|||{{{{{{{{{zzzywotn                     atyz{||}}}}}}}}~~
~~~~}}}}}}}}}}}}}}}}}|||||{{{{{zzzxt>  qf             pttfqeqz{|}}}}}}}}~~~
~~~~~~}}}}}}}}}}}}}}}}}}||||||||{{zyxuxxxwvuq     svwwyzzzyr{||}}}}}}}~~~~~
~~~~~~~~}}}}}}}}}}}}}}}}}}}|||||||||{{zzzyxvn    Knwyz{{{{||||}}}}}}~~~~~~~
~~~~~~~~~~}}}}}}}}}}}}}}}}}}}}|||||||||{{{zyxwoaqwxz{{{|||||}}}}}}~~~~~~~~~
~~~~~~~~~~~~~}}}}}}}}}}}}}}}}}}}}||||||||{{{zyvrwuW{|||||}}}}}}~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~}}}}}}}}}}}}}}}}}}}}}|||||{zmt{{{||||}}}}}~~~~~~~~~~~~~~~~

Discussion

KPV Here's a fun web page [L1 ] that provides a step-by-step explanation of how a simple C mandlebrot program can be made smaller and more obfuscated.