mathNEWS Issue 97.3: Friday, February 18, 2005

Mandelbrot Sets in Bash

So last time I wrote for mathNEWS I promised to give some badly formatted code that rendered a Mandelbrot set using bash in a terminal. I was too lazy to come in last production night, but I'm here now (on Valentines day no less, God I'm so depressed). The code is now here along with some explanation.

Bash can do lots of things, but it doesn't do floating point numbers. To fix this I just fudged fixed point numbers with bash's integer support. So the number 100 is treated as a 1, and 5 is treated as a 0.05. This is approximation is good enough for low-res terminal output.

inmandelbrot() {
let "mag = $1 * $1 + $2 * $2"
if [ $mag -gt "40000" ] || [ $5 -ge $6 ]; then
echo $5
else
let "r = ($1 * $1)/100 - ($2 * $2)/100 + $3"
let "i = ($1 * $2)/100 * 2 + $4"
let "cnt = $5 + 1"

inmandelbrot r i $3 $4 $cnt $6
fi
}

for y in $(seq -20 20);
do
for x in $(seq -20 20);
do
let "rval = x * 10"
let "ival = y * 10"
val=$(inmandelbrot rval ival rval ival 1 10)
if [ $val -eq 10 ]; then
echo -n ".";
else
echo -n $val;
fi
done
echo
done

Now you'd have to be really bored to copy this out. If you're only sort of bored you can go to www.mathnews.ca and copy this code out (hint: use the middle mouse button to paste highlighted text). If you don't want to do that some sample output is displayed below:

11111111111111111111211111111111111111111
11111111111111222222222222211111111111111
11111111111122222222222222222111111111111
11111111112222222222222222222221111111111
11111111222222222222222222222222211111111
11111112222222222222222222222222221111111
11111122222222222222222222222222222111111
11111222222223333322222222222222222211111
11112222233674333333392222222222222221111
11112223333465443333473322222222222221111
11122233333468..4334.83332222222222222111
11123333333446664444..3333322222222222111
1123333333344466555...5333332222222222211
112333333334446........433332222222222211
123333333334457..........5446222222222221
12333333333456..............4222222222221
13333333334568............633322222222221
1443333344568.............433322222222221
1444444558................533322222222221
15.6557...................433322222222221
........................75433332222222222
15.6557...................433322222222221
1444444558................533322222222221
1443333344568.............433322222222221
13333333334568............633322222222221
12333333333456..............4222222222221
123333333334457..........5446222222222221
112333333334446........433332222222222211
1123333333344466555...5333332222222222211
11123333333446664444..3333322222222222111
11122233333468..4334.83332222222222222111
11112223333465443333473322222222222221111
11112222233674333333392222222222222221111
11111222222223333322222222222222222211111
11111122222222222222222222222222222111111
11111112222222222222222222222222221111111
11111111222222222222222222222222211111111
11111111112222222222222222222221111111111
11111111111122222222222222222111111111111
11111111111111222222222222211111111111111
11111111111111111111211111111111111111111

JimmyTheLeper
You're only as clean as your towel.



Copyright © 1998 mathNEWS.